Skip to content

Commit 50bda1b

Browse files
Merge branch 'master' into sezen.leblay/APPSEC-57259-extract-schema-spring
2 parents d8e424c + 63b52b4 commit 50bda1b

File tree

114 files changed

+1137
-939
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+1137
-939
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/GithubActionsInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public CIInfo buildCIInfo() {
9494
@Override
9595
public PullRequestInfo buildPullRequestInfo() {
9696
String baseRef = environment.get(GITHUB_BASE_REF);
97-
if (!Strings.isNotBlank(baseRef)) {
97+
if (Strings.isBlank(baseRef)) {
9898
return PullRequestInfo.EMPTY;
9999
}
100100

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ChangedFiles.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/CiVisibilitySettings.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Path;
55
import java.util.Map;
66
import java.util.Objects;
7+
import javax.annotation.Nullable;
78

89
public class CiVisibilitySettings {
910

@@ -17,7 +18,8 @@ public class CiVisibilitySettings {
1718
false,
1819
false,
1920
EarlyFlakeDetectionSettings.DEFAULT,
20-
TestManagementSettings.DEFAULT);
21+
TestManagementSettings.DEFAULT,
22+
null);
2123

2224
private final boolean itrEnabled;
2325
private final boolean codeCoverage;
@@ -28,6 +30,7 @@ public class CiVisibilitySettings {
2830
private final boolean knownTestsEnabled;
2931
private final EarlyFlakeDetectionSettings earlyFlakeDetectionSettings;
3032
private final TestManagementSettings testManagementSettings;
33+
@Nullable private final String defaultBranch;
3134

3235
CiVisibilitySettings(
3336
boolean itrEnabled,
@@ -38,7 +41,8 @@ public class CiVisibilitySettings {
3841
boolean impactedTestsDetectionEnabled,
3942
boolean knownTestsEnabled,
4043
EarlyFlakeDetectionSettings earlyFlakeDetectionSettings,
41-
TestManagementSettings testManagementSettings) {
44+
TestManagementSettings testManagementSettings,
45+
@Nullable String defaultBranch) {
4246
this.itrEnabled = itrEnabled;
4347
this.codeCoverage = codeCoverage;
4448
this.testsSkipping = testsSkipping;
@@ -48,6 +52,7 @@ public class CiVisibilitySettings {
4852
this.knownTestsEnabled = knownTestsEnabled;
4953
this.earlyFlakeDetectionSettings = earlyFlakeDetectionSettings;
5054
this.testManagementSettings = testManagementSettings;
55+
this.defaultBranch = defaultBranch;
5156
}
5257

5358
public boolean isItrEnabled() {
@@ -86,6 +91,11 @@ public TestManagementSettings getTestManagementSettings() {
8691
return testManagementSettings;
8792
}
8893

94+
@Nullable
95+
public String getDefaultBranch() {
96+
return defaultBranch;
97+
}
98+
8999
@Override
90100
public boolean equals(Object o) {
91101
if (this == o) {
@@ -103,7 +113,8 @@ public boolean equals(Object o) {
103113
&& impactedTestsDetectionEnabled == that.impactedTestsDetectionEnabled
104114
&& knownTestsEnabled == that.knownTestsEnabled
105115
&& Objects.equals(earlyFlakeDetectionSettings, that.earlyFlakeDetectionSettings)
106-
&& Objects.equals(testManagementSettings, that.testManagementSettings);
116+
&& Objects.equals(testManagementSettings, that.testManagementSettings)
117+
&& Objects.equals(defaultBranch, that.defaultBranch);
107118
}
108119

109120
@Override
@@ -117,7 +128,8 @@ public int hashCode() {
117128
impactedTestsDetectionEnabled,
118129
knownTestsEnabled,
119130
earlyFlakeDetectionSettings,
120-
testManagementSettings);
131+
testManagementSettings,
132+
defaultBranch);
121133
}
122134

123135
public interface Factory {
@@ -145,13 +157,20 @@ public CiVisibilitySettings fromJson(Map<String, Object> json) {
145157
EarlyFlakeDetectionSettings.JsonAdapter.INSTANCE.fromJson(
146158
(Map<String, Object>) json.get("early_flake_detection")),
147159
TestManagementSettings.JsonAdapter.INSTANCE.fromJson(
148-
(Map<String, Object>) json.get("test_management")));
160+
(Map<String, Object>) json.get("test_management")),
161+
getString(json, "default_branch", null));
149162
}
150163

151164
private static boolean getBoolean(
152165
Map<String, Object> json, String fieldName, boolean defaultValue) {
153166
Object value = json.get(fieldName);
154167
return value instanceof Boolean ? (Boolean) value : defaultValue;
155168
}
169+
170+
private static String getString(
171+
Map<String, Object> json, String fieldName, String defaultValue) {
172+
Object value = json.get(fieldName);
173+
return value instanceof String ? (String) value : defaultValue;
174+
}
156175
}
157176
}

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ConfigurationApi.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public Map<TestSetting, Map<String, Collection<TestFQN>>> getTestManagementTests
3838
TracerEnvironment tracerEnvironment) {
3939
return Collections.emptyMap();
4040
}
41-
42-
@Override
43-
public ChangedFiles getChangedFiles(TracerEnvironment tracerEnvironment) {
44-
return ChangedFiles.EMPTY;
45-
}
4641
};
4742

4843
CiVisibilitySettings getSettings(TracerEnvironment tracerEnvironment) throws IOException;
@@ -58,6 +53,4 @@ Map<String, Collection<TestFQN>> getKnownTestsByModule(TracerEnvironment tracerE
5853

5954
Map<TestSetting, Map<String, Collection<TestFQN>>> getTestManagementTestsByModule(
6055
TracerEnvironment tracerEnvironment) throws IOException;
61-
62-
ChangedFiles getChangedFiles(TracerEnvironment tracerEnvironment) throws IOException;
6356
}

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ConfigurationApiImpl.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class ConfigurationApiImpl implements ConfigurationApi {
5353

5454
private static final String SETTINGS_URI = "libraries/tests/services/setting";
5555
private static final String SKIPPABLE_TESTS_URI = "ci/tests/skippable";
56-
private static final String CHANGED_FILES_URI = "ci/tests/diffs";
5756
private static final String FLAKY_TESTS_URI = "ci/libraries/tests/flaky";
5857
private static final String KNOWN_TESTS_URI = "ci/libraries/tests";
5958
private static final String TEST_MANAGEMENT_TESTS_URI = "test/libraries/test-management/tests";
@@ -68,7 +67,6 @@ public class ConfigurationApiImpl implements ConfigurationApi {
6867
private final JsonAdapter<EnvelopeDto<KnownTestsDto>> testFullNamesResponseAdapter;
6968
private final JsonAdapter<EnvelopeDto<TestManagementDto>> testManagementRequestAdapter;
7069
private final JsonAdapter<EnvelopeDto<TestManagementTestsDto>> testManagementTestsResponseAdapter;
71-
private final JsonAdapter<EnvelopeDto<ChangedFiles>> changedFilesResponseAdapter;
7270

7371
public ConfigurationApiImpl(BackendApi backendApi, CiVisibilityMetricCollector metricCollector) {
7472
this(backendApi, metricCollector, () -> RandomUtils.randomUUID().toString());
@@ -119,11 +117,6 @@ public ConfigurationApiImpl(BackendApi backendApi, CiVisibilityMetricCollector m
119117
Types.newParameterizedTypeWithOwner(
120118
ConfigurationApiImpl.class, EnvelopeDto.class, TestManagementTestsDto.class);
121119
testManagementTestsResponseAdapter = moshi.adapter(testManagementTestsResponseType);
122-
123-
ParameterizedType changedFilesResponseAdapterType =
124-
Types.newParameterizedTypeWithOwner(
125-
ConfigurationApiImpl.class, EnvelopeDto.class, ChangedFiles.class);
126-
changedFilesResponseAdapter = moshi.adapter(changedFilesResponseAdapterType);
127120
}
128121

129122
@Override
@@ -416,37 +409,6 @@ private Map<TestSetting, Map<String, Collection<TestFQN>>> parseTestManagementTe
416409
return testsByTypeByModule;
417410
}
418411

419-
@Override
420-
public ChangedFiles getChangedFiles(TracerEnvironment tracerEnvironment) throws IOException {
421-
OkHttpUtils.CustomListener telemetryListener =
422-
new TelemetryListener.Builder(metricCollector)
423-
.requestCount(CiVisibilityCountMetric.IMPACTED_TESTS_DETECTION_REQUEST)
424-
.requestErrors(CiVisibilityCountMetric.IMPACTED_TESTS_DETECTION_REQUEST_ERRORS)
425-
.requestDuration(CiVisibilityDistributionMetric.IMPACTED_TESTS_DETECTION_REQUEST_MS)
426-
.responseBytes(CiVisibilityDistributionMetric.IMPACTED_TESTS_DETECTION_RESPONSE_BYTES)
427-
.build();
428-
429-
String uuid = uuidGenerator.get();
430-
EnvelopeDto<TracerEnvironment> request =
431-
new EnvelopeDto<>(new DataDto<>(uuid, "ci_app_tests_diffs_request", tracerEnvironment));
432-
String json = requestAdapter.toJson(request);
433-
RequestBody requestBody = RequestBody.create(JSON, json);
434-
ChangedFiles changedFiles =
435-
backendApi.post(
436-
CHANGED_FILES_URI,
437-
requestBody,
438-
is ->
439-
changedFilesResponseAdapter.fromJson(Okio.buffer(Okio.source(is))).data.attributes,
440-
telemetryListener,
441-
false);
442-
443-
int filesCount = changedFiles.getFiles().size();
444-
LOGGER.debug("Received {} changed files", filesCount);
445-
metricCollector.add(
446-
CiVisibilityDistributionMetric.IMPACTED_TESTS_DETECTION_RESPONSE_FILES, filesCount);
447-
return changedFiles;
448-
}
449-
450412
private static final class EnvelopeDto<T> {
451413
private final DataDto<T> data;
452414

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ExecutionSettingsFactoryImpl.java

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import datadog.trace.api.git.GitInfoProvider;
99
import datadog.trace.civisibility.ci.PullRequestInfo;
1010
import datadog.trace.civisibility.diff.Diff;
11-
import datadog.trace.civisibility.diff.FileDiff;
1211
import datadog.trace.civisibility.diff.LineDiff;
1312
import datadog.trace.civisibility.git.tree.GitClient;
1413
import datadog.trace.civisibility.git.tree.GitDataUploader;
@@ -215,7 +214,8 @@ private Map<String, ExecutionSettings> doCreate(
215214
getTestManagementTestsByModule(
216215
tracerEnvironment, testManagementSettings.isEnabled()));
217216
Future<Diff> pullRequestDiffFuture =
218-
executor.submit(() -> getPullRequestDiff(tracerEnvironment, impactedTestsEnabled));
217+
executor.submit(
218+
() -> getPullRequestDiff(impactedTestsEnabled, settings.getDefaultBranch()));
219219

220220
SkippableTests skippableTests = skippableTestsFuture.get();
221221
Map<String, Collection<TestFQN>> flakyTestsByModule = flakyTestsFuture.get();
@@ -404,8 +404,7 @@ private Map<TestSetting, Map<String, Collection<TestFQN>>> getTestManagementTest
404404
}
405405

406406
@Nonnull
407-
private Diff getPullRequestDiff(
408-
TracerEnvironment tracerEnvironment, boolean impactedTestsDetectionEnabled) {
407+
private Diff getPullRequestDiff(boolean impactedTestsDetectionEnabled, String defaultBranch) {
409408
if (!impactedTestsDetectionEnabled) {
410409
return LineDiff.EMPTY;
411410
}
@@ -414,49 +413,25 @@ private Diff getPullRequestDiff(
414413
if (repositoryRoot != null) {
415414
// ensure repo is not shallow before attempting to get git diff
416415
gitRepoUnshallow.unshallow();
417-
Diff diff =
418-
gitClient.getGitDiff(
419-
pullRequestInfo.getPullRequestBaseBranchSha(),
420-
pullRequestInfo.getGitCommitHeadSha());
416+
417+
String baseCommitSha = pullRequestInfo.getPullRequestBaseBranchSha();
418+
if (baseCommitSha == null) {
419+
baseCommitSha =
420+
gitClient.getBaseCommitSha(pullRequestInfo.getPullRequestBaseBranch(), defaultBranch);
421+
}
422+
423+
Diff diff = gitClient.getGitDiff(baseCommitSha, pullRequestInfo.getGitCommitHeadSha());
421424
if (diff != null) {
422425
return diff;
423426
}
424427
}
425-
426428
} catch (InterruptedException e) {
427429
LOGGER.error("Interrupted while getting git diff for PR: {}", pullRequestInfo, e);
428430
Thread.currentThread().interrupt();
429-
430431
} catch (Exception e) {
431432
LOGGER.error("Could not get git diff for PR: {}", pullRequestInfo, e);
432433
}
433434

434-
if (config.isCiVisibilityImpactedTestsBackendRequestEnabled()) {
435-
try {
436-
ChangedFiles changedFiles = configurationApi.getChangedFiles(tracerEnvironment);
437-
438-
// attempting to use base SHA returned by the backend to calculate git diff
439-
if (repositoryRoot != null) {
440-
// ensure repo is not shallow before attempting to get git diff
441-
gitRepoUnshallow.unshallow();
442-
Diff diff = gitClient.getGitDiff(changedFiles.getBaseSha(), tracerEnvironment.getSha());
443-
if (diff != null) {
444-
return diff;
445-
}
446-
}
447-
448-
// falling back to file-level granularity
449-
return new FileDiff(changedFiles.getFiles());
450-
451-
} catch (InterruptedException e) {
452-
LOGGER.error("Interrupted while getting git diff for: {}", tracerEnvironment, e);
453-
Thread.currentThread().interrupt();
454-
455-
} catch (Exception e) {
456-
LOGGER.error("Could not get git diff for: {}", tracerEnvironment, e);
457-
}
458-
}
459-
460435
return LineDiff.EMPTY;
461436
}
462437

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/diff/Diff.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
public interface Diff extends SerializableType {
77

8-
PolymorphicSerializer<Diff> SERIALIZER =
9-
new PolymorphicSerializer<>(LineDiff.class, FileDiff.class);
8+
PolymorphicSerializer<Diff> SERIALIZER = new PolymorphicSerializer<>(LineDiff.class);
109

1110
boolean contains(String relativePath, int startLine, int endLine);
1211
}

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/diff/FileDiff.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/GitClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ List<String> getObjects(Collection<String> commitsToSkip, Collection<String> com
7272
Path createPackFiles(List<String> objectHashes)
7373
throws IOException, TimeoutException, InterruptedException;
7474

75+
@Nullable
76+
String getBaseCommitSha(@Nullable String baseBranch, @Nullable String defaultBranch)
77+
throws IOException, TimeoutException, InterruptedException;
78+
7579
@Nullable
7680
LineDiff getGitDiff(String baseCommit, String targetCommit)
7781
throws IOException, TimeoutException, InterruptedException;

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/GitRepoUnshallow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public GitRepoUnshallow(Config config, GitClient gitClient) {
1919
this.gitClient = gitClient;
2020
}
2121

22-
public boolean unshallow() throws IOException, InterruptedException, TimeoutException {
22+
public synchronized boolean unshallow()
23+
throws IOException, InterruptedException, TimeoutException {
2324
if (!config.isCiVisibilityGitUnshallowEnabled() || !gitClient.isShallow()) {
2425
return false;
2526
}

0 commit comments

Comments
 (0)