diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2018-09-08 13:01:29 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2018-09-08 13:24:32 +0300 |
commit | cdef13254a2bd5c104ddbb392545828879865c16 (patch) | |
tree | c9fdecb25384582f8ede335ed540ea99e74b79bf /src | |
parent | 94a11c031fca39dc927fa4dd1c362e09df840e8b (diff) | |
download | bbprb-cdef13254a2bd5c104ddbb392545828879865c16.tar.gz |
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+
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java | 58 | ||||
-rw-r--r-- | src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java | 41 |
2 files changed, 39 insertions, 60 deletions
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<AbstractProject<?, ?>> { private transient ApiClient apiClient; + private static final transient ArrayList<String> bbprbSafeParameters = + new ArrayList<String>() { + { + 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<AbstractProject<?, ?>> { SecurityContextHolder.setContext(orig); } + List<ParameterValue> 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<AbstractProject<?, ?>> { return false; } - private ArrayList<ParameterValue> getDefaultParameters() { - Map<String, ParameterValue> values = new HashMap<String, ParameterValue>(); - ParametersDefinitionProperty definitionProperty = - this.job.getProperty(ParametersDefinitionProperty.class); - - if (definitionProperty != null) { - for (ParameterDefinition definition : - definitionProperty.getParameterDefinitions()) { - values.put(definition.getName(), definition.getDefaultParameterValue()); - } - } - return new ArrayList<ParameterValue>(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; - } -} |