Skip to content

Commit aa87d82

Browse files
rhenckeproski
authored andcommitted
Replace AbstractProject and AbstractBuild with Job and Run
This makes it possible to support pipelines. Pipelines don't use AbstractProject and AbstractBuild. Instead, they use WorkflowJob and WorkflowRun. The common ancestors are 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.
1 parent f131f52 commit aa87d82

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildListener.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hudson.Util;
55
import hudson.model.AbstractBuild;
66
import hudson.model.Result;
7+
import hudson.model.Run;
78
import hudson.model.TaskListener;
89
import hudson.model.listeners.RunListener;
910
import java.io.IOException;
@@ -16,57 +17,60 @@
1617

1718
/** Created by Nathan McCarthy */
1819
@Extension
19-
public class StashBuildListener extends RunListener<AbstractBuild<?, ?>> {
20+
public class StashBuildListener extends RunListener<Run<?, ?>> {
2021
private static final Logger logger =
2122
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
2223

2324
@Override
24-
public void onStarted(AbstractBuild<?, ?> abstractBuild, TaskListener listener) {
25+
public void onStarted(Run<?, ?> run, TaskListener listener) {
2526
logger.info("BuildListener onStarted called.");
2627

27-
StashCause cause = abstractBuild.getCause(StashCause.class);
28+
StashCause cause = run.getCause(StashCause.class);
2829
if (cause == null) {
2930
return;
3031
}
3132

3233
try {
33-
abstractBuild.setDescription(cause.getShortDescription());
34+
run.setDescription(cause.getShortDescription());
3435
} catch (IOException e) {
3536
logger.log(Level.SEVERE, "Can't update build description", e);
3637
}
3738
}
3839

3940
@Override
40-
public void onCompleted(AbstractBuild<?, ?> abstractBuild, @Nonnull TaskListener listener) {
41-
StashCause cause = abstractBuild.getCause(StashCause.class);
41+
public void onCompleted(Run<?, ?> run, @Nonnull TaskListener listener) {
42+
StashCause cause = run.getCause(StashCause.class);
4243
if (cause == null) {
4344
return;
4445
}
4546

4647
StashBuildTrigger trigger =
47-
ParameterizedJobMixIn.getTrigger(abstractBuild.getParent(), StashBuildTrigger.class);
48+
ParameterizedJobMixIn.getTrigger(run.getParent(), StashBuildTrigger.class);
4849
if (trigger == null) {
4950
return;
5051
}
5152

5253
StashRepository repository = trigger.getBuilder().getRepository();
53-
Result result = abstractBuild.getResult();
54+
Result result = run.getResult();
5455
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
5556
// as only one instance per runtime is really supported by the current core.
5657
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration.get();
5758
String rootUrl = globalConfig == null ? null : globalConfig.getUrl();
5859
String buildUrl = "";
5960
if (rootUrl == null) {
60-
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + abstractBuild.getUrl();
61+
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + run.getUrl();
6162
} else {
62-
buildUrl = rootUrl + abstractBuild.getUrl();
63+
buildUrl = rootUrl + run.getUrl();
6364
}
6465
repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId());
6566

6667
String additionalComment = "";
68+
StashPostBuildComment comments = null;
6769

68-
StashPostBuildComment comments =
69-
abstractBuild.getProject().getPublishersList().get(StashPostBuildComment.class);
70+
if (run instanceof AbstractBuild) {
71+
AbstractBuild<?, ?> build = (AbstractBuild<?, ?>) run;
72+
comments = build.getProject().getPublishersList().get(StashPostBuildComment.class);
73+
}
7074

7175
if (comments != null) {
7276
String buildComment =
@@ -77,28 +81,27 @@ public void onCompleted(AbstractBuild<?, ?> abstractBuild, @Nonnull TaskListener
7781
if (buildComment != null && !buildComment.isEmpty()) {
7882
String expandedComment;
7983
try {
80-
expandedComment =
81-
Util.fixEmptyAndTrim(abstractBuild.getEnvironment(listener).expand(buildComment));
84+
expandedComment = Util.fixEmptyAndTrim(run.getEnvironment(listener).expand(buildComment));
8285
} catch (IOException | InterruptedException e) {
8386
expandedComment = "Exception while expanding '" + buildComment + "': " + e;
8487
}
8588

8689
additionalComment = "\n\n" + expandedComment;
8790
}
8891
}
89-
String duration = abstractBuild.getDurationString();
92+
String duration = run.getDurationString();
9093
repository.postFinishedComment(
9194
cause.getPullRequestId(),
9295
cause.getSourceCommitHash(),
9396
cause.getDestinationCommitHash(),
9497
result,
9598
buildUrl,
96-
abstractBuild.getNumber(),
99+
run.getNumber(),
97100
additionalComment,
98101
duration);
99102

100103
// Merge PR
101-
if (trigger.getMergeOnSuccess() && abstractBuild.getResult() == Result.SUCCESS) {
104+
if (trigger.getMergeOnSuccess() && run.getResult() == Result.SUCCESS) {
102105
boolean mergeStat =
103106
repository.mergePullRequest(cause.getPullRequestId(), cause.getPullRequestVersion());
104107
if (mergeStat == true) {

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import hudson.Extension;
1212
import hudson.model.AbstractProject;
1313
import hudson.model.Item;
14+
import hudson.model.Job;
1415
import hudson.model.Queue;
1516
import hudson.model.queue.Tasks;
1617
import hudson.security.ACL;
@@ -21,6 +22,7 @@
2122
import java.util.Objects;
2223
import java.util.logging.Level;
2324
import java.util.logging.Logger;
25+
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
2426
import net.sf.json.JSONObject;
2527
import org.kohsuke.stapler.AncestorInPath;
2628
import org.kohsuke.stapler.DataBoundConstructor;
@@ -29,7 +31,7 @@
2931
import org.kohsuke.stapler.StaplerRequest;
3032

3133
/** Created by Nathan McCarthy */
32-
public class StashBuildTrigger extends Trigger<AbstractProject<?, ?>> {
34+
public class StashBuildTrigger extends Trigger<Job<?, ?>> {
3335
private static final Logger logger =
3436
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
3537
private final String projectPath;
@@ -120,11 +122,14 @@ public String getcredentialsId() {
120122
}
121123

122124
private StandardUsernamePasswordCredentials getCredentials() {
125+
// Cast is safe due to isApplicable() check
126+
ParameterizedJob parameterizedJob = (ParameterizedJob) job;
127+
123128
return CredentialsMatchers.firstOrNull(
124129
CredentialsProvider.lookupCredentials(
125130
StandardUsernamePasswordCredentials.class,
126131
this.job,
127-
Tasks.getDefaultAuthenticationOf(this.job),
132+
Tasks.getDefaultAuthenticationOf(parameterizedJob),
128133
URIRequirementBuilder.fromUri(stashHost).build()),
129134
CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId)));
130135
}
@@ -178,11 +183,11 @@ public boolean isCancelOutdatedJobsEnabled() {
178183
}
179184

180185
@Override
181-
public void start(AbstractProject<?, ?> project, boolean newInstance) {
182-
super.start(project, newInstance);
186+
public void start(Job<?, ?> job, boolean newInstance) {
187+
super.start(job, newInstance);
183188
try {
184-
Objects.requireNonNull(project, "project is null");
185-
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(project, this);
189+
Objects.requireNonNull(job, "job is null");
190+
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(job, this);
186191
} catch (NullPointerException e) {
187192
logger.log(Level.SEVERE, "Can't start trigger", e);
188193
return;

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashPullRequestsBuilder.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static java.lang.String.format;
44

5-
import hudson.model.AbstractProject;
5+
import hudson.model.Job;
66
import java.lang.invoke.MethodHandles;
77
import java.util.Collection;
88
import java.util.logging.Logger;
@@ -13,28 +13,23 @@
1313
public class StashPullRequestsBuilder {
1414
private static final Logger logger =
1515
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
16-
private AbstractProject<?, ?> project;
16+
private Job<?, ?> job;
1717
private StashBuildTrigger trigger;
1818
private StashRepository repository;
1919

20-
public StashPullRequestsBuilder(
21-
@Nonnull AbstractProject<?, ?> project, @Nonnull StashBuildTrigger trigger) {
22-
this.project = project;
20+
public StashPullRequestsBuilder(@Nonnull Job<?, ?> job, @Nonnull StashBuildTrigger trigger) {
21+
this.job = job;
2322
this.trigger = trigger;
24-
this.repository = new StashRepository(project, trigger);
23+
this.repository = new StashRepository(job, trigger);
2524
}
2625

2726
public void run() {
28-
logger.info(format("Build Start (%s).", project.getName()));
27+
logger.info(format("Build Start (%s).", job.getName()));
2928
Collection<StashPullRequestResponseValue> targetPullRequests =
3029
this.repository.getTargetPullRequests();
3130
this.repository.addFutureBuildTasks(targetPullRequests);
3231
}
3332

34-
public AbstractProject<?, ?> getProject() {
35-
return this.project;
36-
}
37-
3833
public StashBuildTrigger getTrigger() {
3934
return this.trigger;
4035
}

0 commit comments

Comments
 (0)