From 54088b9e36d95ff701a278239a6347bc9282c8f0 Mon Sep 17 00:00:00 2001 From: Martin Damovsky Date: Wed, 1 Mar 2017 19:48:09 +0100 Subject: Cancel outdated jobs - added support for pipeline jobs --- .../BitbucketBuildTrigger.java | 15 +++++++++++++-- .../BitbucketBuildTrigger/help-cancelOutdatedJobs.html | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-cancelOutdatedJobs.html (limited to 'src/main') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 6b03e44..bf3fd64 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -20,6 +20,7 @@ import org.apache.commons.lang.StringUtils; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -212,6 +213,7 @@ public class BitbucketBuildTrigger extends Trigger> { private void cancelPreviousJobsInQueueThatMatch(@Nonnull BitbucketCause bitbucketCause) { logger.fine("Looking for queued jobs that match PR ID: " + bitbucketCause.getPullRequestId()); Queue queue = getInstance().getQueue(); + for (Queue.Item item : queue.getItems()) { if (hasCauseFromTheSamePullRequest(item.getCauses(), bitbucketCause)) { logger.info("Canceling item in queue: " + item); @@ -231,10 +233,11 @@ public class BitbucketBuildTrigger extends Trigger> { private void abortRunningJobsThatMatch(@Nonnull BitbucketCause bitbucketCause) { logger.fine("Looking for running jobs that match PR ID: " + bitbucketCause.getPullRequestId()); for (Object o : job.getBuilds()) { - if (o instanceof Build) { - Build build = (Build) o; + if (o instanceof Run) { + Run build = (Run) o; if (build.isBuilding() && hasCauseFromTheSamePullRequest(build.getCauses(), bitbucketCause)) { logger.info("Aborting build: " + build + " since PR is outdated"); + setBuildDescription(build); final Executor executor = build.getExecutor(); if (executor == null){ throw new IllegalStateException("Executor can't be NULL"); @@ -245,6 +248,14 @@ public class BitbucketBuildTrigger extends Trigger> { } } + private void setBuildDescription(final Run build) { + try { + build.setDescription("Aborting build by `Bitbucket Pullrequest Builder Plugin`: " + build + " since PR is outdated"); + } catch (IOException e) { + logger.warning("Can't set up build description due to an IOException: " + e.getMessage()); + } + } + private boolean hasCauseFromTheSamePullRequest(@Nullable List causes, @Nullable BitbucketCause pullRequestCause) { if (causes != null && pullRequestCause != null) { for (Cause cause : causes) { diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-cancelOutdatedJobs.html b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-cancelOutdatedJobs.html new file mode 100644 index 0000000..c03651c --- /dev/null +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-cancelOutdatedJobs.html @@ -0,0 +1 @@ +If you make a new commit into your PR and there is already running job on that PR, this option will cancel such a outdated job and allows to run only one job at given PR with the newest commit. \ No newline at end of file -- cgit v1.2.3