From eed8f00e6eafe7272d07ee13cd991633b1393ba8 Mon Sep 17 00:00:00 2001 From: Frode Aannevik Date: Tue, 24 May 2016 21:02:47 +0200 Subject: Adapt to SECURITY-170 Use EnvironmentContributor instead of build parameters Based on https://github.com/jenkinsci/ghprb-plugin/pull/344 Fixes: #86 --- ...tAdditionalParameterEnvironmentContributor.java | 53 ++++++++++++++++++++++ .../BitbucketBuildTrigger.java | 29 +----------- 2 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java new file mode 100644 index 0000000..a4ff97d --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java @@ -0,0 +1,53 @@ +package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; + +import hudson.EnvVars; +import hudson.Extension; +import hudson.model.*; + +import java.io.IOException; +import java.util.ArrayList; + +@Extension +public class BitbucketAdditionalParameterEnvironmentContributor 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; + } + + run.addAction(getDefaultParameters(run)); + + putEnvVar(envVars, "sourceBranch", cause.getSourceBranch()); + putEnvVar(envVars, "targetBranch", cause.getTargetBranch()); + putEnvVar(envVars, "repositoryOwner", cause.getRepositoryOwner()); + putEnvVar(envVars, "repositoryName", cause.getRepositoryName()); + putEnvVar(envVars, "pullRequestId", cause.getPullRequestId()); + putEnvVar(envVars, "destinationRepositoryOwner", cause.getDestinationRepositoryOwner()); + putEnvVar(envVars, "destinationRepositoryName", cause.getDestinationRepositoryName()); + putEnvVar(envVars, "pullRequestTitle", cause.getPullRequestTitle()); + putEnvVar(envVars, "pullRequestAuthor", cause.getPullRequestAuthor()); + + } + + 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; + } + + private ParametersAction getDefaultParameters(Run run) { + ArrayList values = new ArrayList(); + ParametersDefinitionProperty definitionProperty = run.getParent().getProperty(ParametersDefinitionProperty.class); + if (definitionProperty != null) { + for (ParameterDefinition pd : definitionProperty.getParameterDefinitions()) { + values.add(pd.getDefaultParameterValue()); + } + } + return new ParametersAction(values); + } +} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 1120aca..a81e98e 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -16,9 +16,6 @@ import net.sf.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -163,31 +160,7 @@ public class BitbucketBuildTrigger extends Trigger> { } public QueueTaskFuture startJob(BitbucketCause cause) { - Map values = this.getDefaultParameters(); - - values.put("sourceBranch", new StringParameterValue("sourceBranch", cause.getSourceBranch())); - values.put("targetBranch", new StringParameterValue("targetBranch", cause.getTargetBranch())); - values.put("repositoryOwner", new StringParameterValue("repositoryOwner", cause.getRepositoryOwner())); - values.put("repositoryName", new StringParameterValue("repositoryName", cause.getRepositoryName())); - values.put("pullRequestId", new StringParameterValue("pullRequestId", cause.getPullRequestId())); - values.put("destinationRepositoryOwner", new StringParameterValue("destinationRepositoryOwner", cause.getDestinationRepositoryOwner())); - values.put("destinationRepositoryName", new StringParameterValue("destinationRepositoryName", cause.getDestinationRepositoryName())); - values.put("pullRequestTitle", new StringParameterValue("pullRequestTitle", cause.getPullRequestTitle())); - values.put("pullRequestAuthor", new StringParameterValue("pullRequestAuthor", cause.getPullRequestAuthor())); - - return this.job.scheduleBuild2(0, cause, new ParametersAction(new ArrayList(values.values())), new RevisionParameterAction(cause.getSourceCommitHash())); - } - - private Map 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 values; + return this.job.scheduleBuild2(0, cause, new RevisionParameterAction(cause.getSourceCommitHash())); } @Override -- cgit v1.2.3 From 5485845edafde360cd43f035ca329cfe3baba49a Mon Sep 17 00:00:00 2001 From: Frode Aannevik Date: Wed, 25 May 2016 17:30:00 +0200 Subject: remove adding parameters action in the EnvironmentContributor move it back to BitbucketBuildTrigger to fix the the problem that a lot of empty "Parameters" links are added to each build --- ...ucketAdditionalParameterEnvironmentContributor.java | 13 ------------- .../BitbucketBuildTrigger.java | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java index a4ff97d..c531ca1 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketAdditionalParameterEnvironmentContributor.java @@ -5,7 +5,6 @@ import hudson.Extension; import hudson.model.*; import java.io.IOException; -import java.util.ArrayList; @Extension public class BitbucketAdditionalParameterEnvironmentContributor extends EnvironmentContributor { @@ -18,8 +17,6 @@ public class BitbucketAdditionalParameterEnvironmentContributor extends Environm return; } - run.addAction(getDefaultParameters(run)); - putEnvVar(envVars, "sourceBranch", cause.getSourceBranch()); putEnvVar(envVars, "targetBranch", cause.getTargetBranch()); putEnvVar(envVars, "repositoryOwner", cause.getRepositoryOwner()); @@ -40,14 +37,4 @@ public class BitbucketAdditionalParameterEnvironmentContributor extends Environm return actual == null ? d : actual; } - private ParametersAction getDefaultParameters(Run run) { - ArrayList values = new ArrayList(); - ParametersDefinitionProperty definitionProperty = run.getParent().getProperty(ParametersDefinitionProperty.class); - if (definitionProperty != null) { - for (ParameterDefinition pd : definitionProperty.getParameterDefinitions()) { - values.add(pd.getDefaultParameterValue()); - } - } - return new ParametersAction(values); - } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index a81e98e..aedfb91 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -16,6 +16,9 @@ import net.sf.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -160,7 +163,20 @@ public class BitbucketBuildTrigger extends Trigger> { } public QueueTaskFuture startJob(BitbucketCause cause) { - return this.job.scheduleBuild2(0, cause, new RevisionParameterAction(cause.getSourceCommitHash())); + Map values = this.getDefaultParameters(); + return this.job.scheduleBuild2(0, cause, new ParametersAction(new ArrayList(values.values())), new RevisionParameterAction(cause.getSourceCommitHash())); + } + + private Map 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 values; } @Override -- cgit v1.2.3