Skip to content

Commit

Permalink
Add merging rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rcraggs committed Aug 3, 2018
1 parent 183ff31 commit b13ade1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Rules are the checks that will be made on the repository each time the applicati
| `file-tracked-in-branch` | Checks if a file exists in the given branch | `path`, `branch` | - |
| `file-contains-in-branch` | Checks if a file exists with given contents in the given branch | `path`, `branch`, `contents` | `ignore-case` |
| `branch-exists` | Checks if a given branch exists | `branch` | - |
| `commit-with-message-was-made-on-branch` | Check if a certain commit was made on the given branch| `branch`, `content` | `ignore-case` |
| `commit-with-message-was-merged-into-branch` | Check if a certain commit merged into a branch| `branch`, `content` | `ignore-case` |


#### Common Rule Options

Expand Down
30 changes: 28 additions & 2 deletions src/main/java/gitruler/GitFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ boolean isChildOfCommitOnBranch(String branch, String commitMessageContents, boo
RevCommit commitWithMessage = getCommitWithMessageContaining(commitMessageContents, ignoreMessageCase);
Git git = new Git(repo);


if (commitMessageContents == null){
if (commitWithMessage == null){
return false;
}

Expand All @@ -401,4 +400,31 @@ boolean isChildOfCommitOnBranch(String branch, String commitMessageContents, boo

return false;
}

/**
* Check whether the commits underneath a branch reference include a commit with a given message
* @param branch the branch name
* @param commitMessage The text within the message
* @param ignoreCase Whether to ignore the case when checking the commit message
* @return True of the commit with that message was under the branch ref
* @throws BranchNotFoundException Git exception
* @throws IOException Git exception
*/
boolean wasCommitWithMessageMadeOnBranch(String branch, String commitMessage, boolean ignoreCase) throws BranchNotFoundException, IOException {

RevCommit commitWithMessage = getCommitWithMessageContaining(commitMessage, ignoreCase);

// Get a walker for the branch
RevWalk revWalk = new RevWalk(repo);
revWalk.markStart( revWalk.parseCommit(getBranchCommit(branch).getId()));

// Look at each commit in the branch
for( RevCommit commit : revWalk ) {
if (commit.getId().equals(commitWithMessage.getId())){
return true;
}
}
return false;
}

}
25 changes: 23 additions & 2 deletions src/main/java/gitruler/GitInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ RuleResult checkRule(Rule r) {
return checkFileContainsInBranch(r);
case "branch-exists":
return checkBranchExists(r);
case "branch-received-commit-with-message":
case "commit-with-message-was-merged-into-branch":
return checkBranchReceivedCommitWithMessage(r);

case "commit-with-message-was-made-on-branch":
return getCommitWithMessageWasMadeOnBranch(r);
default:
RuleResult result = new RuleResult();
result.setPassed(false);
Expand All @@ -88,6 +89,26 @@ RuleResult checkRule(Rule r) {
}
}

/**
* Check whether a commit was made on a specific branch
* @param r The rule details
* @return The RuleResult
*/
private RuleResult getCommitWithMessageWasMadeOnBranch(Rule r) {

RuleResult result = new RuleResult();
try {
result.setPassed(gitFunctions.wasCommitWithMessageMadeOnBranch(r.getBranch(), r.getContents(), r.getIgnoreCase()));
} catch (IOException e) {
return createResultFromException(e);
} catch (BranchNotFoundException e) {
result.setPassed(false);
result.setMessage("The branch with that name doesn't exist");
}

return result;
}

/**
* Is there a commit on the given branch that has a parent commit that contains a given message?
* This lets us check whether a merge occurred that merged a commit into another branch.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gitruler/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class Rule {
* Add the checkrule in the GitInterator
* Add the documentation
*/

Map<String, Object> details;

Rule(Map<String, Object> details){
Expand Down Expand Up @@ -57,8 +56,10 @@ String getTitle() {
return "A branch called '" + this.getBranch() + "' exists";
case "file-contains-in-branch":
return "The file " + getPath() + " contains the text '" + getContents() + "' in the branch '" + getBranch() + "'";
case "branch-received-commit-with-message":
case "commit-with-message-was-merged-into-branch":
return "The branch '" + getBranch() + "' received a commit with the message containing '" + getContents() + "'";
case "commit-with-message-was-made-on-branch":
return "The commit whose message contained '" + getContents() + "' was made on the branch '" + getBranch() + "'";
default:
return "Unknown rule";
}
Expand Down

0 comments on commit b13ade1

Please sign in to comment.