Skip to content

Commit

Permalink
Made comparison case insensitive, pointed this out in the help text
Browse files Browse the repository at this point in the history
Added an extra test case for the case insensitive scenario
  • Loading branch information
pbalint-ge authored and pbalint committed Jun 1, 2020
1 parent acaad73 commit 72c01dd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -105,18 +106,24 @@ public void onPost(final GitHubTriggerEvent event) {
return; // nothing to do
}
if (useGitExcludedUsers) {
Set<String> excludedUsers = null;
Set<String> lowercaseExcludedUsers = new HashSet<>();
if (job instanceof AbstractProject) {
SCM scm = ((AbstractProject<?, ?>) job).getScm();
if (scm instanceof GitSCM) {
UserExclusion exclusions = ((GitSCM) scm).getExtensions().get(UserExclusion.class);
if (exclusions != null) {
excludedUsers = exclusions.getExcludedUsersNormalized();
for (String userName: exclusions.getExcludedUsersNormalized()) {
lowercaseExcludedUsers.add(userName.toLowerCase());
}
}
}
}

if (excludedUsers != null && excludedUsers.contains(event.getTriggeredByUser())) {
String lowercaseTriggeredByUser = null;
if (event.getTriggeredByUser() != null) {
lowercaseTriggeredByUser = event.getTriggeredByUser().toLowerCase();
}
if (lowercaseExcludedUsers != null && lowercaseExcludedUsers.contains(lowercaseTriggeredByUser)) {
return; // user is excluded from triggering build
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ InlineWarning.setup({
}

f.entry() {
f.checkbox(title: _("Use Git excluded user list (\"Polling ignores commits from certain users\")"), field: "useGitExcludedUsers")
f.checkbox(title: _("Use Git excluded user list (\"Polling ignores commits from certain users\", comparison is case insensitive)"), field: "useGitExcludedUsers")
}
27 changes: 27 additions & 0 deletions src/test/java/com/cloudbees/jenkins/GitHubPushTriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ public void shouldSkipBuildIfExclusionEnabledWithMatchingUser() throws IOExcepti
verify(spiedQueue, times(0)).execute(Mockito.any(Runnable.class));
}

@Test
public void shouldSkipBuildIfExclusionEnabledWithMatchingUserCaseInsensitive() throws IOException {
SequentialExecutionQueue spiedQueue = addSpyToQueueField();

String matchingUserName = "userName".toLowerCase();
FreeStyleProject project = jRule.createFreeStyleProject();
GitHubPushTrigger trigger = new GitHubPushTrigger();
trigger.setUseGitExcludedUsers(true);
trigger.start(project, false);
project.addTrigger(trigger);
GitSCM scm = new GitSCM("https://localhost/dummy.git");
UserExclusion userExclusion = new UserExclusion("something" + System.lineSeparator() +
matchingUserName.toUpperCase() + System.lineSeparator() +
"somethingElse" + System.lineSeparator());
scm.getExtensions().add(userExclusion);
project.setScm(scm);

GitHubTriggerEvent event = GitHubTriggerEvent.create()
.withTimestamp(System.currentTimeMillis())
.withOrigin("origin")
.withTriggeredByUser(matchingUserName)
.build();
trigger.onPost(event);

verify(spiedQueue, times(0)).execute(Mockito.any(Runnable.class));
}

@Test
public void shouldTriggerBuildIfExclusionEnabledWithNonMatchingUser() throws IOException {
SequentialExecutionQueue spiedQueue = addSpyToQueueField();
Expand Down

0 comments on commit 72c01dd

Please sign in to comment.