From 6c539dfc96302037cedd21eaea60ae7d3c01fd09 Mon Sep 17 00:00:00 2001 From: Bastian Doetsch Date: Mon, 29 Nov 2021 20:49:39 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Use=20new=20snyk=20code=20a?= =?UTF-8?q?pi=20[ROAD-495]=20=20(#28)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: ⬆️ update to latest snyk-code api [ROAD-495] * chore: ⬆️ update version to 2.2.0 [ROAD-495] - also fix javadoc and bump dependencies * chore: ⬆️ update suggestions format with field leadURL [ROAD-495] * chore: ⬆️ cleanup redundant semicolon [ROAD-495] * chore: ⬆️ implement new analysis response payload [ROAD-495] * chore: ⬆️ wait for up to two minutes for analysis in integration test [ROAD-495] * chore: ⬆️ fix PR review items [ROAD-495] * chore: 🔥️ remove LoginUtilsBase.java [ROAD-495] * chore: split ExtendBundleRequest; adding some javadocs. * chore: reformat project sources with .editorconfig settings * chore: prepare 2.2.0 release Co-authored-by: Artsiom Chapialiou --- .editorconfig | 25 ++ CHANGELOG.md | 3 + build.gradle | 14 +- .../javaclient/DeepCodeRestApiTest.java | 277 ++++++++---------- .../deepcode/javaclient/DeepCodeRestApi.java | 243 ++++++--------- .../javaclient/core/AnalysisDataBase.java | 138 +++++---- .../core/DeepCodeIgnoreInfoHolderBase.java | 92 +++--- .../javaclient/core/DeepCodeParamsBase.java | 12 +- .../javaclient/core/DeepCodeUtilsBase.java | 17 +- .../javaclient/core/HashContentUtilsBase.java | 37 +-- .../javaclient/core/LoginUtilsBase.java | 129 -------- .../deepcode/javaclient/core/MyTextRange.java | 16 +- .../core/PlatformDependentUtilsBase.java | 8 +- .../javaclient/core/RunUtilsBase.java | 8 +- .../javaclient/core/SuggestionForFile.java | 26 +- .../requests/ExtendBundleRequest.java | 27 -- .../ExtendBundleWithContentRequest.java | 26 ++ .../requests/ExtendBundleWithHashRequest.java | 25 ++ .../javaclient/requests/FileContent.java | 20 -- .../requests/FileContentRequest.java | 16 +- .../requests/FileHash2ContentRequest.java | 43 ++- .../javaclient/requests/FileHashRequest.java | 18 +- .../requests/GetAnalysisRequest.java | 77 ++++- .../javaclient/responses/AnalysisResults.java | 26 -- .../responses/CreateBundleResponse.java | 26 +- .../javaclient/responses/EmptyResponse.java | 5 +- .../responses/GetAnalysisResponse.java | 28 +- .../responses/GetFiltersResponse.java | 3 +- .../javaclient/responses/LoginResponse.java | 2 +- .../javaclient/responses/MarkerPosition.java | 2 +- .../javaclient/responses/Suggestion.java | 9 +- .../core/DeepCodeIgnoreInfoHolderTest.java | 78 ++--- .../javaclient/core/RunUtilsTest.java | 9 +- .../core/mocks/AnalysisDataBaseMock.java | 6 +- .../mocks/DeepCodeIgnoreInfoHolderMock.java | 11 +- .../core/mocks/DeepCodeParamsMock.java | 11 +- .../core/mocks/DeepCodeUtilsMock.java | 7 +- .../javaclient/core/mocks/LoggerMock.java | 12 +- .../PlatformDependentUtilsAbstractMock.java | 44 +-- .../core/mocks/RunUtilsBaseMock.java | 7 +- 40 files changed, 743 insertions(+), 840 deletions(-) create mode 100644 .editorconfig delete mode 100644 src/main/java/ai/deepcode/javaclient/core/LoginUtilsBase.java delete mode 100644 src/main/java/ai/deepcode/javaclient/requests/ExtendBundleRequest.java create mode 100644 src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithContentRequest.java create mode 100644 src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithHashRequest.java delete mode 100644 src/main/java/ai/deepcode/javaclient/requests/FileContent.java delete mode 100644 src/main/java/ai/deepcode/javaclient/responses/AnalysisResults.java diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..73f50c7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,25 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.kt] +indent_size = 4 +wildcard_import_limit = 999 +ij_kotlin_packages_to_use_import_on_demand = ^ +ij_kotlin_name_count_to_use_star_import = 999 +ij_kotlin_name_count_to_use_star_import_for_members = 999 +ij_kotlin_imports_layout = *, java.**, javax.**, kotlin.**, ^ + +[*.java] +ij_java_names_count_to_use_import_on_demand = 999 +ij_java_class_count_to_use_import_on_demand = 999 +ij_java_packages_to_use_import_on_demand = ^ diff --git a/CHANGELOG.md b/CHANGELOG.md index d6529ce..479199f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [2.2.0] - 2020-11-29 +- feat: update to latest snyk-code api + ## [2.1.12] - 2020-10-17 - fix: isFullRescanRequested() should be False after rescan finished and before UI updates. diff --git a/build.gradle b/build.gradle index 082b743..9e4995a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,9 +6,10 @@ plugins { group = "io.snyk.code.sdk" archivesBaseName = "snyk-code-client" -version = "2.1.12" +version = "2.2.0" repositories { + mavenLocal() mavenCentral() } @@ -44,12 +45,13 @@ compileTestJava { } dependencies { - implementation "com.squareup.retrofit2:retrofit:2.7.1" - implementation "com.squareup.retrofit2:converter-gson:2.7.1" - implementation "org.jetbrains:annotations:15.0" + implementation 'com.squareup.retrofit2:retrofit:2.9.0' + implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation 'org.jetbrains:annotations:22.0.0' + implementation 'com.squareup.okhttp3:logging-interceptor:4.9.2' - testImplementation "junit:junit:4.12" - integTestImplementation "junit:junit:4.12" + testImplementation 'junit:junit:4.13.2' + integTestImplementation 'junit:junit:4.13.2' } task integTest(type: Test) { diff --git a/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java b/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java index 38bda7e..70c4931 100644 --- a/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java +++ b/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java @@ -3,8 +3,16 @@ */ package ai.deepcode.javaclient; -import ai.deepcode.javaclient.requests.*; -import ai.deepcode.javaclient.responses.*; +import ai.deepcode.javaclient.requests.ExtendBundleWithContentRequest; +import ai.deepcode.javaclient.requests.ExtendBundleWithHashRequest; +import ai.deepcode.javaclient.requests.FileContentRequest; +import ai.deepcode.javaclient.requests.FileHash2ContentRequest; +import ai.deepcode.javaclient.requests.FileHashRequest; +import ai.deepcode.javaclient.responses.CreateBundleResponse; +import ai.deepcode.javaclient.responses.EmptyResponse; +import ai.deepcode.javaclient.responses.GetAnalysisResponse; +import ai.deepcode.javaclient.responses.GetFiltersResponse; +import org.jetbrains.annotations.NotNull; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; @@ -19,10 +27,13 @@ import java.security.NoSuchAlgorithmException; import java.util.Collections; import java.util.List; +import java.util.Objects; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.IsNull.notNullValue; -import static org.junit.Assert.*; +import static ai.deepcode.javaclient.core.AnalysisDataBase.COMPLETE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class DeepCodeRestApiTest { @@ -44,69 +55,33 @@ public class DeepCodeRestApiTest { private static String bundleId = null; - private static String userAgent = "Java-client-Test"; - - @Test - public void _010_newLogin() { - System.out.println("\n--------------New Login----------------\n"); - try { - DeepCodeRestApi.newLogin(userAgent); - } catch (UnsupportedOperationException e) { - System.out.printf(e.getMessage()); - } - } - - @Test - public void _020_checkSession() { - System.out.println("\n--------------Check Session----------------\n"); - String token = ""; - int status = 0; - status = DeepCodeRestApi.checkSession(token).getStatusCode(); - System.out.printf("Check Session call with token [%1$s] return [%2$d] code.\n", token, status); - assertEquals(401, status); - - token = "blablabla"; - status = DeepCodeRestApi.checkSession(token).getStatusCode(); - System.out.printf("Check Session call with token [%1$s] return [%2$d] code.\n", token, status); - assertEquals(401, status); - - token = loggedToken; - DeepCodeRestApi.setBaseUrl(baseUrl); - status = DeepCodeRestApi.checkSession(token).getStatusCode(); - System.out.printf( - "Check Session call with logged user's token [%1$s] return [%2$d] code.\n", token, status); - assertEquals( - "Check Session call with logged user's token should return 200 code.", 200, status); - } - @Test public void _022_setBaseUrl() { System.out.println("\n--------------Set base URL----------------\n"); try { - doSetBaseUrlTest("", "blabla", 401); - doSetBaseUrlTest("https://www.google.com/", "blabla", 404); - doSetBaseUrlTest("https://deeproxy.snyk.io/", "blabla", 401); + doSetBaseUrlTest("", 401); + doSetBaseUrlTest("https://www.google.com/", 404); + doSetBaseUrlTest("https://deeproxy.snyk.io/", 401); } finally { DeepCodeRestApi.setBaseUrl(""); } } - private void doSetBaseUrlTest(String baseUrl, String token, int expectedStatusCode) { - DeepCodeRestApi.setBaseUrl(baseUrl); - EmptyResponse response = DeepCodeRestApi.checkSession(token); + private void doSetBaseUrlTest(String baseUrl, int expectedStatusCode) { + DeepCodeRestApi.setBaseUrl(baseUrl, false, true); + EmptyResponse response = DeepCodeRestApi.checkBundle("blabla", "irrelevant"); int status = response.getStatusCode(); String description = response.getStatusDescription(); System.out.printf( "Check Session call to [%3$s] with token [%1$s] return [%2$d] code: [%4$s]\n", - token, status, baseUrl, description); + "blabla", status, baseUrl, description); assertEquals(expectedStatusCode, status); } @Test public void _025_getFilters() { System.out.println("\n--------------Get Filters----------------\n"); - String token = loggedToken; - GetFiltersResponse response = DeepCodeRestApi.getFilters(token); + GetFiltersResponse response = DeepCodeRestApi.getFilters(loggedToken); assertNotNull(response); final String errorMsg = "Get Filters return status code: [" @@ -122,30 +97,34 @@ public void _025_getFilters() { } @Test - public void _030_createBundle_from_source() { + public void _030_createBundle_from_source() throws NoSuchAlgorithmException { System.out.println("\n--------------Create Bundle from Source----------------\n"); - DeepCodeRestApi.setBaseUrl(baseUrl); - int status = DeepCodeRestApi.checkSession(loggedToken).getStatusCode(); - assertEquals(200, status); - FileContent fileContent = new FileContent("/AnnotatorTest.java", testFileContent); - FileContentRequest files = new FileContentRequest(Collections.singletonList(fileContent)); - CreateBundleResponse response = DeepCodeRestApi.createBundle(loggedToken, files); + DeepCodeRestApi.setBaseUrl(baseUrl, false, true); + CreateBundleResponse response = createBundleFromSource(loggedToken); assertNotNull(response); System.out.printf( "Create Bundle call return:\nStatus code [%1$d] %3$s \nBundleId: [%2$s]\n", - response.getStatusCode(), response.getBundleId(), response.getStatusDescription()); + response.getStatusCode(), response.getBundleHash(), response.getStatusDescription()); assertEquals(200, response.getStatusCode()); - bundleId = response.getBundleId(); + bundleId = response.getBundleHash(); + } + + @NotNull + private CreateBundleResponse createBundleFromSource(String token) + throws NoSuchAlgorithmException { + FileContentRequest fileContent = new FileContentRequest(); + fileContent.put( + "/AnnotatorTest.java", + new FileHash2ContentRequest(getHash(testFileContent), testFileContent)); + CreateBundleResponse response = DeepCodeRestApi.createBundle(token, fileContent); + return response; } @Test - public void _031_createBundle_wrong_request() { + public void _031_createBundle_wrong_request() throws NoSuchAlgorithmException { System.out.println("\n--------------Create Bundle with wrong requests----------------\n"); - FileContent fileContent = new FileContent("/AnnotatorTest.java", testFileContent); - FileContentRequest files = new FileContentRequest(Collections.singletonList(fileContent)); - CreateBundleResponse response; final String brokenToken = "fff"; - response = DeepCodeRestApi.createBundle(brokenToken, files); + CreateBundleResponse response = createBundleFromSource(brokenToken); assertNotNull(response); assertEquals( "Create Bundle call with malformed token should not be accepted by server", @@ -154,36 +133,27 @@ public void _031_createBundle_wrong_request() { 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()); - - // seems to be a bug on server: it returns 200 - /* - response = - DeepCodeRestApi.createBundle(loggedToken, new FileContentRequest(Collections.emptyList())); - assertNotNull(response); - assertEquals( - "Create Bundle call with malformed (empty) files array should not be accepted by server", - 400, - response.getStatusCode()); - System.out.printf( - "Create Bundle call with malformed (empty) files array is not accepted by server with Status code [%1$d].\n", - response.getStatusCode()); - */ } @Test public void _035_createBundle_with_hash() { System.out.println("\n--------------Create Bundle with Hash----------------\n"); + CreateBundleResponse response = createBundleWithHash(); + assertEquals(200, response.getStatusCode()); + } + + @NotNull + private CreateBundleResponse createBundleWithHash() { FileHashRequest files = createFileHashRequest(null); CreateBundleResponse response = DeepCodeRestApi.createBundle(loggedToken, 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 uploadUrl: %5$s\n", + "Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", response.getStatusCode(), - response.getBundleId(), + response.getBundleHash(), response.getStatusDescription(), - response.getMissingFiles(), - response.getUploadURL()); - assertEquals(200, response.getStatusCode()); + response.getMissingFiles()); + return response; } @Test @@ -194,31 +164,29 @@ public void _036_Check_Bundle() { DeepCodeRestApi.createBundle(loggedToken, 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 uploadUrl: %5$s\n", + "\nCreate Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", createBundleResponse.getStatusCode(), - createBundleResponse.getBundleId(), + createBundleResponse.getBundleHash(), createBundleResponse.getStatusDescription(), - createBundleResponse.getMissingFiles(), - createBundleResponse.getUploadURL()); + createBundleResponse.getMissingFiles()); assertEquals(200, createBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty()); CreateBundleResponse checkBundleResponse = - DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleId()); + DeepCodeRestApi.checkBundle(loggedToken, 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 uploadUrl: %5$s\n", + "\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", checkBundleResponse.getStatusCode(), - checkBundleResponse.getBundleId(), + checkBundleResponse.getBundleHash(), checkBundleResponse.getStatusDescription(), - checkBundleResponse.getMissingFiles(), - checkBundleResponse.getUploadURL()); + checkBundleResponse.getMissingFiles()); assertEquals(200, checkBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", checkBundleResponse.getMissingFiles().isEmpty()); assertEquals( "Checked and returned bundleId's are different.", - createBundleResponse.getBundleId(), - checkBundleResponse.getBundleId()); + createBundleResponse.getBundleHash(), + checkBundleResponse.getBundleHash()); EmptyResponse uploadFileResponse = doUploadFile(createBundleResponse, fileHashRequest); @@ -231,31 +199,27 @@ public void _036_Check_Bundle() { assertEquals(200, uploadFileResponse.getStatusCode()); CreateBundleResponse createBundleResponse1 = - DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleId()); + DeepCodeRestApi.checkBundle(loggedToken, 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 uploadUrl: %5$s\n", + "\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", createBundleResponse1.getStatusCode(), - createBundleResponse1.getBundleId(), + createBundleResponse1.getBundleHash(), createBundleResponse1.getStatusDescription(), - createBundleResponse1.getMissingFiles(), - createBundleResponse1.getUploadURL()); + createBundleResponse1.getMissingFiles()); assertEquals(200, createBundleResponse1.getStatusCode()); assertTrue( "List of missingFiles is NOT empty.", createBundleResponse1.getMissingFiles().isEmpty()); assertEquals( "Checked and returned bundleId's are different.", - createBundleResponse.getBundleId(), - checkBundleResponse.getBundleId()); + createBundleResponse.getBundleHash(), + checkBundleResponse.getBundleHash()); } private FileHashRequest createFileHashRequest(String fakeFileName) { - DeepCodeRestApi.setBaseUrl(baseUrl); - int status = DeepCodeRestApi.checkSession(loggedToken).getStatusCode(); - assertEquals(200, status); + DeepCodeRestApi.setBaseUrl(baseUrl, false, true); final File testFile = new File(getClass().getClassLoader().getResource("AnnotatorTest.java").getFile()); - MessageDigest digest; String absolutePath = testFile.getAbsolutePath(); String deepCodedPath = (absolutePath.startsWith("/") ? "" : "/") @@ -276,20 +240,29 @@ private FileHashRequest createFileHashRequest(String fakeFileName) { try { // ?? com.intellij.openapi.util.io.FileUtil#loadFile(java.io.File, java.nio.charset.Charset) fileText = Files.readString(Paths.get(absolutePath)); - digest = MessageDigest.getInstance("SHA-256"); + String hash = getHash(fileText); + System.out.printf("File hash: %1$s\n", hash); + FileHashRequest fileHashRequest = new FileHashRequest(); + fileHashRequest.put(deepCodedPath, hash); + return fileHashRequest; } catch (IOException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } + } + + @NotNull + private String getHash(String fileText) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); System.out.println(fileText); System.out.println("-----------------"); byte[] encodedhash = digest.digest(fileText.getBytes(StandardCharsets.UTF_8)); String hash = bytesToHex(encodedhash); - System.out.printf("File hash: %1$s\n", hash); - - return new FileHashRequest(Collections.singletonMap(deepCodedPath, hash)); + return hash; } + @SuppressWarnings( + "DuplicatedCode") // in this test we explicitly allow it to test that hashing works private static String bytesToHex(byte[] hash) { StringBuilder hexString = new StringBuilder(); for (byte b : hash) { @@ -303,34 +276,23 @@ private static String bytesToHex(byte[] hash) { @Test public void _037_ExtendBundle() { System.out.println("\n--------------Extend Bundle----------------\n"); - FileHashRequest fileHashRequest = createFileHashRequest(null); - CreateBundleResponse createBundleResponse = - DeepCodeRestApi.createBundle(loggedToken, 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 uploadUrl: %5$s\n", - createBundleResponse.getStatusCode(), - createBundleResponse.getBundleId(), - createBundleResponse.getStatusDescription(), - createBundleResponse.getMissingFiles(), - createBundleResponse.getUploadURL()); + CreateBundleResponse createBundleResponse = createBundleWithHash(); assertEquals(200, createBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty()); FileHashRequest newFileHashRequest = createFileHashRequest("test2.js"); - ExtendBundleRequest extendBundleRequest = - new ExtendBundleRequest(newFileHashRequest.getFiles(), Collections.emptyList()); + ExtendBundleWithHashRequest extendBundleWithHashRequest = + new ExtendBundleWithHashRequest(newFileHashRequest, Collections.emptyList()); CreateBundleResponse extendBundleResponse = DeepCodeRestApi.extendBundle( - loggedToken, createBundleResponse.getBundleId(), extendBundleRequest); + loggedToken, 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 uploadUrl: %5$s\n", + "Extend Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", extendBundleResponse.getStatusCode(), - extendBundleResponse.getBundleId(), + extendBundleResponse.getBundleHash(), extendBundleResponse.getStatusDescription(), - extendBundleResponse.getMissingFiles(), - extendBundleResponse.getUploadURL()); + extendBundleResponse.getMissingFiles()); assertEquals(200, extendBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", extendBundleResponse.getMissingFiles().isEmpty()); } @@ -343,12 +305,11 @@ public void _040_UploadFiles() { DeepCodeRestApi.createBundle(loggedToken, 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 uploadUrl: %5$s\n", + "Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n", createBundleResponse.getStatusCode(), - createBundleResponse.getBundleId(), + createBundleResponse.getBundleHash(), createBundleResponse.getStatusDescription(), - createBundleResponse.getMissingFiles(), - createBundleResponse.getUploadURL()); + createBundleResponse.getMissingFiles()); assertEquals(200, createBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty()); @@ -366,7 +327,9 @@ public void _040_UploadFiles() { private EmptyResponse doUploadFile( CreateBundleResponse createBundleResponse, FileHashRequest fileHashRequest) { final File testFile = - new File(getClass().getClassLoader().getResource("AnnotatorTest.java").getFile()); + new File( + Objects.requireNonNull(getClass().getClassLoader().getResource("AnnotatorTest.java")) + .getFile()); final String absolutePath = testFile.getAbsolutePath(); String fileText; try { @@ -377,29 +340,41 @@ private EmptyResponse doUploadFile( } final String filePath = createBundleResponse.getMissingFiles().get(0); - final String fileHash = fileHashRequest.getFiles().get(filePath); - final List requestBody = - Collections.singletonList(new FileHash2ContentRequest(fileHash, fileText)); - return DeepCodeRestApi.UploadFiles( - loggedToken, createBundleResponse.getBundleId(), requestBody); + final String fileHash = fileHashRequest.get(filePath); + FileContentRequest map = new FileContentRequest(); + map.put(filePath, new FileHash2ContentRequest(fileHash, fileText)); + ExtendBundleWithContentRequest ebr = + new ExtendBundleWithContentRequest(map, Collections.emptyList()); + return DeepCodeRestApi.extendBundle(loggedToken, createBundleResponse.getBundleHash(), ebr); } @Test - public void _090_getAnalysis() { + public void _090_getAnalysis() throws InterruptedException { System.out.println("\n--------------Get Analysis----------------\n"); assertNotNull( "`bundleId` should be initialized at `_030_createBundle_from_source()`", bundleId); final String deepcodedFilePath = - createFileHashRequest(null).getFiles().keySet().stream().findFirst().orElseThrow(); + createFileHashRequest(null).keySet().stream().findFirst().orElseThrow(); final List analysedFiles = Collections.singletonList(deepcodedFilePath); - assertAndPrintGetAnalysisResponse( - DeepCodeRestApi.getAnalysis(loggedToken, bundleId, null, false, analysedFiles)); - System.out.println("\n---- With `Linters` param:\n"); - assertAndPrintGetAnalysisResponse( - DeepCodeRestApi.getAnalysis(loggedToken, bundleId, null, true, analysedFiles)); + GetAnalysisResponse response = doAnalysisAndWait(analysedFiles, null); + assertAndPrintGetAnalysisResponse(response); + boolean resultsEmpty = response.getSuggestions() == null || response.getSuggestions().isEmpty(); + assertFalse("Analysis results must not be empty", resultsEmpty); System.out.println("\n---- With `severity=2` param:\n"); - assertAndPrintGetAnalysisResponse( - DeepCodeRestApi.getAnalysis(loggedToken, bundleId, 2, false, analysedFiles)); + response = doAnalysisAndWait(analysedFiles, 2); + assertAndPrintGetAnalysisResponse(response); + } + + @NotNull + private GetAnalysisResponse doAnalysisAndWait(List analysedFiles, Integer severity) + throws InterruptedException { + GetAnalysisResponse response = null; + for (int i = 0; i < 120; i++) { + response = DeepCodeRestApi.getAnalysis(loggedToken, bundleId, severity, analysedFiles); + if (response.getStatus().equals("COMPLETE")) break; + Thread.sleep(1000); + } + return response; } private void assertAndPrintGetAnalysisResponse(GetAnalysisResponse response) { @@ -407,17 +382,7 @@ private void assertAndPrintGetAnalysisResponse(GetAnalysisResponse response) { System.out.printf( "Get Analysis call for test file: \n-----------\n %1$s \n-----------\nreturns Status code: %2$s \n%3$s\n", testFileContent, response.getStatusCode(), response); - // assertEquals("DONE", response.getStatus()); + assertEquals(COMPLETE, response.getStatus()); assertEquals("Get Analysis request not succeed", 200, response.getStatusCode()); } - - @Test - public void setBaseUrl_shouldUseEmptyTrustManager_whenDisableSslVerificationIsTrue() { - DeepCodeRestApi.setBaseUrl(baseUrl, true); - - EmptyResponse emptyResponse = DeepCodeRestApi.checkSession(loggedToken); - - assertThat(emptyResponse, notNullValue()); - assertThat(emptyResponse.getStatusCode(), equalTo(200)); - } } diff --git a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java index c16ccd5..fbee0c5 100644 --- a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java +++ b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java @@ -3,23 +3,33 @@ */ package ai.deepcode.javaclient; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import ai.deepcode.javaclient.requests.*; -import ai.deepcode.javaclient.responses.*; - +import ai.deepcode.javaclient.requests.ExtendBundleWithContentRequest; +import ai.deepcode.javaclient.requests.ExtendBundleWithHashRequest; +import ai.deepcode.javaclient.requests.FileContentRequest; +import ai.deepcode.javaclient.requests.FileHashRequest; +import ai.deepcode.javaclient.requests.GetAnalysisRequest; +import ai.deepcode.javaclient.responses.CreateBundleResponse; +import ai.deepcode.javaclient.responses.GetAnalysisResponse; +import ai.deepcode.javaclient.responses.GetFiltersResponse; import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -import retrofit2.http.*; +import retrofit2.http.Body; +import retrofit2.http.GET; +import retrofit2.http.Header; +import retrofit2.http.POST; +import retrofit2.http.PUT; +import retrofit2.http.Path; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -34,23 +44,33 @@ */ public final class DeepCodeRestApi { - // private static final Logger LOGGER = LoggerFactory.getLogger(DeepCodeRestApi.class); - private DeepCodeRestApi() {} private static final String API_URL = "https://deeproxy.snyk.io/"; - private static Retrofit retrofit = buildRetrofit(API_URL, false); + private static Retrofit retrofit = buildRetrofit(API_URL, false, false); // Create simple REST adapter which points the baseUrl. - private static Retrofit buildRetrofit(String baseUrl, boolean disableSslVerification) { - OkHttpClient.Builder builder = new OkHttpClient.Builder() + private static Retrofit buildRetrofit( + String baseUrl, boolean disableSslVerification, boolean requestLogging) { + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + // set your desired log level + if (requestLogging) { + logging.setLevel(HttpLoggingInterceptor.Level.BODY); + } else { + logging.setLevel(HttpLoggingInterceptor.Level.NONE); + } + + OkHttpClient.Builder builder = + new OkHttpClient.Builder() .connectTimeout(100, TimeUnit.SECONDS) .writeTimeout(100, TimeUnit.SECONDS) - .readTimeout(100, TimeUnit.SECONDS); + .readTimeout(100, TimeUnit.SECONDS) + .addInterceptor(logging); + if (disableSslVerification) { X509TrustManager x509TrustManager = buildUnsafeTrustManager(); - final TrustManager[] trustAllCertificates = new TrustManager[]{ x509TrustManager }; + final TrustManager[] trustAllCertificates = new TrustManager[] {x509TrustManager}; try { final String sslProtocol = "SSL"; @@ -59,13 +79,15 @@ private static Retrofit buildRetrofit(String baseUrl, boolean disableSslVerifica SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); builder.sslSocketFactory(sslSocketFactory, x509TrustManager); } catch (NoSuchAlgorithmException | KeyManagementException e) { - //TODO(pavel): extract Retrofit and OkHttpClient into configuration object to simplify API client building. + // TODO(pavel): extract Retrofit and OkHttpClient into configuration object to simplify API + // client building. e.printStackTrace(); } } + OkHttpClient client = builder.build(); return new Retrofit.Builder() - .baseUrl(baseUrl + "publicapi/") + .baseUrl(baseUrl) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); @@ -82,7 +104,7 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) {} @Override public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[]{}; + return new X509Certificate[] {}; } }; } @@ -94,65 +116,16 @@ public X509Certificate[] getAcceptedIssuers() { * #API_URL} */ public static void setBaseUrl(@Nullable String baseUrl) { - setBaseUrl(baseUrl, false); - } - - public static void setBaseUrl(@Nullable String baseUrl, boolean disableSslVerification) { - retrofit = buildRetrofit((baseUrl == null || baseUrl.isEmpty()) ? API_URL : baseUrl, disableSslVerification); - } - - private interface LoginCall { - @retrofit2.http.Headers("Content-Type: application/json") - @POST("login") - Call doNewLogin(@Body SourceString source); - } - - /** - * Requests the creation of a new login session. - * - * @return {@link LoginResponse} instance - */ - @NotNull - public static LoginResponse newLogin(@NotNull String userAgent) throws UnsupportedOperationException { - throw new UnsupportedOperationException("login request is not handled anymore"); - } - - private interface CheckSessionCall { - @GET("session") - Call doCheckSession(@Header("Session-Token") String token); + setBaseUrl(baseUrl, false, false); } - /** - * Checks status of the login process. - * - * @return {@link EmptyResponse} instance - */ - @NotNull - public static EmptyResponse checkSession(String token) { - CheckSessionCall checkSessionCall = retrofit.create(CheckSessionCall.class); - final EmptyResponse result = new EmptyResponse(); - final Response retrofitResponse; - try { - retrofitResponse = checkSessionCall.doCheckSession(token).execute(); - } catch (IOException e) { - return result; - } - result.setStatusCode(retrofitResponse.code()); - switch (retrofitResponse.code()) { - case 200: - result.setStatusDescription("The login process was successful"); - break; - case 304: - result.setStatusDescription("The login process has not been completed yet"); - break; - case 401: - result.setStatusDescription("Missing or invalid sessionToken"); - break; - default: - result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code()); - break; - } - return result; + public static void setBaseUrl( + @Nullable String baseUrl, boolean disableSslVerification, boolean requestLogging) { + retrofit = + buildRetrofit( + (baseUrl == null || baseUrl.isEmpty()) ? API_URL : baseUrl, + disableSslVerification, + requestLogging); } private interface CreateBundleCall { @@ -210,7 +183,7 @@ else if (request instanceof FileHashRequest) } /** - * Creates a new bundle with direct file(s) source at {@link FileContent}. + * Creates a new bundle with direct file(s) source. * * @return {@link CreateBundleResponse} instance */ @@ -233,8 +206,8 @@ private interface CheckBundleCall { // @retrofit2.http.Headers("Content-Type: application/json") @GET("bundle/{bundleId}") Call doCheckBundle( - @Header("Session-Token") String token, - @Path(value = "bundleId", encoded = true) String bundleId); + @Header("Session-Token") String token, + @Path(value = "bundleId", encoded = true) String bundleId); } /** @@ -244,13 +217,11 @@ Call doCheckBundle( * @return {@link CreateBundleResponse} instance */ @NotNull - public static CreateBundleResponse checkBundle( - String token, String bundleId) { + public static CreateBundleResponse checkBundle(String token, String bundleId) { CheckBundleCall checkBundleCall = retrofit.create(CheckBundleCall.class); Response retrofitResponse; try { - retrofitResponse = - checkBundleCall.doCheckBundle(token, bundleId).execute(); + retrofitResponse = checkBundleCall.doCheckBundle(token, bundleId).execute(); } catch (IOException e) { return new CreateBundleResponse(); } @@ -283,9 +254,16 @@ private interface ExtendBundleCall { @retrofit2.http.Headers("Content-Type: application/json") @PUT("bundle/{bundleId}") Call doExtendBundle( - @Header("Session-Token") String token, - @Path(value = "bundleId", encoded = true) String bundleId, - @Body ExtendBundleRequest extendBundleRequest); + @Header("Session-Token") String token, + @Path(value = "bundleId", encoded = true) String bundleId, + @Body ExtendBundleWithHashRequest extendBundleWithHashRequest); + + @retrofit2.http.Headers("Content-Type: application/json") + @PUT("bundle/{bundleId}") + Call doExtendBundle( + @Header("Session-Token") String token, + @Path(value = "bundleId", encoded = true) String bundleId, + @Body ExtendBundleWithContentRequest extendBundleWithContentRequest); } /** @@ -295,13 +273,22 @@ Call doExtendBundle( * @return {@link CreateBundleResponse} instance */ @NotNull - public static CreateBundleResponse extendBundle( - String token, String bundleId, ExtendBundleRequest extendBundleRequest) { + public static CreateBundleResponse extendBundle( + String token, String bundleId, Req request) { ExtendBundleCall extendBundleCall = retrofit.create(ExtendBundleCall.class); Response retrofitResponse; try { - retrofitResponse = - extendBundleCall.doExtendBundle(token, bundleId, extendBundleRequest).execute(); + if (request instanceof ExtendBundleWithHashRequest) + retrofitResponse = + extendBundleCall + .doExtendBundle(token, bundleId, (ExtendBundleWithHashRequest) request) + .execute(); + else if (request instanceof ExtendBundleWithContentRequest) + retrofitResponse = + extendBundleCall + .doExtendBundle(token, bundleId, (ExtendBundleWithContentRequest) request) + .execute(); + else throw new IllegalArgumentException(); } catch (IOException e) { return new CreateBundleResponse(); } @@ -315,7 +302,8 @@ public static CreateBundleResponse extendBundle( result.setStatusDescription("The bundle extension was successful"); break; case 400: - result.setStatusDescription("Attempted to extend a git bundle, or ended up with an empty bundle after the extension"); + result.setStatusDescription( + "Attempted to extend a git bundle, or ended up with an empty bundle after the extension"); break; case 401: result.setStatusDescription("Missing sessionToken or incomplete login process"); @@ -336,67 +324,11 @@ public static CreateBundleResponse extendBundle( return result; } - private interface UploadFilesCall { - @retrofit2.http.Headers("Content-Type: application/json;charset=utf-8") - @POST("file/{bundleId}") - Call doUploadFiles( - @Header("Session-Token") String token, - @Path(value = "bundleId", encoded = true) String bundleId, - @Body List listHash2Content); - } - - /** - * Uploads missing files to a bundle. - * - * @param token - * @param bundleId - * @param request List<FileHash2ContentRequest> - * @return EmptyResponse with return code and description. - */ - public static EmptyResponse UploadFiles( - String token, String bundleId, List request) { - UploadFilesCall uploadFilesCall = retrofit.create(UploadFilesCall.class); - Response retrofitResponse; - try { - retrofitResponse = uploadFilesCall.doUploadFiles(token, bundleId, request).execute(); - } catch (IOException e) { - return new EmptyResponse(); - } - EmptyResponse result = new EmptyResponse(); - result.setStatusCode(retrofitResponse.code()); - switch (retrofitResponse.code()) { - case 200: - result.setStatusDescription("Upload succeeded"); - break; - case 400: - result.setStatusDescription( - "Content and hash mismatch or attempted to upload files to a git bundle"); - break; - case 401: - result.setStatusDescription("Missing sessionToken or incomplete login process"); - break; - case 403: - result.setStatusDescription("Unauthorized access to requested bundle"); - break; - case 413: - result.setStatusDescription("Payload too large"); - break; - default: - result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code()); - break; - } - return result; - } - private interface GetAnalysisCall { @retrofit2.http.Headers("Content-Type: application/json") - @POST("analysis/{bundleId}") + @POST("analysis") Call doGetAnalysis( - @Header("Session-Token") String token, - @Path(value = "bundleId", encoded = true) String bundleId, - @Query("severity") Integer severity, - @QueryName String linters, - @Body GetAnalysisRequest filesToAnalyse); + @Header("Session-Token") String token, @Body GetAnalysisRequest filesToAnalyse); } /** @@ -406,21 +338,12 @@ Call doGetAnalysis( */ @NotNull public static GetAnalysisResponse getAnalysis( - String token, - String bundleId, - Integer severity, - boolean useLinters, - List filesToAnalyse) { + String token, String bundleId, Integer severity, List filesToAnalyse) { GetAnalysisCall getAnalysisCall = retrofit.create(GetAnalysisCall.class); try { Response retrofitResponse = getAnalysisCall - .doGetAnalysis( - token, - bundleId, - severity, - (useLinters) ? "linters" : null, - new GetAnalysisRequest(filesToAnalyse)) + .doGetAnalysis(token, new GetAnalysisRequest(bundleId, filesToAnalyse, severity)) .execute(); GetAnalysisResponse result = retrofitResponse.body(); if (result == null) result = new GetAnalysisResponse(); diff --git a/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java b/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java index 8410fb4..686f791 100644 --- a/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java @@ -1,18 +1,41 @@ package ai.deepcode.javaclient.core; import ai.deepcode.javaclient.DeepCodeRestApi; -import ai.deepcode.javaclient.requests.*; -import ai.deepcode.javaclient.responses.*; +import ai.deepcode.javaclient.requests.ExtendBundleWithContentRequest; +import ai.deepcode.javaclient.requests.ExtendBundleWithHashRequest; +import ai.deepcode.javaclient.requests.FileContentRequest; +import ai.deepcode.javaclient.requests.FileHash2ContentRequest; +import ai.deepcode.javaclient.requests.FileHashRequest; +import ai.deepcode.javaclient.responses.CreateBundleResponse; +import ai.deepcode.javaclient.responses.EmptyResponse; +import ai.deepcode.javaclient.responses.FilePosition; +import ai.deepcode.javaclient.responses.FileSuggestions; +import ai.deepcode.javaclient.responses.FilesMap; +import ai.deepcode.javaclient.responses.GetAnalysisResponse; +import ai.deepcode.javaclient.responses.Marker; +import ai.deepcode.javaclient.responses.Position; +import ai.deepcode.javaclient.responses.Suggestion; +import ai.deepcode.javaclient.responses.Suggestions; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; public abstract class AnalysisDataBase { + public static final String COMPLETE = "COMPLETE"; private final PlatformDependentUtilsBase pdUtils; private final HashContentUtilsBase hashContentUtils; private final DeepCodeParamsBase deepCodeParams; @@ -58,9 +81,6 @@ public List getAnalysis(@NotNull Object file) { * Return Suggestions mapped to Files. * *

Look into cached results ONLY. - * - * @param files - * @return */ @NotNull public Map> getAnalysis(@NotNull Collection files) { @@ -79,8 +99,7 @@ public Map> getAnalysis(@NotNull Collection files) { try { final List first50FilesName = files.stream().limit(50).map(pdUtils::getFileName).collect(Collectors.toList()); - dcLogger.logInfo("Request to remove from cache " + files.size() + " files: " + first50FilesName); + dcLogger.logInfo( + "Request to remove from cache " + files.size() + " files: " + first50FilesName); // todo: do we really need mutex here? MUTEX.lock(); dcLogger.logInfo("MUTEX LOCK"); @@ -175,7 +195,7 @@ public boolean isProjectNOTAnalysed(@NotNull Object project) { public void waitForUpdateAnalysisFinish(@NotNull Object project, @Nullable Object progress) { while (isUpdateAnalysisInProgress(project)) { // delay should be less or equal to runInBackgroundCancellable delay - pdUtils.delay(pdUtils.DEFAULT_DELAY_SMALL, progress); + pdUtils.delay(PlatformDependentUtilsBase.DEFAULT_DELAY_SMALL, progress); } } @@ -192,7 +212,7 @@ public void updateCachedResultsForFiles( return; } final List first50FilesName = - allProjectFiles.stream().limit(50).map(pdUtils::getFileName).collect(Collectors.toList()); + allProjectFiles.stream().limit(50).map(pdUtils::getFileName).collect(Collectors.toList()); dcLogger.logInfo( "Update requested for " + allProjectFiles.size() + " files: " + first50FilesName); if (!deepCodeParams.consentGiven(project)) { @@ -222,14 +242,10 @@ public void updateCachedResultsForFiles( + "]"); } else if (!filesToRemove.isEmpty()) { dcLogger.logWarn( - "Nothing to update for " - + allProjectFiles.size() - + " files: " - + allProjectFiles.toString()); + "Nothing to update for " + allProjectFiles.size() + " files: " + allProjectFiles); } if (!filesToRemove.isEmpty()) { - dcLogger.logInfo( - "Files to remove: " + filesToRemove.size() + " files: " + filesToRemove.toString()); + dcLogger.logInfo("Files to remove: " + filesToRemove.size() + " files: " + filesToRemove); } mapFile2Suggestions.putAll( retrieveSuggestions(project, filesToProceed, filesToRemove, progress)); @@ -246,7 +262,8 @@ public void updateCachedResultsForFiles( private static final Set projectsLoginRequested = ConcurrentHashMap.newKeySet(); private static final Set projectsWithNotSucceedWarnShown = ConcurrentHashMap.newKeySet(); - private boolean isNotSucceed(@NotNull Object project, EmptyResponse response, String internalMessage) { + private boolean isNotSucceed( + @NotNull Object project, EmptyResponse response, String internalMessage) { if (response.getStatusCode() == 200) { projectsWithNotSucceedWarnShown.remove(project); projectsLoginRequested.remove(project); @@ -322,7 +339,7 @@ private List createBundleStep( pdUtils.progressSetText(progress, PREPARE_FILES_TEXT); dcLogger.logInfo(PREPARE_FILES_TEXT); pdUtils.progressCheckCanceled(progress); - Map mapPath2Hash = new HashMap<>(); + FileHashRequest hashRequest = new FileHashRequest(); long sizePath2Hash = 0; int fileCounter = 0; int totalFiles = filesToProceed.size(); @@ -339,20 +356,20 @@ private List createBundleStep( if (fileCounter == 1) dcLogger.logInfo("First file to proceed: \npath = " + path + "\nhash = " + hash); - mapPath2Hash.put(path, hash); - sizePath2Hash += (path.length() + hash.length()) * 2; // rough estimation of bytes occupied + hashRequest.put(path, hash); + sizePath2Hash += (path.length() + hash.length()) * 2L; // rough estimation of bytes occupied if (sizePath2Hash > MAX_BUNDLE_SIZE) { CreateBundleResponse tempBundleResponse = - makeNewBundle(project, mapPath2Hash, Collections.emptyList()); + makeNewBundle(project, hashRequest, Collections.emptyList()); sizePath2Hash = 0; - mapPath2Hash.clear(); + hashRequest.clear(); } } // todo break removeFiles in chunks less then MAX_BANDLE_SIZE // needed ?? we do full rescan for large amount of files to remove - CreateBundleResponse createBundleResponse = makeNewBundle(project, mapPath2Hash, filesToRemove); + CreateBundleResponse createBundleResponse = makeNewBundle(project, hashRequest, filesToRemove); - final String bundleId = createBundleResponse.getBundleId(); + final String bundleId = createBundleResponse.getBundleHash(); List missingFiles = createBundleResponse.getMissingFiles(); dcLogger.logInfo( @@ -467,13 +484,12 @@ private List checkBundle(@NotNull Object project, @NotNull String bundle private CreateBundleResponse makeNewBundle( @NotNull Object project, - @NotNull Map mapPath2Hash, + @NotNull FileHashRequest request, @NotNull Collection filesToRemove) { - final FileHashRequest fileHashRequest = new FileHashRequest(mapPath2Hash); final String parentBundleId = mapProject2BundleId.getOrDefault(project, ""); if (!parentBundleId.isEmpty() && !filesToRemove.isEmpty() - && mapPath2Hash.isEmpty() + && request.isEmpty() && filesToRemove.containsAll(cachedFilesOfProject(project))) { dcLogger.logWarn( "Attempt to Extending a bundle by removing all the parent bundle's files: " @@ -485,7 +501,7 @@ private CreateBundleResponse makeNewBundle( (parentBundleId.isEmpty() ? "Creating new Bundle with " : "Extending existing Bundle [" + parentBundleId + "] with ") - + mapPath2Hash.size() + + request.size() + " files" + (removedFiles.isEmpty() ? "" : " and remove " + removedFiles.size() + " files"); dcLogger.logInfo(message); @@ -493,16 +509,15 @@ private CreateBundleResponse makeNewBundle( final CreateBundleResponse bundleResponse; // check if bundleID for the project already been created if (parentBundleId.isEmpty()) - bundleResponse = - DeepCodeRestApi.createBundle(deepCodeParams.getSessionToken(), fileHashRequest); + bundleResponse = DeepCodeRestApi.createBundle(deepCodeParams.getSessionToken(), request); else { bundleResponse = DeepCodeRestApi.extendBundle( deepCodeParams.getSessionToken(), parentBundleId, - new ExtendBundleRequest(fileHashRequest.getFiles(), removedFiles)); + new ExtendBundleWithHashRequest(request, removedFiles)); } - String newBundleId = bundleResponse.getBundleId(); + String newBundleId = bundleResponse.getBundleHash(); // By man: "Extending a bundle by removing all the parent bundle's files is not allowed." // In reality new bundle returned with next bundleID: // .../DEEPCODE_PRIVATE_BUNDLE/0000000000000000000000000000000000000000000000000000000000000000 @@ -514,7 +529,7 @@ private CreateBundleResponse makeNewBundle( isNotSucceed(project, bundleResponse, "Bad Create/Extend Bundle request: "); // just make new bundle in case of 404 Parent bundle has expired return (bundleResponse.getStatusCode() == 404) - ? makeNewBundle(project, mapPath2Hash, filesToRemove) + ? makeNewBundle(project, request, filesToRemove) : bundleResponse; } @@ -525,18 +540,22 @@ private void doUploadFiles( @NotNull Object progress) { dcLogger.logInfo("Uploading " + psiFiles.size() + " files... "); if (psiFiles.isEmpty()) return; - List listHash2Content = new ArrayList<>(psiFiles.size()); + + FileContentRequest files = new FileContentRequest(); for (Object psiFile : psiFiles) { pdUtils.progressCheckCanceled(progress); - listHash2Content.add( + files.put( + pdUtils.getFilePath(psiFile), new FileHash2ContentRequest( hashContentUtils.getHash(psiFile), hashContentUtils.getFileContent(psiFile))); } - if (listHash2Content.isEmpty()) return; // todo make network request in parallel with collecting data EmptyResponse uploadFilesResponse = - DeepCodeRestApi.UploadFiles(deepCodeParams.getSessionToken(), bundleId, listHash2Content); + DeepCodeRestApi.extendBundle( + deepCodeParams.getSessionToken(), + bundleId, + new ExtendBundleWithContentRequest(files, Collections.emptyList())); isNotSucceed(project, uploadFilesResponse, "Bad UploadFiles request: "); } @@ -549,15 +568,14 @@ private GetAnalysisResponse doGetAnalysis( GetAnalysisResponse response; int counter = 0; final long timeout = deepCodeParams.getTimeoutForGettingAnalysesMs(); - final long attempts = timeout / pdUtils.DEFAULT_DELAY; + final long attempts = timeout / PlatformDependentUtilsBase.DEFAULT_DELAY; do { - if (counter > 0) pdUtils.delay(pdUtils.DEFAULT_DELAY, progress); + if (counter > 0) pdUtils.delay(PlatformDependentUtilsBase.DEFAULT_DELAY, progress); response = DeepCodeRestApi.getAnalysis( deepCodeParams.getSessionToken(), bundleId, deepCodeParams.getMinSeverity(), - deepCodeParams.useLinter(), filesToAnalyse); pdUtils.progressCheckCanceled(progress); @@ -576,8 +594,10 @@ private GetAnalysisResponse doGetAnalysis( if (counter >= attempts) { dcLogger.logWarn("Timeout expire for waiting analysis results."); pdUtils.showWarn( - "Can't get analysis results from the server. Timeout of " + timeout/1000 + " sec. is reached." + - " Please, increase timeout or try again later.", + "Can't get analysis results from the server. Timeout of " + + timeout / 1000 + + " sec. is reached." + + " Please, increase timeout or try again later.", project, false); break; @@ -594,7 +614,7 @@ private GetAnalysisResponse doGetAnalysis( } counter++; - } while (!response.getStatus().equals("DONE") + } while (!response.getStatus().equalsIgnoreCase(COMPLETE) // !!!! keep commented in production, for debug only: to emulate long processing // || counter < 10 ); @@ -608,22 +628,22 @@ private Map> parseGetAnalysisResponse( GetAnalysisResponse response, @NotNull Object progress) { Map> result = new HashMap<>(); - if (!response.getStatus().equals("DONE")) return EMPTY_MAP; - AnalysisResults analysisResults = response.getAnalysisResults(); + if (!response.getStatus().equalsIgnoreCase(COMPLETE)) return EMPTY_MAP; mapProject2analysisUrl.put(project, response.getAnalysisURL()); - if (analysisResults == null) { + FilesMap filesMap = response.getFiles(); + if (filesMap == null || filesMap.isEmpty()) { dcLogger.logWarn("AnalysisResults is null for: " + response); return EMPTY_MAP; } for (Object file : files) { // fixme iterate over analysisResults.getFiles() to reduce empty passes final String deepCodedFilePath = pdUtils.getDeepCodedFilePath(file); - FileSuggestions fileSuggestions = analysisResults.getFiles().get(deepCodedFilePath); + FileSuggestions fileSuggestions = filesMap.get(deepCodedFilePath); if (fileSuggestions == null) { result.put(file, Collections.emptyList()); continue; } - final Suggestions suggestions = analysisResults.getSuggestions(); + final Suggestions suggestions = response.getSuggestions(); if (suggestions == null) { dcLogger.logWarn("Suggestions is empty for: " + response); return EMPTY_MAP; @@ -652,12 +672,15 @@ private Map> parseGetAnalysisResponse( new MyTextRange(marker.getMsg().get(0), marker.getMsg().get(1) + 1); final List positions = marker.getPos().stream() - .map(it -> { - final Object fileForMarker = (it.getFile() == null || it.getFile().isEmpty()) - ? file - : pdUtils.getFileByDeepcodedPath(it.getFile(), project); - return parsePosition2MyTextRange(it, fileForMarker, Collections.emptyMap()); - }) + .map( + it -> { + final Object fileForMarker = + (it.getFile() == null || it.getFile().isEmpty()) + ? file + : pdUtils.getFileByDeepcodedPath(it.getFile(), project); + return parsePosition2MyTextRange( + it, fileForMarker, Collections.emptyMap()); + }) .filter(Objects::nonNull) .collect(Collectors.toList()); markers.put(msgRange, positions); @@ -719,11 +742,6 @@ private MyTextRange parsePosition2MyTextRange( position.getFile()); } - private FileContent createFileContent(Object file) { - return new FileContent( - pdUtils.getDeepCodedFilePath(file), hashContentUtils.getFileContent(file)); - } - public Set getAllFilesWithSuggestions(@NotNull final Object project) { return mapFile2Suggestions.entrySet().stream() .filter(e -> pdUtils.getProject(e.getKey()).equals(project)) diff --git a/src/main/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderBase.java b/src/main/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderBase.java index a75afdc..4080c2f 100644 --- a/src/main/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderBase.java @@ -6,7 +6,10 @@ import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.PathMatcher; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.PatternSyntaxException; @@ -17,29 +20,31 @@ public abstract class DeepCodeIgnoreInfoHolderBase { private final DCLoggerBase dcLogger; // .ignore file to Line in .ignore file to PathMatcher - private final Map> map_ignore2PathMatchers = new ConcurrentHashMap<>(); + private final Map> map_ignore2PathMatchers = + new ConcurrentHashMap<>(); // .ignore file to Line in .ignore file to PathMatcher - private final Map> map_ignore2ReIncludePathMatchers = new ConcurrentHashMap<>(); + private final Map> map_ignore2ReIncludePathMatchers = + new ConcurrentHashMap<>(); - private final Map> project2IgnoredFilePaths = new ConcurrentHashMap<>(); + private final Map> project2IgnoredFilePaths = + new ConcurrentHashMap<>(); protected DeepCodeIgnoreInfoHolderBase( - @NotNull HashContentUtilsBase hashContentUtils, - @NotNull PlatformDependentUtilsBase pdUtils, - @NotNull DCLoggerBase dcLogger) { + @NotNull HashContentUtilsBase hashContentUtils, + @NotNull PlatformDependentUtilsBase pdUtils, + @NotNull DCLoggerBase dcLogger) { this.hashContentUtils = hashContentUtils; this.pdUtils = pdUtils; this.dcLogger = dcLogger; } public void scanAllMissedIgnoreFiles( - @NotNull Collection allProjectFiles, - @Nullable Object progress) { + @NotNull Collection allProjectFiles, @Nullable Object progress) { allProjectFiles.stream() - .filter(this::is_ignoreFile) - .filter(ignoreFile -> !map_ignore2PathMatchers.containsKey(ignoreFile)) - .forEach(ignoreFile -> update_ignoreFileContent(ignoreFile, progress)); + .filter(this::is_ignoreFile) + .filter(ignoreFile -> !map_ignore2PathMatchers.containsKey(ignoreFile)) + .forEach(ignoreFile -> update_ignoreFileContent(ignoreFile, progress)); } public boolean isIgnoredFile(@NotNull Object fileToCheck) { @@ -50,41 +55,43 @@ public boolean isIgnoredFile(@NotNull Object fileToCheck) { filePath -> map_ignore2PathMatchers.keySet().stream() .filter(ignoreFile -> inScope(filePath, ignoreFile)) - .anyMatch(ignoreFile -> isIgnoredFile(filePath, ignoreFile)) - ); + .anyMatch(ignoreFile -> isIgnoredFile(filePath, ignoreFile))); } private boolean isIgnoredFile(@NotNull String filePath, @NotNull Object ignoreFile) { final Path path = pathOf(filePath); return map_ignore2PathMatchers.get(ignoreFile).entrySet().stream() - .anyMatch(line2matcher -> { + .anyMatch( + line2matcher -> { final int lineIndex = line2matcher.getKey(); final PathMatcher pathMatcher = line2matcher.getValue(); - return pathMatcher.matches(path) && - // An optional prefix "!" which negates the pattern; - // any matching file excluded by a _previous_ pattern will become included again. - map_ignore2ReIncludePathMatchers.get(ignoreFile).entrySet().stream() - .filter(e -> e.getKey() > lineIndex) - .noneMatch(e -> e.getValue().matches(path)); + return pathMatcher.matches(path) + && + // An optional prefix "!" which negates the pattern; + // any matching file excluded by a _previous_ pattern will become included again. + map_ignore2ReIncludePathMatchers.get(ignoreFile).entrySet().stream() + .filter(e -> e.getKey() > lineIndex) + .noneMatch(e -> e.getValue().matches(path)); }); } private void removeIgnoredFilePaths(@NotNull Object ignoreFile) { final Object project = pdUtils.getProject(ignoreFile); project2IgnoredFilePaths - .getOrDefault(project, Collections.emptyMap()) - .keySet() - .removeIf(filePath -> inScope(filePath, ignoreFile)); + .getOrDefault(project, Collections.emptyMap()) + .keySet() + .removeIf(filePath -> inScope(filePath, ignoreFile)); } /** copy of {@link Path#of(java.lang.String, java.lang.String...)} due to java 8 compatibility */ - private static Path pathOf(String first, String... more){ + private static Path pathOf(String first, String... more) { return FileSystems.getDefault().getPath(first, more); } private boolean inScope(@NotNull String filePathToCheck, @NotNull Object ignoreFile) { return filePathToCheck.startsWith(pdUtils.getDirPath(ignoreFile)); - }; + } + ; public boolean is_ignoreFile(@NotNull Object file) { return is_dcignoreFile(file) || is_gitignoreFile(file); @@ -105,12 +112,18 @@ public void remove_ignoreFileContent(@NotNull Object ignoreFile) { } public void removeProject(@NotNull Object project) { - map_ignore2PathMatchers.keySet().forEach(file -> { - if (pdUtils.getProject(file).equals(project)) remove_ignoreFileContent(file); - }); - map_ignore2ReIncludePathMatchers.keySet().forEach(file -> { - if (pdUtils.getProject(file).equals(project)) remove_ignoreFileContent(file); - }); + map_ignore2PathMatchers + .keySet() + .forEach( + file -> { + if (pdUtils.getProject(file).equals(project)) remove_ignoreFileContent(file); + }); + map_ignore2ReIncludePathMatchers + .keySet() + .forEach( + file -> { + if (pdUtils.getProject(file).equals(project)) remove_ignoreFileContent(file); + }); project2IgnoredFilePaths.remove(project); } @@ -137,7 +150,8 @@ private void parse_ignoreFile2Globs(@NotNull Object ignoreFile, @Nullable Object // An optional prefix "!" which negates the pattern; // any matching file excluded by a previous pattern will become included again. - // todo??? It is not possible to re-include a file if a parent directory of that file is excluded. + // todo??? It is not possible to re-include a file if a parent directory of that file is + // excluded. boolean isReIncludePattern = line.startsWith("!"); if (isReIncludePattern) line = line.substring(1); @@ -160,14 +174,14 @@ private void parse_ignoreFile2Globs(@NotNull Object ignoreFile, @Nullable Object // If there is a separator at the end of the pattern then the pattern will only match // directories, otherwise the pattern can match both files and directories. String postfix = - (line.endsWith("/")) - ? "?**" // should be dir - : "{/?**,}"; // could be dir or file + (line.endsWith("/")) + ? "?**" // should be dir + : "{/?**,}"; // could be dir or file // glob sanity check for validity try { - PathMatcher globToMatch = FileSystems.getDefault() - .getPathMatcher("glob:" + prefix + line + postfix); + PathMatcher globToMatch = + FileSystems.getDefault().getPathMatcher("glob:" + prefix + line + postfix); if (isReIncludePattern) { reIncludedMatchers.put(lineIndex, globToMatch); @@ -177,7 +191,7 @@ private void parse_ignoreFile2Globs(@NotNull Object ignoreFile, @Nullable Object } catch (PatternSyntaxException e) { dcLogger.logWarn("Incorrect Glob syntax in .ignore file: " + e.getMessage()); } - pdUtils.progressSetFraction(progress, (double) lineIndex/lines.length); + pdUtils.progressSetFraction(progress, (double) lineIndex / lines.length); pdUtils.progressCheckCanceled(progress); } map_ignore2ReIncludePathMatchers.put(ignoreFile, reIncludedMatchers); diff --git a/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java b/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java index 1b7a5ef..6bc6447 100644 --- a/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java @@ -29,8 +29,7 @@ protected DeepCodeParamsBase( String sessionToken, String loginUrl, String ideProductName, - Supplier getTimeoutForGettingAnalysesMs - ) { + Supplier getTimeoutForGettingAnalysesMs) { this.isEnable = isEnable; this.apiUrl = apiUrl; this.disableSslVerification = disableSslVerification; @@ -91,12 +90,17 @@ public void setApiUrl(@NotNull String apiUrl) { } public void setApiUrl(@NotNull String apiUrl, boolean disableSslVerification) { - if (apiUrl.isEmpty()) apiUrl = "https://www.deepcode.ai/"; + setApiUrl(apiUrl, disableSslVerification, false); + } + + public void setApiUrl( + @NotNull String apiUrl, boolean disableSslVerification, boolean requestLogging) { + if (apiUrl.isEmpty()) apiUrl = "https://deeproxy.snyk.io/"; if (!apiUrl.endsWith("/")) apiUrl += "/"; if (apiUrl.equals(this.apiUrl)) return; this.apiUrl = apiUrl; this.disableSslVerification = disableSslVerification; - DeepCodeRestApi.setBaseUrl(apiUrl, disableSslVerification); + DeepCodeRestApi.setBaseUrl(apiUrl, disableSslVerification, requestLogging); } public boolean isDisableSslVerification() { diff --git a/src/main/java/ai/deepcode/javaclient/core/DeepCodeUtilsBase.java b/src/main/java/ai/deepcode/javaclient/core/DeepCodeUtilsBase.java index 32b9aed..6be128c 100644 --- a/src/main/java/ai/deepcode/javaclient/core/DeepCodeUtilsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/DeepCodeUtilsBase.java @@ -5,7 +5,13 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; public abstract class DeepCodeUtilsBase { @@ -34,9 +40,7 @@ protected DeepCodeUtilsBase( protected static Set supportedConfigFiles = Collections.emptySet(); public List getAllSupportedFilesInProject( - @NotNull Object project, - boolean scanAllMissedIgnoreFile, - @Nullable Object progress) { + @NotNull Object project, boolean scanAllMissedIgnoreFile, @Nullable Object progress) { final Collection allProjectFiles = allProjectFiles(project); if (allProjectFiles.isEmpty()) { dcLogger.logWarn("Empty files list for project: " + project); @@ -50,8 +54,9 @@ public List getAllSupportedFilesInProject( final int totalSize = allProjectFiles.size(); final List result = new ArrayList<>(); for (Object file : allProjectFiles) { - pdUtils.progressSetText(progress, "Checked if supported " + counter + " files of " + totalSize); - pdUtils.progressSetFraction(progress, ((double) counter++/ totalSize)); + pdUtils.progressSetText( + progress, "Checked if supported " + counter + " files of " + totalSize); + pdUtils.progressSetFraction(progress, ((double) counter++ / totalSize)); if (isSupportedFileFormat(file)) { result.add(file); } diff --git a/src/main/java/ai/deepcode/javaclient/core/HashContentUtilsBase.java b/src/main/java/ai/deepcode/javaclient/core/HashContentUtilsBase.java index 54e25c1..0757aef 100644 --- a/src/main/java/ai/deepcode/javaclient/core/HashContentUtilsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/HashContentUtilsBase.java @@ -12,10 +12,10 @@ public abstract class HashContentUtilsBase { private final PlatformDependentUtilsBase platformDependentUtils; - protected HashContentUtilsBase(@NotNull PlatformDependentUtilsBase platformDependentUtils){ + protected HashContentUtilsBase(@NotNull PlatformDependentUtilsBase platformDependentUtils) { this.platformDependentUtils = platformDependentUtils; - }; - + } + private static final Map mapFile2Hash = new ConcurrentHashMap<>(); private static final Map mapFile2Content = new ConcurrentHashMap<>(); @@ -48,20 +48,20 @@ public boolean isHashChanged(@NotNull Object file) { String newHash = doGetHash(doGetFileContent(file)); String oldHash = mapFile2Hash.put(file, newHash); // fixme debug only -/* - DCLogger.getInstance().info( - "Hash check (if file been changed) for " - + file.getName() - + "\noldHash = " - + oldHash - + "\nnewHash = " - + newHash); -*/ + /* + DCLogger.getInstance().info( + "Hash check (if file been changed) for " + + file.getName() + + "\noldHash = " + + oldHash + + "\nnewHash = " + + newHash); + */ return !newHash.equals(oldHash); } - String getHash(@NotNull Object file) { + public String getHash(@NotNull Object file) { return mapFile2Hash.computeIfAbsent(file, this::doGetHash); } @@ -69,7 +69,7 @@ private String doGetHash(@NotNull Object file) { return doGetHash(getFileContent(file)); } - private String doGetHash(@NotNull String fileText) { + private static String doGetHash(@NotNull String fileText) { MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("SHA-256"); @@ -80,19 +80,14 @@ private String doGetHash(@NotNull String fileText) { return bytesToHex(encodedHash); } - /** - * Look for cached content first, require manual cache invalidation if file been changed - */ + /** Look for cached content first, require manual cache invalidation if file been changed */ @NotNull public String getFileContent(@NotNull Object file) { // potential OutOfMemoryException for too large projects return mapFile2Content.computeIfAbsent(file, this::doGetFileContent); } - /** - * Make direct read of File content. NO cache check. - */ + /** Make direct read of File content. NO cache check. */ @NotNull public abstract String doGetFileContent(@NotNull Object file); - } diff --git a/src/main/java/ai/deepcode/javaclient/core/LoginUtilsBase.java b/src/main/java/ai/deepcode/javaclient/core/LoginUtilsBase.java deleted file mode 100644 index 61a8382..0000000 --- a/src/main/java/ai/deepcode/javaclient/core/LoginUtilsBase.java +++ /dev/null @@ -1,129 +0,0 @@ -package ai.deepcode.javaclient.core; - -import ai.deepcode.javaclient.DeepCodeRestApi; -import ai.deepcode.javaclient.responses.EmptyResponse; -import ai.deepcode.javaclient.responses.LoginResponse; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.HashSet; -import java.util.Set; - -public abstract class LoginUtilsBase { - - private final PlatformDependentUtilsBase pdUtils; - private final DeepCodeParamsBase deepCodeParams; - private final AnalysisDataBase analysisData; - private final DCLoggerBase dcLogger; - - protected LoginUtilsBase( - @NotNull PlatformDependentUtilsBase platformDependentUtils, - @NotNull DeepCodeParamsBase deepCodeParams, - @NotNull AnalysisDataBase analysisData, - @NotNull DCLoggerBase dcLogger) { - this.pdUtils = platformDependentUtils; - this.deepCodeParams = deepCodeParams; - this.analysisData = analysisData; - this.dcLogger = dcLogger; - } - - protected abstract String getUserAgent(); - - private static final Set PROJECTS_WITH_LOGIN_CHECK_LOOP_STARTED = new HashSet<>(); - - /** inner network request! */ - public boolean isLogged(@Nullable Object project, boolean userActionNeeded) { - boolean isLogged = checkLogin(project, userActionNeeded); - if (isLogged && project != null) { - isLogged = checkConsent(project, userActionNeeded); - } - return isLogged; - } - - /** network request! */ - public boolean checkLogin(@Nullable Object project, boolean userActionNeeded) { - final String sessionToken = deepCodeParams.getSessionToken(); - // pdUtils.progressCheckCanceled(); - final EmptyResponse response = DeepCodeRestApi.checkSession(sessionToken); - boolean isLogged = response.getStatusCode() == 200; - String message = response.getStatusDescription(); - if (isLogged) { - dcLogger.logInfo("Login check succeed." + " Token: " + sessionToken); - } else { - dcLogger.logWarn("Login check fails: " + message + " Token: " + sessionToken); - } - if (!isLogged && userActionNeeded) { - if (response.getStatusCode() != 401) { - pdUtils.showError(message, project); - } - if (sessionToken.isEmpty()) { - pdUtils.showLoginLink( - project, "Authenticate using your GitHub, Bitbucket or GitLab account"); - } else { - pdUtils.showLoginLink( - project, "Invalid Token. Correct it or create a new one, please."); - } - } - return isLogged; - } - - public boolean checkConsent(@NotNull Object project, boolean userActionNeeded) { - final boolean consentGiven = deepCodeParams.consentGiven(project); - if (consentGiven) { - dcLogger.logInfo("Consent check succeed for: " + pdUtils.getProjectName(project)); - } else { - dcLogger.logWarn("Consent check fail! Project: " + pdUtils.getProjectName(project)); - if (userActionNeeded) { - pdUtils.showConsentRequest(project, userActionNeeded); - } - } - return consentGiven; - } - - /** network request! */ - public void requestNewLogin(@Nullable Object project, boolean openBrowser) { - dcLogger.logInfo("New Login requested."); - deepCodeParams.clearLoginParams(); - LoginResponse response = DeepCodeRestApi.newLogin(getUserAgent()); - if (response.getStatusCode() == 200) { - dcLogger.logInfo("New Login request succeed. New Token: " + response.getSessionToken()); - deepCodeParams.setSessionToken(response.getSessionToken()); - deepCodeParams.setLoginUrl(response.getLoginURL()); - if (openBrowser) { - pdUtils.showInBrowser(deepCodeParams.getLoginUrl()); - // BrowserUtil.open(DeepCodeParams.getInstance().getLoginUrl()); - } - // all projects should be re-scanned - for (Object prj : pdUtils.getOpenProjects()) { - if (PROJECTS_WITH_LOGIN_CHECK_LOOP_STARTED.add(prj)) { - pdUtils.runInBackground( - prj, - "Waiting Login for " + pdUtils.getProjectName(prj), - (progress) -> startLoginCheckLoop(prj, progress)); - dcLogger.logInfo("LoginCheckLoop started for " + pdUtils.getProjectName(prj)); - } - } - } else { - dcLogger.logWarn("New Login request fail: " + response.getStatusDescription()); - pdUtils.showError(response.getStatusDescription(), project); - } - } - - private void startLoginCheckLoop(@NotNull Object project, @NotNull Object progress) { - try { - do { - pdUtils.delay(pdUtils.DEFAULT_DELAY, progress); - } while (!checkLogin(project, false)); - } finally { - PROJECTS_WITH_LOGIN_CHECK_LOOP_STARTED.remove(project); - dcLogger.logInfo("LoginCheckLoop finished for project: " + pdUtils.getProjectName(project)); - } - // pdUtils.showInfo("Login succeed", project); - analysisData.resetCachesAndTasks(project); // do we need it?? - if (checkConsent(project, true)) { - pdUtils.doFullRescan(project); - } - // AnalysisData.getInstance().resetCachesAndTasks(project); - // RunUtils.asyncAnalyseProjectAndUpdatePanel(project); - } -} diff --git a/src/main/java/ai/deepcode/javaclient/core/MyTextRange.java b/src/main/java/ai/deepcode/javaclient/core/MyTextRange.java index f36361d..b4aed26 100644 --- a/src/main/java/ai/deepcode/javaclient/core/MyTextRange.java +++ b/src/main/java/ai/deepcode/javaclient/core/MyTextRange.java @@ -16,14 +16,14 @@ public class MyTextRange { private final String file; MyTextRange( - int start, - int end, - int startRow, - int endRow, - int startCol, - int endCol, - Map> markers, - String file) { + int start, + int end, + int startRow, + int endRow, + int startCol, + int endCol, + Map> markers, + String file) { this.start = start; this.end = end; diff --git a/src/main/java/ai/deepcode/javaclient/core/PlatformDependentUtilsBase.java b/src/main/java/ai/deepcode/javaclient/core/PlatformDependentUtilsBase.java index 3f1f6fc..ccd2601 100644 --- a/src/main/java/ai/deepcode/javaclient/core/PlatformDependentUtilsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/PlatformDependentUtilsBase.java @@ -32,11 +32,15 @@ public void delay(int millis, @Nullable Object progress) { @NotNull public abstract String getFileName(@NotNull Object file); - /** @return path as String with `/` as separator (even on Windows). See {@link File#separatorChar} */ + /** + * @return path as String with `/` as separator (even on Windows). See {@link File#separatorChar} + */ @NotNull public abstract String getFilePath(@NotNull Object file); - /** @return path as String with `/` as separator (even on Windows). See {@link File#separatorChar} */ + /** + * @return path as String with `/` as separator (even on Windows). See {@link File#separatorChar} + */ @NotNull public abstract String getDirPath(@NotNull Object file); diff --git a/src/main/java/ai/deepcode/javaclient/core/RunUtilsBase.java b/src/main/java/ai/deepcode/javaclient/core/RunUtilsBase.java index 609eb1f..38fc2f9 100644 --- a/src/main/java/ai/deepcode/javaclient/core/RunUtilsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/RunUtilsBase.java @@ -3,7 +3,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -143,7 +147,7 @@ public void runInBackgroundCancellable( + runId); final Object project = pdUtils.getProject(file); - //analysisData.setUpdateInProgress(project); + // analysisData.setUpdateInProgress(project); doBackgroundRun( project, diff --git a/src/main/java/ai/deepcode/javaclient/core/SuggestionForFile.java b/src/main/java/ai/deepcode/javaclient/core/SuggestionForFile.java index 2b59788..090bd92 100644 --- a/src/main/java/ai/deepcode/javaclient/core/SuggestionForFile.java +++ b/src/main/java/ai/deepcode/javaclient/core/SuggestionForFile.java @@ -20,19 +20,19 @@ public class SuggestionForFile { private final List cwe; public SuggestionForFile( - String id, - String rule, - String message, - String title, - String text, - int severity, - int repoDatasetSize, - List exampleCommitDescriptions, - List exampleCommitFixes, - List ranges, - List categories, - List tags, - List cwe) { + String id, + String rule, + String message, + String title, + String text, + int severity, + int repoDatasetSize, + List exampleCommitDescriptions, + List exampleCommitFixes, + List ranges, + List categories, + List tags, + List cwe) { this.id = id; this.rule = rule; this.message = message; diff --git a/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleRequest.java b/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleRequest.java deleted file mode 100644 index 30036e5..0000000 --- a/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleRequest.java +++ /dev/null @@ -1,27 +0,0 @@ -package ai.deepcode.javaclient.requests; - -import java.util.List; -import java.util.Map; - -public class ExtendBundleRequest { - private Map files; - private List removedFiles; - - /** - * @param files filePath: fileHash - * @param removedFiles List of FilePaths - */ - public ExtendBundleRequest(Map files, List removedFiles) { - super(); - this.files = files; - this.removedFiles = removedFiles; - } - - public Map getFiles() { - return files; - } - - public List getRemovedFiles() { - return removedFiles; - } -} diff --git a/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithContentRequest.java b/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithContentRequest.java new file mode 100644 index 0000000..1c9d607 --- /dev/null +++ b/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithContentRequest.java @@ -0,0 +1,26 @@ +package ai.deepcode.javaclient.requests; + +import java.util.List; + +public class ExtendBundleWithContentRequest { + private final FileContentRequest files; + private final List removedFiles; + + /** + * @param files FileContentRequest + * @param removedFiles List of FilePaths + */ + public ExtendBundleWithContentRequest(FileContentRequest files, List removedFiles) { + super(); + this.files = files; + this.removedFiles = removedFiles; + } + + public FileContentRequest getFiles() { + return files; + } + + public List getRemovedFiles() { + return removedFiles; + } +} diff --git a/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithHashRequest.java b/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithHashRequest.java new file mode 100644 index 0000000..9eb5c58 --- /dev/null +++ b/src/main/java/ai/deepcode/javaclient/requests/ExtendBundleWithHashRequest.java @@ -0,0 +1,25 @@ +package ai.deepcode.javaclient.requests; + +import java.util.List; + +public class ExtendBundleWithHashRequest { + private final FileHashRequest files; + private final List removedFiles; + + /** + * @param fileHashRequest FileHashRequest i.e. filePath: fileHash + * @param removedFiles List of FilePaths + */ + public ExtendBundleWithHashRequest(FileHashRequest fileHashRequest, List removedFiles) { + this.files = fileHashRequest; + this.removedFiles = removedFiles; + } + + public FileHashRequest getFiles() { + return files; + } + + public List getRemovedFiles() { + return removedFiles; + } +} diff --git a/src/main/java/ai/deepcode/javaclient/requests/FileContent.java b/src/main/java/ai/deepcode/javaclient/requests/FileContent.java deleted file mode 100644 index daf5d55..0000000 --- a/src/main/java/ai/deepcode/javaclient/requests/FileContent.java +++ /dev/null @@ -1,20 +0,0 @@ -package ai.deepcode.javaclient.requests; - -public class FileContent { - private String filePath; - private String fileContent; - - public FileContent(String filePath, String fileContent) { - super(); - this.filePath = filePath; - this.fileContent = fileContent; - } - - public String getFilePath() { - return filePath; - } - - public String getFileContent() { - return fileContent; - } -} diff --git a/src/main/java/ai/deepcode/javaclient/requests/FileContentRequest.java b/src/main/java/ai/deepcode/javaclient/requests/FileContentRequest.java index d625144..3c4133b 100644 --- a/src/main/java/ai/deepcode/javaclient/requests/FileContentRequest.java +++ b/src/main/java/ai/deepcode/javaclient/requests/FileContentRequest.java @@ -1,16 +1,6 @@ package ai.deepcode.javaclient.requests; -import java.util.List; +import java.util.HashMap; -public class FileContentRequest { - private List files = null; - - public FileContentRequest(List files) { - super(); - this.files = files; - } - - public List getFiles() { - return files; - } -} +/** filePath : FileHash2ContentRequest */ +public class FileContentRequest extends HashMap {} diff --git a/src/main/java/ai/deepcode/javaclient/requests/FileHash2ContentRequest.java b/src/main/java/ai/deepcode/javaclient/requests/FileHash2ContentRequest.java index 3564169..e92fff4 100644 --- a/src/main/java/ai/deepcode/javaclient/requests/FileHash2ContentRequest.java +++ b/src/main/java/ai/deepcode/javaclient/requests/FileHash2ContentRequest.java @@ -1,20 +1,47 @@ package ai.deepcode.javaclient.requests; +import java.util.Objects; + public class FileHash2ContentRequest { - private String fileHash; - private String fileContent; + private String hash; + private String content; public FileHash2ContentRequest(String fileHash, String fileContent) { super(); - this.fileHash = fileHash; - this.fileContent = fileContent; + this.hash = fileHash; + this.content = fileContent; + } + + public String getHash() { + return hash; + } + + public String getContent() { + return content; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + FileHash2ContentRequest that = (FileHash2ContentRequest) o; + return hash.equals(that.hash); } - public String getFileHash() { - return fileHash; + @Override + public int hashCode() { + return Objects.hash(hash); } - public String getFileContent() { - return fileContent; + @Override + public String toString() { + return "FileHash2ContentRequest{" + + "hash='" + + hash + + '\'' + + ", content='" + + content + + '\'' + + '}'; } } diff --git a/src/main/java/ai/deepcode/javaclient/requests/FileHashRequest.java b/src/main/java/ai/deepcode/javaclient/requests/FileHashRequest.java index 1bd90a2..11b61c9 100644 --- a/src/main/java/ai/deepcode/javaclient/requests/FileHashRequest.java +++ b/src/main/java/ai/deepcode/javaclient/requests/FileHashRequest.java @@ -1,19 +1,7 @@ package ai.deepcode.javaclient.requests; +import java.util.HashMap; import java.util.Map; -public class FileHashRequest { - private Map files; - - /** - * @param files filePath: fileHash - */ - public FileHashRequest(Map files) { - super(); - this.files = files; - } - - public Map getFiles() { - return files; - } -} +/** filePath : fileHash */ +public class FileHashRequest extends HashMap implements Map {} diff --git a/src/main/java/ai/deepcode/javaclient/requests/GetAnalysisRequest.java b/src/main/java/ai/deepcode/javaclient/requests/GetAnalysisRequest.java index da40863..6e33d26 100644 --- a/src/main/java/ai/deepcode/javaclient/requests/GetAnalysisRequest.java +++ b/src/main/java/ai/deepcode/javaclient/requests/GetAnalysisRequest.java @@ -1,20 +1,83 @@ package ai.deepcode.javaclient.requests; import java.util.List; +import java.util.Objects; public class GetAnalysisRequest { - private List files; + private GetAnalysisKey key; + private Integer severity; + private boolean prioritized = false; + private boolean legacy = true; /** - * @param files List of FilePaths + * @param bundleHash + * @param limitToFiles list of filePath + * @param severity + * @param prioritized + * @param legacy */ - public GetAnalysisRequest(List files) { - super(); - this.files = files; + public GetAnalysisRequest( + String bundleHash, + List limitToFiles, + Integer severity, + boolean prioritized, + boolean legacy) { + this.key = new GetAnalysisKey(bundleHash, limitToFiles); + this.severity = severity; + this.prioritized = prioritized; + this.legacy = legacy; } - public List getFiles() { - return files; + public GetAnalysisRequest(String bundleHash, List limitToFiles, Integer severity) { + this(bundleHash, limitToFiles, severity, false, true); } + private class GetAnalysisKey { + private String type = "file"; + private String hash; + private List limitToFiles; + + public GetAnalysisKey(String hash, List limitToFiles) { + this.type = type; + this.hash = hash; + this.limitToFiles = limitToFiles; + } + + public String getHash() { + return hash; + } + + public List getLimitToFiles() { + return limitToFiles; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + 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); + } + + @Override + public String toString() { + return "GetAnalysisKey{" + + "type='" + + type + + '\'' + + ", hash='" + + hash + + '\'' + + ", limitToFiles=" + + limitToFiles + + '}'; + } + + @Override + public int hashCode() { + return Objects.hash(type, hash, limitToFiles); + } + } } diff --git a/src/main/java/ai/deepcode/javaclient/responses/AnalysisResults.java b/src/main/java/ai/deepcode/javaclient/responses/AnalysisResults.java deleted file mode 100644 index 64cd2bb..0000000 --- a/src/main/java/ai/deepcode/javaclient/responses/AnalysisResults.java +++ /dev/null @@ -1,26 +0,0 @@ -package ai.deepcode.javaclient.responses; - -public class AnalysisResults { - - private FilesMap files; - private Suggestions suggestions; - - public AnalysisResults(FilesMap files, Suggestions suggestions) { - super(); - this.files = files; - this.suggestions = suggestions; - } - - public FilesMap getFiles() { - return files; - } - - public Suggestions getSuggestions() { - return suggestions; - } - - @Override - public String toString() { - return "AnalysisResults object:\n" + files + "\n" + suggestions; - } -} \ No newline at end of file diff --git a/src/main/java/ai/deepcode/javaclient/responses/CreateBundleResponse.java b/src/main/java/ai/deepcode/javaclient/responses/CreateBundleResponse.java index abd4f98..9f89986 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/CreateBundleResponse.java +++ b/src/main/java/ai/deepcode/javaclient/responses/CreateBundleResponse.java @@ -4,32 +4,36 @@ import java.util.List; public class CreateBundleResponse extends EmptyResponse { - private final String bundleId; + private final String bundleHash; private final List missingFiles; - private final String uploadURL; public CreateBundleResponse() { super(); - this.bundleId = ""; + this.bundleHash = ""; this.missingFiles = Collections.emptyList(); - this.uploadURL = ""; } - public CreateBundleResponse(String bundleId, List missingFiles, String uploadURL) { - this.bundleId = bundleId; + public CreateBundleResponse(String bundleHash, List missingFiles, String uploadURL) { + this.bundleHash = bundleHash; this.missingFiles = missingFiles; - this.uploadURL = uploadURL; } - public String getBundleId() { - return bundleId; + public String getBundleHash() { + return bundleHash; } public List getMissingFiles() { return missingFiles; } - public String getUploadURL() { - return uploadURL; + @Override + public String toString() { + return "CreateBundleResponse{" + + "bundleHash='" + + bundleHash + + '\'' + + ", missingFiles=" + + missingFiles + + '}'; } } diff --git a/src/main/java/ai/deepcode/javaclient/responses/EmptyResponse.java b/src/main/java/ai/deepcode/javaclient/responses/EmptyResponse.java index 6b532cd..76e11ef 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/EmptyResponse.java +++ b/src/main/java/ai/deepcode/javaclient/responses/EmptyResponse.java @@ -4,8 +4,9 @@ public class EmptyResponse { private int statusCode = 0; private String statusDescription = ""; - public EmptyResponse(){ - this.statusDescription = "Error connecting to the server. Check your Settings, Network connection and/or try again later."; + public EmptyResponse() { + this.statusDescription = + "Error connecting to the server. Check your Settings, Network connection and/or try again later."; } public void setStatusCode(int statusCode) { diff --git a/src/main/java/ai/deepcode/javaclient/responses/GetAnalysisResponse.java b/src/main/java/ai/deepcode/javaclient/responses/GetAnalysisResponse.java index 60610fe..5fa65eb 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/GetAnalysisResponse.java +++ b/src/main/java/ai/deepcode/javaclient/responses/GetAnalysisResponse.java @@ -4,23 +4,29 @@ public class GetAnalysisResponse extends EmptyResponse { private final String status; private final double progress; private final String analysisURL; - private final AnalysisResults analysisResults; -// private final JsonElement analysisResults; + private final FilesMap files; + private final Suggestions suggestions; - public GetAnalysisResponse(){ + public GetAnalysisResponse() { super(); status = ""; progress = 0; analysisURL = ""; - analysisResults = null; + suggestions = null; + files = null; } public GetAnalysisResponse( - String status, double progress, String analysisURL, AnalysisResults analysisResults) { + String status, + double progress, + String analysisURL, + FilesMap analysisResults, + Suggestions suggestions) { this.status = status; this.progress = progress; this.analysisURL = analysisURL; - this.analysisResults = analysisResults; + this.files = analysisResults; + this.suggestions = suggestions; } public String getStatus() { @@ -35,8 +41,8 @@ public String getAnalysisURL() { return analysisURL; } - public AnalysisResults getAnalysisResults() { - return analysisResults; + public FilesMap getFiles() { + return files; } @Override @@ -49,6 +55,10 @@ public String toString() { + "\nanalysisURL: " + analysisURL + "\nanalysisResult: " - + analysisResults; + + suggestions; + } + + public Suggestions getSuggestions() { + return suggestions; } } diff --git a/src/main/java/ai/deepcode/javaclient/responses/GetFiltersResponse.java b/src/main/java/ai/deepcode/javaclient/responses/GetFiltersResponse.java index 1155557..2e29222 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/GetFiltersResponse.java +++ b/src/main/java/ai/deepcode/javaclient/responses/GetFiltersResponse.java @@ -7,11 +7,12 @@ public class GetFiltersResponse extends EmptyResponse { private final List extensions; private final List configFiles; - public GetFiltersResponse(){ + public GetFiltersResponse() { super(); extensions = Collections.emptyList(); configFiles = Collections.emptyList(); } + public GetFiltersResponse(List extensions, List configFiles) { this.extensions = extensions; this.configFiles = configFiles; diff --git a/src/main/java/ai/deepcode/javaclient/responses/LoginResponse.java b/src/main/java/ai/deepcode/javaclient/responses/LoginResponse.java index 93b577a..7535378 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/LoginResponse.java +++ b/src/main/java/ai/deepcode/javaclient/responses/LoginResponse.java @@ -22,4 +22,4 @@ public String getSessionToken() { public String getLoginURL() { return loginURL; } -} \ No newline at end of file +} diff --git a/src/main/java/ai/deepcode/javaclient/responses/MarkerPosition.java b/src/main/java/ai/deepcode/javaclient/responses/MarkerPosition.java index b5f96d3..b3cc995 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/MarkerPosition.java +++ b/src/main/java/ai/deepcode/javaclient/responses/MarkerPosition.java @@ -27,7 +27,7 @@ public List getCols() { @Override public String toString() { - return "marker range: rows: " + rows + " cols: " + cols+ " file: " + file; + return "marker range: rows: " + rows + " cols: " + cols + " file: " + file; } @Override diff --git a/src/main/java/ai/deepcode/javaclient/responses/Suggestion.java b/src/main/java/ai/deepcode/javaclient/responses/Suggestion.java index b8f416e..9a29ef0 100644 --- a/src/main/java/ai/deepcode/javaclient/responses/Suggestion.java +++ b/src/main/java/ai/deepcode/javaclient/responses/Suggestion.java @@ -16,6 +16,7 @@ public class Suggestion { private final List categories; private final List tags; private final List cwe; + private final String leadURL; public Suggestion( String id, @@ -29,7 +30,8 @@ public Suggestion( List exampleCommitFixes, List categories, List tags, - List cwe) { + List cwe, + String leadURL) { super(); this.id = id; this.rule = rule; @@ -43,6 +45,7 @@ public Suggestion( this.categories = categories; this.tags = tags; this.cwe = cwe; + this.leadURL = leadURL; } public String getId() { @@ -121,4 +124,8 @@ public List getTags() { public List getCwe() { return cwe; } + + public String getLeadURL() { + return leadURL; + } } diff --git a/src/test/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderTest.java b/src/test/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderTest.java index 87af552..dab7a4b 100644 --- a/src/test/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderTest.java +++ b/src/test/java/ai/deepcode/javaclient/core/DeepCodeIgnoreInfoHolderTest.java @@ -15,10 +15,12 @@ public class DeepCodeIgnoreInfoHolderTest { - private final File basicIgnoreFile = new File(getClass().getClassLoader().getResource("basic/.dcignore").getPath()); + private final File basicIgnoreFile = + new File(getClass().getClassLoader().getResource("basic/.dcignore").getPath()); private final File basicProject = basicIgnoreFile.getParentFile(); - private final File fullDcignoreFile = new File(getClass().getClassLoader().getResource("full/.dcignore").getPath()); + private final File fullDcignoreFile = + new File(getClass().getClassLoader().getResource("full/.dcignore").getPath()); private final File fullDcignoreProject = fullDcignoreFile.getParentFile(); @Test @@ -47,10 +49,11 @@ public void fullIgnoreFile() { assertTrue(fullDcignoreFile.exists()); ignoreInfoHolder.update_ignoreFileContent(fullDcignoreFile, null); - assertFalse(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject,"1.js"))); - assertTrue(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject,"scripts/1.js"))); - assertTrue(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject,"node_modules/1.js"))); - assertTrue(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject,"node_modules/1/1/1.js"))); + assertFalse(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject, "1.js"))); + assertTrue(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject, "scripts/1.js"))); + assertTrue(ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject, "node_modules/1.js"))); + assertTrue( + ignoreInfoHolder.isIgnoredFile(new File(fullDcignoreProject, "node_modules/1/1/1.js"))); // # Hidden directories // .*/ @@ -121,41 +124,40 @@ public void scanAllMissedIgnoreFiles() { assertTrue(ignoreInfoHolder.isIgnoredFile(new File(basicProject, "2.js"))); } - private PlatformDependentUtilsBase pdUtils = new PlatformDependentUtilsAbstractMock() { - @Override - public @NotNull Object getProject(@NotNull Object file) { - final String filePath = ((File)file).getPath(); - if (filePath.startsWith(basicProject.getPath())) return basicProject; - if (filePath.startsWith(fullDcignoreProject.getPath())) return fullDcignoreProject; - throw new IllegalArgumentException(file.toString()); - } - - @Override - public @NotNull String getProjectName(@NotNull Object project) { - return project.toString(); - } - - @Override - public @NotNull String getFileName(@NotNull Object file) { - return ((File) file).getName(); - } - - @Override - public @NotNull String getFilePath(@NotNull Object file) { - return ((File) file).getPath().replaceAll("\\\\", "/"); // case for Windows base path - } - - @Override - public @NotNull String getDirPath(@NotNull Object file) { - return ((File) file).getParent().replaceAll("\\\\", "/"); // case for Windows base path - } - }; + private PlatformDependentUtilsBase pdUtils = + new PlatformDependentUtilsAbstractMock() { + @Override + public @NotNull Object getProject(@NotNull Object file) { + final String filePath = ((File) file).getPath(); + if (filePath.startsWith(basicProject.getPath())) return basicProject; + if (filePath.startsWith(fullDcignoreProject.getPath())) return fullDcignoreProject; + throw new IllegalArgumentException(file.toString()); + } + + @Override + public @NotNull String getProjectName(@NotNull Object project) { + return project.toString(); + } + + @Override + public @NotNull String getFileName(@NotNull Object file) { + return ((File) file).getName(); + } + + @Override + public @NotNull String getFilePath(@NotNull Object file) { + return ((File) file).getPath().replaceAll("\\\\", "/"); // case for Windows base path + } + + @Override + public @NotNull String getDirPath(@NotNull Object file) { + return ((File) file).getParent().replaceAll("\\\\", "/"); // case for Windows base path + } + }; @NotNull private DeepCodeIgnoreInfoHolderBase getNewIgnoreInfoHolder() { return new DeepCodeIgnoreInfoHolderMock( - new HashContentUtilsMock(pdUtils), - pdUtils, - new LoggerMock()); + new HashContentUtilsMock(pdUtils), pdUtils, new LoggerMock()); } } diff --git a/src/test/java/ai/deepcode/javaclient/core/RunUtilsTest.java b/src/test/java/ai/deepcode/javaclient/core/RunUtilsTest.java index c18525d..82ce095 100644 --- a/src/test/java/ai/deepcode/javaclient/core/RunUtilsTest.java +++ b/src/test/java/ai/deepcode/javaclient/core/RunUtilsTest.java @@ -1,6 +1,13 @@ package ai.deepcode.javaclient.core; -import ai.deepcode.javaclient.core.mocks.*; +import ai.deepcode.javaclient.core.mocks.AnalysisDataBaseMock; +import ai.deepcode.javaclient.core.mocks.DeepCodeIgnoreInfoHolderMock; +import ai.deepcode.javaclient.core.mocks.DeepCodeParamsMock; +import ai.deepcode.javaclient.core.mocks.DeepCodeUtilsMock; +import ai.deepcode.javaclient.core.mocks.HashContentUtilsMock; +import ai.deepcode.javaclient.core.mocks.LoggerMock; +import ai.deepcode.javaclient.core.mocks.PlatformDependentUtilsAbstractMock; +import ai.deepcode.javaclient.core.mocks.RunUtilsBaseMock; import org.jetbrains.annotations.NotNull; import org.junit.Test; diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/AnalysisDataBaseMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/AnalysisDataBaseMock.java index df22f0b..dd0702b 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/AnalysisDataBaseMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/AnalysisDataBaseMock.java @@ -1,6 +1,10 @@ package ai.deepcode.javaclient.core.mocks; -import ai.deepcode.javaclient.core.*; +import ai.deepcode.javaclient.core.AnalysisDataBase; +import ai.deepcode.javaclient.core.DCLoggerBase; +import ai.deepcode.javaclient.core.DeepCodeParamsBase; +import ai.deepcode.javaclient.core.HashContentUtilsBase; +import ai.deepcode.javaclient.core.PlatformDependentUtilsBase; import org.jetbrains.annotations.NotNull; import java.util.Collection; diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeIgnoreInfoHolderMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeIgnoreInfoHolderMock.java index ef62792..2d54b1d 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeIgnoreInfoHolderMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeIgnoreInfoHolderMock.java @@ -1,14 +1,17 @@ package ai.deepcode.javaclient.core.mocks; -import ai.deepcode.javaclient.core.*; +import ai.deepcode.javaclient.core.DCLoggerBase; +import ai.deepcode.javaclient.core.DeepCodeIgnoreInfoHolderBase; +import ai.deepcode.javaclient.core.HashContentUtilsBase; +import ai.deepcode.javaclient.core.PlatformDependentUtilsBase; import org.jetbrains.annotations.NotNull; public class DeepCodeIgnoreInfoHolderMock extends DeepCodeIgnoreInfoHolderBase { public DeepCodeIgnoreInfoHolderMock( - @NotNull HashContentUtilsBase hashContentUtils, - @NotNull PlatformDependentUtilsBase pdUtils, - @NotNull DCLoggerBase dcLogger) { + @NotNull HashContentUtilsBase hashContentUtils, + @NotNull PlatformDependentUtilsBase pdUtils, + @NotNull DCLoggerBase dcLogger) { super(hashContentUtils, pdUtils, dcLogger); } } diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeParamsMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeParamsMock.java index 38a5b9c..e2c930b 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeParamsMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeParamsMock.java @@ -6,16 +6,7 @@ public class DeepCodeParamsMock extends DeepCodeParamsBase { public DeepCodeParamsMock() { - super( - true, - "", - false, - false, - 1, - "", - "", - "", - () -> 1000L); + super(true, "", false, false, 1, "", "", "", () -> 1000L); } @Override diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeUtilsMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeUtilsMock.java index 83b0fe4..f9a6b46 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeUtilsMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/DeepCodeUtilsMock.java @@ -1,6 +1,11 @@ package ai.deepcode.javaclient.core.mocks; -import ai.deepcode.javaclient.core.*; +import ai.deepcode.javaclient.core.AnalysisDataBase; +import ai.deepcode.javaclient.core.DCLoggerBase; +import ai.deepcode.javaclient.core.DeepCodeIgnoreInfoHolderBase; +import ai.deepcode.javaclient.core.DeepCodeParamsBase; +import ai.deepcode.javaclient.core.DeepCodeUtilsBase; +import ai.deepcode.javaclient.core.PlatformDependentUtilsBase; import org.jetbrains.annotations.NotNull; import java.util.Collection; diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/LoggerMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/LoggerMock.java index 1956cca..2d055ef 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/LoggerMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/LoggerMock.java @@ -6,12 +6,12 @@ public class LoggerMock extends DCLoggerBase { public LoggerMock() { super( - () -> System.out::println, - () -> System.out::println, - () -> true, - () -> true, - "ai.deepcode", - ""); + () -> System.out::println, + () -> System.out::println, + () -> true, + () -> true, + "ai.deepcode", + ""); } @Override diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/PlatformDependentUtilsAbstractMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/PlatformDependentUtilsAbstractMock.java index 3143624..5b19740 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/PlatformDependentUtilsAbstractMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/PlatformDependentUtilsAbstractMock.java @@ -60,27 +60,20 @@ public int getLineStartOffset(@NotNull Object file, int line) { @Override public void runInBackgroundCancellable( - @NotNull Object file, @NotNull String title, @NotNull Consumer progressConsumer) { - } + @NotNull Object file, @NotNull String title, @NotNull Consumer progressConsumer) {} @Override public void runInBackground( - @NotNull Object project, - @NotNull String title, - @NotNull Consumer progressConsumer) { - } + @NotNull Object project, @NotNull String title, @NotNull Consumer progressConsumer) {} @Override - public void cancelRunningIndicators(@NotNull Object project) { - } + public void cancelRunningIndicators(@NotNull Object project) {} @Override - public void doFullRescan(@NotNull Object project) { - } + public void doFullRescan(@NotNull Object project) {} @Override - public void refreshPanel(@NotNull Object project) { - } + public void refreshPanel(@NotNull Object project) {} @Override public boolean isLogged(@Nullable Object project, boolean userActionNeeded) { @@ -88,12 +81,10 @@ public boolean isLogged(@Nullable Object project, boolean userActionNeeded) { } @Override - public void progressSetText(@Nullable Object progress, String text) { - } + public void progressSetText(@Nullable Object progress, String text) {} @Override - public void progressCheckCanceled(@Nullable Object progress) { - } + public void progressCheckCanceled(@Nullable Object progress) {} @Override public boolean progressCanceled(@Nullable Object progress) { @@ -101,30 +92,23 @@ public boolean progressCanceled(@Nullable Object progress) { } @Override - public void progressSetFraction(@Nullable Object progress, double fraction) { - } + public void progressSetFraction(@Nullable Object progress, double fraction) {} @Override - public void showInBrowser(@NotNull String url) { - } + public void showInBrowser(@NotNull String url) {} @Override - public void showLoginLink(@Nullable Object project, String message) { - } + public void showLoginLink(@Nullable Object project, String message) {} @Override - public void showConsentRequest(Object project, boolean userActionNeeded) { - } + public void showConsentRequest(Object project, boolean userActionNeeded) {} @Override - public void showInfo(String message, @Nullable Object project) { - } + public void showInfo(String message, @Nullable Object project) {} @Override - public void showWarn(String message, @Nullable Object project, boolean wasWarnShown) { - } + public void showWarn(String message, @Nullable Object project, boolean wasWarnShown) {} @Override - public void showError(String message, @Nullable Object project) { - } + public void showError(String message, @Nullable Object project) {} } diff --git a/src/test/java/ai/deepcode/javaclient/core/mocks/RunUtilsBaseMock.java b/src/test/java/ai/deepcode/javaclient/core/mocks/RunUtilsBaseMock.java index 3436ca2..676b5cf 100644 --- a/src/test/java/ai/deepcode/javaclient/core/mocks/RunUtilsBaseMock.java +++ b/src/test/java/ai/deepcode/javaclient/core/mocks/RunUtilsBaseMock.java @@ -1,6 +1,11 @@ package ai.deepcode.javaclient.core.mocks; -import ai.deepcode.javaclient.core.*; +import ai.deepcode.javaclient.core.AnalysisDataBase; +import ai.deepcode.javaclient.core.DCLoggerBase; +import ai.deepcode.javaclient.core.DeepCodeUtilsBase; +import ai.deepcode.javaclient.core.HashContentUtilsBase; +import ai.deepcode.javaclient.core.PlatformDependentUtilsBase; +import ai.deepcode.javaclient.core.RunUtilsBase; import org.jetbrains.annotations.NotNull; import java.util.Collection;