Skip to content

Commit c58ca97

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 6f02fba commit c58ca97

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ 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+
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.
149+
150+
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".
151+
140152
## Copyright
141153

142154
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
@@ -267,12 +267,27 @@ public static final class DescriptorImpl extends TriggerDescriptor {
267267
public static final String DEFAULT_CI_SKIP_PHRASES = "NO TEST";
268268
public static final String DEFAULT_CI_BUILD_PHRASES = "test this please";
269269

270+
private boolean enablePipelineSupport;
271+
270272
public DescriptorImpl() {
271273
load();
272274
}
273275

276+
public boolean getEnablePipelineSupport() {
277+
return enablePipelineSupport;
278+
}
279+
280+
@DataBoundSetter
281+
public void setEnablePipelineSupport(boolean enablePipelineSupport) {
282+
this.enablePipelineSupport = enablePipelineSupport;
283+
}
284+
274285
@Override
275286
public boolean isApplicable(Item item) {
287+
if (enablePipelineSupport) {
288+
return item instanceof Job && item instanceof ParameterizedJob;
289+
}
290+
276291
return item instanceof AbstractProject;
277292
}
278293

@@ -283,6 +298,9 @@ public String getDisplayName() {
283298

284299
@Override
285300
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
301+
enablePipelineSupport = false;
302+
303+
req.bindJSON(this, json);
286304
save();
287305
return super.configure(req, json);
288306
}
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)