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+ --- README.md | 27 +++++----- pom.xml | 16 ++---- screenshots/bbprb-git-config.png | Bin 0 -> 61606 bytes .../plugins/bbprb/BitbucketBuildTrigger.java | 58 ++++++++++++++------- .../org/jenkinsci/plugins/bbprb/BitbucketEnv.java | 41 --------------- 5 files changed, 57 insertions(+), 85 deletions(-) create mode 100644 screenshots/bbprb-git-config.png delete mode 100644 src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java diff --git a/README.md b/README.md index 2b969ed..4cff261 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ BBPRB This plugin is a revision of original [Bitbucket Pull Request Builder Plugin](https://wiki.jenkins.io/display/JENKINS/Bitbucket+pullrequest+builder+plugin). It was started by adding support for [Bitbucket webhooks](https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html) -and resulted in massive rewrite and deleting the code. +and resulted in massive rewrite and deleting the code. Jenkins 2.60+ is required. Configuration @@ -16,13 +16,7 @@ This plugin needs credentials to access Bitbucket API. The credentials comprise the user's name and a password. This password is known as "App password" and it is not the same as the user's password. -Only Git repositories are supported. No special configration is required -for the [Git plugin](https://plugins.jenkins.io/git), but the repository URL -should point to the same repository specified for this plugins. In fact, -they may not match. After receiving a web-hook this plugin uses its options -to find a job to be triggered, sets some relevant options (like commit hash) -and starts the build. If repositories are not the same, you will just get a -failure because the hash is not found, etc. +Only Git repositories are supported. These environment variables are set when the job is triggered by this plugin: @@ -38,6 +32,19 @@ bbprbSourceCommitHash bbprbSourceRepository ``` +Recommended Git configuration: + +* Repository URL: `git@bitbucket.org:${bbprbDestinationRepository}.git` +* Branch Specifier: `${bbprbSourceCommitHash}` +* [Optional] Branch to merge to: `${bbprbDestinationBranch}` + +![Recommended git configuration](./screenshots/bbprb-git-config.png) + + +Trigger's options in Jenkins graphical user interface: + +![Jenkins trigger configuration](./screenshots/bbprb-config.png) + Trigger's options in the job's XML configuration file: @@ -55,10 +62,6 @@ Trigger's options in the job's XML configuration file: ``` -Trigger's options in Jenkins graphical user interface: - -![Jenkins trigger configuration](./screenshots/bbprb-config.png) - Configuring Bitbucket web-hook ------------------------------ diff --git a/pom.xml b/pom.xml index d6254d7..8be3790 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 2.11 + 3.0 bbprb Bitbucket Pullrequest Builder Plugin @@ -52,18 +52,8 @@ org.jenkins-ci.plugins - git - 2.2.4 - - - org.jenkins-ci - symbol-annotation - 1.1 - - - com.google.guava - guava - 14.0-rc3 + credentials + 2.1.16 diff --git a/screenshots/bbprb-git-config.png b/screenshots/bbprb-git-config.png new file mode 100644 index 0000000..19c1bf7 Binary files /dev/null and b/screenshots/bbprb-git-config.png differ 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