Skip to content

Commit

Permalink
Merge pull request #199 from gradle/gk/revertDetachedHead
Browse files Browse the repository at this point in the history
Revert detached head detection
  • Loading branch information
ribafish authored Feb 27, 2024
2 parents 2d366d8 + 8c9e1b6 commit 61d14ee
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
2 changes: 0 additions & 2 deletions release/changes.md
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
- [NEW] Improve tags/values in GitHub Actions `pull_request` workflows
- [NEW] Improve tags/values in Jenkins where `GIT_BRANCH` is available
35 changes: 3 additions & 32 deletions src/main/java/com/gradle/CustomBuildScanEnhancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import org.apache.maven.execution.MavenSession;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static com.gradle.CiUtils.isAzurePipelines;
Expand Down Expand Up @@ -242,8 +242,6 @@ private void execute() {
addCustomValueAndSearchLink(buildScan, "CI workflow", value));
envVariable("GITHUB_RUN_ID").ifPresent(value ->
addCustomValueAndSearchLink(buildScan, "CI run", value));
envVariable("GITHUB_HEAD_REF").ifPresent(value ->
buildScan.value("PR branch", value));
}

if (isGitLab()) {
Expand Down Expand Up @@ -398,14 +396,6 @@ private String getGitBranchName(Supplier<String> gitCommand) {
if (branch.isPresent()) {
return branch.get();
}

Optional<String> gitBranch = envVariable("GIT_BRANCH");
if (gitBranch.isPresent()) {
Optional<String> localBranch = getLocalBranch(gitBranch.get());
if (localBranch.isPresent()) {
return localBranch.get();
}
}
} else if (isGitLab()) {
Optional<String> branch = envVariable("CI_COMMIT_REF_NAME");
if (branch.isPresent()) {
Expand All @@ -416,28 +406,9 @@ private String getGitBranchName(Supplier<String> gitCommand) {
if (branch.isPresent()) {
return branch.get();
}
} else if (isGitHubActions()) {
Optional<String> branch = envVariable("GITHUB_REF_NAME");
if (branch.isPresent()) {
return branch.get();
}
}
return gitCommand.get();
}

private static Optional<String> getLocalBranch(String remoteBranch) {
// This finds the longest matching remote name. This is because, for example, a local git clone could have
// two remotes named `origin` and `origin/two`. In this scenario, we would want a remote branch of
// `origin/two/main` to match to the `origin/two` remote, not to `origin`
Function<String, Optional<String>> findLongestMatchingRemote = remotes -> Arrays.stream(remotes.split("\\R"))
.filter(remote -> remoteBranch.startsWith(remote + "/"))
.max(Comparator.comparingInt(String::length));

return Optional.ofNullable(execAndGetStdOut("git", "remote"))
.filter(Utils::isNotEmpty)
.flatMap(findLongestMatchingRemote)
.map(remote -> remoteBranch.replaceFirst("^" + remote + "/", ""));
}
}

private void captureSkipTestsFlags() {
Expand Down

0 comments on commit 61d14ee

Please sign in to comment.