From cdef13254a2bd5c104ddbb392545828879865c16 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sat, 8 Sep 2018 13:01:29 +0300 Subject: Really allow rebuilding pull requests (With the Rebuild plugin - https://wiki.jenkins.io/display/JENKINS/Rebuild+Plugin) * Make builds implicitly parameterized. * Make sure `bbprb*` are passed in environment and preserved. * Update Jenkins requirements to 2.60+ --- .../plugins/bbprb/BitbucketBuildTrigger.java | 58 +++++++++++++++------- .../org/jenkinsci/plugins/bbprb/BitbucketEnv.java | 41 --------------- 2 files changed, 39 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java (limited to 'src/main/java/org/jenkinsci/plugins') diff --git a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java index cc4b285..5481aee 100644 --- a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java @@ -20,7 +20,7 @@ import hudson.model.queue.QueueTaskFuture; import hudson.model.Queue; import hudson.model.Result; import hudson.model.Run; -import hudson.plugins.git.RevisionParameterAction; +import hudson.model.StringParameterValue; import hudson.security.ACL; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; @@ -35,7 +35,6 @@ import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; import jenkins.model.Jenkins; -import jenkins.model.ParameterizedJobMixIn; import net.sf.json.JSONObject; import org.acegisecurity.context.SecurityContext; import org.acegisecurity.context.SecurityContextHolder; @@ -75,6 +74,21 @@ public class BitbucketBuildTrigger extends Trigger> { private transient ApiClient apiClient; + private static final transient ArrayList bbprbSafeParameters = + new ArrayList() { + { + add("bbprbDestinationBranch"); + add("bbprbDestinationCommitHash"); + add("bbprbDestinationRepository"); + add("bbprbPullRequestAuthor"); + add("bbprbPullRequestId"); + add("bbprbPullRequestTitle"); + add("bbprbSourceBranch"); + add("bbprbSourceCommitHash"); + add("bbprbSourceRepository"); + } + }; + public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor(); @@ -143,11 +157,31 @@ public class BitbucketBuildTrigger extends Trigger> { SecurityContextHolder.setContext(orig); } + List bbprb = new ArrayList<>(); + + bbprb.add(new StringParameterValue("bbprbDestinationBranch", + cause.getDestinationBranch())); + bbprb.add(new StringParameterValue("bbprbDestinationCommitHash", + cause.getDestinationCommitHash())); + bbprb.add(new StringParameterValue("bbprbDestinationRepository", + cause.getDestinationRepository())); + bbprb.add(new StringParameterValue("bbprbPullRequestAuthor", + cause.getPullRequestAuthor())); + bbprb.add(new StringParameterValue("bbprbPullRequestId", + cause.getPullRequestId())); + bbprb.add(new StringParameterValue("bbprbPullRequestTitle", + cause.getPullRequestTitle())); + bbprb.add( + new StringParameterValue("bbprbSourceBranch", cause.getSourceBranch())); + bbprb.add(new StringParameterValue("bbprbSourceCommitHash", + cause.getSourceCommitHash())); + bbprb.add(new StringParameterValue("bbprbSourceRepository", + cause.getSourceRepository())); + setPRState(cause, BuildState.INPROGRESS, this.job.getUrl()); - this.job.scheduleBuild2( - 0, cause, new ParametersAction(this.getDefaultParameters()), - new RevisionParameterAction(cause.getSourceCommitHash())); + this.job.scheduleBuild2(0, cause, + new ParametersAction(bbprb, bbprbSafeParameters)); } private void @@ -222,20 +256,6 @@ public class BitbucketBuildTrigger extends Trigger> { return false; } - private ArrayList getDefaultParameters() { - Map values = new HashMap(); - ParametersDefinitionProperty definitionProperty = - this.job.getProperty(ParametersDefinitionProperty.class); - - if (definitionProperty != null) { - for (ParameterDefinition definition : - definitionProperty.getParameterDefinitions()) { - values.put(definition.getName(), definition.getDefaultParameterValue()); - } - } - return new ArrayList(values.values()); - } - public void handlePR(JSONObject pr) { JSONObject src = pr.getJSONObject("source"); JSONObject dst = pr.getJSONObject("destination"); diff --git a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java b/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java deleted file mode 100644 index 816f57d..0000000 --- a/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.jenkinsci.plugins.bbprb; - -import hudson.EnvVars; -import hudson.Extension; -import hudson.model.*; - -import java.io.IOException; - -@Extension -public class BitbucketEnv extends EnvironmentContributor { - @Override - public void buildEnvironmentFor(Run run, EnvVars envVars, - TaskListener taskListener) - throws IOException, InterruptedException { - - BitbucketCause cause = (BitbucketCause)run.getCause(BitbucketCause.class); - if (cause == null) { - return; - } - - putEnvVar(envVars, "bbprbDestinationBranch", cause.getDestinationBranch()); - putEnvVar(envVars, "bbprbDestinationCommitHash", - cause.getDestinationCommitHash()); - putEnvVar(envVars, "bbprbDestinationRepository", - cause.getDestinationRepository()); - putEnvVar(envVars, "bbprbPullRequestAuthor", cause.getPullRequestAuthor()); - putEnvVar(envVars, "bbprbPullRequestId", cause.getPullRequestId()); - putEnvVar(envVars, "bbprbPullRequestTitle", cause.getPullRequestTitle()); - putEnvVar(envVars, "bbprbSourceBranch", cause.getSourceBranch()); - putEnvVar(envVars, "bbprbSourceCommitHash", cause.getSourceCommitHash()); - putEnvVar(envVars, "bbprbSourceRepository", cause.getSourceRepository()); - } - - private static void putEnvVar(EnvVars envs, String name, String value) { - envs.put(name, getString(value, "")); - } - - private static String getString(String actual, String d) { - return actual == null ? d : actual; - } -} -- cgit v1.2.3