Skip to content

Commit f978b5c

Browse files
committed
Get branch name from env var (#74)
1 parent 3bcb219 commit f978b5c

File tree

3 files changed

+70
-43
lines changed

3 files changed

+70
-43
lines changed

src/main/java/com/gradle/CustomBuildScanEnhancements.java

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
import java.util.Map;
1515
import java.util.Optional;
1616
import java.util.Properties;
17-
import java.util.function.Supplier;
1817
import java.util.regex.Matcher;
1918
import java.util.regex.Pattern;
2019
import java.util.stream.Stream;
2120

21+
import static com.gradle.GitMetadata.given;
2222
import static com.gradle.Utils.appendIfMissing;
2323
import static com.gradle.Utils.execAndCheckSuccess;
24-
import static com.gradle.Utils.execAndGetStdOut;
25-
import static com.gradle.Utils.isNotEmpty;
2624
import static com.gradle.Utils.redactUserInfo;
2725
import static com.gradle.Utils.urlEncode;
28-
2926
/**
3027
* Adds a standard set of useful tags, links and custom values to all build scans published.
3128
*/
@@ -281,44 +278,35 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin
281278

282279
@Override
283280
public void execute(BuildScanExtension buildScan) {
284-
if (!isGitInstalled()) {
285-
return;
286-
}
287-
288-
String gitRepo = execAndGetStdOut("git", "config", "--get", "remote.origin.url");
289-
String gitCommitId = execAndGetStdOut("git", "rev-parse", "--verify", "HEAD");
290-
String gitCommitShortId = execAndGetStdOut("git", "rev-parse", "--short=8", "--verify", "HEAD");
291-
String gitBranchName = getGitBranchName(() -> execAndGetStdOut("git", "rev-parse", "--abbrev-ref", "HEAD"));
292-
String gitStatus = execAndGetStdOut("git", "status", "--porcelain");
293-
294-
if (isNotEmpty(gitRepo)) {
295-
buildScan.value("Git repository", redactUserInfo(gitRepo));
296-
}
297-
if (isNotEmpty(gitCommitId)) {
298-
buildScan.value("Git commit id", gitCommitId);
299-
}
300-
if (isNotEmpty(gitCommitShortId)) {
301-
customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", gitCommitShortId);
302-
}
303-
if (isNotEmpty(gitBranchName)) {
304-
buildScan.tag(gitBranchName);
305-
buildScan.value("Git branch", gitBranchName);
306-
}
307-
if (isNotEmpty(gitStatus)) {
281+
boolean isGitInstalled = isGitInstalled();
282+
Optional<String> gitRepo = given(isGitInstalled).fromCmd("git", "config", "--get", "remote.origin.url").resolve();
283+
Optional<String> gitCommitId = given(isGitInstalled).fromCmd("git", "rev-parse", "--verify", "HEAD").resolve();
284+
Optional<String> gitCommitShortId = given(isGitInstalled).fromCmd("git", "rev-parse", "--short=8", "--verify", "HEAD").resolve();
285+
Optional<String> gitBranchName = given(isGitInstalled).fromCmd("git", "rev-parse", "--abbrev-ref", "HEAD").fromEnv(this::getGitBranchNameFromEnv).resolve();
286+
Optional<String> gitStatus = given(isGitInstalled).fromCmd( "git", "status", "--porcelain").resolve();
287+
288+
gitRepo.ifPresent(s -> buildScan.value("Git repository", redactUserInfo(s)));
289+
gitCommitId.ifPresent(s -> buildScan.value("Git commit id", s));
290+
gitCommitShortId.ifPresent(s -> customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", s));
291+
gitBranchName.ifPresent(s -> {
292+
buildScan.tag(s);
293+
buildScan.value("Git branch", s);
294+
});
295+
gitStatus.ifPresent(s -> {
308296
buildScan.tag("Dirty");
309-
buildScan.value("Git status", gitStatus);
310-
}
297+
buildScan.value("Git status", s);
298+
});
311299

312-
if (isNotEmpty(gitRepo) && isNotEmpty(gitCommitId)) {
313-
if (gitRepo.contains("github.com/") || gitRepo.contains("github.com:")) {
314-
Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo);
300+
if (gitRepo.isPresent() && gitCommitId.isPresent()) {
301+
if (gitRepo.get().contains("github.com/") || gitRepo.get().contains("github.com:")) {
302+
Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo.get());
315303
if (matcher.matches()) {
316304
String rawRepoPath = matcher.group(2);
317305
String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath;
318306
buildScan.link("Github source", "https://github.com/" + repoPath + "/tree/" + gitCommitId);
319307
}
320-
} else if (gitRepo.contains("gitlab.com/") || gitRepo.contains("gitlab.com:")) {
321-
Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo);
308+
} else if (gitRepo.get().contains("gitlab.com/") || gitRepo.get().contains("gitlab.com:")) {
309+
Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo.get());
322310
if (matcher.matches()) {
323311
String rawRepoPath = matcher.group(2);
324312
String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath;
@@ -332,14 +320,11 @@ private boolean isGitInstalled() {
332320
return execAndCheckSuccess("git", "--version");
333321
}
334322

335-
private String getGitBranchName(Supplier<String> gitCommand) {
323+
private Optional<String> getGitBranchNameFromEnv() {
336324
if (isJenkins() || isHudson()) {
337-
Optional<String> branch = Utils.envVariable("BRANCH_NAME", providers);
338-
if (branch.isPresent()) {
339-
return branch.get();
340-
}
325+
return Utils.envVariable("BRANCH_NAME", providers);
341326
}
342-
return gitCommand.get();
327+
return Optional.empty();
343328
}
344329

345330
private boolean isJenkins() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.gradle;
2+
3+
import java.util.Optional;
4+
import java.util.function.Supplier;
5+
6+
import static com.gradle.Utils.execAndGetStdOut;
7+
8+
class GitMetadata {
9+
10+
private String[] args;
11+
boolean isGitInstalled;
12+
private Supplier<Optional<String>> fromEnv;
13+
14+
private GitMetadata(boolean isGitInstalled) {
15+
this.isGitInstalled = isGitInstalled;
16+
}
17+
18+
static GitMetadata given(boolean isGitInstalled) {
19+
return new GitMetadata(isGitInstalled);
20+
}
21+
22+
GitMetadata fromCmd(String... args) {
23+
this.args = args;
24+
return this;
25+
}
26+
27+
GitMetadata fromEnv(Supplier<Optional<String>> fromEnv) {
28+
this.fromEnv = fromEnv;
29+
return this;
30+
}
31+
32+
Optional<String> resolve() {
33+
if (fromEnv != null) {
34+
return fromEnv.get();
35+
}
36+
if (isGitInstalled) {
37+
return execAndGetStdOut(args);
38+
}
39+
return Optional.empty();
40+
}
41+
42+
}

src/main/java/com/gradle/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static boolean execAndCheckSuccess(String... args) {
162162
}
163163
}
164164

165-
static String execAndGetStdOut(String... args) {
165+
static Optional<String> execAndGetStdOut(String... args) {
166166
Runtime runtime = Runtime.getRuntime();
167167
Process process;
168168
try {
@@ -177,7 +177,7 @@ static String execAndGetStdOut(String... args) {
177177
String ignore = readFully(error);
178178

179179
boolean finished = process.waitFor(10, TimeUnit.SECONDS);
180-
return finished && process.exitValue() == 0 ? trimAtEnd(standardText) : null;
180+
return finished && process.exitValue() == 0 ? Optional.of(trimAtEnd(standardText)) : Optional.empty();
181181
}
182182
} catch (IOException | InterruptedException e) {
183183
throw new RuntimeException(e);

0 commit comments

Comments
 (0)