-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #416 from cwholmes/filter-drafts-trait
Add trait for filtering draft PRs
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...in/java/org/jenkinsci/plugins/github_branch_source/IgnoreDraftPullRequestFilterTrait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.jenkinsci.plugins.github_branch_source; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import java.io.IOException; | ||
import jenkins.scm.api.SCMHead; | ||
import jenkins.scm.api.trait.SCMHeadFilter; | ||
import jenkins.scm.api.trait.SCMSourceContext; | ||
import jenkins.scm.api.trait.SCMSourceRequest; | ||
import jenkins.scm.api.trait.SCMSourceTrait; | ||
import jenkins.scm.api.trait.SCMSourceTraitDescriptor; | ||
import jenkins.scm.impl.trait.Selection; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** Trait used to filter any pull requests current set as a draft from building. */ | ||
public class IgnoreDraftPullRequestFilterTrait extends SCMSourceTrait { | ||
|
||
@DataBoundConstructor | ||
public IgnoreDraftPullRequestFilterTrait() {} | ||
|
||
protected void decorateContext(SCMSourceContext<?, ?> context) { | ||
context.withFilter( | ||
new SCMHeadFilter() { | ||
@Override | ||
public boolean isExcluded( | ||
@NonNull final SCMSourceRequest request, @NonNull final SCMHead head) | ||
throws IOException { | ||
if (!(request instanceof GitHubSCMSourceRequest) | ||
|| !(head instanceof PullRequestSCMHead)) { | ||
return false; | ||
} | ||
GitHubSCMSourceRequest githubRequest = (GitHubSCMSourceRequest) request; | ||
PullRequestSCMHead prHead = (PullRequestSCMHead) head; | ||
for (GHPullRequest pullRequest : githubRequest.getPullRequests()) { | ||
if (pullRequest.getNumber() != prHead.getNumber()) { | ||
continue; | ||
} | ||
if (pullRequest.isDraft()) { | ||
request | ||
.listener() | ||
.getLogger() | ||
.format( | ||
"%n Won't Build PR %s. Marked as a draft.%n", "#" + prHead.getNumber()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
@Symbol({"gitHubIgnoreDraftPullRequestFilter"}) | ||
@Extension | ||
@Selection | ||
public static class DescriptorImpl extends SCMSourceTraitDescriptor { | ||
public DescriptorImpl() {} | ||
|
||
public String getDisplayName() { | ||
return Messages.IgnoreDraftPullRequestFilterTrait_DisplayName(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ava/org/jenkinsci/plugins/github_branch_source/IgnoreDraftPullRequestFilterTraitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.jenkinsci.plugins.github_branch_source; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import jenkins.scm.api.SCMHead; | ||
import jenkins.scm.api.SCMHeadObserver; | ||
import jenkins.scm.api.SCMHeadOrigin; | ||
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy; | ||
import jenkins.scm.api.trait.SCMHeadFilter; | ||
import org.junit.Test; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.mockito.Mockito; | ||
|
||
public class IgnoreDraftPullRequestFilterTraitTest extends GitSCMSourceBase { | ||
|
||
public IgnoreDraftPullRequestFilterTraitTest() { | ||
this.source = new GitHubSCMSource("cloudbeers", "yolo", null, false); | ||
} | ||
|
||
@Test | ||
public void testTraitFiltersDraft() throws IOException, InterruptedException { | ||
GitHubSCMSourceContext probe = new GitHubSCMSourceContext(null, SCMHeadObserver.collect()); | ||
IgnoreDraftPullRequestFilterTrait instance = new IgnoreDraftPullRequestFilterTrait(); | ||
instance.decorateContext(probe); | ||
List<SCMHeadFilter> filters = probe.filters(); | ||
assertThat(filters, hasSize(1)); | ||
SCMHeadFilter filter = filters.get(0); | ||
SCMHead scmHead = | ||
new PullRequestSCMHead( | ||
"PR-5", | ||
"cloudbeers", | ||
"http://localhost:" + githubApi.port(), | ||
"feature/5", | ||
5, | ||
new BranchSCMHead("master"), | ||
SCMHeadOrigin.DEFAULT, | ||
ChangeRequestCheckoutStrategy.MERGE); | ||
GitHubSCMSourceRequest request = new GitHubSCMSourceRequest(source, probe, null); | ||
// Situation: Hitting the Github API for a PR and getting a PR that is a draft | ||
GHPullRequest pullRequest = Mockito.mock(GHPullRequest.class); | ||
Mockito.when(pullRequest.getNumber()).thenReturn(5); | ||
Mockito.when(pullRequest.isDraft()).thenReturn(true); | ||
request.setPullRequests(Collections.singleton(pullRequest)); | ||
assertThat(filter.isExcluded(request, scmHead), equalTo(true)); | ||
} | ||
|
||
@Test | ||
public void testTraitDoesNotFilterNonDraft() throws IOException, InterruptedException { | ||
GitHubSCMSourceContext probe = new GitHubSCMSourceContext(null, SCMHeadObserver.collect()); | ||
IgnoreDraftPullRequestFilterTrait instance = new IgnoreDraftPullRequestFilterTrait(); | ||
instance.decorateContext(probe); | ||
List<SCMHeadFilter> filters = probe.filters(); | ||
assertThat(filters, hasSize(1)); | ||
SCMHeadFilter filter = filters.get(0); | ||
SCMHead scmHead = | ||
new PullRequestSCMHead( | ||
"PR-5", | ||
"cloudbeers", | ||
"http://localhost:" + githubApi.port(), | ||
"feature/5", | ||
5, | ||
new BranchSCMHead("master"), | ||
SCMHeadOrigin.DEFAULT, | ||
ChangeRequestCheckoutStrategy.MERGE); | ||
GitHubSCMSourceRequest request = new GitHubSCMSourceRequest(source, probe, null); | ||
// Situation: Hitting the Github API for a PR and getting a PR that is not a draft | ||
GHPullRequest pullRequest = Mockito.mock(GHPullRequest.class); | ||
Mockito.when(pullRequest.getNumber()).thenReturn(5); | ||
Mockito.when(pullRequest.isDraft()).thenReturn(false); | ||
request.setPullRequests(Collections.singleton(pullRequest)); | ||
assertThat(filter.isExcluded(request, scmHead), equalTo(false)); | ||
} | ||
} |