Skip to content

Commit 4b70d9d

Browse files
committed
Get branch name from env var (#74)
1 parent 1035b44 commit 4b70d9d

File tree

3 files changed

+83
-51
lines changed

3 files changed

+83
-51
lines changed

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

+27-49
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@
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

2221
import static com.gradle.Utils.appendIfMissing;
2322
import static com.gradle.Utils.execAndCheckSuccess;
24-
import static com.gradle.Utils.execAndGetStdOut;
25-
import static com.gradle.Utils.isNotEmpty;
2623
import static com.gradle.Utils.redactUserInfo;
2724
import static com.gradle.Utils.urlEncode;
28-
2925
/**
3026
* Adds a standard set of useful tags, links and custom values to all build scans published.
3127
*/
@@ -302,44 +298,35 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin
302298

303299
@Override
304300
public void execute(BuildScanExtension buildScan) {
305-
if (!isGitInstalled()) {
306-
return;
307-
}
308-
309-
String gitRepo = execAndGetStdOut("git", "config", "--get", "remote.origin.url");
310-
String gitCommitId = execAndGetStdOut("git", "rev-parse", "--verify", "HEAD");
311-
String gitCommitShortId = execAndGetStdOut("git", "rev-parse", "--short=8", "--verify", "HEAD");
312-
String gitBranchName = getGitBranchName(() -> execAndGetStdOut("git", "rev-parse", "--abbrev-ref", "HEAD"));
313-
String gitStatus = execAndGetStdOut("git", "status", "--porcelain");
314-
315-
if (isNotEmpty(gitRepo)) {
316-
buildScan.value("Git repository", redactUserInfo(gitRepo));
317-
}
318-
if (isNotEmpty(gitCommitId)) {
319-
buildScan.value("Git commit id", gitCommitId);
320-
}
321-
if (isNotEmpty(gitCommitShortId)) {
322-
customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", gitCommitShortId);
323-
}
324-
if (isNotEmpty(gitBranchName)) {
325-
buildScan.tag(gitBranchName);
326-
buildScan.value("Git branch", gitBranchName);
327-
}
328-
if (isNotEmpty(gitStatus)) {
301+
GitMetadata.GitMetadataBuilder builder = new GitMetadata.GitMetadataBuilder(isGitInstalled());
302+
Optional<String> gitRepo = builder.fromCmd("git", "config", "--get", "remote.origin.url").build().resolve();
303+
Optional<String> gitCommitId = builder.fromCmd("git", "rev-parse", "--verify", "HEAD").build().resolve();
304+
Optional<String> gitCommitShortId = builder.fromCmd("git", "rev-parse", "--short=8", "--verify", "HEAD").build().resolve();
305+
Optional<String> gitStatus = builder.fromCmd( "git", "status", "--porcelain").build().resolve();
306+
Optional<String> gitBranchName = builder.fromCmd("git", "rev-parse", "--abbrev-ref", "HEAD").fromEnv(this::getGitBranchNameFromEnv).build().resolve();
307+
308+
gitRepo.ifPresent(s -> buildScan.value("Git repository", redactUserInfo(s)));
309+
gitCommitId.ifPresent(s -> buildScan.value("Git commit id", s));
310+
gitCommitShortId.ifPresent(s -> customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", s));
311+
gitBranchName.ifPresent(s -> {
312+
buildScan.tag(s);
313+
buildScan.value("Git branch", s);
314+
});
315+
gitStatus.ifPresent(s -> {
329316
buildScan.tag("Dirty");
330-
buildScan.value("Git status", gitStatus);
331-
}
317+
buildScan.value("Git status", s);
318+
});
332319

333-
if (isNotEmpty(gitRepo) && isNotEmpty(gitCommitId)) {
334-
if (gitRepo.contains("github.com/") || gitRepo.contains("github.com:")) {
335-
Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo);
320+
if (gitRepo.isPresent() && gitCommitId.isPresent()) {
321+
if (gitRepo.get().contains("github.com/") || gitRepo.get().contains("github.com:")) {
322+
Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo.get());
336323
if (matcher.matches()) {
337324
String rawRepoPath = matcher.group(2);
338325
String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath;
339326
buildScan.link("Github source", "https://github.com/" + repoPath + "/tree/" + gitCommitId);
340327
}
341-
} else if (gitRepo.contains("gitlab.com/") || gitRepo.contains("gitlab.com:")) {
342-
Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo);
328+
} else if (gitRepo.get().contains("gitlab.com/") || gitRepo.get().contains("gitlab.com:")) {
329+
Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo.get());
343330
if (matcher.matches()) {
344331
String rawRepoPath = matcher.group(2);
345332
String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath;
@@ -353,24 +340,15 @@ private boolean isGitInstalled() {
353340
return execAndCheckSuccess("git", "--version");
354341
}
355342

356-
private String getGitBranchName(Supplier<String> gitCommand) {
343+
private Optional<String> getGitBranchNameFromEnv() {
357344
if (isJenkins() || isHudson()) {
358-
Optional<String> branch = Utils.envVariable("BRANCH_NAME", providers);
359-
if (branch.isPresent()) {
360-
return branch.get();
361-
}
345+
return Utils.envVariable("BRANCH_NAME", providers);
362346
} else if (isGitLab()) {
363-
Optional<String> branch = Utils.envVariable("CI_COMMIT_REF_NAME", providers);
364-
if (branch.isPresent()) {
365-
return branch.get();
366-
}
347+
return Utils.envVariable("CI_COMMIT_REF_NAME", providers);
367348
} else if (isAzurePipelines()) {
368-
Optional<String> branch = Utils.envVariable("BUILD_SOURCEBRANCH", providers);
369-
if (branch.isPresent()) {
370-
return branch.get();
371-
}
349+
return Utils.envVariable("BUILD_SOURCEBRANCH", providers);
372350
}
373-
return gitCommand.get();
351+
return Optional.empty();
374352
}
375353

376354
private boolean isJenkins() {
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 final String[] args;
11+
private final boolean isGitInstalled;
12+
private final Supplier<Optional<String>> fromEnv;
13+
14+
private GitMetadata(GitMetadataBuilder metadataBuilder) {
15+
this.args = metadataBuilder.args;
16+
this.isGitInstalled = metadataBuilder.isGitInstalled;
17+
this.fromEnv = metadataBuilder.fromEnv;
18+
}
19+
20+
Optional<String> resolve() {
21+
if (fromEnv != null) {
22+
return fromEnv.get();
23+
}
24+
if (isGitInstalled && args != null) {
25+
return execAndGetStdOut(args);
26+
}
27+
return Optional.empty();
28+
}
29+
30+
static class GitMetadataBuilder {
31+
private String[] args;
32+
private final boolean isGitInstalled;
33+
private Supplier<Optional<String>> fromEnv;
34+
35+
GitMetadataBuilder(boolean isGitInstalled) {
36+
this.isGitInstalled = isGitInstalled;
37+
}
38+
39+
GitMetadataBuilder fromCmd(String... args) {
40+
this.args = args;
41+
return this;
42+
}
43+
44+
GitMetadataBuilder fromEnv(Supplier<Optional<String>> fromEnv) {
45+
this.fromEnv = fromEnv;
46+
return this;
47+
}
48+
49+
GitMetadata build() {
50+
return new GitMetadata(this);
51+
}
52+
}
53+
54+
}

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

+2-2
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)