From 6febc1552d3a16477f6b8f464510d5e4f3268d2d Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Wed, 18 Dec 2024 17:13:21 +0100 Subject: [PATCH] Do not use commit SHA as branch name --- .../plugins/datadog/traces/GitInfoUtils.java | 3 +- .../util/git/RepositoryInfoCallback.java | 35 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/GitInfoUtils.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/GitInfoUtils.java index 92eaaf51..d1f91a62 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/GitInfoUtils.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/GitInfoUtils.java @@ -1,5 +1,6 @@ package org.datadog.jenkins.plugins.datadog.traces; +import org.datadog.jenkins.plugins.datadog.util.git.GitUtils; import java.net.URI; import java.net.URISyntaxException; @@ -29,7 +30,7 @@ public static String normalizeTag(String tagName) { * @return normalized git tag */ public static String normalizeBranch(String branchName) { - if(branchName == null || branchName.isEmpty() || branchName.contains("tags")) { + if(branchName == null || branchName.isEmpty() || branchName.contains("tags") || isSha(branchName)) { return null; } diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/RepositoryInfoCallback.java b/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/RepositoryInfoCallback.java index 351341bc..9ff72ea9 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/RepositoryInfoCallback.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/util/git/RepositoryInfoCallback.java @@ -1,13 +1,12 @@ package org.datadog.jenkins.plugins.datadog.util.git; import hudson.remoting.VirtualChannel; -import org.datadog.jenkins.plugins.datadog.traces.GitInfoUtils; -import org.eclipse.jgit.lib.*; -import org.jenkinsci.plugins.gitclient.RepositoryCallback; - import java.io.IOException; import java.util.Set; import java.util.logging.Logger; +import org.datadog.jenkins.plugins.datadog.traces.GitInfoUtils; +import org.eclipse.jgit.lib.*; +import org.jenkinsci.plugins.gitclient.RepositoryCallback; /** * Returns the RepositoryInfo instance for a certain repository @@ -74,20 +73,22 @@ private String getDefaultBranch(Repository repository, String remoteName) throws private static String getBranch(Repository repository) throws IOException { String branch = repository.getBranch(); - if (GitInfoUtils.isSha(branch)) { - // A detached HEAD is checked out. - // Iterate over available refs to see if any of them points to the checked out commit. - for (Ref ref : repository.getRefDatabase().getRefs()) { - String refName = ref.getName(); - if (Constants.HEAD.equals(refName)) { - continue; - } - ObjectId refObjectId = ref.getObjectId(); - if (branch.equals(refObjectId.getName())) { - return refName; - } + if (!GitInfoUtils.isSha(branch)) { + return branch; + } + + // A detached HEAD is checked out. + // Iterate over available refs to see if any of them points to the checked out commit. + for (Ref ref : repository.getRefDatabase().getRefs()) { + String refName = ref.getName(); + if (Constants.HEAD.equals(refName) || GitInfoUtils.isSha(branch)) { + continue; + } + ObjectId refObjectId = ref.getObjectId(); + if (branch.equals(refObjectId.getName())) { + return refName; } } - return branch; + return null; } }