Skip to content

Commit d68b817

Browse files
rhenckeproski
authored andcommitted
Enable support for Jenkins pipelines
Pipelines don't use AbstractProject and AbstractBuild. Instead, they use WorkflowJob and WorkflowRun. The common ancestors are Job and Run. Implement the code in terms of Job and Run. In some cases, Job doesn't provide the needed API. However, both AbstractProject and WorkflowJob implement ParameterizedJob interface, which provides the required functionality. Require the job to implement ParameterizedJob. Original patch by Robert Hencke <[email protected]>
1 parent 7424217 commit d68b817

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
1111
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1212
import hudson.Extension;
13-
import hudson.model.AbstractProject;
1413
import hudson.model.Build;
1514
import hudson.model.Cause;
15+
import hudson.model.CauseAction;
1616
import hudson.model.Item;
17+
import hudson.model.Job;
1718
import hudson.model.ParameterDefinition;
1819
import hudson.model.ParameterValue;
1920
import hudson.model.ParametersAction;
@@ -37,6 +38,8 @@
3738
import javax.annotation.Nonnull;
3839
import javax.annotation.Nullable;
3940
import jenkins.model.Jenkins;
41+
import jenkins.model.ParameterizedJobMixIn;
42+
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
4043
import net.sf.json.JSONObject;
4144
import org.apache.commons.lang.StringUtils;
4245
import org.kohsuke.stapler.AncestorInPath;
@@ -47,7 +50,7 @@
4750

4851
/** Created by Nathan McCarthy */
4952
@SuppressFBWarnings({"WMI_WRONG_MAP_ITERATOR", "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"})
50-
public class StashBuildTrigger extends Trigger<AbstractProject<?, ?>> {
53+
public class StashBuildTrigger extends Trigger<Job<?, ?>> {
5154
private static final Logger logger =
5255
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
5356
private final String projectPath;
@@ -142,7 +145,7 @@ private StandardUsernamePasswordCredentials getCredentials() {
142145
CredentialsProvider.lookupCredentials(
143146
StandardUsernamePasswordCredentials.class,
144147
this.job,
145-
Tasks.getDefaultAuthenticationOf(this.job),
148+
Tasks.getDefaultAuthenticationOf((ParameterizedJob) job),
146149
URIRequirementBuilder.fromUri(stashHost).build()),
147150
CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId)));
148151
}
@@ -196,15 +199,15 @@ public boolean isCancelOutdatedJobsEnabled() {
196199
}
197200

198201
@Override
199-
public void start(AbstractProject<?, ?> project, boolean newInstance) {
202+
public void start(Job<?, ?> job, boolean newInstance) {
200203
try {
201-
Objects.requireNonNull(project, "project is null");
202-
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(project, this);
204+
Objects.requireNonNull(job, "job is null");
205+
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(job, this);
203206
} catch (NullPointerException e) {
204207
logger.log(Level.SEVERE, "Can't start trigger", e);
205208
return;
206209
}
207-
super.start(project, newInstance);
210+
super.start(job, newInstance);
208211
}
209212

210213
public StashPullRequestsBuilder getBuilder() {
@@ -226,7 +229,19 @@ public QueueTaskFuture<?> startJob(StashCause cause) {
226229
abortRunningJobsThatMatch(cause);
227230
}
228231

229-
return job.scheduleBuild2(job.getQuietPeriod(), cause, new ParametersAction(values));
232+
// TODO: Use ParameterizedJob#getParameterizedJobMixIn() in Jenkins 2.61+
233+
ParameterizedJobMixIn<?, ?> scheduledJob =
234+
new ParameterizedJobMixIn() {
235+
@Override
236+
protected Job<?, ?> asJob() {
237+
return StashBuildTrigger.this.job;
238+
}
239+
};
240+
241+
return scheduledJob.scheduleBuild2(
242+
((ParameterizedJob) job).getQuietPeriod(),
243+
new CauseAction(cause),
244+
new ParametersAction(values));
230245
}
231246

232247
private void cancelPreviousJobsInQueueThatMatch(@Nonnull StashCause stashCause) {
@@ -333,7 +348,7 @@ public StashBuildTriggerDescriptor() {
333348

334349
@Override
335350
public boolean isApplicable(Item item) {
336-
return item instanceof AbstractProject;
351+
return item instanceof Job && item instanceof ParameterizedJob;
337352
}
338353

339354
@Override

0 commit comments

Comments
 (0)