Skip to content

Commit

Permalink
SLI-388 Protect against duplicate file submission in case of problema…
Browse files Browse the repository at this point in the history
…tic module setup
  • Loading branch information
jblievremont committed Sep 8, 2020
1 parent 4a8ea33 commit d04ac70
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Expand Up @@ -30,9 +30,9 @@
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.Icon;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -76,15 +76,15 @@ protected boolean isVisible(String place) {
}

private static Collection<VirtualFile> getAllFiles(Project project) {
List<VirtualFile> fileList = new ArrayList<>();
Set<VirtualFile> fileSet = new LinkedHashSet<>();
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
fileIndex.iterateContent(vFile -> {
if (!vFile.isDirectory() && !ProjectCoreUtil.isProjectOrWorkspaceFile(vFile, vFile.getFileType())) {
fileList.add(vFile);
fileSet.add(vFile);
}
return true;
});
return fileList;
return fileSet;
}

static boolean showWarning() {
Expand Down
Expand Up @@ -29,9 +29,9 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import icons.SonarLintIcons;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.Icon;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void actionPerformed(AnActionEvent e) {
return;
}

List<VirtualFile> fileList = Arrays.stream(files)
Set<VirtualFile> fileSet = Arrays.stream(files)
.flatMap(f -> {
if (f.isDirectory()) {
CollectFilesVisitor visitor = new CollectFilesVisitor();
Expand All @@ -110,19 +110,18 @@ public void actionPerformed(AnActionEvent e) {
return Stream.of(f);
}
})
.distinct()
.collect(Collectors.toList());
.collect(Collectors.toSet());

SonarLintSubmitter submitter = SonarLintUtils.getService(project, SonarLintSubmitter.class);
AnalysisCallback callback;

if (SonarLintToolWindowFactory.TOOL_WINDOW_ID.equals(e.getPlace())) {
callback = new ShowCurrentFileCallable(project);
} else {
callback = new ShowAnalysisResultsCallable(project, fileList, whatAnalyzed(fileList.size()));
callback = new ShowAnalysisResultsCallable(project, fileSet, whatAnalyzed(fileSet.size()));
}

submitter.submitFiles(fileList, TriggerType.ACTION, callback, executeBackground(e));
submitter.submitFiles(fileSet, TriggerType.ACTION, callback, executeBackground(e));
}

private static String whatAnalyzed(int numFiles) {
Expand All @@ -134,7 +133,7 @@ private static String whatAnalyzed(int numFiles) {
}

private static class CollectFilesVisitor extends VirtualFileVisitor {
private List<VirtualFile> files = new ArrayList<>();
private final Set<VirtualFile> files = new LinkedHashSet<>();

public CollectFilesVisitor() {
super(VirtualFileVisitor.NO_FOLLOW_SYMLINKS);
Expand Down
Expand Up @@ -78,7 +78,7 @@ public void testRun() {
Messages.setTestDialog(x -> Messages.OK);
action.actionPerformed(event);

verify(submitter).submitFiles(eq(Collections.singletonList(file)), eq(TriggerType.ALL), any(AnalysisCallback.class), eq(false));
verify(submitter).submitFiles(eq(Collections.singleton(file)), eq(TriggerType.ALL), any(AnalysisCallback.class), eq(false));
}

}

0 comments on commit d04ac70

Please sign in to comment.