Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package datadog.trace.civisibility.ci

import datadog.trace.api.Config
import datadog.trace.api.git.GitInfo
import datadog.trace.api.git.GitInfoProvider
import datadog.trace.api.git.UserSuppliedGitInfoBuilder
import datadog.trace.bootstrap.instrumentation.api.Tags
import datadog.trace.civisibility.ci.env.CiEnvironmentImpl
import datadog.trace.civisibility.git.CILocalGitInfoBuilder
import datadog.trace.civisibility.git.CIProviderGitInfoBuilder
import datadog.trace.civisibility.git.tree.GitClient
import datadog.trace.util.Strings
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables
import org.junit.contrib.java.lang.system.RestoreSystemProperties
Expand Down Expand Up @@ -69,7 +69,7 @@ abstract class CITagsProviderTest extends Specification {
environmentVariables.set(it.key, it.value)
}

environmentVariables.set(GitInfo.DD_GIT_COMMIT_SHA, "1234567890123456789012345678901234567890")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_SHA), "1234567890123456789012345678901234567890")

when:
CIProviderInfoFactory ciProviderInfoFactory = new CIProviderInfoFactory(Config.get(), GIT_FOLDER_FOR_TESTS, new CiEnvironmentImpl(System.getenv()))
Expand All @@ -88,7 +88,7 @@ abstract class CITagsProviderTest extends Specification {
environmentVariables.set(it.key, it.value)
}

environmentVariables.set(GitInfo.DD_GIT_REPOSITORY_URL, "local supplied repo url")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_REPOSITORY_URL), "local supplied repo url")

when:
CIProviderInfoFactory ciProviderInfoFactory = new CIProviderInfoFactory(Config.get(), GIT_FOLDER_FOR_TESTS, new CiEnvironmentImpl(System.getenv()))
Expand Down
12 changes: 0 additions & 12 deletions internal-api/src/main/java/datadog/trace/api/git/GitInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ public class GitInfo {

public static final GitInfo NOOP = new GitInfo(null, null, null, CommitInfo.NOOP);

public static final String DD_GIT_REPOSITORY_URL = "DD_GIT_REPOSITORY_URL";
public static final String DD_GIT_BRANCH = "DD_GIT_BRANCH";
public static final String DD_GIT_TAG = "DD_GIT_TAG";
public static final String DD_GIT_COMMIT_SHA = "DD_GIT_COMMIT_SHA";
public static final String DD_GIT_COMMIT_MESSAGE = "DD_GIT_COMMIT_MESSAGE";
public static final String DD_GIT_COMMIT_AUTHOR_NAME = "DD_GIT_COMMIT_AUTHOR_NAME";
public static final String DD_GIT_COMMIT_AUTHOR_EMAIL = "DD_GIT_COMMIT_AUTHOR_EMAIL";
public static final String DD_GIT_COMMIT_AUTHOR_DATE = "DD_GIT_COMMIT_AUTHOR_DATE";
public static final String DD_GIT_COMMIT_COMMITTER_NAME = "DD_GIT_COMMIT_COMMITTER_NAME";
public static final String DD_GIT_COMMIT_COMMITTER_EMAIL = "DD_GIT_COMMIT_COMMITTER_EMAIL";
public static final String DD_GIT_COMMIT_COMMITTER_DATE = "DD_GIT_COMMIT_COMMITTER_DATE";

private final String repositoryURL;
private final String branch;
private final String tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package datadog.trace.api.git;

import static datadog.trace.api.git.GitInfo.DD_GIT_BRANCH;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_AUTHOR_DATE;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_AUTHOR_EMAIL;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_AUTHOR_NAME;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_COMMITTER_DATE;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_COMMITTER_EMAIL;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_COMMITTER_NAME;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_MESSAGE;
import static datadog.trace.api.git.GitInfo.DD_GIT_COMMIT_SHA;
import static datadog.trace.api.git.GitInfo.DD_GIT_REPOSITORY_URL;
import static datadog.trace.api.git.GitInfo.DD_GIT_TAG;

import datadog.trace.api.Config;
import datadog.trace.api.ConfigCollector;
import datadog.trace.api.ConfigOrigin;
import datadog.trace.api.config.GeneralConfig;
import datadog.trace.bootstrap.config.provider.ConfigProvider;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.util.Strings;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserSuppliedGitInfoBuilder implements GitInfoBuilder {

public static final String DD_GIT_REPOSITORY_URL = "git.repository.url";
public static final String DD_GIT_BRANCH = "git.branch";
public static final String DD_GIT_TAG = "git.tag";
public static final String DD_GIT_COMMIT_SHA = "git.commit.sha";
public static final String DD_GIT_COMMIT_MESSAGE = "git.commit.message";
public static final String DD_GIT_COMMIT_AUTHOR_NAME = "git.commit.author.name";
public static final String DD_GIT_COMMIT_AUTHOR_EMAIL = "git.commit.author.email";
public static final String DD_GIT_COMMIT_AUTHOR_DATE = "git.commit.author.date";
public static final String DD_GIT_COMMIT_COMMITTER_NAME = "git.commit.committer.name";
public static final String DD_GIT_COMMIT_COMMITTER_EMAIL = "git.commit.committer.email";
public static final String DD_GIT_COMMIT_COMMITTER_DATE = "git.commit.committer.date";
private static final Logger log = LoggerFactory.getLogger(UserSuppliedGitInfoBuilder.class);

@Override
public GitInfo build(@Nullable String repositoryPath) {
String gitRepositoryUrl = System.getenv(DD_GIT_REPOSITORY_URL);
ConfigProvider configProvider = ConfigProvider.getInstance();

String gitRepositoryUrl = configProvider.getString(DD_GIT_REPOSITORY_URL);
if (gitRepositoryUrl == null) {
gitRepositoryUrl = Config.get().getGlobalTags().get(Tags.GIT_REPOSITORY_URL);
}
Expand All @@ -36,9 +37,9 @@ public GitInfo build(@Nullable String repositoryPath) {
// using the value returned by the CI Provider, so
// we need to normalize the value. Also, it can contain
// the tag (e.g. origin/tags/0.1.0)
String gitTag = System.getenv(DD_GIT_TAG);
String gitTag = configProvider.getString(DD_GIT_TAG);
String gitBranch = null;
final String gitBranchOrTag = System.getenv(DD_GIT_BRANCH);
final String gitBranchOrTag = configProvider.getString(DD_GIT_BRANCH);
if (gitBranchOrTag != null) {
if (!GitUtils.isTagReference(gitBranchOrTag)) {
gitBranch = GitUtils.normalizeBranch(gitBranchOrTag);
Expand All @@ -47,21 +48,18 @@ public GitInfo build(@Nullable String repositoryPath) {
}
}

String gitCommitSha = System.getenv(DD_GIT_COMMIT_SHA);
String gitCommitSha = configProvider.getString(DD_GIT_COMMIT_SHA);
if (gitCommitSha == null) {
gitCommitSha = Config.get().getGlobalTags().get(Tags.GIT_COMMIT_SHA);
}

ConfigCollector.get().put(DD_GIT_REPOSITORY_URL, gitRepositoryUrl, ConfigOrigin.ENV);
ConfigCollector.get().put(DD_GIT_COMMIT_SHA, gitCommitSha, ConfigOrigin.ENV);

final String gitCommitMessage = System.getenv(DD_GIT_COMMIT_MESSAGE);
final String gitCommitAuthorName = System.getenv(DD_GIT_COMMIT_AUTHOR_NAME);
final String gitCommitAuthorEmail = System.getenv(DD_GIT_COMMIT_AUTHOR_EMAIL);
final String gitCommitAuthorDate = System.getenv(DD_GIT_COMMIT_AUTHOR_DATE);
final String gitCommitCommitterName = System.getenv(DD_GIT_COMMIT_COMMITTER_NAME);
final String gitCommitCommitterEmail = System.getenv(DD_GIT_COMMIT_COMMITTER_EMAIL);
final String gitCommitCommitterDate = System.getenv(DD_GIT_COMMIT_COMMITTER_DATE);
final String gitCommitMessage = configProvider.getString(DD_GIT_COMMIT_MESSAGE);
final String gitCommitAuthorName = configProvider.getString(DD_GIT_COMMIT_AUTHOR_NAME);
final String gitCommitAuthorEmail = configProvider.getString(DD_GIT_COMMIT_AUTHOR_EMAIL);
final String gitCommitAuthorDate = configProvider.getString(DD_GIT_COMMIT_AUTHOR_DATE);
final String gitCommitCommitterName = configProvider.getString(DD_GIT_COMMIT_COMMITTER_NAME);
final String gitCommitCommitterEmail = configProvider.getString(DD_GIT_COMMIT_COMMITTER_EMAIL);
final String gitCommitCommitterDate = configProvider.getString(DD_GIT_COMMIT_COMMITTER_DATE);

GitInfo gitInfo =
new GitInfo(
Expand All @@ -82,8 +80,8 @@ public GitInfo build(@Nullable String repositoryPath) {
if (repoUrl == null || repoUrl.isEmpty()) {
log.error(
"Could not resolve git repository URL (can be provided via "
+ GitInfo.DD_GIT_REPOSITORY_URL
+ " env var, "
+ Strings.propertyNameToEnvironmentVariableName(DD_GIT_REPOSITORY_URL)
+ " env var or corresponding system property, "
+ GeneralConfig.TAGS
+ " config property or by embedding git metadata at build time)");
}
Expand All @@ -94,10 +92,10 @@ public GitInfo build(@Nullable String repositoryPath) {
"Git commit SHA could not be resolved or is invalid: "
+ commitSha
+ " (can be provided via "
+ GitInfo.DD_GIT_COMMIT_SHA
+ " env var, "
+ Strings.propertyNameToEnvironmentVariableName(DD_GIT_COMMIT_SHA)
+ " env var or corresponding system property, "
+ GeneralConfig.TAGS
+ " config property or by embedding git metadata at build time; must be a full-length SHA_");
+ " config property or by embedding git metadata at build time; must be a full-length SHA");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class UserSuppliedGitInfoBuilderTest extends DDSpecification {
gitInfo.isEmpty()
}

def "user supplied git info: #envVariable"() {
def "user supplied git info: env var #envVariable"() {
setup:
environmentVariables.set(envVariable, value)
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(envVariable), value)

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)
Expand All @@ -32,23 +32,49 @@ class UserSuppliedGitInfoBuilderTest extends DDSpecification {
gitInfoValueProvider.call(gitInfo) == value

where:
envVariable | value | gitInfoValueProvider
GitInfo.DD_GIT_REPOSITORY_URL | "git repo URL" | { it.repositoryURL }
GitInfo.DD_GIT_BRANCH | "git branch" | { it.branch }
GitInfo.DD_GIT_TAG | "git tag" | { it.tag }
GitInfo.DD_GIT_COMMIT_SHA | "commit SHA" | { it.commit.sha }
GitInfo.DD_GIT_COMMIT_MESSAGE | "commit message" | { it.commit.fullMessage }
GitInfo.DD_GIT_COMMIT_AUTHOR_NAME | "commit author" | { it.commit.author.name }
GitInfo.DD_GIT_COMMIT_AUTHOR_EMAIL | "commit author mail" | { it.commit.author.email }
GitInfo.DD_GIT_COMMIT_AUTHOR_DATE | "2022-12-29T11:38:44.254Z" | { it.commit.author.iso8601Date }
GitInfo.DD_GIT_COMMIT_COMMITTER_NAME | "committer" | { it.commit.committer.name }
GitInfo.DD_GIT_COMMIT_COMMITTER_EMAIL | "committer mail" | { it.commit.committer.email }
GitInfo.DD_GIT_COMMIT_COMMITTER_DATE | "2022-12-29T10:38:44.254Z" | { it.commit.committer.iso8601Date }
envVariable | value | gitInfoValueProvider
UserSuppliedGitInfoBuilder.DD_GIT_REPOSITORY_URL | "git repo URL" | { it.repositoryURL }
UserSuppliedGitInfoBuilder.DD_GIT_BRANCH | "git branch" | { it.branch }
UserSuppliedGitInfoBuilder.DD_GIT_TAG | "git tag" | { it.tag }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_SHA | "commit SHA" | { it.commit.sha }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_MESSAGE | "commit message" | { it.commit.fullMessage }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_NAME | "commit author" | { it.commit.author.name }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_EMAIL | "commit author mail" | { it.commit.author.email }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_DATE | "2022-12-29T11:38:44.254Z" | { it.commit.author.iso8601Date }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_NAME | "committer" | { it.commit.committer.name }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_EMAIL | "committer mail" | { it.commit.committer.email }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_DATE | "2022-12-29T10:38:44.254Z" | { it.commit.committer.iso8601Date }
}

def "user supplied git info: system property #systemProperty"() {
setup:
System.setProperty(Strings.propertyNameToSystemPropertyName(systemProperty), value)

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)

then:
!gitInfo.isEmpty()
gitInfoValueProvider.call(gitInfo) == value

where:
systemProperty | value | gitInfoValueProvider
UserSuppliedGitInfoBuilder.DD_GIT_REPOSITORY_URL | "git repo URL" | { it.repositoryURL }
UserSuppliedGitInfoBuilder.DD_GIT_BRANCH | "git branch" | { it.branch }
UserSuppliedGitInfoBuilder.DD_GIT_TAG | "git tag" | { it.tag }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_SHA | "commit SHA" | { it.commit.sha }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_MESSAGE | "commit message" | { it.commit.fullMessage }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_NAME | "commit author" | { it.commit.author.name }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_EMAIL | "commit author mail" | { it.commit.author.email }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_AUTHOR_DATE | "2022-12-29T11:38:44.254Z" | { it.commit.author.iso8601Date }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_NAME | "committer" | { it.commit.committer.name }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_EMAIL | "committer mail" | { it.commit.committer.email }
UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_COMMITTER_DATE | "2022-12-29T10:38:44.254Z" | { it.commit.committer.iso8601Date }
}

def "branch name is normalized"() {
setup:
environmentVariables.set(GitInfo.DD_GIT_BRANCH, "origin/myBranch")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_BRANCH), "origin/myBranch")

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)
Expand All @@ -60,7 +86,7 @@ class UserSuppliedGitInfoBuilderTest extends DDSpecification {

def "tag can be supplied in branch var"() {
setup:
environmentVariables.set(GitInfo.DD_GIT_BRANCH, "refs/tags/myTag")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_BRANCH), "refs/tags/myTag")

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)
Expand All @@ -73,8 +99,8 @@ class UserSuppliedGitInfoBuilderTest extends DDSpecification {

def "dedicated tag var has preference over tag supplied inside branch var"() {
setup:
environmentVariables.set(GitInfo.DD_GIT_TAG, "myProvidedTag")
environmentVariables.set(GitInfo.DD_GIT_BRANCH, "refs/tags/myTag")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_TAG), "myProvidedTag")
environmentVariables.set(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_BRANCH), "refs/tags/myTag")

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)
Expand All @@ -101,8 +127,8 @@ class UserSuppliedGitInfoBuilderTest extends DDSpecification {
def "global tags have lower priority than dedicated environment variables"() {
setup:
injectEnvConfig(Strings.toEnvVar(GeneralConfig.TAGS), Tags.GIT_REPOSITORY_URL + ":repo_url," + Tags.GIT_COMMIT_SHA + ":commit_sha")
injectEnvConfig(GitInfo.DD_GIT_REPOSITORY_URL, "overridden_repo_url")
injectEnvConfig(GitInfo.DD_GIT_COMMIT_SHA, "overridden_commit_sha")
injectEnvConfig(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_REPOSITORY_URL), "overridden_repo_url")
injectEnvConfig(Strings.propertyNameToEnvironmentVariableName(UserSuppliedGitInfoBuilder.DD_GIT_COMMIT_SHA), "overridden_commit_sha")

when:
def gitInfo = new UserSuppliedGitInfoBuilder().build(null)
Expand Down