Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit cc044e1

Browse files
authored
Merge pull request #35 from snyk/feat/ROAD-742_analysisContext_for_getAnlysis
feat: provide `analysisContext` key (`getAnalysis` request) for better tracking/logging on backend [ROAD-742]
2 parents 33da807 + 09f1370 commit cc044e1

File tree

11 files changed

+140
-26
lines changed

11 files changed

+140
-26
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
- fix: do not try to getAnalysis if `upload files` is not succeed (i.e. `missingFiles` is not empty after uploads)
55
- fix: avoid remove operation for empty immutable List
66
- fix: check file in marker for nullability before proceed
7+
- fix: internal ConcurrentModificationException
78
- feat: provide unique (per project) `shard` to getAnalysis call
9+
- feat: provide `analysisContext` key (`getAnalysis` request) for better tracking/logging on backend
810
- chore: reshape/refactor REST API wrapper to be replaceable through constructor base DI
911

1012
## [2.2.1] - 2021-12-10

src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiImplTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,15 @@ private GetAnalysisResponse doAnalysisAndWait(List<String> analysedFiles, Intege
372372
throws InterruptedException {
373373
GetAnalysisResponse response = null;
374374
for (int i = 0; i < 120; i++) {
375-
response = restApiClient.getAnalysis(loggedToken, bundleId, severity, analysedFiles, bundleId);
375+
response = restApiClient.getAnalysis(
376+
loggedToken,
377+
bundleId,
378+
severity,
379+
analysedFiles,
380+
bundleId,
381+
"test-java-client-ide",
382+
"test-java-client-org"
383+
);
376384
if (response.getStatus().equals("COMPLETE")) break;
377385
Thread.sleep(1000);
378386
}

src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ void setBaseUrl(
5757
* @return {@link GetAnalysisResponse} instance}
5858
*/
5959
@NotNull GetAnalysisResponse getAnalysis(
60-
String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard);
60+
String token,
61+
String bundleId,
62+
Integer severity,
63+
List<String> filesToAnalyse,
64+
String shard,
65+
String ideProductName,
66+
String orgDisplayName
67+
);
6168

6269
/**
6370
* Requests current filtering options for uploaded bundles.

src/main/java/ai/deepcode/javaclient/DeepCodeRestApiImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,19 @@ Call<GetAnalysisResponse> doGetAnalysis(
335335
@Override
336336
@NotNull
337337
public GetAnalysisResponse getAnalysis(
338-
String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
338+
String token,
339+
String bundleId,
340+
Integer severity,
341+
List<String> filesToAnalyse,
342+
String shard,
343+
String ideProductName,
344+
String orgDisplayName
345+
) {
339346
GetAnalysisCall getAnalysisCall = retrofit.create(GetAnalysisCall.class);
340347
try {
341348
Response<GetAnalysisResponse> retrofitResponse =
342349
getAnalysisCall
343-
.doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard))
350+
.doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgDisplayName))
344351
.execute();
345352
GetAnalysisResponse result = retrofitResponse.body();
346353
if (result == null) result = new GetAnalysisResponse();

src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,9 @@ private GetAnalysisResponse doGetAnalysis(
593593
bundleId,
594594
deepCodeParams.getMinSeverity(),
595595
filesToAnalyse,
596-
HashContentUtilsBase.calculateHash(pdUtils.getProjectName(project))
597-
);
596+
HashContentUtilsBase.calculateHash(pdUtils.getProjectName(project)),
597+
deepCodeParams.getIdeProductName(),
598+
deepCodeParams.getOrgDisplayName());
598599

599600
pdUtils.progressCheckCanceled(progress);
600601
dcLogger.logInfo(response.toString());

src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public abstract class DeepCodeParamsBase {
1818
// Inner params
1919
private String loginUrl;
2020
private String ideProductName;
21+
private String orgDisplayName;
2122
private Supplier<Long> getTimeoutForGettingAnalysesMs;
2223
private final DeepCodeRestApi restApi;
2324

@@ -30,6 +31,7 @@ protected DeepCodeParamsBase(
3031
String sessionToken,
3132
String loginUrl,
3233
String ideProductName,
34+
String orgDisplayName,
3335
Supplier<Long> getTimeoutForGettingAnalysesMs,
3436
DeepCodeRestApi restApi
3537
) {
@@ -41,6 +43,7 @@ protected DeepCodeParamsBase(
4143
this.sessionToken = sessionToken;
4244
this.loginUrl = loginUrl;
4345
this.ideProductName = ideProductName;
46+
this.orgDisplayName = orgDisplayName;
4447
this.getTimeoutForGettingAnalysesMs = getTimeoutForGettingAnalysesMs;
4548
this.restApi = restApi;
4649
}
@@ -134,4 +137,8 @@ public String getIdeProductName() {
134137
public long getTimeoutForGettingAnalysesMs() {
135138
return getTimeoutForGettingAnalysesMs.get();
136139
}
140+
141+
public String getOrgDisplayName() {
142+
return orgDisplayName;
143+
}
137144
}

src/main/java/ai/deepcode/javaclient/core/RunUtilsBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected abstract void doBackgroundRun(
8686
private static final Map<Object, Set<Object>> mapProject2Progresses = new ConcurrentHashMap<>();
8787

8888
protected static synchronized Set<Object> getRunningProgresses(@NotNull Object project) {
89-
return mapProject2Progresses.computeIfAbsent(project, p -> new HashSet<>());
89+
return mapProject2Progresses.computeIfAbsent(project, p -> ConcurrentHashMap.newKeySet());
9090
}
9191

9292
// ??? list of all running background tasks

src/main/java/ai/deepcode/javaclient/requests/GetAnalysisRequest.java

+73-15
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55

66
public class GetAnalysisRequest {
77
private GetAnalysisKey key;
8+
private AnalysisContext analysisContext;
89
private Integer severity;
910
private boolean prioritized;
1011
private boolean legacy;
1112

1213
/**
1314
* @param bundleHash
14-
* @param limitToFiles list of filePath
15+
* @param limitToFiles list of filePath
1516
* @param severity
16-
* @param shard uniq String (hash) per Project to optimize jobs on backend (run on the same worker to reuse caches)
17+
* @param shard uniq String (hash) per Project to optimize jobs on backend (run on the same worker to reuse caches)
18+
* @param ideProductName specific IDE
19+
* @param orgDisplayName client’s snyk organization name
1720
* @param prioritized
1821
* @param legacy
1922
*/
@@ -22,17 +25,27 @@ public GetAnalysisRequest(
2225
List<String> limitToFiles,
2326
Integer severity,
2427
String shard,
28+
String ideProductName,
29+
String orgDisplayName,
2530
boolean prioritized,
2631
boolean legacy
2732
) {
2833
this.key = new GetAnalysisKey(bundleHash, limitToFiles, shard);
34+
this.analysisContext = new AnalysisContext(ideProductName, orgDisplayName);
2935
this.severity = severity;
3036
this.prioritized = prioritized;
3137
this.legacy = legacy;
3238
}
3339

34-
public GetAnalysisRequest(String bundleHash, List<String> limitToFiles, Integer severity, String shard) {
35-
this(bundleHash, limitToFiles, severity, shard, false, true);
40+
public GetAnalysisRequest(
41+
String bundleHash,
42+
List<String> limitToFiles,
43+
Integer severity,
44+
String shard,
45+
String ideProductName,
46+
String orgDisplayName
47+
) {
48+
this(bundleHash, limitToFiles, severity, shard, ideProductName, orgDisplayName, false, true);
3649
}
3750

3851
private static class GetAnalysisKey {
@@ -65,27 +78,72 @@ public boolean equals(Object o) {
6578
if (o == null || getClass() != o.getClass()) return false;
6679
GetAnalysisKey that = (GetAnalysisKey) o;
6780
return type.equals(that.type)
68-
&& hash.equals(that.hash)
69-
&& Objects.equals(limitToFiles, that.limitToFiles);
81+
&& hash.equals(that.hash)
82+
&& Objects.equals(limitToFiles, that.limitToFiles);
7083
}
7184

7285
@Override
7386
public String toString() {
7487
return "GetAnalysisKey{"
75-
+ "type='"
76-
+ type
77-
+ '\''
78-
+ ", hash='"
79-
+ hash
80-
+ '\''
81-
+ ", limitToFiles="
82-
+ limitToFiles
83-
+ '}';
88+
+ "type='"
89+
+ type
90+
+ '\''
91+
+ ", hash='"
92+
+ hash
93+
+ '\''
94+
+ ", limitToFiles="
95+
+ limitToFiles
96+
+ '}';
8497
}
8598

8699
@Override
87100
public int hashCode() {
88101
return Objects.hash(type, hash, limitToFiles);
89102
}
90103
}
104+
105+
private static class AnalysisContext {
106+
private final String flow;
107+
private final String initiator = "IDE";
108+
private final String orgDisplayName;
109+
110+
public AnalysisContext(String flow, String orgDisplayName) {
111+
this.flow = flow;
112+
this.orgDisplayName = orgDisplayName;
113+
}
114+
115+
public String getFlow() {
116+
return flow;
117+
}
118+
119+
public String getOrgDisplayName() {
120+
return orgDisplayName;
121+
}
122+
123+
public String getInitiator() {
124+
return initiator;
125+
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) return true;
130+
if (o == null || getClass() != o.getClass()) return false;
131+
AnalysisContext that = (AnalysisContext) o;
132+
return Objects.equals(flow, that.flow) && Objects.equals(orgDisplayName, that.orgDisplayName);
133+
}
134+
135+
@Override
136+
public int hashCode() {
137+
return Objects.hash(flow, orgDisplayName);
138+
}
139+
140+
@Override
141+
public String toString() {
142+
return "AnalysisContext{" +
143+
"flow='" + flow + '\'' +
144+
", initiator='" + initiator + '\'' +
145+
", orgDisplayName='" + orgDisplayName + '\'' +
146+
'}';
147+
}
148+
}
91149
}

src/test/java/ai/deepcode/javaclient/core/AnalysisDataTest.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ public void reupload_files_if_initial_upload_does_not_succeed() {
104104
public void if_file_upload_fail_getAnalysis_should_not_be_invoked() {
105105
restApi = new RestApiMockWithBrokenFileUpload() {
106106
@Override
107-
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
107+
public @NotNull GetAnalysisResponse getAnalysis(
108+
String token,
109+
String bundleId,
110+
Integer severity,
111+
List<String> filesToAnalyse,
112+
String shard,
113+
String ideProductName,
114+
String orgDisplayName
115+
) {
108116
throw new RuntimeException("getAnalysis should NOT be invoked");
109117
}
110118
};
@@ -158,7 +166,15 @@ public void getAnalysis_recover_during_polling_if_operation_sometimes_does_not_s
158166
}
159167

160168
@Override
161-
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
169+
public @NotNull GetAnalysisResponse getAnalysis(
170+
String token,
171+
String bundleId,
172+
Integer severity,
173+
List<String> filesToAnalyse,
174+
String shard,
175+
String ideProductName,
176+
String orgDisplayName
177+
) {
162178
final GetAnalysisResponse response = Objects.requireNonNull(responses.poll());
163179
if (response.getStatus().equals(COMPLETE)) {
164180
isCompleted[0] = true;

src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeParamsMock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class DeepCodeParamsMock extends DeepCodeParamsBase {
88

99
public DeepCodeParamsMock(DeepCodeRestApi restApi) {
10-
super(true, "", false, false, 1, "", "", "", () -> 1000L, restApi);
10+
super(true, "", false, false, 1, "", "", "", "", () -> 1000L, restApi);
1111
}
1212

1313
@Override

src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeRestApiMock.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ public void setBaseUrl(@Nullable String baseUrl, boolean disableSslVerification,
3636
}
3737

3838
@Override
39-
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
39+
public @NotNull GetAnalysisResponse getAnalysis(
40+
String token,
41+
String bundleId,
42+
Integer severity,
43+
List<String> filesToAnalyse,
44+
String shard,
45+
String ideProductName,
46+
String orgDisplayName
47+
) {
4048
throw new UnsupportedOperationException();
4149
}
4250

0 commit comments

Comments
 (0)