Skip to content

Commit

Permalink
Merge pull request #424 from snyk/feat/code-orgname-header
Browse files Browse the repository at this point in the history
feat: provided a customer http header with organization name
  • Loading branch information
Arvi3d authored Feb 16, 2023
2 parents 8e30a78 + d7d6786 commit cc615a0
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 87 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
run: ./gradlew clean check -x detekt
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
SNYK_ORG_NAME: ${{ secrets.SNYK_ORG_NAME }}

pluginVerifier:
name: Plugin Verifier
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- name: Test
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
SNYK_ORG_NAME: ${{ secrets.SNYK_ORG_NAME }}
run: |
./gradlew test integTest verifyPlugin clean
git clean -d --force
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#################################
build/
out/
/bin/

#################################
# Gradle files #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class DeepCodeRestApiImplTest {

// !!! Will works only with already logged sessionToken
private static final String loggedToken = System.getenv("SNYK_TOKEN");
private static final String loggedOrgName = System.getenv("SNYK_ORG_NAME");
private static final String baseUrl = System.getenv("DEEPROXY_API_URL");

private static String bundleId = null;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void _025_getFilters() {
@Test
public void _030_createBundle_from_source() throws NoSuchAlgorithmException {
System.out.println("\n--------------Create Bundle from Source----------------\n");
CreateBundleResponse response = createBundleFromSource(loggedToken);
CreateBundleResponse response = createBundleFromSource(loggedToken, loggedOrgName);
assertNotNull(response);
System.out.printf(
"Create Bundle call return:\nStatus code [%1$d] %3$s \nBundleId: [%2$s]\n",
Expand All @@ -88,29 +89,30 @@ public void _030_createBundle_from_source() throws NoSuchAlgorithmException {
}

@NotNull
private CreateBundleResponse createBundleFromSource(String token)
private CreateBundleResponse createBundleFromSource(String token, String orgName)
throws NoSuchAlgorithmException {
FileContentRequest fileContent = new FileContentRequest();
fileContent.put(
"/AnnotatorTest.java",
new FileHash2ContentRequest(getHash(testFileContent), testFileContent));
CreateBundleResponse response = restApiClient.createBundle(token, fileContent);
CreateBundleResponse response = restApiClient.createBundle(token, orgName, fileContent);
return response;
}

@Test
public void _031_createBundle_wrong_request() throws NoSuchAlgorithmException {
System.out.println("\n--------------Create Bundle with wrong requests----------------\n");
final String brokenToken = "fff";
CreateBundleResponse response = createBundleFromSource(brokenToken);
final String brokenOrgName = "org-name";
CreateBundleResponse response = createBundleFromSource(brokenToken, brokenOrgName);
assertNotNull(response);
assertEquals(
"Create Bundle call with malformed token should not be accepted by server",
401,
response.getStatusCode());
System.out.printf(
"Create Bundle call with malformed token [%1$s] is not accepted by server with Status code [%2$d].\n",
brokenToken, response.getStatusCode());
"Create Bundle call with malformed token [%1$s] and org name [%2$s] is not accepted by server with Status code [%3$d].\n",
brokenToken, brokenOrgName, response.getStatusCode());
}

@Test
Expand All @@ -123,7 +125,7 @@ public void _035_createBundle_with_hash() {
@NotNull
private CreateBundleResponse createBundleWithHash() {
FileHashRequest files = createFileHashRequest(null);
CreateBundleResponse response = restApiClient.createBundle(loggedToken, files);
CreateBundleResponse response = restApiClient.createBundle(loggedToken, loggedOrgName, files);
assertNotNull(response);
System.out.printf(
"Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -139,7 +141,7 @@ public void _036_Check_Bundle() {
System.out.println("\n--------------Check Bundle----------------\n");
FileHashRequest fileHashRequest = createFileHashRequest(null);
CreateBundleResponse createBundleResponse =
restApiClient.createBundle(loggedToken, fileHashRequest);
restApiClient.createBundle(loggedToken, loggedOrgName, fileHashRequest);
assertNotNull(createBundleResponse);
System.out.printf(
"\nCreate Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -151,7 +153,7 @@ public void _036_Check_Bundle() {
assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty());

CreateBundleResponse checkBundleResponse =
restApiClient.checkBundle(loggedToken, createBundleResponse.getBundleHash());
restApiClient.checkBundle(loggedToken, loggedOrgName, createBundleResponse.getBundleHash());
assertNotNull(checkBundleResponse);
System.out.printf(
"\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -177,7 +179,7 @@ public void _036_Check_Bundle() {
assertEquals(200, uploadFileResponse.getStatusCode());

CreateBundleResponse createBundleResponse1 =
restApiClient.checkBundle(loggedToken, createBundleResponse.getBundleHash());
restApiClient.checkBundle(loggedToken, loggedOrgName, createBundleResponse.getBundleHash());
assertNotNull(createBundleResponse1);
System.out.printf(
"\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand Down Expand Up @@ -261,7 +263,7 @@ public void _037_ExtendBundle() {
new ExtendBundleWithHashRequest(newFileHashRequest, Collections.emptyList());
CreateBundleResponse extendBundleResponse =
restApiClient.extendBundle(
loggedToken, createBundleResponse.getBundleHash(), extendBundleWithHashRequest);
loggedToken, loggedOrgName, createBundleResponse.getBundleHash(), extendBundleWithHashRequest);
assertNotNull(extendBundleResponse);
System.out.printf(
"Extend Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -278,7 +280,7 @@ public void _040_UploadFiles() {
System.out.println("\n--------------Upload Files by Hash----------------\n");
FileHashRequest fileHashRequest = createFileHashRequest(null);
CreateBundleResponse createBundleResponse =
restApiClient.createBundle(loggedToken, fileHashRequest);
restApiClient.createBundle(loggedToken, loggedOrgName, fileHashRequest);
assertNotNull(createBundleResponse);
System.out.printf(
"Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand Down Expand Up @@ -321,7 +323,7 @@ private EmptyResponse doUploadFile(
map.put(filePath, new FileHash2ContentRequest(fileHash, fileText));
ExtendBundleWithContentRequest ebr =
new ExtendBundleWithContentRequest(map, Collections.emptyList());
return restApiClient.extendBundle(loggedToken, createBundleResponse.getBundleHash(), ebr);
return restApiClient.extendBundle(loggedToken, loggedOrgName, createBundleResponse.getBundleHash(), ebr);
}

@Test
Expand All @@ -348,12 +350,12 @@ private GetAnalysisResponse doAnalysisAndWait(List<String> analysedFiles, Intege
for (int i = 0; i < 120; i++) {
response = restApiClient.getAnalysis(
loggedToken,
loggedOrgName,
bundleId,
severity,
analysedFiles,
bundleId,
"test-java-client-ide",
"test-java-client-org"
"test-java-client-ide"
);
if (response.getStatus().equals("COMPLETE")) break;
Thread.sleep(1000);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public interface DeepCodeRestApi {
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse createBundle(String token, FileContentRequest files);
CreateBundleResponse createBundle(String token, String orgName, FileContentRequest files);

/**
* Creates a new bundle for file(s) with Hash.
*
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse createBundle(String token, FileHashRequest files);
CreateBundleResponse createBundle(String token, String orgName, FileHashRequest files);

/**
* Checks the status of a bundle.
Expand All @@ -33,7 +33,7 @@ public interface DeepCodeRestApi {
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse checkBundle(String token, String bundleId);
CreateBundleResponse checkBundle(String token, String orgName, String bundleId);

/**
* Creates a new bundle by extending a previously uploaded one.
Expand All @@ -43,7 +43,7 @@ public interface DeepCodeRestApi {
*/
@NotNull
<Req> CreateBundleResponse extendBundle(
String token, String bundleId, Req request);
String token, String orgName, String bundleId, Req request);

/**
* Starts a new bundle analysis or checks its current status and available results.
Expand All @@ -53,12 +53,12 @@ <Req> CreateBundleResponse extendBundle(
@NotNull
GetAnalysisResponse getAnalysis(
String token,
String orgName,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
String ideProductName
);

/**
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,28 @@ private interface CreateBundleCall {
@retrofit2.http.Headers("Content-Type: application/json")
@POST("bundle")
Call<CreateBundleResponse> doCreateBundle(
@Header("Session-Token") String token, @Body FileContentRequest files);
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Body FileContentRequest files);

@retrofit2.http.Headers("Content-Type: application/json")
@POST("bundle")
Call<CreateBundleResponse> doCreateBundle(
@Header("Session-Token") String token, @Body FileHashRequest files);
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Body FileHashRequest files);
}

private static <Req> CreateBundleResponse doCreateBundle(String token, Req request) {
private static <Req> CreateBundleResponse doCreateBundle(String token, String orgName, Req request) {
CreateBundleCall createBundleCall = retrofit.create(CreateBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
if (request instanceof FileContentRequest)
retrofitResponse =
createBundleCall.doCreateBundle(token, (FileContentRequest) request).execute();
createBundleCall.doCreateBundle(token, orgName, (FileContentRequest) request).execute();
else if (request instanceof FileHashRequest)
retrofitResponse =
createBundleCall.doCreateBundle(token, (FileHashRequest) request).execute();
createBundleCall.doCreateBundle(token, orgName, (FileHashRequest) request).execute();
else throw new IllegalArgumentException();
} catch (IOException e) {
return new CreateBundleResponse();
Expand Down Expand Up @@ -175,8 +179,8 @@ else if (request instanceof FileHashRequest)
*/
@Override
@NotNull
public CreateBundleResponse createBundle(String token, FileContentRequest files) {
return doCreateBundle(token, files);
public CreateBundleResponse createBundle(String token, String orgName, FileContentRequest files) {
return doCreateBundle(token, orgName, files);
}

/**
Expand All @@ -186,15 +190,16 @@ public CreateBundleResponse createBundle(String token, FileContentRequest files)
*/
@Override
@NotNull
public CreateBundleResponse createBundle(String token, FileHashRequest files) {
return doCreateBundle(token, files);
public CreateBundleResponse createBundle(String token, String orgName, FileHashRequest files) {
return doCreateBundle(token, orgName, files);
}

private interface CheckBundleCall {
// @retrofit2.http.Headers("Content-Type: application/json")
@GET("bundle/{bundleId}")
Call<CreateBundleResponse> doCheckBundle(
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Path(value = "bundleId", encoded = true) String bundleId);
}

Expand All @@ -206,11 +211,11 @@ Call<CreateBundleResponse> doCheckBundle(
*/
@Override
@NotNull
public CreateBundleResponse checkBundle(String token, String bundleId) {
public CreateBundleResponse checkBundle(String token, String orgName, String bundleId) {
CheckBundleCall checkBundleCall = retrofit.create(CheckBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
retrofitResponse = checkBundleCall.doCheckBundle(token, bundleId).execute();
retrofitResponse = checkBundleCall.doCheckBundle(token, orgName, bundleId).execute();
} catch (IOException e) {
return new CreateBundleResponse();
}
Expand Down Expand Up @@ -244,13 +249,15 @@ private interface ExtendBundleCall {
@PUT("bundle/{bundleId}")
Call<CreateBundleResponse> doExtendBundle(
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Path(value = "bundleId", encoded = true) String bundleId,
@Body ExtendBundleWithHashRequest extendBundleWithHashRequest);

@retrofit2.http.Headers("Content-Type: application/json")
@PUT("bundle/{bundleId}")
Call<CreateBundleResponse> doExtendBundle(
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Path(value = "bundleId", encoded = true) String bundleId,
@Body ExtendBundleWithContentRequest extendBundleWithContentRequest);
}
Expand All @@ -264,19 +271,19 @@ Call<CreateBundleResponse> doExtendBundle(
@Override
@NotNull
public <Req> CreateBundleResponse extendBundle(
String token, String bundleId, Req request) {
String token, String orgName, String bundleId, Req request) {
ExtendBundleCall extendBundleCall = retrofit.create(ExtendBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
if (request instanceof ExtendBundleWithHashRequest)
retrofitResponse =
extendBundleCall
.doExtendBundle(token, bundleId, (ExtendBundleWithHashRequest) request)
.doExtendBundle(token, orgName, bundleId, (ExtendBundleWithHashRequest) request)
.execute();
else if (request instanceof ExtendBundleWithContentRequest)
retrofitResponse =
extendBundleCall
.doExtendBundle(token, bundleId, (ExtendBundleWithContentRequest) request)
.doExtendBundle(token, orgName, bundleId, (ExtendBundleWithContentRequest) request)
.execute();
else throw new IllegalArgumentException();
} catch (IOException e) {
Expand Down Expand Up @@ -318,7 +325,9 @@ private interface GetAnalysisCall {
@retrofit2.http.Headers("Content-Type: application/json")
@POST("analysis")
Call<GetAnalysisResponse> doGetAnalysis(
@Header("Session-Token") String token, @Body GetAnalysisRequest filesToAnalyse);
@Header("Session-Token") String token,
@Header("snyk-org-name") String orgName,
@Body GetAnalysisRequest filesToAnalyse);
}

/**
Expand All @@ -330,18 +339,18 @@ Call<GetAnalysisResponse> doGetAnalysis(
@NotNull
public GetAnalysisResponse getAnalysis(
String token,
String orgName,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
String shard,
String ideProductName,
String orgDisplayName
String ideProductName
) {
GetAnalysisCall getAnalysisCall = retrofit.create(GetAnalysisCall.class);
try {
Response<GetAnalysisResponse> retrofitResponse =
getAnalysisCall
.doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgDisplayName))
.doGetAnalysis(token, orgName, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgName))
.execute();
GetAnalysisResponse result = retrofitResponse.body();
if (result == null) result = new GetAnalysisResponse();
Expand Down
Loading

0 comments on commit cc615a0

Please sign in to comment.