Skip to content

Commit

Permalink
Get branch name from env var, refactor (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextu committed Jul 25, 2022
1 parent 4b70d9d commit 9024531
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 61 deletions.
15 changes: 8 additions & 7 deletions src/main/java/com/gradle/CustomBuildScanEnhancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -212,7 +213,7 @@ private void captureCiMetadata() {
customValueSearchLinker.addCustomValueAndSearchLink("CI stage", value));
}

if(isAzurePipelines()) {
if (isAzurePipelines()) {
Optional<String> azureServerUrl = envVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
Optional<String> azureProject = envVariable("SYSTEM_TEAMPROJECT");
Optional<String> buildId = envVariable("BUILD_BUILDID");
Expand Down Expand Up @@ -298,12 +299,12 @@ private CaptureGitMetadataAction(ProviderFactory providers, CustomValueSearchLin

@Override
public void execute(BuildScanExtension buildScan) {
GitMetadata.GitMetadataBuilder builder = new GitMetadata.GitMetadataBuilder(isGitInstalled());
Optional<String> gitRepo = builder.fromCmd("git", "config", "--get", "remote.origin.url").build().resolve();
Optional<String> gitCommitId = builder.fromCmd("git", "rev-parse", "--verify", "HEAD").build().resolve();
Optional<String> gitCommitShortId = builder.fromCmd("git", "rev-parse", "--short=8", "--verify", "HEAD").build().resolve();
Optional<String> gitStatus = builder.fromCmd( "git", "status", "--porcelain").build().resolve();
Optional<String> gitBranchName = builder.fromCmd("git", "rev-parse", "--abbrev-ref", "HEAD").fromEnv(this::getGitBranchNameFromEnv).build().resolve();
GitMetadataResolver gitMetadataResolver = new GitMetadataResolver(isGitInstalled());
Optional<String> gitRepo = gitMetadataResolver.resolve("git", "config", "--get", "remote.origin.url");
Optional<String> gitCommitId = gitMetadataResolver.resolve("git", "rev-parse", "--verify", "HEAD");
Optional<String> gitCommitShortId = gitMetadataResolver.resolve("git", "rev-parse", "--short=8", "--verify", "HEAD");
Optional<String> gitStatus = gitMetadataResolver.resolve("git", "status", "--porcelain");
Optional<String> 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));
Expand Down
54 changes: 0 additions & 54 deletions src/main/java/com/gradle/GitMetadata.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/java/com/gradle/GitMetadataResolver.java
Original file line number Diff line number Diff line change
@@ -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<String> resolve(String... args) {
return resolve( null, args);
}

Optional<String> resolve(Supplier<Optional<String>> fromEnv, String... args) {
if (fromEnv != null) {
return fromEnv.get();
}
if (isGitInstalled && args != null) {
return execAndGetStdOut(args);
}
return Optional.empty();
}
}

0 comments on commit 9024531

Please sign in to comment.