Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a warning for the user when the SCM project doesn't match (AST-78507) #297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) &&
elchananarb marked this conversation as resolved.
Show resolved Hide resolved
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() {
elchananarb marked this conversation as resolved.
Show resolved Hide resolved
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?
elchananarb marked this conversation as resolved.
Show resolved Hide resolved
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
Loading