From a4ea7f7da56ff89480f783731bb76482a39fc30b Mon Sep 17 00:00:00 2001 From: Alexis Tual Date: Fri, 15 Jul 2022 12:40:13 +0200 Subject: [PATCH] Get branch name from env var (#74) --- .../gradle/CustomBuildScanEnhancements.java | 66 +++++++------------ src/main/java/com/gradle/GitMetadata.java | 54 +++++++++++++++ src/main/java/com/gradle/Utils.java | 4 +- 3 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/gradle/GitMetadata.java diff --git a/src/main/java/com/gradle/CustomBuildScanEnhancements.java b/src/main/java/com/gradle/CustomBuildScanEnhancements.java index 3353682e..21d677af 100644 --- a/src/main/java/com/gradle/CustomBuildScanEnhancements.java +++ b/src/main/java/com/gradle/CustomBuildScanEnhancements.java @@ -14,18 +14,14 @@ import java.util.Map; import java.util.Optional; import java.util.Properties; -import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; import static com.gradle.Utils.appendIfMissing; import static com.gradle.Utils.execAndCheckSuccess; -import static com.gradle.Utils.execAndGetStdOut; -import static com.gradle.Utils.isNotEmpty; import static com.gradle.Utils.redactUserInfo; import static com.gradle.Utils.urlEncode; - /** * Adds a standard set of useful tags, links and custom values to all build scans published. */ @@ -281,44 +277,35 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin @Override public void execute(BuildScanExtension buildScan) { - if (!isGitInstalled()) { - return; - } - - String gitRepo = execAndGetStdOut("git", "config", "--get", "remote.origin.url"); - String gitCommitId = execAndGetStdOut("git", "rev-parse", "--verify", "HEAD"); - String gitCommitShortId = execAndGetStdOut("git", "rev-parse", "--short=8", "--verify", "HEAD"); - String gitBranchName = getGitBranchName(() -> execAndGetStdOut("git", "rev-parse", "--abbrev-ref", "HEAD")); - String gitStatus = execAndGetStdOut("git", "status", "--porcelain"); - - if (isNotEmpty(gitRepo)) { - buildScan.value("Git repository", redactUserInfo(gitRepo)); - } - if (isNotEmpty(gitCommitId)) { - buildScan.value("Git commit id", gitCommitId); - } - if (isNotEmpty(gitCommitShortId)) { - customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", gitCommitShortId); - } - if (isNotEmpty(gitBranchName)) { - buildScan.tag(gitBranchName); - buildScan.value("Git branch", gitBranchName); - } - if (isNotEmpty(gitStatus)) { + GitMetadata.GitMetadataBuilder builder = new GitMetadata.GitMetadataBuilder(isGitInstalled()); + Optional gitRepo = builder.fromCmd("git", "config", "--get", "remote.origin.url").build().resolve(); + Optional gitCommitId = builder.fromCmd("git", "rev-parse", "--verify", "HEAD").build().resolve(); + Optional gitCommitShortId = builder.fromCmd("git", "rev-parse", "--short=8", "--verify", "HEAD").build().resolve(); + Optional gitStatus = builder.fromCmd( "git", "status", "--porcelain").build().resolve(); + Optional gitBranchName = builder.fromCmd("git", "rev-parse", "--abbrev-ref", "HEAD").fromEnv(this::getGitBranchNameFromEnv).build().resolve(); + + gitRepo.ifPresent(s -> buildScan.value("Git repository", redactUserInfo(s))); + gitCommitId.ifPresent(s -> buildScan.value("Git commit id", s)); + gitCommitShortId.ifPresent(s -> customValueSearchLinker.addCustomValueAndSearchLink("Git commit id", "Git commit id short", s)); + gitBranchName.ifPresent(s -> { + buildScan.tag(s); + buildScan.value("Git branch", s); + }); + gitStatus.ifPresent(s -> { buildScan.tag("Dirty"); - buildScan.value("Git status", gitStatus); - } + buildScan.value("Git status", s); + }); - if (isNotEmpty(gitRepo) && isNotEmpty(gitCommitId)) { - if (gitRepo.contains("github.com/") || gitRepo.contains("github.com:")) { - Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo); + if (gitRepo.isPresent() && gitCommitId.isPresent()) { + if (gitRepo.get().contains("github.com/") || gitRepo.get().contains("github.com:")) { + Matcher matcher = Pattern.compile("(.*)github\\.com[/|:](.*)").matcher(gitRepo.get()); if (matcher.matches()) { String rawRepoPath = matcher.group(2); String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath; buildScan.link("Github source", "https://github.com/" + repoPath + "/tree/" + gitCommitId); } - } else if (gitRepo.contains("gitlab.com/") || gitRepo.contains("gitlab.com:")) { - Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo); + } else if (gitRepo.get().contains("gitlab.com/") || gitRepo.get().contains("gitlab.com:")) { + Matcher matcher = Pattern.compile("(.*)gitlab\\.com[/|:](.*)").matcher(gitRepo.get()); if (matcher.matches()) { String rawRepoPath = matcher.group(2); String repoPath = rawRepoPath.endsWith(".git") ? rawRepoPath.substring(0, rawRepoPath.length() - 4) : rawRepoPath; @@ -332,14 +319,11 @@ private boolean isGitInstalled() { return execAndCheckSuccess("git", "--version"); } - private String getGitBranchName(Supplier gitCommand) { + private Optional getGitBranchNameFromEnv() { if (isJenkins() || isHudson()) { - Optional branch = Utils.envVariable("BRANCH_NAME", providers); - if (branch.isPresent()) { - return branch.get(); - } + return Utils.envVariable("BRANCH_NAME", providers); } - return gitCommand.get(); + return Optional.empty(); } private boolean isJenkins() { diff --git a/src/main/java/com/gradle/GitMetadata.java b/src/main/java/com/gradle/GitMetadata.java new file mode 100644 index 00000000..99dad912 --- /dev/null +++ b/src/main/java/com/gradle/GitMetadata.java @@ -0,0 +1,54 @@ +package com.gradle; + +import java.util.Optional; +import java.util.function.Supplier; + +import static com.gradle.Utils.execAndGetStdOut; + +class GitMetadata { + + private final String[] args; + private final boolean isGitInstalled; + private final Supplier> fromEnv; + + private GitMetadata(GitMetadataBuilder metadataBuilder) { + this.args = metadataBuilder.args; + this.isGitInstalled = metadataBuilder.isGitInstalled; + this.fromEnv = metadataBuilder.fromEnv; + } + + Optional resolve() { + if (fromEnv != null) { + return fromEnv.get(); + } + if (isGitInstalled && args != null) { + return execAndGetStdOut(args); + } + return Optional.empty(); + } + + static class GitMetadataBuilder { + private String[] args; + private final boolean isGitInstalled; + private Supplier> fromEnv; + + GitMetadataBuilder(boolean isGitInstalled) { + this.isGitInstalled = isGitInstalled; + } + + GitMetadataBuilder fromCmd(String... args) { + this.args = args; + return this; + } + + GitMetadataBuilder fromEnv(Supplier> fromEnv) { + this.fromEnv = fromEnv; + return this; + } + + GitMetadata build() { + return new GitMetadata(this); + } + } + +} diff --git a/src/main/java/com/gradle/Utils.java b/src/main/java/com/gradle/Utils.java index a388e995..4985d8e4 100644 --- a/src/main/java/com/gradle/Utils.java +++ b/src/main/java/com/gradle/Utils.java @@ -162,7 +162,7 @@ static boolean execAndCheckSuccess(String... args) { } } - static String execAndGetStdOut(String... args) { + static Optional execAndGetStdOut(String... args) { Runtime runtime = Runtime.getRuntime(); Process process; try { @@ -177,7 +177,7 @@ static String execAndGetStdOut(String... args) { String ignore = readFully(error); boolean finished = process.waitFor(10, TimeUnit.SECONDS); - return finished && process.exitValue() == 0 ? trimAtEnd(standardText) : null; + return finished && process.exitValue() == 0 ? Optional.of(trimAtEnd(standardText)) : Optional.empty(); } } catch (IOException | InterruptedException e) { throw new RuntimeException(e);