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

Commit

Permalink
Merge pull request #35 from snyk/feat/ROAD-742_analysisContext_for_ge…
Browse files Browse the repository at this point in the history
…tAnlysis

feat: provide `analysisContext` key (`getAnalysis` request) for better tracking/logging on backend [ROAD-742]
  • Loading branch information
ArtsiomCh authored Mar 28, 2022
2 parents 33da807 + 09f1370 commit cc044e1
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
- fix: do not try to getAnalysis if `upload files` is not succeed (i.e. `missingFiles` is not empty after uploads)
- fix: avoid remove operation for empty immutable List
- fix: check file in marker for nullability before proceed
- fix: internal ConcurrentModificationException
- feat: provide unique (per project) `shard` to getAnalysis call
- feat: provide `analysisContext` key (`getAnalysis` request) for better tracking/logging on backend
- chore: reshape/refactor REST API wrapper to be replaceable through constructor base DI

## [2.2.1] - 2021-12-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,15 @@ private GetAnalysisResponse doAnalysisAndWait(List<String> analysedFiles, Intege
throws InterruptedException {
GetAnalysisResponse response = null;
for (int i = 0; i < 120; i++) {
response = restApiClient.getAnalysis(loggedToken, bundleId, severity, analysedFiles, bundleId);
response = restApiClient.getAnalysis(
loggedToken,
bundleId,
severity,
analysedFiles,
bundleId,
"test-java-client-ide",
"test-java-client-org"
);
if (response.getStatus().equals("COMPLETE")) break;
Thread.sleep(1000);
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ void setBaseUrl(
* @return {@link GetAnalysisResponse} instance}
*/
@NotNull GetAnalysisResponse getAnalysis(
String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard);
String token,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
);

/**
* Requests current filtering options for uploaded bundles.
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,19 @@ Call<GetAnalysisResponse> doGetAnalysis(
@Override
@NotNull
public GetAnalysisResponse getAnalysis(
String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
String token,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
) {
GetAnalysisCall getAnalysisCall = retrofit.create(GetAnalysisCall.class);
try {
Response<GetAnalysisResponse> retrofitResponse =
getAnalysisCall
.doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard))
.doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgDisplayName))
.execute();
GetAnalysisResponse result = retrofitResponse.body();
if (result == null) result = new GetAnalysisResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,9 @@ private GetAnalysisResponse doGetAnalysis(
bundleId,
deepCodeParams.getMinSeverity(),
filesToAnalyse,
HashContentUtilsBase.calculateHash(pdUtils.getProjectName(project))
);
HashContentUtilsBase.calculateHash(pdUtils.getProjectName(project)),
deepCodeParams.getIdeProductName(),
deepCodeParams.getOrgDisplayName());

pdUtils.progressCheckCanceled(progress);
dcLogger.logInfo(response.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class DeepCodeParamsBase {
// Inner params
private String loginUrl;
private String ideProductName;
private String orgDisplayName;
private Supplier<Long> getTimeoutForGettingAnalysesMs;
private final DeepCodeRestApi restApi;

Expand All @@ -30,6 +31,7 @@ protected DeepCodeParamsBase(
String sessionToken,
String loginUrl,
String ideProductName,
String orgDisplayName,
Supplier<Long> getTimeoutForGettingAnalysesMs,
DeepCodeRestApi restApi
) {
Expand All @@ -41,6 +43,7 @@ protected DeepCodeParamsBase(
this.sessionToken = sessionToken;
this.loginUrl = loginUrl;
this.ideProductName = ideProductName;
this.orgDisplayName = orgDisplayName;
this.getTimeoutForGettingAnalysesMs = getTimeoutForGettingAnalysesMs;
this.restApi = restApi;
}
Expand Down Expand Up @@ -134,4 +137,8 @@ public String getIdeProductName() {
public long getTimeoutForGettingAnalysesMs() {
return getTimeoutForGettingAnalysesMs.get();
}

public String getOrgDisplayName() {
return orgDisplayName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected abstract void doBackgroundRun(
private static final Map<Object, Set<Object>> mapProject2Progresses = new ConcurrentHashMap<>();

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

// ??? list of all running background tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

public class GetAnalysisRequest {
private GetAnalysisKey key;
private AnalysisContext analysisContext;
private Integer severity;
private boolean prioritized;
private boolean legacy;

/**
* @param bundleHash
* @param limitToFiles list of filePath
* @param limitToFiles list of filePath
* @param severity
* @param shard uniq String (hash) per Project to optimize jobs on backend (run on the same worker to reuse caches)
* @param shard uniq String (hash) per Project to optimize jobs on backend (run on the same worker to reuse caches)
* @param ideProductName specific IDE
* @param orgDisplayName client’s snyk organization name
* @param prioritized
* @param legacy
*/
Expand All @@ -22,17 +25,27 @@ public GetAnalysisRequest(
List<String> limitToFiles,
Integer severity,
String shard,
String ideProductName,
String orgDisplayName,
boolean prioritized,
boolean legacy
) {
this.key = new GetAnalysisKey(bundleHash, limitToFiles, shard);
this.analysisContext = new AnalysisContext(ideProductName, orgDisplayName);
this.severity = severity;
this.prioritized = prioritized;
this.legacy = legacy;
}

public GetAnalysisRequest(String bundleHash, List<String> limitToFiles, Integer severity, String shard) {
this(bundleHash, limitToFiles, severity, shard, false, true);
public GetAnalysisRequest(
String bundleHash,
List<String> limitToFiles,
Integer severity,
String shard,
String ideProductName,
String orgDisplayName
) {
this(bundleHash, limitToFiles, severity, shard, ideProductName, orgDisplayName, false, true);
}

private static class GetAnalysisKey {
Expand Down Expand Up @@ -65,27 +78,72 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
GetAnalysisKey that = (GetAnalysisKey) o;
return type.equals(that.type)
&& hash.equals(that.hash)
&& Objects.equals(limitToFiles, that.limitToFiles);
&& hash.equals(that.hash)
&& Objects.equals(limitToFiles, that.limitToFiles);
}

@Override
public String toString() {
return "GetAnalysisKey{"
+ "type='"
+ type
+ '\''
+ ", hash='"
+ hash
+ '\''
+ ", limitToFiles="
+ limitToFiles
+ '}';
+ "type='"
+ type
+ '\''
+ ", hash='"
+ hash
+ '\''
+ ", limitToFiles="
+ limitToFiles
+ '}';
}

@Override
public int hashCode() {
return Objects.hash(type, hash, limitToFiles);
}
}

private static class AnalysisContext {
private final String flow;
private final String initiator = "IDE";
private final String orgDisplayName;

public AnalysisContext(String flow, String orgDisplayName) {
this.flow = flow;
this.orgDisplayName = orgDisplayName;
}

public String getFlow() {
return flow;
}

public String getOrgDisplayName() {
return orgDisplayName;
}

public String getInitiator() {
return initiator;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AnalysisContext that = (AnalysisContext) o;
return Objects.equals(flow, that.flow) && Objects.equals(orgDisplayName, that.orgDisplayName);
}

@Override
public int hashCode() {
return Objects.hash(flow, orgDisplayName);
}

@Override
public String toString() {
return "AnalysisContext{" +
"flow='" + flow + '\'' +
", initiator='" + initiator + '\'' +
", orgDisplayName='" + orgDisplayName + '\'' +
'}';
}
}
}
20 changes: 18 additions & 2 deletions src/test/java/ai/deepcode/javaclient/core/AnalysisDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ public void reupload_files_if_initial_upload_does_not_succeed() {
public void if_file_upload_fail_getAnalysis_should_not_be_invoked() {
restApi = new RestApiMockWithBrokenFileUpload() {
@Override
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
public @NotNull GetAnalysisResponse getAnalysis(
String token,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
) {
throw new RuntimeException("getAnalysis should NOT be invoked");
}
};
Expand Down Expand Up @@ -158,7 +166,15 @@ public void getAnalysis_recover_during_polling_if_operation_sometimes_does_not_s
}

@Override
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
public @NotNull GetAnalysisResponse getAnalysis(
String token,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
) {
final GetAnalysisResponse response = Objects.requireNonNull(responses.poll());
if (response.getStatus().equals(COMPLETE)) {
isCompleted[0] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class DeepCodeParamsMock extends DeepCodeParamsBase {

public DeepCodeParamsMock(DeepCodeRestApi restApi) {
super(true, "", false, false, 1, "", "", "", () -> 1000L, restApi);
super(true, "", false, false, 1, "", "", "", "", () -> 1000L, restApi);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public void setBaseUrl(@Nullable String baseUrl, boolean disableSslVerification,
}

@Override
public @NotNull GetAnalysisResponse getAnalysis(String token, String bundleId, Integer severity, List<String> filesToAnalyse, String shard) {
public @NotNull GetAnalysisResponse getAnalysis(
String token,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
) {
throw new UnsupportedOperationException();
}

Expand Down

0 comments on commit cc044e1

Please sign in to comment.