Skip to content

Commit

Permalink
Merge pull request #195 from aheuermann/target_branch
Browse files Browse the repository at this point in the history
Adding target and source branch env variables for pull requests
  • Loading branch information
suryagaddipati committed Nov 20, 2015
2 parents 5e53026 + 33b0814 commit 85673ee
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/user-guide/Templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -56,6 +51,7 @@ public abstract class BuildCause extends Cause {
public abstract String getPullRequestNumber();

public abstract CommitInfo getCommitInfo();

@Exported
public abstract String getName();

Expand All @@ -67,7 +63,7 @@ public Map<String, String> getEnvVars() {
return vars;
}

private static void putIfNotNull(Map<String, String> vars, String key, String value) {
protected static void putIfNotNull(Map<String, String> vars, String key, String value) {
if (value != null) {
vars.put(key, value);
}
Expand All @@ -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 = "[email protected]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> getEnvVars() {
Map vars = super.getEnvVars();
putIfNotNull(vars, "DOTCI_PULL_REQUEST_TARGET_BRANCH", getTargetBranch());
putIfNotNull(vars, "DOTCI_PULL_REQUEST_SOURCE_BRANCH", getSourceBranch());
return vars;
}

@Override
Expand All @@ -45,4 +59,12 @@ public String getShortDescription() {
public String getName() {
return "GITHUB_PULL_REQUEST";
}

public String getTargetBranch() {
return targetBranch;
}

public String getSourceBranch() {
return sourceBranch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/groupon/jenkins/github/Payload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 85673ee

Please sign in to comment.