Skip to content

Commit 896b74f

Browse files
Merge pull request #297 from Checkmarx/feature/elchanan/adding_warning_project_doesnt_match
Adding a warning for the user when the SCM project doesn't match (AST-78507)
2 parents 16cce36 + 4c1415b commit 896b74f

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/main/java/com/checkmarx/intellij/tool/window/actions/StartScanAction.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static Boolean getUserHasPermissionsToScan() {
8585
@Override
8686
public void actionPerformed(@NotNull AnActionEvent e) {
8787
Repository repository = Utils.getRootRepository(workspaceProject);
88-
boolean matchProject = astProjectMatchesWorkspaceProject();
88+
boolean matchProject = isAstProjectMatchesWorkspaceProject();
8989
// Case it is a git repo check for project and branch match
9090
if (repository != null) {
9191
String storedBranch = Optional.ofNullable(propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY)).orElse(StringUtils.EMPTY);
@@ -118,37 +118,39 @@ public void actionPerformed(@NotNull AnActionEvent e) {
118118
*
119119
* @return True if matches. False otherwise
120120
*/
121-
private boolean astProjectMatchesWorkspaceProject() {
122-
List<Result> results = cxToolWindowPanel.getCurrentState().getResultOutput().getResults();
123-
List<String> resultsFileNames = new ArrayList<>();
124-
125-
if(results.isEmpty()) {
126-
return true;
127-
}
121+
private boolean isAstProjectMatchesWorkspaceProject() {
122+
// Get the selected project from propertiesComponent
123+
String pluginProjectName = propertiesComponent.getValue("Checkmarx.SelectedProject");
124+
String workspaceProjectName = getRepositoryProjectName();
125+
126+
// Return true if the selected project matches the expected project name
127+
return StringUtils.isNotBlank(pluginProjectName) &&
128+
workspaceProjectName != null &&
129+
pluginProjectName.equals(workspaceProjectName);
130+
}
128131

129-
for(Result result : results) {
130-
if(!Optional.ofNullable(result.getData().getNodes()).orElse(Collections.emptyList()).isEmpty()){
131-
// Add SAST file name
132-
resultsFileNames.add(result.getData().getNodes().get(0).getFileName());
133-
} else if(StringUtils.isNotBlank(result.getData().getFileName())) {
134-
// Add KICS file name
135-
resultsFileNames.add(result.getData().getFileName());
136-
}
132+
/**
133+
* Helper method to retrieve the repository project name
134+
*
135+
* @return The repository project name or null if unavailable
136+
*/
137+
private String getRepositoryProjectName() {
138+
Repository repository = Utils.getRootRepository(workspaceProject);
139+
if (repository == null) {
140+
return null;
137141
}
138142

139-
for(String fileName : resultsFileNames) {
140-
List<VirtualFile> files = FilenameIndex.getVirtualFilesByName(workspaceProject, FilenameUtils.getName(fileName),
141-
GlobalSearchScope.projectScope(workspaceProject))
142-
.stream()
143-
.filter(f -> f.getPath().contains(fileName))
144-
.collect(Collectors.toList());
145-
146-
if(!files.isEmpty()) {
147-
return true;
143+
String repositoryInfo = repository.toLogString();
144+
int myUrlsIndex = repositoryInfo.indexOf("myUrls=[");
145+
if (myUrlsIndex != -1) {
146+
int start = myUrlsIndex + "myUrls=[".length();
147+
int end = repositoryInfo.indexOf("]", start);
148+
if (end != -1) {
149+
String url = repositoryInfo.substring(start, end).split(",")[0];
150+
return url.replaceFirst(".*://[a-zA-Z0-9.]+/", "").replaceFirst("\\.git$", "");
148151
}
149152
}
150-
151-
return false;
153+
return null;
152154
}
153155

154156
/**

src/main/resources/messages/CxBundle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ SCAN_FINISHED=Checkmarx scan completed successfully
8080
SCAN_FINISHED_LOAD_RESULTS=Would you like to load the results?
8181
LOAD_RESULTS=Loading results for scan id {0}...
8282
PROJECT_DOES_NOT_MATCH_TITLE=Wrong project
83-
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?
83+
PROJECT_DOES_NOT_MATCH_QUESTION=Git project doesn't match the selected Checkmarx project. Do you want to scan anyway?
8484
BRANCH_DOES_NOT_MATCH_TITLE=Wrong branch
8585
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?
8686
ACTION_SCAN_ANYWAY=Run scan

0 commit comments

Comments
 (0)