Skip to content

Commit

Permalink
Do not use commit SHA as branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Dec 18, 2024
1 parent 9bd7908 commit 6febc15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 6febc15

Please sign in to comment.