diff options
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder')
4 files changed, 70 insertions, 47 deletions
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<AbstractBuild> { - private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); +public class BitbucketBuildListener extends RunListener<Run<?, ?>> { + 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/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 90de6b5..846ecb5 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<AbstractProject<?, ?>> { +public class BitbucketBuildTrigger extends Trigger<Job<?, ?>> { 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<AbstractProject<?, ?>> { } @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<AbstractProject<?, ?>> { 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<String, ParameterValue> values = this.getDefaultParameters(); @@ -183,7 +194,10 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { 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,12 +253,13 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { @Override public void run() { - if(this.getBuilder().getProject().isDisabled()) { - logger.info("Build Skip."); - } else { - 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 @@ -259,7 +274,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { @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/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()); } } 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() { |