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..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,37 +118,39 @@ public void actionPerformed(@NotNull AnActionEvent e) { * * @return True if matches. False otherwise */ - private boolean astProjectMatchesWorkspaceProject() { - List results = cxToolWindowPanel.getCurrentState().getResultOutput().getResults(); - List resultsFileNames = new ArrayList<>(); - - if(results.isEmpty()) { - return true; - } + 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) && + workspaceProjectName != null && + pluginProjectName.equals(workspaceProjectName); + } - 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()); - } + /** + * 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 null; } - 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; + 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]; + return url.replaceFirst(".*://[a-zA-Z0-9.]+/", "").replaceFirst("\\.git$", ""); } } - - 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