diff --git a/README.md b/README.md index fcfe21ca..bffdf242 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,18 @@ These comments can contain environment variables that will be translated when po This feature can be used to post for instance a URL to the deployed application or code coverage at a successful build and why the build failed like what tests that did not pass. +## Pipeline support + +Support for Jenkins pipelines is currently experimental. To enable it, go to *Manage Jenkins*, then *Configure System* and check for *Enable Pipeline Support*. + +The configuration is the same as for other projects, but it is not currently possible to customize the comments posted to the Bitbucket Server after the job completion. + +Stash Pull Request Builder Plugin could serve as a stepping stone to a more advanced setup using Bitbucket Branch Source Plugin. Unlike the later, this plugin supports inline Groovy scripts for pipeline configuration, which may be handy while figuring out the optimal configuration for the build. Once that configuration is established, it can be added to the sources as `Jenkinsfile`. + +Stash Pull Request Builder Plugin makes conversion to pipelines easier the users, as it behaves the same way as before, it just triggers a pipeline project instead of a freestyle project. This plugin works with pull requests only and relies on comments for deciding when and what to build. It would not scan the git repository for all branches and build them all. It will never build a branch before a pull request is created from it. + +To use Stash Pull Request Builder Plugin from Jenkinsfile, make sure to define it in properties. Otherwise, the trigger will be disabled after the the job runs. To find the exact syntax, open the Pipeline Syntax link and select "properties" as the Sample Step. Set up the trigger in the GUI and click on "Generate Pipeline Script". + ## Copyright Copyright © 2015 Nathan McCarthy. diff --git a/pom.xml b/pom.xml index 4b5944d2..9a8abbe0 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,12 @@ junit test + + org.jenkins-ci.plugins.workflow + workflow-job + 2.9 + test + diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index 15c8f2c7..f7d2f5e8 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -267,12 +267,27 @@ public static final class DescriptorImpl extends TriggerDescriptor { public static final String DEFAULT_CI_SKIP_PHRASES = "NO TEST"; public static final String DEFAULT_CI_BUILD_PHRASES = "test this please"; + private boolean enablePipelineSupport; + public DescriptorImpl() { load(); } + public boolean getEnablePipelineSupport() { + return enablePipelineSupport; + } + + @DataBoundSetter + public void setEnablePipelineSupport(boolean enablePipelineSupport) { + this.enablePipelineSupport = enablePipelineSupport; + } + @Override public boolean isApplicable(Item item) { + if (enablePipelineSupport) { + return item instanceof Job && item instanceof ParameterizedJob; + } + return item instanceof AbstractProject; } @@ -283,6 +298,9 @@ public String getDisplayName() { @Override public boolean configure(StaplerRequest req, JSONObject json) throws FormException { + enablePipelineSupport = false; + + req.bindJSON(this, json); save(); return super.configure(req, json); } diff --git a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/global.jelly b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/global.jelly new file mode 100644 index 00000000..235452df --- /dev/null +++ b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/global.jelly @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/help-enablePipelineSupport.html b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/help-enablePipelineSupport.html new file mode 100644 index 00000000..3ca81e09 --- /dev/null +++ b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/help-enablePipelineSupport.html @@ -0,0 +1,11 @@ +
+

+ Enable support for pipelines. When checked, Stash Pull Request + Builder will be available in pipeline based projects. Please note + that pipeline support is experimental. +

+

+ Another way to support pipelines with Bitbucket Server is to use + Bitbucket Branch Source Plugin. +

+
diff --git a/src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTriggerTest.java b/src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTriggerTest.java index f5b42aa9..2d1aead1 100644 --- a/src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTriggerTest.java +++ b/src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTriggerTest.java @@ -3,13 +3,25 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; import hudson.model.Descriptor.PropertyType; +import hudson.model.FreeStyleProject; import java.util.Arrays; +import java.util.Collections; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import net.sf.json.JSONObject; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.jvnet.hudson.test.JenkinsRule; +import org.kohsuke.stapler.RequestImpl; +import org.kohsuke.stapler.Stapler; +import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.WebApp; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoRule; @@ -21,6 +33,65 @@ public class StashBuildTriggerTest { @Rule public JenkinsRule jenkinsRule = new JenkinsRule(); @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); + private StaplerRequest makeStaplerRequest() { + ServletContext servletContext = mock(ServletContext.class); + WebApp webApp = new WebApp(servletContext); + + Stapler stapler = mock(Stapler.class); + lenient().when(stapler.getWebApp()).thenReturn(webApp); + + HttpServletRequest servletRequest = mock(HttpServletRequest.class); + + return new RequestImpl(stapler, servletRequest, Collections.emptyList(), null); + } + + @Test + public void pipeline_jobs_not_supported_by_default() throws Exception { + JSONObject json = new JSONObject(); + + StaplerRequest staplerRequest = makeStaplerRequest(); + StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl(); + descriptor.configure(staplerRequest, json); + + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); + assertThat(descriptor.isApplicable(project), is(true)); + + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); + assertThat(descriptor.isApplicable(workflowJob), is(false)); + } + + @Test + public void pipeline_jobs_not_supported_when_pipeline_support_disabled() throws Exception { + JSONObject json = new JSONObject(); + json.put("enablePipelineSupport", "false"); + + StaplerRequest staplerRequest = makeStaplerRequest(); + StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl(); + descriptor.configure(staplerRequest, json); + + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); + assertThat(descriptor.isApplicable(project), is(true)); + + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); + assertThat(descriptor.isApplicable(workflowJob), is(false)); + } + + @Test + public void pipeline_jobs_supported_when_pipeline_support_enabled() throws Exception { + JSONObject json = new JSONObject(); + json.put("enablePipelineSupport", "true"); + + StaplerRequest staplerRequest = makeStaplerRequest(); + StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl(); + descriptor.configure(staplerRequest, json); + + FreeStyleProject project = jenkinsRule.createFreeStyleProject(); + assertThat(descriptor.isApplicable(project), is(true)); + + WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class); + assertThat(descriptor.isApplicable(workflowJob), is(true)); + } + @Test public void check_getters() throws Exception { StashBuildTrigger.DescriptorImpl descriptor = StashBuildTrigger.descriptor;