From 657ba08c858035f1e276f107bf682d3cf4d37c31 Mon Sep 17 00:00:00 2001 From: elchananarb Date: Thu, 26 Dec 2024 13:54:38 +0200 Subject: [PATCH 1/3] Adding a warning for the user when the SCM project doesn't match the CX project. --- .../tool/window/actions/StartScanAction.java | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java index 63282eba..3194ef60 100644 --- a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java +++ b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java @@ -113,41 +113,40 @@ public void actionPerformed(@NotNull AnActionEvent e) { } } + /** * Check if project in workspace matches the selected checkmarx plugin project * * @return True if matches. False otherwise */ private boolean astProjectMatchesWorkspaceProject() { - List results = cxToolWindowPanel.getCurrentState().getResultOutput().getResults(); - List resultsFileNames = new ArrayList<>(); + // Get the selected project from propertiesComponent + String pluginProjectName = propertiesComponent.getValue("Checkmarx.SelectedProject"); - if(results.isEmpty()) { - return true; + // Retrieve the repository object + Repository repository = Utils.getRootRepository(workspaceProject); + if (repository == null) { + return false; } - - for(Result result : results) { - if(!Optional.ofNullable(result.getData().getNodes()).orElse(Collections.emptyList()).isEmpty()){ - // Add SAST file name - resultsFileNames.add(result.getData().getNodes().get(0).getFileName()); - } else if(StringUtils.isNotBlank(result.getData().getFileName())) { - // Add KICS file name - resultsFileNames.add(result.getData().getFileName()); + // Extract the repository information (myUrls) from repository.toLogString() + String repositoryInfo = repository.toLogString(); + String workspaceProjectName = null; + + // Parse the repository information to find the project URL (myUrls) + int myUrlsIndex = repositoryInfo.indexOf("myUrls=["); + if (myUrlsIndex != -1) { + int start = myUrlsIndex + "myUrls=[".length(); + int end = repositoryInfo.indexOf("]", start); + if (end != -1) { + String url = repositoryInfo.substring(start, end).split(",")[0]; + workspaceProjectName = url.replaceFirst(".*://[a-zA-Z0-9.]+/", "").replaceFirst("\\.git$", ""); } } - - for(String fileName : resultsFileNames) { - List files = FilenameIndex.getVirtualFilesByName(workspaceProject, FilenameUtils.getName(fileName), - GlobalSearchScope.projectScope(workspaceProject)) - .stream() - .filter(f -> f.getPath().contains(fileName)) - .collect(Collectors.toList()); - - if(!files.isEmpty()) { - return true; - } + // Return true if the selected project matches the expected project name + if (StringUtils.isNotBlank(pluginProjectName) && pluginProjectName.equalsIgnoreCase(workspaceProjectName)) { + return true; } - + // If no match, return false return false; } From 8e19ab3cd3752803116bf0e62bec86d644e994f0 Mon Sep 17 00:00:00 2001 From: elchananarb Date: Mon, 30 Dec 2024 13:25:25 +0200 Subject: [PATCH 2/3] update msg --- .../tool/window/actions/StartScanAction.java | 31 ++++++++++--------- .../resources/messages/CxBundle.properties | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java index 3194ef60..8d834b21 100644 --- a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java +++ b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java @@ -113,7 +113,6 @@ public void actionPerformed(@NotNull AnActionEvent e) { } } - /** * Check if project in workspace matches the selected checkmarx plugin project * @@ -122,32 +121,36 @@ public void actionPerformed(@NotNull AnActionEvent e) { private boolean astProjectMatchesWorkspaceProject() { // Get the selected project from propertiesComponent String pluginProjectName = propertiesComponent.getValue("Checkmarx.SelectedProject"); + String workspaceProjectName = getRepositoryProjectName(); + + // Return true if the selected project matches the expected project name + return StringUtils.isNotBlank(pluginProjectName) && + StringUtils.isNotBlank(workspaceProjectName) && + pluginProjectName.equals(workspaceProjectName); + } - // Retrieve the repository object + /** + * Helper method to retrieve the repository project name + * + * @return The repository project name or null if unavailable + */ + private String getRepositoryProjectName() { Repository repository = Utils.getRootRepository(workspaceProject); if (repository == null) { - return false; + return null; } - // Extract the repository information (myUrls) from repository.toLogString() - String repositoryInfo = repository.toLogString(); - String workspaceProjectName = null; - // Parse the repository information to find the project URL (myUrls) + String repositoryInfo = repository.toLogString(); int myUrlsIndex = repositoryInfo.indexOf("myUrls=["); if (myUrlsIndex != -1) { int start = myUrlsIndex + "myUrls=[".length(); int end = repositoryInfo.indexOf("]", start); if (end != -1) { String url = repositoryInfo.substring(start, end).split(",")[0]; - workspaceProjectName = url.replaceFirst(".*://[a-zA-Z0-9.]+/", "").replaceFirst("\\.git$", ""); + return url.replaceFirst(".*://[a-zA-Z0-9.]+/", "").replaceFirst("\\.git$", ""); } } - // Return true if the selected project matches the expected project name - if (StringUtils.isNotBlank(pluginProjectName) && pluginProjectName.equalsIgnoreCase(workspaceProjectName)) { - return true; - } - // If no match, return false - return false; + return null; } /** diff --git a/src/main/resources/messages/CxBundle.properties b/src/main/resources/messages/CxBundle.properties index 7f1c10e2..9687053a 100644 --- a/src/main/resources/messages/CxBundle.properties +++ b/src/main/resources/messages/CxBundle.properties @@ -80,7 +80,7 @@ SCAN_FINISHED=Checkmarx scan completed successfully SCAN_FINISHED_LOAD_RESULTS=Would you like to load the results? LOAD_RESULTS=Loading results for scan id {0}... PROJECT_DOES_NOT_MATCH_TITLE=Wrong project -PROJECT_DOES_NOT_MATCH_QUESTION=The files open in your workspace don't match the files previously scanned in this Checkmarx project. Do you want to scan anyway? +PROJECT_DOES_NOT_MATCH_QUESTION=Git project doesn't match the selected Checkmarx project. Do you want to scan anyway? BRANCH_DOES_NOT_MATCH_TITLE=Wrong branch BRANCH_DOES_NOT_MATCH_QUESTION=The Git branch open in your workspace isn't the same as the branch that was previously scanned in this Checkmarx project. Do you want to scan anyway? ACTION_SCAN_ANYWAY=Run scan From 8be6d9acf4f6d651f4043b662109890038e765db Mon Sep 17 00:00:00 2001 From: elchananarb Date: Thu, 2 Jan 2025 09:40:08 +0200 Subject: [PATCH 3/3] fix comments --- .../intellij/tool/window/actions/StartScanAction.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java index 8d834b21..15931c38 100644 --- a/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java +++ b/src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java @@ -85,7 +85,7 @@ public static Boolean getUserHasPermissionsToScan() { @Override public void actionPerformed(@NotNull AnActionEvent e) { Repository repository = Utils.getRootRepository(workspaceProject); - boolean matchProject = astProjectMatchesWorkspaceProject(); + boolean matchProject = isAstProjectMatchesWorkspaceProject(); // Case it is a git repo check for project and branch match if (repository != null) { String storedBranch = Optional.ofNullable(propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY)).orElse(StringUtils.EMPTY); @@ -118,14 +118,14 @@ public void actionPerformed(@NotNull AnActionEvent e) { * * @return True if matches. False otherwise */ - private boolean astProjectMatchesWorkspaceProject() { + private boolean isAstProjectMatchesWorkspaceProject() { // Get the selected project from propertiesComponent String pluginProjectName = propertiesComponent.getValue("Checkmarx.SelectedProject"); String workspaceProjectName = getRepositoryProjectName(); // Return true if the selected project matches the expected project name return StringUtils.isNotBlank(pluginProjectName) && - StringUtils.isNotBlank(workspaceProjectName) && + workspaceProjectName != null && pluginProjectName.equals(workspaceProjectName); }