Skip to content

Commit 4a38d32

Browse files
committed
Enable optional support for Jenkins pipelines
Add a global option to enable pipeline support. If it's enabled, don't require the job to inherit from AbstractProject. Allow any Job subclass that implements ParameterizedJob, which is true both for freestyle projects and for pipelines. Make it clear in the option help text and in README.md that pipeline support is currently experimental. Add workflow-job as a test dependency. Add unit tests to make sure that WorkflowJob is supported only when pipeline support is enabled.
1 parent 692225c commit 4a38d32

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ These comments can contain environment variables that will be translated when po
137137

138138
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.
139139

140+
## Pipeline support
141+
142+
Support for Jenkins pipelines is currently experimental. To enable it, go to *Manage Jenkins*, then *Configure System* and check for *Enable Pipeline Support*.
143+
144+
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.
145+
146+
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`.
147+
148+
Also, this plugin makes conversion to pipelines transparent to the users. The plugin will still post comments about the build status, the rebuild can still be requested with the *"test this please"* comment, the additional parameters continue to work.
149+
140150
## Copyright
141151

142152
Copyright © 2015 Nathan McCarthy.

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
<artifactId>junit</artifactId>
109109
<scope>test</scope>
110110
</dependency>
111+
<dependency>
112+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
113+
<artifactId>workflow-job</artifactId>
114+
<version>2.9</version>
115+
<scope>test</scope>
116+
</dependency>
111117
</dependencies>
112118

113119
<repositories>

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java

+18
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,27 @@ public boolean getOnlyBuildOnComment() {
229229
}
230230

231231
public static final class DescriptorImpl extends TriggerDescriptor {
232+
private boolean enablePipelineSupport;
233+
232234
public DescriptorImpl() {
233235
load();
234236
}
235237

238+
public boolean getEnablePipelineSupport() {
239+
return enablePipelineSupport;
240+
}
241+
242+
@DataBoundSetter
243+
public void setEnablePipelineSupport(boolean enablePipelineSupport) {
244+
this.enablePipelineSupport = enablePipelineSupport;
245+
}
246+
236247
@Override
237248
public boolean isApplicable(Item item) {
249+
if (enablePipelineSupport) {
250+
return item instanceof Job && item instanceof ParameterizedJob;
251+
}
252+
238253
return item instanceof AbstractProject;
239254
}
240255

@@ -245,6 +260,9 @@ public String getDisplayName() {
245260

246261
@Override
247262
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
263+
enablePipelineSupport = false;
264+
265+
req.bindJSON(this, json);
248266
save();
249267
return super.configure(req, json);
250268
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
3+
<f:section title="Stash Pull Request Builder">
4+
<f:entry title="Enable pipeline support (experimental)" field="enablePipelineSupport">
5+
<f:checkbox />
6+
</f:entry>
7+
</f:section>
8+
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div>
2+
<p>
3+
Enable support for pipelines. When checked, Stash Pull Request
4+
Builder will be available in pipeline based projects. Please note
5+
that pipeline support is experimental.
6+
</p>
7+
<p>
8+
Another way to support pipelines with Bitbucket Server is to use
9+
Bitbucket Branch Source Plugin.
10+
</p>
11+
</div>

src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTriggerTest.java

+71
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55
import static org.hamcrest.Matchers.notNullValue;
6+
import static org.mockito.Mockito.lenient;
7+
import static org.mockito.Mockito.mock;
68

79
import hudson.model.Descriptor.PropertyType;
10+
import hudson.model.FreeStyleProject;
811
import java.util.Arrays;
12+
import java.util.Collections;
13+
import javax.servlet.ServletContext;
14+
import javax.servlet.http.HttpServletRequest;
15+
import net.sf.json.JSONObject;
16+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
917
import org.junit.Rule;
1018
import org.junit.Test;
1119
import org.junit.runner.RunWith;
1220
import org.jvnet.hudson.test.JenkinsRule;
21+
import org.kohsuke.stapler.RequestImpl;
22+
import org.kohsuke.stapler.Stapler;
23+
import org.kohsuke.stapler.StaplerRequest;
24+
import org.kohsuke.stapler.WebApp;
1325
import org.mockito.junit.MockitoJUnit;
1426
import org.mockito.junit.MockitoJUnitRunner;
1527
import org.mockito.junit.MockitoRule;
@@ -21,6 +33,65 @@ public class StashBuildTriggerTest {
2133
@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
2234
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
2335

36+
private StaplerRequest makeStaplerRequest() {
37+
ServletContext servletContext = mock(ServletContext.class);
38+
WebApp webApp = new WebApp(servletContext);
39+
40+
Stapler stapler = mock(Stapler.class);
41+
lenient().when(stapler.getWebApp()).thenReturn(webApp);
42+
43+
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
44+
45+
return new RequestImpl(stapler, servletRequest, Collections.emptyList(), null);
46+
}
47+
48+
@Test
49+
public void pipeline_jobs_not_supported_by_default() throws Exception {
50+
JSONObject json = new JSONObject();
51+
52+
StaplerRequest staplerRequest = makeStaplerRequest();
53+
StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl();
54+
descriptor.configure(staplerRequest, json);
55+
56+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
57+
assertThat(descriptor.isApplicable(project), is(true));
58+
59+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
60+
assertThat(descriptor.isApplicable(workflowJob), is(false));
61+
}
62+
63+
@Test
64+
public void pipeline_jobs_not_supported_when_pipeline_support_disabled() throws Exception {
65+
JSONObject json = new JSONObject();
66+
json.put("enablePipelineSupport", "false");
67+
68+
StaplerRequest staplerRequest = makeStaplerRequest();
69+
StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl();
70+
descriptor.configure(staplerRequest, json);
71+
72+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
73+
assertThat(descriptor.isApplicable(project), is(true));
74+
75+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
76+
assertThat(descriptor.isApplicable(workflowJob), is(false));
77+
}
78+
79+
@Test
80+
public void pipeline_jobs_supported_when_pipeline_support_enabled() throws Exception {
81+
JSONObject json = new JSONObject();
82+
json.put("enablePipelineSupport", "true");
83+
84+
StaplerRequest staplerRequest = makeStaplerRequest();
85+
StashBuildTrigger.DescriptorImpl descriptor = new StashBuildTrigger.DescriptorImpl();
86+
descriptor.configure(staplerRequest, json);
87+
88+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
89+
assertThat(descriptor.isApplicable(project), is(true));
90+
91+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
92+
assertThat(descriptor.isApplicable(workflowJob), is(true));
93+
}
94+
2495
@Test
2596
public void check_getters() throws Exception {
2697
StashBuildTrigger.DescriptorImpl descriptor = StashBuildTrigger.descriptor;

0 commit comments

Comments
 (0)