diff --git a/pom.xml b/pom.xml index 485abbf..21763c5 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,12 @@ 1703.vd5a_2b_29c6cdc + + com.github.tomakehurst + wiremock-jre8-standalone + 2.35.2 + test + diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObject.java b/src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObject.java index 9416635..83cdecc 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObject.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObject.java @@ -283,7 +283,7 @@ public int getChangedFiles() { } @Whitelisted - public boolean isMergeable() { + public Boolean isMergeable() { // Boolean instead of boolean because this can be null! return pullRequest.isMergeable(); } diff --git a/src/test/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObjectTest.java b/src/test/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObjectTest.java index f678832..a0fc645 100644 --- a/src/test/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObjectTest.java +++ b/src/test/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObjectTest.java @@ -1,10 +1,21 @@ package org.jenkinsci.plugins.pipeline.github; +import static org.junit.Assert.assertEquals; + +import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; +import org.jenkinsci.plugins.github_branch_source.PullRequestSCMHead; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +import hudson.model.Item; +import jenkins.scm.api.SCMHead; +import jenkins.scm.api.SCMSource; + /** * @author Aaron Whiteside */ @@ -13,10 +24,61 @@ public class PullRequestGroovyObjectTest { @Rule public JenkinsRule r = new JenkinsRule(); + @Rule + public WireMockRule wireMockRule = new WireMockRule(); + + public static class TestHeadByItemImpl extends SCMHead.HeadByItem { + @Override + public SCMHead getHead(Item item) { + return new PullRequestSCMHead("PR-42", "owner", "repo", null, 42, null, null, null); + } + } + + public static class TestSourceByItemImpl extends SCMSource.SourceByItem { + public TestSourceByItemImpl(int port) { + this.port = port; + } + + @Override + public SCMSource getSource(Item item) { + GitHubSCMSource result = new GitHubSCMSource("owner", "repo", null, false); + result.setCredentialsId("credentialsId"); + result.setApiUri(String.format("http://localhost:%d", port)); + return result; + } + + private int port; + } + @Test public void testReadOnlyProperties() throws Exception { WorkflowJob job = r.createProject(WorkflowJob.class, "p"); r.getInstance().getAllItems().forEach(System.out::println); } + @Test + public void testPullRequestMergeabilityUndecided() throws Exception { + WorkflowJob job = r.createProject(WorkflowJob.class, "p"); + r.jenkins.getExtensionList(SCMHead.HeadByItem.class).add(new TestHeadByItemImpl()); + r.jenkins.getExtensionList(SCMSource.SourceByItem.class).add(new TestSourceByItemImpl(wireMockRule.port())); + stubFor(get(urlPathMatching("/api/v3/repos/owner/repo/pulls/42")) + .willReturn(aResponse() + .withStatus(200) + .withBody("{}"))); + PullRequestGroovyObject prgo = new PullRequestGroovyObject((WorkflowJob)job); + assertEquals(null, prgo.isMergeable()); + } + + @Test + public void testPullRequestMergeable() throws Exception { + WorkflowJob job = r.createProject(WorkflowJob.class, "p"); + r.jenkins.getExtensionList(SCMHead.HeadByItem.class).add(new TestHeadByItemImpl()); + r.jenkins.getExtensionList(SCMSource.SourceByItem.class).add(new TestSourceByItemImpl(wireMockRule.port())); + stubFor(get(urlPathMatching("/api/v3/repos/owner/repo/pulls/42")) + .willReturn(aResponse() + .withStatus(200) + .withBody("{\"mergeable\":\"true\"}"))); + PullRequestGroovyObject prgo = new PullRequestGroovyObject((WorkflowJob)job); + assertEquals(true, prgo.isMergeable()); + } } \ No newline at end of file