2
2
3
3
import datadog .trace .api .cache .DDCache ;
4
4
import datadog .trace .api .cache .DDCaches ;
5
+ import datadog .trace .api .civisibility .InstrumentationBridge ;
6
+ import datadog .trace .api .civisibility .telemetry .CiVisibilityCountMetric ;
7
+ import datadog .trace .api .civisibility .telemetry .tag .GitProviderDiscrepant ;
8
+ import datadog .trace .api .civisibility .telemetry .tag .GitProviderExpected ;
9
+ import datadog .trace .api .civisibility .telemetry .tag .GitShaDiscrepancyType ;
10
+ import datadog .trace .api .civisibility .telemetry .tag .GitShaMatch ;
5
11
import datadog .trace .util .Strings ;
6
12
import java .nio .file .Paths ;
7
13
import java .util .ArrayList ;
8
14
import java .util .Collection ;
9
15
import java .util .Collections ;
10
16
import java .util .Comparator ;
17
+ import java .util .HashSet ;
11
18
import java .util .LinkedHashMap ;
12
19
import java .util .List ;
13
20
import java .util .Map ;
21
+ import java .util .Objects ;
22
+ import java .util .Set ;
14
23
import java .util .function .Function ;
15
24
import java .util .function .Predicate ;
16
25
import javax .annotation .Nullable ;
@@ -51,37 +60,89 @@ public GitInfo getGitInfo(@Nullable String repositoryPath) {
51
60
52
61
private GitInfo buildGitInfo (String repositoryPath ) {
53
62
Evaluator evaluator = new Evaluator (repositoryPath , builders );
54
- return new GitInfo (
55
- evaluator .get (
56
- gi -> GitUtils .filterSensitiveInfo (gi .getRepositoryURL ()),
57
- GitInfoProvider ::validateGitRemoteUrl ),
58
- evaluator .get (GitInfo ::getBranch , Strings ::isNotBlank ),
59
- evaluator .get (GitInfo ::getTag , Strings ::isNotBlank ),
60
- new CommitInfo (
61
- evaluator .get (gi1 -> gi1 .getCommit ().getSha (), Strings ::isNotBlank ),
62
- new PersonInfo (
63
+ GitInfo gitInfo =
64
+ new GitInfo (
65
+ evaluator .get (
66
+ gi -> GitUtils .filterSensitiveInfo (gi .getRepositoryURL ()),
67
+ GitInfoProvider ::validateGitRemoteUrl ),
68
+ evaluator .get (GitInfo ::getBranch , Strings ::isNotBlank ),
69
+ evaluator .get (GitInfo ::getTag , Strings ::isNotBlank ),
70
+ new CommitInfo (
71
+ evaluator .get (gi1 -> gi1 .getCommit ().getSha (), Strings ::isNotBlank ),
72
+ new PersonInfo (
73
+ evaluator .getIfCommitShaMatches (
74
+ gi -> gi .getCommit ().getAuthor ().getName (), Strings ::isNotBlank ),
75
+ evaluator .getIfCommitShaMatches (
76
+ gi -> gi .getCommit ().getAuthor ().getEmail (), Strings ::isNotBlank ),
77
+ evaluator .getIfCommitShaMatches (
78
+ gi -> gi .getCommit ().getAuthor ().getIso8601Date (), Strings ::isNotBlank )),
79
+ new PersonInfo (
80
+ evaluator .getIfCommitShaMatches (
81
+ gi -> gi .getCommit ().getCommitter ().getName (), Strings ::isNotBlank ),
82
+ evaluator .getIfCommitShaMatches (
83
+ gi -> gi .getCommit ().getCommitter ().getEmail (), Strings ::isNotBlank ),
84
+ evaluator .getIfCommitShaMatches (
85
+ gi -> gi .getCommit ().getCommitter ().getIso8601Date (), Strings ::isNotBlank )),
63
86
evaluator .getIfCommitShaMatches (
64
- gi -> gi .getCommit ().getAuthor ().getName (), Strings ::isNotBlank ),
65
- evaluator .getIfCommitShaMatches (
66
- gi -> gi .getCommit ().getAuthor ().getEmail (), Strings ::isNotBlank ),
67
- evaluator .getIfCommitShaMatches (
68
- gi -> gi .getCommit ().getAuthor ().getIso8601Date (), Strings ::isNotBlank )),
69
- new PersonInfo (
70
- evaluator .getIfCommitShaMatches (
71
- gi -> gi .getCommit ().getCommitter ().getName (), Strings ::isNotBlank ),
72
- evaluator .getIfCommitShaMatches (
73
- gi -> gi .getCommit ().getCommitter ().getEmail (), Strings ::isNotBlank ),
74
- evaluator .getIfCommitShaMatches (
75
- gi -> gi .getCommit ().getCommitter ().getIso8601Date (), Strings ::isNotBlank )),
76
- evaluator .getIfCommitShaMatches (
77
- gi -> gi .getCommit ().getFullMessage (), Strings ::isNotBlank )));
87
+ gi -> gi .getCommit ().getFullMessage (), Strings ::isNotBlank )));
88
+
89
+ InstrumentationBridge .getMetricCollector ()
90
+ .add (
91
+ CiVisibilityCountMetric .GIT_COMMIT_SHA_MATCH ,
92
+ 1 ,
93
+ evaluator .shaDiscrepancies .isEmpty () ? GitShaMatch .TRUE : GitShaMatch .FALSE );
94
+ for (ShaDiscrepancy mismatch : evaluator .shaDiscrepancies ) {
95
+ mismatch .addTelemetry ();
96
+ }
97
+
98
+ return gitInfo ;
78
99
}
79
100
80
101
private static boolean validateGitRemoteUrl (String s ) {
81
102
// we cannot work with URL that uses "file://" protocol
82
103
return Strings .isNotBlank (s ) && !s .startsWith ("file:" );
83
104
}
84
105
106
+ private static final class ShaDiscrepancy {
107
+ private final GitProviderExpected expectedGitProvider ;
108
+ private final GitProviderDiscrepant discrepantGitProvider ;
109
+ private final GitShaDiscrepancyType discrepancyType ;
110
+
111
+ private ShaDiscrepancy (
112
+ GitProviderExpected expectedGitProvider ,
113
+ GitProviderDiscrepant discrepantGitProvider ,
114
+ GitShaDiscrepancyType discrepancyType ) {
115
+ this .expectedGitProvider = expectedGitProvider ;
116
+ this .discrepantGitProvider = discrepantGitProvider ;
117
+ this .discrepancyType = discrepancyType ;
118
+ }
119
+
120
+ private void addTelemetry () {
121
+ InstrumentationBridge .getMetricCollector ()
122
+ .add (
123
+ CiVisibilityCountMetric .GIT_COMMIT_SHA_DISCREPANCY ,
124
+ 1 ,
125
+ expectedGitProvider ,
126
+ discrepantGitProvider ,
127
+ discrepancyType );
128
+ }
129
+
130
+ @ Override
131
+ public boolean equals (Object obj ) {
132
+ if (this == obj ) return true ;
133
+ if (obj == null || getClass () != obj .getClass ()) return false ;
134
+ ShaDiscrepancy that = (ShaDiscrepancy ) obj ;
135
+ return expectedGitProvider .equals (that .expectedGitProvider )
136
+ && discrepantGitProvider .equals (that .discrepantGitProvider )
137
+ && discrepancyType .equals (that .discrepancyType );
138
+ }
139
+
140
+ @ Override
141
+ public int hashCode () {
142
+ return Objects .hash (expectedGitProvider , discrepantGitProvider , discrepancyType );
143
+ }
144
+ }
145
+
85
146
/**
86
147
* Uses provided GitInfoBuilder instances to get GitInfo data.
87
148
*
@@ -95,10 +156,12 @@ private static boolean validateGitRemoteUrl(String s) {
95
156
private static final class Evaluator {
96
157
private final String repositoryPath ;
97
158
private final Map <GitInfoBuilder , GitInfo > infos ;
159
+ private final Set <ShaDiscrepancy > shaDiscrepancies ;
98
160
99
161
private Evaluator (String repositoryPath , Collection <GitInfoBuilder > builders ) {
100
162
this .repositoryPath = repositoryPath ;
101
163
this .infos = new LinkedHashMap <>();
164
+ this .shaDiscrepancies = new HashSet <>();
102
165
for (GitInfoBuilder builder : builders ) {
103
166
infos .put (builder , null );
104
167
}
@@ -121,7 +184,10 @@ private String get(
121
184
Function <GitInfo , String > function ,
122
185
Predicate <String > validator ,
123
186
boolean checkShaIntegrity ) {
124
- String commitSha = null ;
187
+ String expectedCommitSha = null ;
188
+ String expectedRepoUrl = null ;
189
+ GitProviderExpected expectedGitProvider = null ;
190
+
125
191
for (Map .Entry <GitInfoBuilder , GitInfo > e : infos .entrySet ()) {
126
192
GitInfo info = e .getValue ();
127
193
if (info == null ) {
@@ -134,11 +200,22 @@ private String get(
134
200
CommitInfo currentCommit = info .getCommit ();
135
201
String currentCommitSha = currentCommit != null ? currentCommit .getSha () : null ;
136
202
if (Strings .isNotBlank (currentCommitSha )) {
137
- if (commitSha == null ) {
138
- commitSha = currentCommitSha ;
139
- } else if (!commitSha .equals (currentCommitSha )) {
203
+ if (expectedCommitSha == null ) {
204
+ expectedCommitSha = currentCommitSha ;
205
+ expectedRepoUrl = info .getRepositoryURL ();
206
+ expectedGitProvider = e .getKey ().providerAsExpected ();
207
+ } else if (!expectedCommitSha .equals (currentCommitSha )) {
140
208
// We already have a commit SHA from source that has higher priority.
141
209
// Commit SHA from current source is different, so we have to skip it
210
+ GitShaDiscrepancyType discrepancyType = GitShaDiscrepancyType .COMMIT_DISCREPANCY ;
211
+ String repoUrl = info .getRepositoryURL ();
212
+ if (expectedRepoUrl != null && repoUrl != null && !repoUrl .equals (expectedRepoUrl )) {
213
+ discrepancyType = GitShaDiscrepancyType .REPOSITORY_DISCREPANCY ;
214
+ }
215
+
216
+ shaDiscrepancies .add (
217
+ new ShaDiscrepancy (
218
+ expectedGitProvider , e .getKey ().providerAsDiscrepant (), discrepancyType ));
142
219
continue ;
143
220
}
144
221
}
0 commit comments