From 8848748deb57205e102355e7ad535030d2c6b037 Mon Sep 17 00:00:00 2001 From: Andrew Heuermann Date: Thu, 19 Nov 2015 21:21:39 -0600 Subject: [PATCH 1/2] Set target and source branch environment variables for pull requests --- .../dynamic/build/cause/BuildCause.java | 9 +++---- .../build/cause/GitHubPullRequestCause.java | 24 ++++++++++++++++++- .../groupon/jenkins/github/GithubWebhook.java | 1 - .../com/groupon/jenkins/github/Payload.java | 17 +++++++++++-- .../dynamic/build/cause/BuildCauseTest.java | 13 ++++++++++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/groupon/jenkins/dynamic/build/cause/BuildCause.java b/src/main/java/com/groupon/jenkins/dynamic/build/cause/BuildCause.java index 2067545e..e7873640 100644 --- a/src/main/java/com/groupon/jenkins/dynamic/build/cause/BuildCause.java +++ b/src/main/java/com/groupon/jenkins/dynamic/build/cause/BuildCause.java @@ -25,14 +25,9 @@ of this software and associated documentation files (the "Software"), to deal import com.groupon.jenkins.git.GitBranch; import com.groupon.jenkins.github.Payload; -import com.jcraft.jsch.jce.MD5; import hudson.model.Cause; -import java.io.UnsupportedEncodingException; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; @@ -56,6 +51,7 @@ public abstract class BuildCause extends Cause { public abstract String getPullRequestNumber(); public abstract CommitInfo getCommitInfo(); + @Exported public abstract String getName(); @@ -67,7 +63,7 @@ public Map getEnvVars() { return vars; } - private static void putIfNotNull(Map vars, String key, String value) { + protected static void putIfNotNull(Map vars, String key, String value) { if (value != null) { vars.put(key, value); } @@ -88,6 +84,7 @@ public static class CommitInfo{ private String branch; private final String sha; private String commitUrl; + //for backward compat private CommitInfo(){ this.committerEmail = "unknown@unknown.com"; diff --git a/src/main/java/com/groupon/jenkins/dynamic/build/cause/GitHubPullRequestCause.java b/src/main/java/com/groupon/jenkins/dynamic/build/cause/GitHubPullRequestCause.java index fe1fcf62..76b0598f 100644 --- a/src/main/java/com/groupon/jenkins/dynamic/build/cause/GitHubPullRequestCause.java +++ b/src/main/java/com/groupon/jenkins/dynamic/build/cause/GitHubPullRequestCause.java @@ -25,15 +25,29 @@ of this software and associated documentation files (the "Software"), to deal import com.groupon.jenkins.github.Payload; +import java.util.Map; + public class GitHubPullRequestCause extends GithubCause { private final String label; private final String number; + private final String targetBranch; + private final String sourceBranch; - public GitHubPullRequestCause(Payload payload, String sha, String label, String number) { + public GitHubPullRequestCause(Payload payload, String sha, String label, String number, String sourceBranch, String targetBranch) { super(payload, sha); this.label = label; this.number = number; + this.targetBranch = targetBranch; + this.sourceBranch = sourceBranch; + + } + @Override + public Map getEnvVars() { + Map vars = super.getEnvVars(); + putIfNotNull(vars, "DOTCI_PULL_REQUEST_TARGET_BRANCH", getTargetBranch()); + putIfNotNull(vars, "DOTCI_PULL_REQUEST_SOURCE_BRANCH", getSourceBranch()); + return vars; } @Override @@ -45,4 +59,12 @@ public String getShortDescription() { public String getName() { return "GITHUB_PULL_REQUEST"; } + + public String getTargetBranch() { + return targetBranch; + } + + public String getSourceBranch() { + return sourceBranch; + } } diff --git a/src/main/java/com/groupon/jenkins/github/GithubWebhook.java b/src/main/java/com/groupon/jenkins/github/GithubWebhook.java index 7e5f1d87..9f1e096b 100644 --- a/src/main/java/com/groupon/jenkins/github/GithubWebhook.java +++ b/src/main/java/com/groupon/jenkins/github/GithubWebhook.java @@ -28,7 +28,6 @@ of this software and associated documentation files (the "Software"), to deal import com.groupon.jenkins.dynamic.build.DynamicProject; import com.groupon.jenkins.dynamic.build.repository.DynamicProjectRepository; import hudson.Extension; -import hudson.model.AbstractProject; import hudson.model.Job; import hudson.model.ParameterDefinition; import hudson.model.ParameterValue; diff --git a/src/main/java/com/groupon/jenkins/github/Payload.java b/src/main/java/com/groupon/jenkins/github/Payload.java index 4ef734dc..99683ae8 100644 --- a/src/main/java/com/groupon/jenkins/github/Payload.java +++ b/src/main/java/com/groupon/jenkins/github/Payload.java @@ -50,8 +50,7 @@ public Cause getCause() { JSONObject pullRequest = getPullRequest(); final String label = pullRequest.getJSONObject("head").getString("label"); String number = pullRequest.getString("number"); - return new GitHubPullRequestCause(this, getSha(), label, number); - + return new GitHubPullRequestCause(this, getSha(), label, number, getPullRequestSourceBranch(), getPullRequestTargetBranch()); } else { final String pusherName = payloadJson.getJSONObject("pusher").getString("name"); final String email = payloadJson.getJSONObject("pusher").getString("email"); @@ -97,6 +96,20 @@ private boolean isPullRequestFromWithinSameRepo() { return headRepoUrl.equals(pullRequestRepoUrl); } + public String getPullRequestSourceBranch() { + if (!isPullRequest()) { + return null; + } + return getPullRequest().getJSONObject("head").getString("ref"); + } + + public String getPullRequestTargetBranch() { + if (!isPullRequest()) { + return null; + } + return getPullRequest().getJSONObject("base").getString("ref"); + } + public String getBranchDescription() { if (isPullRequest()) { return "Pull Request " + getPullRequestNumber(); diff --git a/src/test/java/com/groupon/jenkins/dynamic/build/cause/BuildCauseTest.java b/src/test/java/com/groupon/jenkins/dynamic/build/cause/BuildCauseTest.java index 16d8c7a2..376da0e4 100644 --- a/src/test/java/com/groupon/jenkins/dynamic/build/cause/BuildCauseTest.java +++ b/src/test/java/com/groupon/jenkins/dynamic/build/cause/BuildCauseTest.java @@ -51,6 +51,19 @@ public void should_export_env_vars() { assertEquals("44", cause.getEnvVars().get("DOTCI_PULL_REQUEST")); } + @Test + public void should_export_pr_env_vars() { + GitHubPullRequestCause cause = mock(GitHubPullRequestCause.class); + doCallRealMethod().when(cause).getEnvVars(); + + doReturn("feature").when(cause).getSourceBranch(); + doReturn("master").when(cause).getTargetBranch(); + + assertNotNull(cause.getEnvVars()); + assertEquals("feature", cause.getEnvVars().get("DOTCI_PULL_REQUEST_SOURCE_BRANCH")); + assertEquals("master", cause.getEnvVars().get("DOTCI_PULL_REQUEST_TARGET_BRANCH")); + } + @Test public void should_export_not_export_null_env_vars() { BuildCause cause = mock(BuildCause.class); From 33b08140d71defcb7531cf5af98bd6627fdb1990 Mon Sep 17 00:00:00 2001 From: Andrew Heuermann Date: Thu, 19 Nov 2015 21:37:43 -0600 Subject: [PATCH 2/2] Adding docs for target and source branch --- docs/user-guide/Templating.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/user-guide/Templating.md b/docs/user-guide/Templating.md index 54ba0e0e..572e59e1 100644 --- a/docs/user-guide/Templating.md +++ b/docs/user-guide/Templating.md @@ -13,6 +13,8 @@ DotCi provides the following global variables: * `GIT_URL` - git url * `DOTCI_PUSHER` - github username whose git push triggred this build * `DOTCI_PULL_REQUEST` - pull request number being built +* `DOTCI_PULL_REQUEST_SOURCE_BRANCH` - the pull request branch +* `DOTCI_PULL_REQUEST_TARGET_BRANCH` - the branch the pull request is targeting ## Groovy templating