diff options
author | Martin Damovsky <martin.damovsky@gmail.com> | 2017-03-01 19:48:09 +0100 |
---|---|---|
committer | Martin Damovsky <martin.damovsky@gmail.com> | 2017-03-01 19:48:09 +0100 |
commit | 54088b9e36d95ff701a278239a6347bc9282c8f0 (patch) | |
tree | ebb8d61483308947df3832aceba5de6a9863f6a3 /src | |
parent | 41c25a06eab4cbfd291237946b64fe5dfdebfb4b (diff) | |
download | bbprb-54088b9e36d95ff701a278239a6347bc9282c8f0.tar.gz |
Cancel outdated jobs - added support for pipeline jobs
Diffstat (limited to 'src')
2 files changed, 14 insertions, 2 deletions
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<Job<?, ?>> { 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<Job<?, ?>> { 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<Job<?, ?>> { } } + 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<Cause> 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 |