Skip to content

Commit 1f1bec0

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 188f398 commit 1f1bec0

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;
@@ -258,12 +259,12 @@ private void cancelPreviousJobsInQueueThatMatch(@Nonnull StashCause stashCause)
258259

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

0 commit comments

Comments
 (0)