Skip to content

Commit f9b0f3f

Browse files
committed
Fix canceling outdated Maven projects
The original code would only look for instances of Build to be canceled. MavenBuild inherits from AbstractBuild but not from Build, so Maven builds would not be canceled. Use Run for elements of the job.getBuilds(), it is type safe and provides the necessary API. Use run.getId() in the log for better readability. Check the result of getExecutor(), it has @checkfornull attribute.
1 parent 212acb2 commit f9b0f3f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
1111
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1212
import hudson.Extension;
13-
import hudson.model.Build;
1413
import hudson.model.Cause;
1514
import hudson.model.CauseAction;
15+
import hudson.model.Executor;
1616
import hudson.model.Item;
1717
import hudson.model.Job;
1818
import hudson.model.ParameterDefinition;
@@ -21,6 +21,7 @@
2121
import hudson.model.ParametersDefinitionProperty;
2222
import hudson.model.Queue;
2323
import hudson.model.Result;
24+
import hudson.model.Run;
2425
import hudson.model.StringParameterValue;
2526
import hudson.model.queue.QueueTaskFuture;
2627
import hudson.model.queue.Tasks;
@@ -257,12 +258,12 @@ private void cancelPreviousJobsInQueueThatMatch(@Nonnull StashCause stashCause)
257258

258259
private void abortRunningJobsThatMatch(@Nonnull StashCause stashCause) {
259260
logger.fine("Looking for running jobs that match PR ID: " + stashCause.getPullRequestId());
260-
for (Object o : job.getBuilds()) {
261-
if (o instanceof Build) {
262-
Build build = (Build) o;
263-
if (build.isBuilding() && hasCauseFromTheSamePullRequest(build.getCauses(), stashCause)) {
264-
logger.info("Aborting build: " + build + " since PR is outdated");
265-
build.getExecutor().interrupt(Result.ABORTED);
261+
for (Run<?, ?> run : job.getBuilds()) {
262+
if (run.isBuilding() && hasCauseFromTheSamePullRequest(run.getCauses(), stashCause)) {
263+
logger.info("Aborting build: " + run.getId() + " since PR is outdated");
264+
Executor executor = run.getExecutor();
265+
if (executor != null) {
266+
executor.interrupt(Result.ABORTED);
266267
}
267268
}
268269
}

0 commit comments

Comments
 (0)