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

Commit 1389241

Browse files
committed
feat: provide analysisContext key (getAnalysis request) for better tracking/logging on backend
1 parent 1a9b7e8 commit 1389241

File tree

10 files changed

+137
-27
lines changed

10 files changed

+137
-27
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- fix: avoid remove operation for empty immutable List
66
- fix: check file in marker for nullability before proceed
77
- feat: provide unique (per project) `shard` to getAnalysis call
8+
- feat: provide `analysisContext` key (`getAnalysis` request) for better tracking/logging on backend
89
- chore: reshape/refactor REST API wrapper to be replaceable through constructor base DI
910

1011
## [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",
382+
"test-java-client"
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-1
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,7 +31,7 @@ protected DeepCodeParamsBase(
3031
String sessionToken,
3132
String loginUrl,
3233
String ideProductName,
33-
Supplier<Long> getTimeoutForGettingAnalysesMs,
34+
String orgDisplayName, Supplier<Long> getTimeoutForGettingAnalysesMs,
3435
DeepCodeRestApi restApi
3536
) {
3637
this.isEnable = isEnable;
@@ -41,6 +42,7 @@ protected DeepCodeParamsBase(
4142
this.sessionToken = sessionToken;
4243
this.loginUrl = loginUrl;
4344
this.ideProductName = ideProductName;
45+
this.orgDisplayName = orgDisplayName;
4446
this.getTimeoutForGettingAnalysesMs = getTimeoutForGettingAnalysesMs;
4547
this.restApi = restApi;
4648
}
@@ -134,4 +136,8 @@ public String getIdeProductName() {
134136
public long getTimeoutForGettingAnalysesMs() {
135137
return getTimeoutForGettingAnalysesMs.get();
136138
}
139+
140+
public String getOrgDisplayName() {
141+
return orgDisplayName;
142+
}
137143
}

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

+72-16
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,25 @@ public GetAnalysisRequest(
2225
List<String> limitToFiles,
2326
Integer severity,
2427
String shard,
25-
boolean prioritized,
28+
String ideProductName, String orgDisplayName, boolean prioritized,
2629
boolean legacy
2730
) {
2831
this.key = new GetAnalysisKey(bundleHash, limitToFiles, shard);
32+
this.analysisContext = new AnalysisContext(ideProductName, orgDisplayName);
2933
this.severity = severity;
3034
this.prioritized = prioritized;
3135
this.legacy = legacy;
3236
}
3337

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

3849
private static class GetAnalysisKey {
@@ -65,27 +76,72 @@ public boolean equals(Object o) {
6576
if (o == null || getClass() != o.getClass()) return false;
6677
GetAnalysisKey that = (GetAnalysisKey) o;
6778
return type.equals(that.type)
68-
&& hash.equals(that.hash)
69-
&& Objects.equals(limitToFiles, that.limitToFiles);
79+
&& hash.equals(that.hash)
80+
&& Objects.equals(limitToFiles, that.limitToFiles);
7081
}
7182

7283
@Override
7384
public String toString() {
7485
return "GetAnalysisKey{"
75-
+ "type='"
76-
+ type
77-
+ '\''
78-
+ ", hash='"
79-
+ hash
80-
+ '\''
81-
+ ", limitToFiles="
82-
+ limitToFiles
83-
+ '}';
86+
+ "type='"
87+
+ type
88+
+ '\''
89+
+ ", hash='"
90+
+ hash
91+
+ '\''
92+
+ ", limitToFiles="
93+
+ limitToFiles
94+
+ '}';
8495
}
8596

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

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)