Skip to content

Commit

Permalink
Merge pull request #297 from Checkmarx/feature/elchanan/adding_warnin…
Browse files Browse the repository at this point in the history
…g_project_doesnt_match

Adding a warning for the user when the SCM project doesn't match (AST-78507)
  • Loading branch information
elchananarb authored Jan 2, 2025
2 parents 16cce36 + 4c1415b commit 896b74f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -118,37 +118,39 @@ public void actionPerformed(@NotNull AnActionEvent e) {
*
* @return True if matches. False otherwise
*/
private boolean astProjectMatchesWorkspaceProject() {
List<Result> results = cxToolWindowPanel.getCurrentState().getResultOutput().getResults();
List<String> 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<VirtualFile> 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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/CxBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 896b74f

Please sign in to comment.