Skip to content

Commit

Permalink
Re-implement label ignore feature.
Browse files Browse the repository at this point in the history
This reverts commit 6fe5895.
  • Loading branch information
Ben Patterson committed Dec 6, 2016
1 parent 7540b35 commit 0891211
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ For more details, see https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+re
* The phrase for adding users to the whitelist via comment. (Java regexp)
* The phrase for accepting a pull request for testing. (Java regexp)
* The phrase for starting a new build. (Java regexp)
* The crontab line. This specify default setting for new jobs.
* The crontab line. This specify default setting for new jobs.
* Github labels for which the build will not be triggered (Separated by newline characters)
* Under Application Setup
* There are global and job default extensions that can be configured for things like:
* Commit status updates
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ private String checkSkipBuildInString( String string ) {
}
return null;
}


public Set<String> getLabelsIgnoreList() {
String labelsField = getTrigger().getLabelsIgnoreList();
Set<String> labels = new HashSet<String>();
if (labelsField != null) {
String[] split = labelsField.split("\\n+");
Collections.addAll(labels, split);
}
return labels;
}

private Pattern whitelistPhrasePattern() {
return compilePattern(trigger.getDescriptor().getWhitelistPhrase());
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.kohsuke.github.GHCommitPointer;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestCommitDetail;
import org.kohsuke.github.GHUser;
Expand All @@ -17,6 +18,7 @@
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -165,9 +167,29 @@ public void check(GHPullRequest ghpr, boolean isWebhook) {
// Call update PR with the update PR info and no comment
updatePR(ghpr, null /*GHIssueComment*/, isWebhook);
checkSkipBuild();
checkLabels();
tryBuild();
}

private void checkLabels() {
Set<String> labelsToIgnore = helper.getLabelsIgnoreList();
if (labelsToIgnore != null && !labelsToIgnore.isEmpty()) {
try {
for (GHLabel label : pr.getLabels()) {
if (labelsToIgnore.contains(label.getName())) {
logger.log(Level.INFO,
"Found label {0} in ignore list, pull request will be ignored.",
label.getName());
shouldRun = false;
}
}

} catch(IOException e) {
logger.log(Level.SEVERE, "Failed to read labels", e);
}
}
}

private void checkSkipBuild() {
synchronized (this) {
String skipBuildPhrase = helper.checkSkipBuild(this.pr);
Expand All @@ -188,6 +210,7 @@ public void check(GHIssueComment comment) {

updatePR(null /*GHPullRequest*/, comment, true);
checkSkipBuild();
checkLabels();
tryBuild();
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class GhprbTrigger extends GhprbTriggerBackwardsCompatible {
private String gitHubAuthId;
private String triggerPhrase;
private String skipBuildPhrase;
private String labelsIgnoreList;


private transient Ghprb helper;
Expand Down Expand Up @@ -139,6 +140,7 @@ public GhprbTrigger(String adminlist,
String commitStatusContext,
String gitHubAuthId,
String buildDescTemplate,
String labelsIgnoreList,
List<GhprbExtension> extensions
) throws ANTLRException {
super(cron);
Expand All @@ -158,6 +160,7 @@ public GhprbTrigger(String adminlist,
this.gitHubAuthId = gitHubAuthId;
this.allowMembersOfWhitelistedOrgsAsAdmin = allowMembersOfWhitelistedOrgsAsAdmin;
this.buildDescTemplate = buildDescTemplate;
this.labelsIgnoreList = labelsIgnoreList;
setExtensions(extensions);
configVersion = latestVersion;
}
Expand Down Expand Up @@ -485,6 +488,10 @@ public String getSkipBuildPhrase() {
return skipBuildPhrase;
}

public String getLabelsIgnoreList() {
return labelsIgnoreList;
}

public Boolean getOnlyTriggerPhrase() {
return onlyTriggerPhrase != null && onlyTriggerPhrase;
}
Expand Down Expand Up @@ -646,6 +653,7 @@ public static final class DescriptorImpl extends TriggerDescriptor {
private List<GhprbBranch> blackListTargetBranches;
private Boolean autoCloseFailedPullRequests = false;
private Boolean displayBuildErrorsOnDownstreamBuilds = false;
private String labelsIgnoreList;

private List<GhprbGitHubAuth> githubAuth;

Expand Down Expand Up @@ -758,6 +766,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
unstableAs = GHCommitState.valueOf(formData.getString("unstableAs"));
autoCloseFailedPullRequests = formData.getBoolean("autoCloseFailedPullRequests");
displayBuildErrorsOnDownstreamBuilds = formData.getBoolean("displayBuildErrorsOnDownstreamBuilds");
labelsIgnoreList = formData.getString("labelsIgnoreList");

githubAuth = req.bindJSONToList(GhprbGitHubAuth.class, formData.get("githubAuth"));

Expand Down Expand Up @@ -836,6 +845,10 @@ public Boolean getUseDetailedComments() {
return useDetailedComments;
}

public String getLabelsIgnoreList() {
return labelsIgnoreList;
}

public Boolean getManageWebhooks() {
return manageWebhooks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Object githubPullRequest(Runnable closure) throws ANTLRException {
null,
null,
null,
null,
context.buildDescriptionTemplate,
context.extensionContext.extensions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ f.advanced() {
f.entry(field: "orgslist", title: _("List of organizations. Their members will be whitelisted.")) {
f.textarea()
}
f.entry(field: "labelsIgnoreList", title: _("List of github labels for which the build should not be triggered.")) {
f.textarea()
}
f.entry(field: "allowMembersOfWhitelistedOrgsAsAdmin", title: "Allow members of whitelisted organizations as admins") {
f.checkbox()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ f.section(title: descriptor.displayName) {
f.entry(field: "cron", title: _("Crontab line"), help: "/descriptor/hudson.triggers.TimerTrigger/help/spec") {
f.textbox(default: "H/5 * * * *", checkUrl: "'descriptorByName/hudson.triggers.TimerTrigger/checkSpec?value=' + encodeURIComponent(this.value)")
}
f.entry(field: "labelsIgnoreList", title: _("List of github labels for which the build should not be triggered.")) {
f.textarea()
}
}
f.entry(title: _("Application Setup")) {
f.hetero_list(items: descriptor.extensions, name: "extensions", oneEach: "true", hasHeader: "true", descriptors: descriptor.getGlobalExtensionDescriptors())
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/jenkinsci/plugins/ghprb/GhprbRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.ghprb;

import org.apache.commons.codec.binary.Hex;
import org.fest.util.Collections;
import org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus;
import org.joda.time.DateTime;
import org.junit.Assert;
Expand All @@ -13,6 +14,7 @@
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRateLimit;
import org.kohsuke.github.GHRepository;
Expand Down Expand Up @@ -40,6 +42,7 @@
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -220,6 +223,7 @@ public void testCheckMethodWithOnlyExistingPRs() throws Exception {
verify(helper).getBlackListTargetBranches();
verify(helper, times(2)).isProjectDisabled();
verify(helper).checkSkipBuild(eq(ghPullRequest));
verify(helper, times(1)).getLabelsIgnoreList();
verifyNoMoreInteractions(helper);
verifyNoMoreInteractions(gt);

Expand Down Expand Up @@ -256,6 +260,7 @@ public void testCheckMethodWithNewPR() throws Exception {
given(helper.ifOnlyTriggerPhrase()).willReturn(false);
given(helper.isWhitelisted(ghUser)).willReturn(true);
given(helper.getTrigger()).willReturn(trigger);
given(helper.getLabelsIgnoreList()).willReturn(Collections.set("bug", "help wanted"));

// WHEN
ghprbRepository.check();
Expand Down Expand Up @@ -283,6 +288,7 @@ public void testCheckMethodWithNewPR() throws Exception {
verify(ghPullRequest, times(1)).listCommits();
verify(ghPullRequest, times(1)).getBody();
verify(ghPullRequest, times(1)).getId();
verify(ghPullRequest, times(2)).getLabels();
verifyNoMoreInteractions(ghPullRequest);

verify(helper, times(1)).isWhitelisted(eq(ghUser)); // Call to Github API
Expand All @@ -292,13 +298,62 @@ public void testCheckMethodWithNewPR() throws Exception {
verify(helper, times(2)).getBlackListTargetBranches();
verify(helper, times(4)).isProjectDisabled();
verify(helper, times(2)).checkSkipBuild(eq(ghPullRequest));
verify(helper, times(2)).getLabelsIgnoreList();
verifyNoMoreInteractions(helper);

verify(ghUser, times(1)).getEmail(); // Call to Github API
verify(ghUser, times(1)).getLogin();
verifyNoMoreInteractions(ghUser);
}

@Test
public void testShouldSkipBuildIfLabelInIgnoreListWithNewPR() throws Exception {
// GIVEN
GHLabel label1 = mock(GHLabel.class);
given(label1.getName()).willReturn("in progress");
GHLabel label2 = mock(GHLabel.class);
given(label2.getName()).willReturn("testing");
List<GHLabel> ghLabels = Arrays.asList(label1, label2);

List<GHPullRequest> ghPullRequests = createListWithMockPR();
ghPullRequests.add(ghPullRequest);

GhprbBuilds builds = mockBuilds();

mockHeadAndBase();
mockCommitList();

given(ghRepository.getPullRequests(eq(GHIssueState.OPEN))).willReturn(ghPullRequests);

given(ghPullRequest.getUpdatedAt()).willReturn(UPDATE_DATE);
given(ghPullRequest.getNumber()).willReturn(100);
given(ghPullRequest.getMergeable()).willReturn(true);
given(ghPullRequest.getTitle()).willReturn("title");
given(ghPullRequest.getUser()).willReturn(ghUser);
given(ghPullRequest.getHtmlUrl()).willReturn(new URL("https://github.com/org/repo/pull/100"));
given(ghPullRequest.getApiURL()).willReturn(new URL("https://github.com/org/repo/pull/100"));
given(ghPullRequest.getId()).willReturn(100);
given(ghPullRequest.getLabels()).willReturn(ghLabels);
given(ghRepository.getPullRequest(ghPullRequest.getId())).willReturn(ghPullRequest);

given(ghUser.getEmail()).willReturn("email");

given(helper.ifOnlyTriggerPhrase()).willReturn(false);
given(helper.isWhitelisted(ghUser)).willReturn(true);
given(helper.getTrigger()).willReturn(trigger);
given(helper.getLabelsIgnoreList()).willReturn(Collections.set("in progress"));

// WHEN
ghprbRepository.check();

// THEN
verifyGetGithub(2, 2, 1);
verifyNoMoreInteractions(gt);

/** Verify no attempt was made to start a build */
verifyNoMoreInteractions(builds);
}

private GhprbBuilds mockBuilds() throws IOException {
GhprbBuilds builds = spy(new GhprbBuilds(trigger, ghprbRepository));
given(helper.getBuilds()).willReturn(builds);
Expand Down Expand Up @@ -337,6 +392,7 @@ public void testCheckMethodWhenPrWasUpdatedWithNonKeyPhrase() throws Exception {
given(helper.ifOnlyTriggerPhrase()).willReturn(false);
given(helper.isWhitelisted(ghUser)).willReturn(true);
given(helper.getTrigger()).willReturn(trigger);
given(helper.getLabelsIgnoreList()).willReturn(Collections.set("bug", "help wanted"));

// WHEN
ghprbRepository.check(); // PR was created
Expand Down Expand Up @@ -370,13 +426,15 @@ public void testCheckMethodWhenPrWasUpdatedWithNonKeyPhrase() throws Exception {
verify(ghPullRequest, times(1)).listCommits();
verify(ghPullRequest, times(1)).getBody();
verify(ghPullRequest, times(1)).getId();
verify(ghPullRequest, times(2)).getLabels();
verifyNoMoreInteractions(ghPullRequest);

verify(helper, times(1)).isWhitelisted(eq(ghUser)); // Call to Github API
verify(helper, times(2)).ifOnlyTriggerPhrase();
verify(helper, times(1)).getBuilds();
verify(helper, times(2)).getWhiteListTargetBranches();
verify(helper, times(2)).getBlackListTargetBranches();
verify(helper, times(2)).getLabelsIgnoreList();

// verify(helper).isBotUser(eq(ghUser));
verify(helper).isWhitelistPhrase(eq("comment body"));
Expand Down Expand Up @@ -430,6 +488,7 @@ public void testCheckMethodWhenPrWasUpdatedWithRetestPhrase() throws Exception {
given(helper.isRetestPhrase(eq("test this please"))).willReturn(true);
given(helper.isWhitelisted(ghUser)).willReturn(true);
given(helper.getTrigger()).willReturn(trigger);
given(helper.getLabelsIgnoreList()).willReturn(Collections.set("bug", "help wanted"));

// WHEN
ghprbRepository.check(); // PR was created
Expand Down Expand Up @@ -459,6 +518,7 @@ public void testCheckMethodWhenPrWasUpdatedWithRetestPhrase() throws Exception {
verify(ghPullRequest, times(2)).getUpdatedAt();
verify(ghPullRequest, times(1)).getCreatedAt();
verify(ghPullRequest, times(2)).getHtmlUrl();
verify(ghPullRequest, times(2)).getLabels();

verify(ghPullRequest, times(1)).getId();
verify(ghPullRequest, times(1)).getComments();
Expand All @@ -473,6 +533,7 @@ public void testCheckMethodWhenPrWasUpdatedWithRetestPhrase() throws Exception {
verify(helper, times(2)).getBuilds();
verify(helper, times(2)).getWhiteListTargetBranches();
verify(helper, times(2)).getBlackListTargetBranches();
verify(helper, times(2)).getLabelsIgnoreList();

verify(helper).isWhitelistPhrase(eq("test this please"));
verify(helper).isOktotestPhrase(eq("test this please"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ public static void setupGhprbTriggerDescriptor(Map<String, Object> config) throw
jsonObject.put("msgSuccess", "Success");
jsonObject.put("msgFailure", "Failure");
jsonObject.put("commitStatusContext", "Status Context");
jsonObject.put("labelsIgnoreList", "in progress");

JSONObject githubAuth = new JSONObject();
githubAuth.put("credentialsId", getCredentialsId());
Expand Down

0 comments on commit 0891211

Please sign in to comment.