Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excluded Commits configuration to apply to commit user/email #158

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/main/java/com/isroot/stash/plugin/YaccServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,29 @@ private List<YaccError> checkCommit(Settings settings, YaccCommit commit, boolea
ApplicationUser stashUser = stashAuthenticationContext.getCurrentUser();

if (stashUser == null) {
// This should never happen
// This should never happen, so skip the checks.
log.warn("Unauthenticated user is committing - skipping committer validate checks");
} else {
// Only validate 'normal' users - service users like
return errors;
}

//Skip all checks if the commit or branch are excluded.
if(!isCommitExcluded(settings, commit) && !isBranchExcluded(settings, branchName)) {

// Only validate email/name for 'normal' users - service users like
// the ssh access keys use the key comment as the 'name' and don't have emails
// Neither of these are useful to validate, so just skip them
if (stashUser.getType() == UserType.NORMAL) {
errors.addAll(checkCommitterEmail(settings, commit, stashUser));
errors.addAll(checkCommitterName(settings, commit, stashUser));
}
}

if(checkMessages && !isCommitExcluded(settings, commit) && !isBranchExcluded(settings, branchName)) {
errors.addAll(checkCommitMessageRegex(settings, commit));

// Checking JIRA issues might be dependent on the commit message regex, so only proceed if there are no errors.
if (errors.isEmpty()) {
errors.addAll(checkJiraIssues(settings, commit));

if(checkMessages) {
errors.addAll(checkCommitMessageRegex(settings, commit));
// Checking JIRA issues might be dependent on the commit message regex, so only proceed if there are no errors.
if (errors.isEmpty()) {
errors.addAll(checkJiraIssues(settings, commit));
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/ut/com/isroot/stash/plugin/YaccServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ public void testCheckRefChange_requireMatchingAuthorName_rejectOnMismatch() thro
assertThat(errors).containsOnly(new YaccError(YaccError.Type.COMMITTER_NAME,
"deadbeef: expected committer name 'John Smith' but found 'Incorrect Name'"));
}

@Test
public void testCheckRefChange_requireMatchingAuthorName_allowOnMismatchAndExcludedUser() throws Exception {
when(settings.getBoolean("requireMatchingAuthorName", false)).thenReturn(true);
when(settings.getString("excludeUsers")).thenReturn("userName");
when(stashUser.getType()).thenReturn(UserType.NORMAL);
when(stashUser.getDisplayName()).thenReturn("John Smith");
when(stashUser.getName()).thenReturn("userName");

YaccCommit commit = mockCommit();
when(commit.getCommitter().getName()).thenReturn("Incorrect Name");
when(commitsService.getNewCommits(any(Repository.class), any(RefChange.class))).thenReturn(Sets.newHashSet(commit));

List<YaccError> errors = yaccService.checkRefChange(null, settings, mockRefChange());
assertThat(errors).isEmpty();
}

@Test
public void testCheckRefChange_requireMatchingAuthorName_allowOnMatch() throws Exception {
Expand Down Expand Up @@ -130,6 +146,22 @@ public void testCheckRefChange_requireMatchingAuthorEmail_rejectOnMismatch() thr
assertThat(errors).containsOnly(new YaccError(YaccError.Type.COMMITTER_EMAIL,
"deadbeef: expected committer email 'correct@email.com' but found 'wrong@email.com'"));
}

@Test
public void testCheckRefChange_requireMatchingAuthorEmail_allowOnMismatchAndExcludedUser() throws Exception {
when(settings.getBoolean("requireMatchingAuthorEmail", false)).thenReturn(true);
when(settings.getString("excludeUsers")).thenReturn("userName");
when(stashUser.getType()).thenReturn(UserType.NORMAL);
when(stashUser.getEmailAddress()).thenReturn("correct@email.com");
when(stashUser.getName()).thenReturn("userName");

YaccCommit commit = mockCommit();
when(commit.getCommitter().getEmailAddress()).thenReturn("wrong@email.com");
when(commitsService.getNewCommits(any(Repository.class), any(RefChange.class))).thenReturn(Sets.newHashSet(commit));

List<YaccError> errors = yaccService.checkRefChange(null, settings, mockRefChange());
assertThat(errors).isEmpty();
}

@Test
public void testCheckRefChange_requireMatchingAuthorEmail_allowOnMatch() throws Exception {
Expand Down