Skip to content

Commit fe93654

Browse files
author
Daniel Rodriguez Hernandez
committed
Refactor CIProviderInfo
1 parent adb46f9 commit fe93654

File tree

13 files changed

+108
-148
lines changed

13 files changed

+108
-148
lines changed

gradle/java.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ task forkedTest(type: Test) {
2424

2525
test {
2626
exclude("**/*ForkedTest*")
27-
dependsOn forkedTest
27+
//dependsOn forkedTest
2828
}
2929

3030
def applyCodeCoverage = !(

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/AppVeyorInfo.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ class AppVeyorInfo extends CIProviderInfo {
2222
final String url = buildPipelineUrl(repoName, buildId);
2323
final String repoProvider = System.getenv(APPVEYOR_REPO_PROVIDER);
2424
final String tag = buildGitTag(repoProvider);
25-
final String commit = buildGitCommit(repoProvider);
2625

2726
this.ciTags =
28-
new CITagsBuilder()
27+
CITagsBuilder.from(this.ciTags)
2928
.withCiProviderName(APPVEYOR_PROVIDER_NAME)
3029
.withCiPipelineId(buildId)
3130
.withCiPipelineName(repoName)
@@ -35,20 +34,9 @@ class AppVeyorInfo extends CIProviderInfo {
3534
.withCiWorkspacePath(getWorkspace())
3635
.withGitRepositoryUrl(
3736
buildGitRepositoryUrl(repoProvider, repoName), getLocalGitRepositoryUrl())
38-
.withGitCommit(commit, getLocalGitCommitSha())
37+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
3938
.withGitBranch(buildGitBranch(repoProvider, tag), getLocalGitBranch())
4039
.withGitTag(tag, getLocalGitTag())
41-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
42-
.withGitCommitAuthorEmail(
43-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
44-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
45-
.withGitCommitCommitterName(
46-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
47-
.withGitCommitCommitterEmail(
48-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
49-
.withGitCommitCommitterDate(
50-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
51-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
5240
.build();
5341
}
5442

@@ -79,8 +67,9 @@ private String buildGitBranch(final String repoProvider, final String gitTag) {
7967
return null;
8068
}
8169

82-
private String buildGitCommit(final String repoProvider) {
83-
if ("github".equals(repoProvider)) {
70+
@Override
71+
protected String buildGitCommit() {
72+
if ("github".equals(System.getenv(APPVEYOR_REPO_PROVIDER))) {
8473
return System.getenv(APPVEYOR_REPO_COMMIT);
8574
}
8675
return null;

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/AzurePipelinesInfo.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ class AzurePipelinesInfo extends CIProviderInfo {
2929
final String buildId = System.getenv(AZURE_BUILD_BUILDID);
3030
final String jobId = System.getenv(AZURE_SYSTEM_JOBID);
3131
final String taskId = System.getenv(AZURE_SYSTEM_TASKINSTANCEID);
32-
final String commit = buildGitCommit();
3332

3433
this.ciTags =
35-
new CITagsBuilder()
34+
CITagsBuilder.from(this.ciTags)
3635
.withCiProviderName(AZURE_PROVIDER_NAME)
3736
.withCiPipelineId(System.getenv(AZURE_BUILD_BUILDID))
3837
.withCiPipelineName(System.getenv(AZURE_PIPELINE_NAME))
@@ -41,20 +40,9 @@ class AzurePipelinesInfo extends CIProviderInfo {
4140
.withCiJorUrl(buildCiJobUrl(uri, project, buildId, jobId, taskId))
4241
.withCiWorkspacePath(getWorkspace())
4342
.withGitRepositoryUrl(buildGitRepositoryUrl(), getLocalGitRepositoryUrl())
44-
.withGitCommit(commit, getLocalGitCommitSha())
43+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
4544
.withGitBranch(buildGitBranch(), getLocalGitBranch())
4645
.withGitTag(buildGitTag(), getLocalGitTag())
47-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
48-
.withGitCommitAuthorEmail(
49-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
50-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
51-
.withGitCommitCommitterName(
52-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
53-
.withGitCommitCommitterEmail(
54-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
55-
.withGitCommitCommitterDate(
56-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
57-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
5846
.build();
5947
}
6048

@@ -87,7 +75,8 @@ private String buildGitBranch() {
8775
}
8876
}
8977

90-
private String buildGitCommit() {
78+
@Override
79+
protected String buildGitCommit() {
9180
String commit = System.getenv(AZURE_SYSTEM_PULLREQUEST_SOURCECOMMITID);
9281
if (commit == null || commit.isEmpty()) {
9382
commit = System.getenv(AZURE_BUILD_SOURCEVERSION);

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/BitBucketInfo.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ class BitBucketInfo extends CIProviderInfo {
2020
final String repo = System.getenv(BITBUCKET_REPO_FULL_NAME);
2121
final String number = System.getenv(BITBUCKET_BUILD_NUMBER);
2222
final String url = buildPipelineUrl(repo, number);
23-
final String commit = System.getenv(BITBUCKET_GIT_COMMIT);
2423

2524
this.ciTags =
26-
new CITagsBuilder()
25+
CITagsBuilder.from(this.ciTags)
2726
.withCiProviderName(BITBUCKET_PROVIDER_NAME)
2827
.withCiPipelineId(buildPipelineId())
2928
.withCiPipelineName(repo)
@@ -34,23 +33,17 @@ class BitBucketInfo extends CIProviderInfo {
3433
.withGitRepositoryUrl(
3534
filterSensitiveInfo(System.getenv(BITBUCKET_GIT_REPOSITORY_URL)),
3635
getLocalGitRepositoryUrl())
37-
.withGitCommit(commit, getLocalGitCommitSha())
36+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
3837
.withGitBranch(normalizeRef(System.getenv(BITBUCKET_GIT_BRANCH)), getLocalGitBranch())
3938
.withGitTag(normalizeRef(System.getenv(BITBUCKET_GIT_TAG)), getLocalGitTag())
40-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
41-
.withGitCommitAuthorEmail(
42-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
43-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
44-
.withGitCommitCommitterName(
45-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
46-
.withGitCommitCommitterEmail(
47-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
48-
.withGitCommitCommitterDate(
49-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
50-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
5139
.build();
5240
}
5341

42+
@Override
43+
protected String buildGitCommit() {
44+
return System.getenv(BITBUCKET_GIT_COMMIT);
45+
}
46+
5447
@Override
5548
protected String buildWorkspace() {
5649
return System.getenv(BITBUCKET_WORKSPACE_PATH);

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/BitriseInfo.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ class BitriseInfo extends CIProviderInfo {
1818

1919
BitriseInfo() {
2020
final String gitTag = normalizeRef(System.getenv(BITRISE_GIT_TAG));
21-
final String commit = buildGitCommit();
2221

2322
this.ciTags =
24-
new CITagsBuilder()
23+
CITagsBuilder.from(this.ciTags)
2524
.withCiProviderName(BITRISE_PROVIDER_NAME)
2625
.withCiPipelineId(System.getenv(BITRISE_PIPELINE_ID))
2726
.withCiPipelineName(System.getenv(BITRISE_PIPELINE_NAME))
@@ -31,20 +30,9 @@ class BitriseInfo extends CIProviderInfo {
3130
.withGitRepositoryUrl(
3231
filterSensitiveInfo(System.getenv(BITRISE_GIT_REPOSITORY_URL)),
3332
getLocalGitRepositoryUrl())
34-
.withGitCommit(buildGitCommit(), getLocalGitCommitSha())
33+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
3534
.withGitBranch(buildGitBranch(gitTag), getLocalGitBranch())
3635
.withGitTag(gitTag, getLocalGitTag())
37-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
38-
.withGitCommitAuthorEmail(
39-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
40-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
41-
.withGitCommitCommitterName(
42-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
43-
.withGitCommitCommitterEmail(
44-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
45-
.withGitCommitCommitterDate(
46-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
47-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
4836
.build();
4937
}
5038

@@ -66,7 +54,8 @@ private String buildGitBranch(final String gitTag) {
6654
}
6755
}
6856

69-
private String buildGitCommit() {
57+
@Override
58+
protected String buildGitCommit() {
7059
final String fromCommit = System.getenv(BITRISE_GIT_PR_COMMIT);
7160
if (fromCommit != null && !fromCommit.isEmpty()) {
7261
return fromCommit;

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/BuildkiteInfo.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ class BuildkiteInfo extends CIProviderInfo {
1818

1919
BuildkiteInfo() {
2020
final String ciPipelineUrl = System.getenv(BUILDKITE_BUILD_URL);
21-
final String commit = System.getenv(BUILDKITE_GIT_COMMIT);
2221

2322
this.ciTags =
24-
new CITagsBuilder()
23+
CITagsBuilder.from(this.ciTags)
2524
.withCiProviderName(BUILDKITE_PROVIDER_NAME)
2625
.withCiPipelineId(System.getenv(BUILDKITE_PIPELINE_ID))
2726
.withCiPipelineName(System.getenv(BUILDKITE_PIPELINE_NAME))
@@ -32,23 +31,17 @@ class BuildkiteInfo extends CIProviderInfo {
3231
.withGitRepositoryUrl(
3332
filterSensitiveInfo(System.getenv(BUILDKITE_GIT_REPOSITORY_URL)),
3433
getLocalGitRepositoryUrl())
35-
.withGitCommit(commit, getLocalGitCommitSha())
34+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
3635
.withGitBranch(normalizeRef(System.getenv(BUILDKITE_GIT_BRANCH)), getLocalGitBranch())
3736
.withGitTag(normalizeRef(System.getenv(BUILDKITE_GIT_TAG)), getLocalGitTag())
38-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
39-
.withGitCommitAuthorEmail(
40-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
41-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
42-
.withGitCommitCommitterName(
43-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
44-
.withGitCommitCommitterEmail(
45-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
46-
.withGitCommitCommitterDate(
47-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
48-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
4937
.build();
5038
}
5139

40+
@Override
41+
protected String buildGitCommit() {
42+
return System.getenv(BUILDKITE_GIT_COMMIT);
43+
}
44+
5245
@Override
5346
protected String buildWorkspace() {
5447
return System.getenv(BUILDKITE_WORKSPACE_PATH);

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/CIProviderInfo.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,41 @@
2525
@SuppressForbidden
2626
public abstract class CIProviderInfo {
2727

28-
protected Map<String, String> ciTags = new HashMap<>();
28+
protected Map<String, String> ciTags;
2929
protected GitInfoExtractor localFSGitInfoExtractor = new LocalFSGitInfoExtractor();
3030

3131
private final String workspace;
32+
private final String commit;
3233
private final GitInfo localGitInfo;
3334

3435
public CIProviderInfo() {
3536
this.workspace = expandTilde(buildWorkspace());
3637
this.localGitInfo =
3738
this.localFSGitInfoExtractor.headCommit(
3839
Paths.get(this.workspace, getGitFolderName()).toFile().getAbsolutePath());
40+
this.commit = buildGitCommit();
41+
42+
this.ciTags =
43+
CITagsBuilder.newBuilder()
44+
.withGitCommitAuthorName(
45+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitAuthorName())
46+
.withGitCommitAuthorEmail(
47+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
48+
.withGitCommitAuthorDate(
49+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
50+
.withGitCommitCommitterName(
51+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitCommitterName())
52+
.withGitCommitCommitterEmail(
53+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
54+
.withGitCommitCommitterDate(
55+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
56+
.withGitCommitMessage(
57+
getGitCommit(), getLocalGitCommitSha(), getLocalGitCommitMessage())
58+
.build();
3959
}
4060

61+
protected abstract String buildGitCommit();
62+
4163
protected String getGitFolderName() {
4264
return ".git";
4365
}
@@ -107,6 +129,18 @@ public static class CITagsBuilder {
107129

108130
private final Map<String, String> ciTags = new HashMap<>();
109131

132+
private CITagsBuilder(final Map<String, String> ciTags) {
133+
this.ciTags.putAll(ciTags);
134+
}
135+
136+
public static CITagsBuilder newBuilder() {
137+
return new CITagsBuilder(new HashMap<String, String>());
138+
}
139+
140+
public static CITagsBuilder from(final Map<String, String> ciTags) {
141+
return new CITagsBuilder(ciTags);
142+
}
143+
110144
public CITagsBuilder withCiProviderName(final String ciProviderName) {
111145
return putTagValue(Tags.CI_PROVIDER_NAME, ciProviderName);
112146
}
@@ -261,6 +295,10 @@ public String getWorkspace() {
261295
return this.workspace;
262296
}
263297

298+
public String getGitCommit() {
299+
return this.commit;
300+
}
301+
264302
public GitInfo getLocalGitInfo() {
265303
return this.localGitInfo;
266304
}

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/CircleCIInfo.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ class CircleCIInfo extends CIProviderInfo {
1717

1818
CircleCIInfo() {
1919
final String gitTag = normalizeRef(System.getenv(CIRCLECI_GIT_TAG));
20-
final String commit = System.getenv(CIRCLECI_GIT_COMMIT);
2120

2221
this.ciTags =
23-
new CITagsBuilder()
22+
CITagsBuilder.from(this.ciTags)
2423
.withCiProviderName(CIRCLECI_PROVIDER_NAME)
2524
.withCiPipelineId(System.getenv(CIRCLECI_PIPELINE_ID))
2625
.withCiPipelineName(System.getenv(CIRCLECI_PIPELINE_NAME))
@@ -31,23 +30,17 @@ class CircleCIInfo extends CIProviderInfo {
3130
.withGitRepositoryUrl(
3231
filterSensitiveInfo(System.getenv(CIRCLECI_GIT_REPOSITORY_URL)),
3332
getLocalGitRepositoryUrl())
34-
.withGitCommit(System.getenv(CIRCLECI_GIT_COMMIT), getLocalGitCommitSha())
33+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
3534
.withGitBranch(buildGitBranch(gitTag), getLocalGitBranch())
3635
.withGitTag(gitTag, getLocalGitTag())
37-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
38-
.withGitCommitAuthorEmail(
39-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
40-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
41-
.withGitCommitCommitterName(
42-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
43-
.withGitCommitCommitterEmail(
44-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
45-
.withGitCommitCommitterDate(
46-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
47-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
4836
.build();
4937
}
5038

39+
@Override
40+
protected String buildGitCommit() {
41+
return System.getenv(CIRCLECI_GIT_COMMIT);
42+
}
43+
5144
@Override
5245
protected String buildWorkspace() {
5346
return System.getenv(CIRCLECI_WORKSPACE_PATH);

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/ci/GitLabInfo.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ class GitLabInfo extends CIProviderInfo {
2222
public static final String GITLAB_GIT_TAG = "CI_COMMIT_TAG";
2323

2424
GitLabInfo() {
25-
final String commit = System.getenv(GITLAB_GIT_COMMIT);
26-
2725
this.ciTags =
28-
new CITagsBuilder()
26+
CITagsBuilder.from(this.ciTags)
2927
.withCiProviderName(GITLAB_PROVIDER_NAME)
3028
.withCiPipelineId(System.getenv(GITLAB_PIPELINE_ID))
3129
.withCiPipelineName(System.getenv(GITLAB_PIPELINE_NAME))
@@ -38,23 +36,17 @@ class GitLabInfo extends CIProviderInfo {
3836
.withGitRepositoryUrl(
3937
filterSensitiveInfo(System.getenv(GITLAB_GIT_REPOSITORY_URL)),
4038
getLocalGitRepositoryUrl())
41-
.withGitCommit(commit, getLocalGitCommitSha())
39+
.withGitCommit(getGitCommit(), getLocalGitCommitSha())
4240
.withGitBranch(normalizeRef(System.getenv(GITLAB_GIT_BRANCH)), getLocalGitBranch())
4341
.withGitTag(normalizeRef(System.getenv(GITLAB_GIT_TAG)), getLocalGitTag())
44-
.withGitCommitAuthorName(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorName())
45-
.withGitCommitAuthorEmail(
46-
commit, getLocalGitCommitSha(), getLocalGitCommitAuthorEmail())
47-
.withGitCommitAuthorDate(commit, getLocalGitCommitSha(), getLocalGitCommitAuthorDate())
48-
.withGitCommitCommitterName(
49-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterName())
50-
.withGitCommitCommitterEmail(
51-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterEmail())
52-
.withGitCommitCommitterDate(
53-
commit, getLocalGitCommitSha(), getLocalGitCommitCommitterDate())
54-
.withGitCommitMessage(commit, getLocalGitCommitSha(), getLocalGitCommitMessage())
5542
.build();
5643
}
5744

45+
@Override
46+
protected String buildGitCommit() {
47+
return System.getenv(GITLAB_GIT_COMMIT);
48+
}
49+
5850
@Override
5951
protected String buildWorkspace() {
6052
return System.getenv(GITLAB_WORKSPACE_PATH);

0 commit comments

Comments
 (0)