Skip to content

Commit 09cb28b

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 1aafe5d commit 09cb28b

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-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
@@ -237,12 +237,27 @@ public boolean isOnlyBuildOnComment() {
237237
}
238238

239239
public static final class StashBuildTriggerDescriptor extends TriggerDescriptor {
240+
private boolean enablePipelineSupport;
241+
240242
public StashBuildTriggerDescriptor() {
241243
load();
242244
}
243245

246+
public boolean getEnablePipelineSupport() {
247+
return enablePipelineSupport;
248+
}
249+
250+
@DataBoundSetter
251+
public void setEnablePipelineSupport(boolean enablePipelineSupport) {
252+
this.enablePipelineSupport = enablePipelineSupport;
253+
}
254+
244255
@Override
245256
public boolean isApplicable(Item item) {
257+
if (enablePipelineSupport) {
258+
return item instanceof Job && item instanceof ParameterizedJob;
259+
}
260+
246261
return item instanceof AbstractProject;
247262
}
248263

@@ -253,6 +268,9 @@ public String getDisplayName() {
253268

254269
@Override
255270
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
271+
enablePipelineSupport = false;
272+
273+
req.bindJSON(this, json);
256274
save();
257275
return super.configure(req, json);
258276
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package stashpullrequestbuilder.stashpullrequestbuilder;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.mockito.Mockito.lenient;
6+
import static org.mockito.Mockito.mock;
7+
8+
import hudson.model.FreeStyleProject;
9+
import java.util.Collections;
10+
import javax.servlet.ServletContext;
11+
import javax.servlet.http.HttpServletRequest;
12+
import net.sf.json.JSONObject;
13+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
14+
import org.junit.Rule;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.jvnet.hudson.test.JenkinsRule;
18+
import org.kohsuke.stapler.RequestImpl;
19+
import org.kohsuke.stapler.Stapler;
20+
import org.kohsuke.stapler.StaplerRequest;
21+
import org.kohsuke.stapler.WebApp;
22+
import org.mockito.junit.MockitoJUnit;
23+
import org.mockito.junit.MockitoJUnitRunner;
24+
import org.mockito.junit.MockitoRule;
25+
import org.mockito.quality.Strictness;
26+
import stashpullrequestbuilder.stashpullrequestbuilder.StashBuildTrigger.StashBuildTriggerDescriptor;
27+
28+
@RunWith(MockitoJUnitRunner.class)
29+
public class StashBuildTriggerTest {
30+
31+
@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
32+
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
33+
34+
private StaplerRequest makeStaplerRequest() {
35+
ServletContext servletContext = mock(ServletContext.class);
36+
WebApp webApp = new WebApp(servletContext);
37+
38+
Stapler stapler = mock(Stapler.class);
39+
lenient().when(stapler.getWebApp()).thenReturn(webApp);
40+
41+
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
42+
43+
return new RequestImpl(stapler, servletRequest, Collections.emptyList(), null);
44+
}
45+
46+
@Test
47+
public void pipeline_jobs_not_supported_by_default() throws Exception {
48+
JSONObject json = new JSONObject();
49+
50+
StaplerRequest staplerRequest = makeStaplerRequest();
51+
StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor();
52+
descriptor.configure(staplerRequest, json);
53+
54+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
55+
assertThat(descriptor.isApplicable(project), is(true));
56+
57+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
58+
assertThat(descriptor.isApplicable(workflowJob), is(false));
59+
}
60+
61+
@Test
62+
public void pipeline_jobs_not_supported_when_pipeline_support_disabled() throws Exception {
63+
JSONObject json = new JSONObject();
64+
json.put("enablePipelineSupport", "false");
65+
66+
StaplerRequest staplerRequest = makeStaplerRequest();
67+
StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor();
68+
descriptor.configure(staplerRequest, json);
69+
70+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
71+
assertThat(descriptor.isApplicable(project), is(true));
72+
73+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
74+
assertThat(descriptor.isApplicable(workflowJob), is(false));
75+
}
76+
77+
@Test
78+
public void pipeline_jobs_supported_when_pipeline_support_enabled() throws Exception {
79+
JSONObject json = new JSONObject();
80+
json.put("enablePipelineSupport", "true");
81+
82+
StaplerRequest staplerRequest = makeStaplerRequest();
83+
StashBuildTriggerDescriptor descriptor = new StashBuildTriggerDescriptor();
84+
descriptor.configure(staplerRequest, json);
85+
86+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
87+
assertThat(descriptor.isApplicable(project), is(true));
88+
89+
WorkflowJob workflowJob = jenkinsRule.createProject(WorkflowJob.class);
90+
assertThat(descriptor.isApplicable(workflowJob), is(true));
91+
}
92+
}

0 commit comments

Comments
 (0)