aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2018-09-08 13:01:29 +0300
committerIgor Pashev <pashev.igor@gmail.com>2018-09-08 13:24:32 +0300
commitcdef13254a2bd5c104ddbb392545828879865c16 (patch)
treec9fdecb25384582f8ede335ed540ea99e74b79bf
parent94a11c031fca39dc927fa4dd1c362e09df840e8b (diff)
downloadbbprb-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+
-rw-r--r--README.md27
-rw-r--r--pom.xml16
-rw-r--r--screenshots/bbprb-git-config.pngbin0 -> 61606 bytes
-rw-r--r--src/main/java/org/jenkinsci/plugins/bbprb/BitbucketBuildTrigger.java58
-rw-r--r--src/main/java/org/jenkinsci/plugins/bbprb/BitbucketEnv.java41
5 files changed, 57 insertions, 85 deletions
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 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
- <version>2.11</version>
+ <version>3.0</version>
</parent>
<artifactId>bbprb</artifactId>
<name>Bitbucket Pullrequest Builder Plugin</name>
@@ -52,18 +52,8 @@
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
- <artifactId>git</artifactId>
- <version>2.2.4</version>
- </dependency>
- <dependency>
- <groupId>org.jenkins-ci</groupId>
- <artifactId>symbol-annotation</artifactId>
- <version>1.1</version>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>14.0-rc3</version>
+ <artifactId>credentials</artifactId>
+ <version>2.1.16</version>
</dependency>
</dependencies>
<pluginRepositories>
diff --git a/screenshots/bbprb-git-config.png b/screenshots/bbprb-git-config.png
new file mode 100644
index 0000000..19c1bf7
--- /dev/null
+++ b/screenshots/bbprb-git-config.png
Binary files 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<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;
- }
-}