Skip to content

Commit af597fe

Browse files
committed
Merge pull request #80 from if6was9/master
allow git.branch to return accurate information from Jenkins/Hudson jobs
2 parents 0c06cf3 + 0ee7e5b commit af597fe

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.io.Closeables;
2222
import com.google.common.io.Files;
23+
2324
import org.apache.maven.plugin.AbstractMojo;
2425
import org.apache.maven.plugin.MojoExecutionException;
2526
import org.apache.maven.project.MavenProject;
@@ -30,6 +31,7 @@
3031
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
34+
3335
import pl.project13.jgit.DescribeCommand;
3436
import pl.project13.jgit.DescribeResult;
3537
import pl.project13.maven.git.log.LoggerBridge;
@@ -41,6 +43,7 @@
4143
import java.text.SimpleDateFormat;
4244
import java.util.Date;
4345
import java.util.List;
46+
import java.util.Map;
4447
import java.util.Properties;
4548

4649
/**
@@ -379,7 +382,7 @@ void loadGitData(@NotNull Properties properties) throws IOException, MojoExecuti
379382

380383
try {
381384
// git.branch
382-
String branch = git.getBranch();
385+
String branch = determineBranchName(git);
383386
put(properties, BRANCH, branch);
384387

385388
// git.commit.id.describe
@@ -526,6 +529,31 @@ private boolean directoryExists(@Nullable File fileLocation) {
526529
private boolean directoryDoesNotExits(File fileLocation) {
527530
return !directoryExists(fileLocation);
528531
}
532+
533+
/**
534+
* If running within Jenkins/Hudosn, honor the branch name passed via GIT_BRANCH env var. This
535+
* is necessary because Jenkins/Hudson alwways invoke build in a detached head state.
536+
*
537+
* @param git
538+
* @return results of git.getBranch() or, if in Jenkins/Hudson, value of GIT_BRANCH
539+
*/
540+
protected String determineBranchName(Repository git) throws IOException {
541+
Map<String,String> env = System.getenv();
542+
return determineBranchName(git,env);
543+
}
544+
545+
protected String determineBranchName(Repository git, Map<String,String> env) throws IOException {
546+
String branch = git.getBranch();
547+
548+
// Special processing if we're in Jenkins/Hudson
549+
if (env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL")) {
550+
String branchName = env.get("GIT_BRANCH");
551+
if (branchName!=null && branchName.length()>0) {
552+
branch=branchName;
553+
}
554+
}
555+
return branch;
556+
}
529557

530558
// SETTERS FOR TESTS ----------------------------------------------------
531559

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
import org.junit.Before;
2323
import org.junit.Test;
2424

25+
import com.google.common.collect.Maps;
26+
2527
import java.io.File;
28+
import java.io.IOException;
29+
import java.util.Map;
2630
import java.util.Properties;
2731

2832
import static org.fest.assertions.Assertions.assertThat;
2933
import static org.mockito.Mockito.*;
34+
import static org.junit.Assert.assertEquals;
3035

3136
/**
3237
* I'm not a big fan of this test - let's move to integration test from now on.
@@ -90,4 +95,39 @@ public void shouldSkipDescribeWhenConfiguredToDoSo() throws Exception {
9095
verify(mojo, never()).putGitDescribe(any(Properties.class), any(Repository.class));
9196
}
9297

98+
@Test
99+
public void shouldUseJenkinsBranchInfoWhenAvailable() throws IOException {
100+
Repository git = mock(Repository.class);
101+
Map<String,String> env = Maps.newHashMap();
102+
103+
String detachedHeadSHA1 = "16bb801934e652f5e291a003db05e364d83fba25";
104+
String ciUrl = "http://myciserver.com";
105+
106+
when(git.getBranch()).thenReturn(detachedHeadSHA1);
107+
108+
// In a detached head state, getBranch() will return the SHA1...standard behavior
109+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
110+
111+
// Again, SHA1 will be returned if we're in jenkins, but GIT_BRANCH is not set
112+
env.put("JENKINS_URL", "http://myjenkinsserver.com");
113+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
114+
115+
// Now set GIT_BRANCH too and see that the branch name from env var is returned
116+
env.clear();
117+
env.put("JENKINS_URL", ciUrl);
118+
env.put("GIT_BRANCH", "mybranch");
119+
assertEquals("mybranch", mojo.determineBranchName(git, env));
120+
121+
122+
// Same, but for hudson
123+
env.clear();
124+
env.put("GIT_BRANCH", "mybranch");
125+
env.put("HUDSON_URL", ciUrl);
126+
assertEquals("mybranch", mojo.determineBranchName(git, env));
127+
128+
// GIT_BRANCH but no HUDSON_URL or JENKINS_URL
129+
env.clear();
130+
env.put("GIT_BRANCH", "mybranch");
131+
assertEquals(detachedHeadSHA1, mojo.determineBranchName(git, env));
132+
}
93133
}

0 commit comments

Comments
 (0)