diff --git a/src/main/java/com/gradle/CustomBuildScanEnhancements.java b/src/main/java/com/gradle/CustomBuildScanEnhancements.java index b19de30b..ebd74c74 100644 --- a/src/main/java/com/gradle/CustomBuildScanEnhancements.java +++ b/src/main/java/com/gradle/CustomBuildScanEnhancements.java @@ -22,6 +22,7 @@ import static com.gradle.Utils.execAndCheckSuccess; 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. */ @@ -212,7 +213,7 @@ private void captureCiMetadata() { customValueSearchLinker.addCustomValueAndSearchLink("CI stage", value)); } - if(isAzurePipelines()) { + if (isAzurePipelines()) { Optional azureServerUrl = envVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"); Optional azureProject = envVariable("SYSTEM_TEAMPROJECT"); Optional buildId = envVariable("BUILD_BUILDID"); @@ -298,12 +299,12 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin @Override public void execute(BuildScanExtension buildScan) { - 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(); + GitMetadataResolver gitMetadataResolver = new GitMetadataResolver(isGitInstalled()); + Optional gitRepo = gitMetadataResolver.resolve("git", "config", "--get", "remote.origin.url"); + Optional gitCommitId = gitMetadataResolver.resolve("git", "rev-parse", "--verify", "HEAD"); + Optional gitCommitShortId = gitMetadataResolver.resolve("git", "rev-parse", "--short=8", "--verify", "HEAD"); + Optional gitStatus = gitMetadataResolver.resolve("git", "status", "--porcelain"); + Optional gitBranchName = gitMetadataResolver.resolve(this::getGitBranchNameFromEnv, "git", "rev-parse", "--abbrev-ref", "HEAD"); gitRepo.ifPresent(s -> buildScan.value("Git repository", redactUserInfo(s))); gitCommitId.ifPresent(s -> buildScan.value("Git commit id", s)); diff --git a/src/main/java/com/gradle/GitMetadata.java b/src/main/java/com/gradle/GitMetadata.java deleted file mode 100644 index 99dad912..00000000 --- a/src/main/java/com/gradle/GitMetadata.java +++ /dev/null @@ -1,54 +0,0 @@ -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/GitMetadataResolver.java b/src/main/java/com/gradle/GitMetadataResolver.java new file mode 100644 index 00000000..10217f9a --- /dev/null +++ b/src/main/java/com/gradle/GitMetadataResolver.java @@ -0,0 +1,29 @@ +package com.gradle; + +import java.util.Optional; +import java.util.function.Supplier; + +import static com.gradle.Utils.execAndGetStdOut; + +class GitMetadataResolver { + + private final boolean isGitInstalled; + + GitMetadataResolver(boolean isGitInstalled) { + this.isGitInstalled = isGitInstalled; + } + + Optional resolve(String... args) { + return resolve( null, args); + } + + Optional resolve(Supplier> fromEnv, String... args) { + if (fromEnv != null) { + return fromEnv.get(); + } + if (isGitInstalled && args != null) { + return execAndGetStdOut(args); + } + return Optional.empty(); + } +}