From a6374a41b6ad80ca586a606823f6b22f5c5c4193 Mon Sep 17 00:00:00 2001 From: Olivier Bellemare Date: Thu, 27 Oct 2016 10:49:17 -0400 Subject: Add option to cancel outdated builds when a pull request is updated --- .../BitbucketBuildTrigger.java | 64 +++++++++++++++++++++- .../BitbucketBuildTrigger/config.jelly | 3 + src/test/java/BitbucketBuildRepositoryTest.java | 12 ++-- 3 files changed, 72 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index aedfb91..90de6b5 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -7,20 +7,26 @@ import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredenti import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials; import hudson.Extension; import hudson.model.*; +import hudson.model.Queue; import hudson.model.queue.QueueTaskFuture; import hudson.plugins.git.RevisionParameterAction; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.util.ListBoxModel; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.apache.commons.lang.StringUtils; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf; @@ -43,11 +49,12 @@ public class BitbucketBuildTrigger extends Trigger> { private final String ciSkipPhrases; private final boolean checkDestinationCommit; private final boolean approveIfSuccess; + private final boolean cancelOutdatedJobs; transient private BitbucketPullRequestsBuilder bitbucketPullRequestsBuilder; @Extension - public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor(); + public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor(); @DataBoundConstructor public BitbucketBuildTrigger( @@ -64,7 +71,8 @@ public class BitbucketBuildTrigger extends Trigger> { String ciName, String ciSkipPhrases, boolean checkDestinationCommit, - boolean approveIfSuccess + boolean approveIfSuccess, + boolean cancelOutdatedJobs ) throws ANTLRException { super(cron); this.projectPath = projectPath; @@ -81,6 +89,7 @@ public class BitbucketBuildTrigger extends Trigger> { this.ciSkipPhrases = ciSkipPhrases; this.checkDestinationCommit = checkDestinationCommit; this.approveIfSuccess = approveIfSuccess; + this.cancelOutdatedJobs = cancelOutdatedJobs; } public String getProjectPath() { @@ -114,7 +123,7 @@ public class BitbucketBuildTrigger extends Trigger> { public String getBranchesFilter() { return branchesFilter; } - + public boolean getBranchesFilterBySCMIncludes() { return branchesFilterBySCMIncludes; } @@ -139,6 +148,10 @@ public class BitbucketBuildTrigger extends Trigger> { return approveIfSuccess; } + public boolean getCancelOutdatedJobs() { + return cancelOutdatedJobs; + } + @Override public void start(AbstractProject project, boolean newInstance) { try { @@ -164,9 +177,54 @@ public class BitbucketBuildTrigger extends Trigger> { public QueueTaskFuture startJob(BitbucketCause cause) { Map values = this.getDefaultParameters(); + + if (getCancelOutdatedJobs()) { + cancelPreviousJobsInQueueThatMatch(cause); + abortRunningJobsThatMatch(cause); + } + return this.job.scheduleBuild2(0, cause, new ParametersAction(new ArrayList(values.values())), new RevisionParameterAction(cause.getSourceCommitHash())); } + private void cancelPreviousJobsInQueueThatMatch(@Nonnull BitbucketCause bitbucketCause) { + logger.fine("Looking for queued jobs that match PR ID: " + bitbucketCause.getPullRequestId()); + Queue queue = Jenkins.getInstance().getQueue(); + for (Queue.Item item : queue.getItems()) { + if (hasCauseFromTheSamePullRequest(item.getCauses(), bitbucketCause)) { + logger.info("Canceling item in queue: " + item); + queue.cancel(item); + } + } + } + + 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 (build.isBuilding() && hasCauseFromTheSamePullRequest(build.getCauses(), bitbucketCause)) { + logger.info("Aborting build: " + build + " since PR is outdated"); + build.getExecutor().interrupt(Result.ABORTED); + } + } + } + } + + 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.getRepositoryName(), pullRequestCause.getRepositoryName())) { + return true; + } + } + } + } + return false; + } + private Map getDefaultParameters() { Map values = new HashMap(); ParametersDefinitionProperty definitionProperty = this.job.getProperty(ParametersDefinitionProperty.class); diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index d4b23da..971954f 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -38,4 +38,7 @@ + + + diff --git a/src/test/java/BitbucketBuildRepositoryTest.java b/src/test/java/BitbucketBuildRepositoryTest.java index 953bd81..148e504 100644 --- a/src/test/java/BitbucketBuildRepositoryTest.java +++ b/src/test/java/BitbucketBuildRepositoryTest.java @@ -118,7 +118,8 @@ public class BitbucketBuildRepositoryTest { "", true, "", "", "", true, - true + true, + false ); BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); @@ -148,7 +149,8 @@ public class BitbucketBuildRepositoryTest { "", true, "", "", "", true, - true + true, + false ); BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); @@ -202,7 +204,8 @@ public class BitbucketBuildRepositoryTest { "", true, "jenkins", "Jenkins", "", true, - true + true, + false ); BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); @@ -248,7 +251,8 @@ public class BitbucketBuildRepositoryTest { "", true, "jenkins-too-long-ci-key", "Jenkins", "", true, - true + true, + false ); final MessageDigest MD5 = MessageDigest.getInstance("MD5"); -- cgit v1.2.3 From 703361bb82c9ea6e52525cfa0abb5b636eb55186 Mon Sep 17 00:00:00 2001 From: elifarley Date: Wed, 8 Feb 2017 16:57:53 -0200 Subject: Remove all "e.printStackTrace()" calls --- .../bitbucketpullrequestbuilder/bitbucket/ApiClient.java | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index 759c696..0c87478 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -119,10 +119,8 @@ public class ApiClient { return new String(Hex.encodeHex(SHA1.digest(computedKey.getBytes("UTF-8")))); } catch(NoSuchAlgorithmException e) { logger.log(Level.WARNING, "Failed to create hash provider", e); - e.printStackTrace(); } catch (UnsupportedEncodingException e) { logger.log(Level.WARNING, "Failed to create hash provider", e); - e.printStackTrace(); } } return (computedKey.length() <= MAX_KEY_SIZE_BB_API) ? computedKey : computedKey.substring(0, MAX_KEY_SIZE_BB_API); @@ -173,7 +171,6 @@ public class ApiClient { new NameValuePair[]{}), Pullrequest.Participant.class); } catch (IOException e) { logger.log(Level.WARNING, "Invalid pull request approval response.", e); - e.printStackTrace(); } return null; } @@ -186,7 +183,6 @@ public class ApiClient { return parse(post(v1("/pullrequests/" + pullRequestId + "/comments"), data), new TypeReference() {}); } catch(Exception e) { logger.log(Level.WARNING, "Invalid pull request comment response.", e); - e.printStackTrace(); } return null; } @@ -203,7 +199,6 @@ public class ApiClient { } while (url != null); } catch (Exception e) { logger.log(Level.WARNING, "invalid response.", e); - e.printStackTrace(); } return values; } @@ -255,10 +250,8 @@ public class ApiClient { return req.getResponseBodyAsString(); } catch (HttpException e) { logger.log(Level.WARNING, "Failed to send request.", e); - e.printStackTrace(); } catch (IOException e) { logger.log(Level.WARNING, "Failed to send request.", e); - e.printStackTrace(); } finally { req.releaseConnection(); } -- cgit v1.2.3 From d79f5c0fdaf9a40970c83fb33916a17a060b198f Mon Sep 17 00:00:00 2001 From: elifarley Date: Wed, 8 Feb 2017 16:59:36 -0200 Subject: Remove all "e.printStackTrace()" calls --- .../bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index d337796..899edba 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -69,10 +69,8 @@ public class BitbucketPullRequestsBuilder { return new String(Hex.encodeHex(MD5.digest(this.project.getFullName().getBytes("UTF-8")))); } catch (NoSuchAlgorithmException exc) { logger.log(Level.WARNING, "Failed to produce hash", exc); - exc.printStackTrace(); } catch (UnsupportedEncodingException exc) { logger.log(Level.WARNING, "Failed to produce hash", exc); - exc.printStackTrace(); } return this.project.getFullName(); } -- cgit v1.2.3 From 725311827f86f49edcbbee1988f281dee4c6b4f2 Mon Sep 17 00:00:00 2001 From: Frode Aannevik Date: Fri, 5 Aug 2016 22:53:48 +0200 Subject: Add support for Jenkins 2 (incomplete) Replace use of AbstractProject with Job to fix ClassCastException thrown when using the plugin in a pipeline job The fix is "not" complete, it remains to find a replacement to the call AbstractProject.isDisabled used to decide if the build should be skipped Previously called in BitbucketBuildTrigger.run() fixes #94 --- .../BitbucketBuildTrigger.java | 29 +++++++++++++++------- .../BitbucketPullRequestsBuilder.java | 9 ++++--- 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 90de6b5..373c228 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -14,6 +14,7 @@ import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.util.ListBoxModel; import jenkins.model.Jenkins; +import jenkins.model.ParameterizedJobMixIn; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.kohsuke.stapler.DataBoundConstructor; @@ -33,7 +34,7 @@ import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf; /** * Created by nishio */ -public class BitbucketBuildTrigger extends Trigger> { +public class BitbucketBuildTrigger extends Trigger> { private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); private final String projectPath; private final String cron; @@ -153,7 +154,7 @@ public class BitbucketBuildTrigger extends Trigger> { } @Override - public void start(AbstractProject project, boolean newInstance) { + public void start(Job project, boolean newInstance) { try { this.bitbucketPullRequestsBuilder = BitbucketPullRequestsBuilder.getBuilder(); this.bitbucketPullRequestsBuilder.setProject(project); @@ -175,6 +176,16 @@ public class BitbucketBuildTrigger extends Trigger> { return this.bitbucketPullRequestsBuilder; } + private ParameterizedJobMixIn retrieveScheduleJob(final Job job) { + // TODO 1.621+ use standard method + return new ParameterizedJobMixIn() { + @Override + protected Job asJob() { + return job; + } + }; + } + public QueueTaskFuture startJob(BitbucketCause cause) { Map values = this.getDefaultParameters(); @@ -183,7 +194,10 @@ public class BitbucketBuildTrigger extends Trigger> { abortRunningJobsThatMatch(cause); } - return this.job.scheduleBuild2(0, cause, new ParametersAction(new ArrayList(values.values())), new RevisionParameterAction(cause.getSourceCommitHash())); + return retrieveScheduleJob(this.job).scheduleBuild2(0, + new CauseAction(cause), + new ParametersAction(new ArrayList(values.values())), + new RevisionParameterAction(cause.getSourceCommitHash())); } private void cancelPreviousJobsInQueueThatMatch(@Nonnull BitbucketCause bitbucketCause) { @@ -239,11 +253,8 @@ public class BitbucketBuildTrigger extends Trigger> { @Override public void run() { - if(this.getBuilder().getProject().isDisabled()) { - logger.info("Build Skip."); - } else { - this.bitbucketPullRequestsBuilder.run(); - } + logger.log(Level.INFO, "TODO: check if project is disabled before calling bitbucketPullRequestsBuilder.run()"); + this.bitbucketPullRequestsBuilder.run(); this.getDescriptor().save(); } @@ -259,7 +270,7 @@ public class BitbucketBuildTrigger extends Trigger> { @Override public boolean isApplicable(Item item) { - return true; + return item instanceof Job && item instanceof ParameterizedJobMixIn.ParameterizedJob; } @Override diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index 899edba..42fe562 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -1,7 +1,6 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; -import hudson.model.AbstractProject; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; @@ -11,6 +10,7 @@ import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; +import hudson.model.Job; import org.apache.commons.codec.binary.Hex; /** @@ -18,7 +18,7 @@ import org.apache.commons.codec.binary.Hex; */ public class BitbucketPullRequestsBuilder { private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); - private AbstractProject project; + private Job project; private BitbucketBuildTrigger trigger; private BitbucketRepository repository; private BitbucketBuilds builds; @@ -47,7 +47,7 @@ public class BitbucketPullRequestsBuilder { return this; } - public void setProject(AbstractProject project) { + public void setProject(Job project) { this.project = project; } @@ -55,7 +55,7 @@ public class BitbucketPullRequestsBuilder { this.trigger = trigger; } - public AbstractProject getProject() { + public Job getProject() { return this.project; } @@ -73,6 +73,7 @@ public class BitbucketPullRequestsBuilder { logger.log(Level.WARNING, "Failed to produce hash", exc); } return this.project.getFullName(); + } public BitbucketBuildTrigger getTrigger() { -- cgit v1.2.3 From 066fc6386f08729ac2861e29b4bba91427541724 Mon Sep 17 00:00:00 2001 From: Gazal K Date: Tue, 7 Feb 2017 15:26:14 +1100 Subject: check for disabled build for older jobs looks like pipeline jobs don't have a disable option --- .../bitbucketpullrequestbuilder/BitbucketBuildTrigger.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 373c228..846ecb5 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -253,9 +253,13 @@ public class BitbucketBuildTrigger extends Trigger> { @Override public void run() { - logger.log(Level.INFO, "TODO: check if project is disabled before calling bitbucketPullRequestsBuilder.run()"); - this.bitbucketPullRequestsBuilder.run(); - this.getDescriptor().save(); + Job project = this.getBuilder().getProject(); + if (project instanceof AbstractProject && ((AbstractProject)project).isDisabled()) { + logger.info("Build Skip."); + } else { + this.bitbucketPullRequestsBuilder.run(); + this.getDescriptor().save(); + } } @Override -- cgit v1.2.3 From 0debdfbdf1846f74928e0c4e932475b77e655a06 Mon Sep 17 00:00:00 2001 From: Frode Aannevik Date: Sun, 12 Feb 2017 17:05:15 +0100 Subject: Fix PR build status and update of job description BitbucketBuildListener was not invoked on pipeline jobs Change BitbucketBuildListener to extend from RunListener> so it will invoked to update job description and PR build result --- .../BitbucketBuildListener.java | 48 ++++++++++++++++------ .../BitbucketBuilds.java | 25 +++-------- 2 files changed, 40 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildListener.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildListener.java index 95931ad..9e067e7 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildListener.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildListener.java @@ -2,8 +2,12 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; import hudson.Extension; import hudson.model.AbstractBuild; +import hudson.model.Job; +import hudson.model.Run; import hudson.model.TaskListener; import hudson.model.listeners.RunListener; +import hudson.triggers.Trigger; +import jenkins.model.ParameterizedJobMixIn; import javax.annotation.Nonnull; import java.util.logging.Logger; @@ -12,25 +16,43 @@ import java.util.logging.Logger; * Created by nishio */ @Extension -public class BitbucketBuildListener extends RunListener { - private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); +public class BitbucketBuildListener extends RunListener> { + private static final Logger logger = Logger.getLogger(BitbucketBuildListener.class.getName()); @Override - public void onStarted(AbstractBuild abstractBuild, TaskListener listener) { - logger.info("BuildListener onStarted called."); - BitbucketBuildTrigger trigger = BitbucketBuildTrigger.getTrigger(abstractBuild.getProject()); - if (trigger == null) { - return; + public void onStarted(Run r, TaskListener listener) { + logger.info("BitbucketBuildListener onStarted called."); + BitbucketBuilds builds = builds(r); + if (builds != null) { + builds.onStarted((BitbucketCause) r.getCause(BitbucketCause.class), r); } - trigger.getBuilder().getBuilds().onStarted(abstractBuild); } @Override - public void onCompleted(AbstractBuild abstractBuild, @Nonnull TaskListener listener) { - BitbucketBuildTrigger trigger = BitbucketBuildTrigger.getTrigger(abstractBuild.getProject()); - if (trigger == null) { - return; + public void onCompleted(Run r, @Nonnull TaskListener listener) { + logger.info("BitbucketBuildListener onCompleted called."); + BitbucketBuilds builds = builds(r); + if (builds != null) { + builds.onCompleted((BitbucketCause) r.getCause(BitbucketCause.class), r.getResult(), r.getUrl()); } - trigger.getBuilder().getBuilds().onCompleted(abstractBuild); } + + private BitbucketBuilds builds(Run r) { + BitbucketBuildTrigger trigger = null; + if (r instanceof AbstractBuild) { + trigger = BitbucketBuildTrigger.getTrigger(((AbstractBuild) r).getProject()); + } else { + Job job = r.getParent(); + if (job instanceof ParameterizedJobMixIn.ParameterizedJob) { + + for (Trigger t : ((ParameterizedJobMixIn.ParameterizedJob) job).getTriggers().values()) { + if (t instanceof BitbucketBuildTrigger) { + trigger = (BitbucketBuildTrigger) t; + } + } + } + } + return trigger == null ? null : trigger.getBuilder().getBuilds(); + } + } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java index ea8e892..216287d 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java @@ -1,10 +1,7 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BuildState; -import hudson.model.AbstractBuild; -import hudson.model.Cause; -import hudson.model.Result; -import jenkins.model.Jenkins; +import hudson.model.*; import jenkins.model.JenkinsLocationConfiguration; import java.io.IOException; @@ -24,16 +21,7 @@ public class BitbucketBuilds { this.repository = repository; } - public BitbucketCause getCause(AbstractBuild build) { - Cause cause = build.getCause(BitbucketCause.class); - if (cause == null || !(cause instanceof BitbucketCause)) { - return null; - } - return (BitbucketCause) cause; - } - - public void onStarted(AbstractBuild build) { - BitbucketCause cause = this.getCause(build); + void onStarted(BitbucketCause cause, Run build) { if (cause == null) { return; } @@ -44,24 +32,21 @@ public class BitbucketBuilds { } } - public void onCompleted(AbstractBuild build) { - BitbucketCause cause = this.getCause(build); + void onCompleted(BitbucketCause cause, Result result, String buildUrl) { if (cause == null) { return; } - Result result = build.getResult(); JenkinsLocationConfiguration globalConfig = new JenkinsLocationConfiguration(); String rootUrl = globalConfig.getUrl(); - String buildUrl = ""; if (rootUrl == null) { logger.warning("PLEASE SET JENKINS ROOT URL IN GLOBAL CONFIGURATION FOR BUILD STATE REPORTING"); } else { - buildUrl = rootUrl + build.getUrl(); + buildUrl = rootUrl + buildUrl; BuildState state = result == Result.SUCCESS ? BuildState.SUCCESSFUL : BuildState.FAILED; repository.setBuildStatus(cause, state, buildUrl); } - if ( this.trigger.getApproveIfSuccess() && result == Result.SUCCESS ) { + if (this.trigger.getApproveIfSuccess() && result == Result.SUCCESS) { this.repository.postPullRequestApproval(cause.getPullRequestId()); } } -- cgit v1.2.3