From 290653e67596b635acb38aaf278c5bef314b9a22 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sat, 30 Mar 2019 21:46:22 +0300 Subject: Refactor trigger for future features For handling more events, for more configuration options, etc. --- .../plugins/bbprb/BitbucketBuildTrigger.java | 102 +++++++++++---------- .../plugins/bbprb/BitbucketHookReceiver.java | 9 +- 2 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java index 2946d7a..4f57f04 100644 --- a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java @@ -150,12 +150,6 @@ public class BitbucketBuildTrigger extends Trigger> { } private void startJob(BitbucketCause cause) { - if (this.cancelOutdatedJobs) { - SecurityContext orig = ACL.impersonate(ACL.SYSTEM); - cancelPreviousJobsInQueueThatMatch(cause); - abortRunningJobsThatMatch(cause); - SecurityContextHolder.setContext(orig); - } List bbprb = new ArrayList<>(); @@ -184,37 +178,53 @@ public class BitbucketBuildTrigger extends Trigger> { new ParametersAction(bbprb, bbprbSafeParameters)); } - private void - cancelPreviousJobsInQueueThatMatch(@Nonnull BitbucketCause cause) { + private Jenkins getInstance() { + final Jenkins instance = Jenkins.getInstance(); + if (instance == null) { + throw new IllegalStateException("Jenkins instance is NULL!"); + } + return instance; + } + + private boolean + hasCauseFromTheSamePullRequest(@Nullable List causes, + @Nullable BitbucketCause pullRequestCause) { + if (causes != null && pullRequestCause != null) { + for (Cause cause : causes) { + if (cause instanceof BitbucketCause) { + BitbucketCause sc = (BitbucketCause)cause; + if (StringUtils.equals(sc.getPullRequestId(), + pullRequestCause.getPullRequestId()) && + StringUtils.equals(sc.getSourceRepository(), + pullRequestCause.getSourceRepository())) { + return true; + } + } + } + } + return false; + } + + private void cancelPR(BitbucketCause cause) { + SecurityContext orig = ACL.impersonate(ACL.SYSTEM); + logger.log(Level.FINE, "Looking for queued jobs that match PR #{0}", cause.getPullRequestId()); Queue queue = getInstance().getQueue(); - for (Queue.Item item : queue.getItems()) { if (hasCauseFromTheSamePullRequest(item.getCauses(), cause)) { logger.fine("Canceling item in queue: " + item); queue.cancel(item); } } - } - - private Jenkins getInstance() { - final Jenkins instance = Jenkins.getInstance(); - if (instance == null) { - throw new IllegalStateException("Jenkins instance is NULL!"); - } - return instance; - } - private void - abortRunningJobsThatMatch(@Nonnull BitbucketCause bitbucketCause) { logger.log(Level.FINE, "Looking for running jobs that match PR #{0}", - bitbucketCause.getPullRequestId()); + cause.getPullRequestId()); for (Object o : job.getBuilds()) { if (o instanceof Run) { Run build = (Run)o; if (build.isBuilding() && - hasCauseFromTheSamePullRequest(build.getCauses(), bitbucketCause)) { + hasCauseFromTheSamePullRequest(build.getCauses(), cause)) { logger.fine("Aborting '" + build + "' since the PR is outdated"); try { build.setDescription("Aborted build since the PR is outdated"); @@ -230,32 +240,22 @@ public class BitbucketBuildTrigger extends Trigger> { } } } + SecurityContextHolder.setContext(orig); } - private boolean - hasCauseFromTheSamePullRequest(@Nullable List causes, - @Nullable BitbucketCause pullRequestCause) { - if (causes != null && pullRequestCause != null) { - for (Cause cause : causes) { - if (cause instanceof BitbucketCause) { - BitbucketCause sc = (BitbucketCause)cause; - if (StringUtils.equals(sc.getPullRequestId(), - pullRequestCause.getPullRequestId()) && - StringUtils.equals(sc.getSourceRepository(), - pullRequestCause.getSourceRepository())) { - return true; - } - } - } - } - return false; - } + public void handlePR(String event, JSONObject pr) { - public void handlePR(JSONObject pr) { JSONObject src = pr.getJSONObject("source"); JSONObject dst = pr.getJSONObject("destination"); String dstRepository = dst.getJSONObject("repository").getString("full_name"); + if (!dstRepository.equals(this.destinationRepository)) { + logger.log(Level.FINE, + "Job `{0}`: repository `{1}` does not match `{2}`. Skipping.", + new Object[] {this.job.getFullName(), dstRepository, + this.destinationRepository}); + return; + } BitbucketCause cause = new BitbucketCause( src.getJSONObject("branch").getString("name"), dst.getJSONObject("branch").getString("name"), @@ -265,14 +265,20 @@ public class BitbucketBuildTrigger extends Trigger> { src.getJSONObject("commit").getString("hash"), dst.getJSONObject("commit").getString("hash"), pr.getJSONObject("author").getString("username")); - if (!dstRepository.equals(this.destinationRepository)) { - logger.log(Level.FINE, - "Job `{0}`: repository `{1}` does not match `{2}`. Skipping.", - new Object[] {this.job.getFullName(), dstRepository, - this.destinationRepository}); - return; + switch (event) { + case "pullrequest:created": + startJob(cause); + break; + case "pullrequest:updated": + if (this.cancelOutdatedJobs) { + cancelPR(cause); + } + startJob(cause); + break; + default: + logger.log(Level.WARNING, "Unhandled event: `{0}`", + new Object[] {event}); } - startJob(cause); } @Extension diff --git a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketHookReceiver.java b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketHookReceiver.java index 3562a7e..355a901 100644 --- a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketHookReceiver.java +++ b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketHookReceiver.java @@ -97,15 +97,8 @@ public class BitbucketHookReceiver JSONObject payload = JSONObject.fromObject(body); if (event.startsWith("pullrequest:")) { JSONObject pr = payload.getJSONObject("pullrequest"); - String state = pr.getString("state"); - if (!"OPEN".equals(state)) { - LOGGER.log( - Level.INFO, "Ignoring closed PR ({0}): #{1} {2}", - new Object[] {state, pr.getInt("id"), pr.getString("title")}); - return; - } for (BitbucketBuildTrigger trigger : getBitbucketTriggers()) { - trigger.handlePR(pr); + trigger.handlePR(event, pr); } return; } -- cgit v1.2.3