Skip to content

Commit 37de8c3

Browse files
authored
Honor new combined Sonar flag (#489)
pixee/codemodder-specs#43
1 parent 8c50fe9 commit 37de8c3

File tree

22 files changed

+89
-126
lines changed

22 files changed

+89
-126
lines changed

core-codemods/src/test/java/io/codemodder/codemods/AddMissingI18nCodemodTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ private CodemodLoader createLoader(final Class<? extends CodeChanger> codemodTyp
254254
Files.list(dir).toList(),
255255
Map.of(),
256256
List.of(),
257-
List.of(),
258257
null,
259258
null,
260259
null);

core-codemods/src/test/java/io/codemodder/codemods/JSPScriptletXSSCodemodTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ void it_fixes_jsp(
5050
List.of(jsp),
5151
Map.of(),
5252
List.of(),
53-
List.of(),
5453
null,
5554
null,
5655
null);

core-codemods/src/test/java/io/codemodder/codemods/VerbTamperingCodemodTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ void it_removes_verb_tampering(
6060
List.of(webxml),
6161
Map.of(),
6262
List.of(),
63-
List.of(),
6463
null,
6564
null,
6665
null);

core-codemods/src/test/java/io/codemodder/codemods/sonar/AddMissingOverrideCodemodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
testResourceDir = "add-missing-override-s1161",
99
renameTestFile = "src/main/java/SqlInjectionLesson10b.java",
1010
dependencies = {},
11-
sonarIssuesJsonFiles = {"sonar-issues_1.json", "sonar-issues_2.json"})
11+
sonarJsonFiles = {"sonar-issues_1.json", "sonar-issues_2.json"})
1212
final class AddMissingOverrideCodemodTest implements CodemodTestMixin {}

core-codemods/src/test/resources/avoid-implicit-public-constructor-s1118/sonar-issues.json renamed to core-codemods/src/test/resources/avoid-implicit-public-constructor-s1118/sonar.json

File renamed without changes.

framework/codemodder-base/src/main/java/io/codemodder/CLI.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,18 @@ final class CLI implements Callable<Integer> {
116116
private String projectName;
117117

118118
@CommandLine.Option(
119-
names = {"--sonar-issues-json"},
119+
names = {"--sonar-json"},
120120
description =
121-
"comma-separated set of path(s) to file(s) containing the result of a call to the Sonar Web API Issues endpoint",
121+
"comma-separated set of path(s) to file(s) containing the result of a call to the Sonar Web API Issues or Hotspots endpoint (or both such files merged together)",
122122
split = ",")
123-
private List<String> sonarIssuesJsonFilePaths;
123+
private List<String> sonarJsons;
124124

125125
@CommandLine.Option(
126126
names = {"--defectdojo-findings-json"},
127127
description =
128128
"a path to a file containing the result of a call to the DefectDojo v2 Findings API endpoint")
129129
private Path defectDojoFindingsJsonFilePath;
130130

131-
@CommandLine.Option(
132-
names = {"--sonar-hotspots-json"},
133-
description =
134-
"comma-separated set of path(s) to file(s) containing the result of a call to the Sonar Web API Hotspots endpoint",
135-
split = ",")
136-
private List<String> sonarHotspotsJsonFilePaths;
137-
138131
@CommandLine.Option(
139132
names = {"--contrast-vulnerabilities-xml"},
140133
description =
@@ -394,8 +387,7 @@ public Integer call() throws IOException {
394387
log.debug("Loading input files");
395388
CodeDirectory codeDirectory = new DefaultCodeDirectory(projectPath);
396389
List<Path> sarifFiles = convertToPaths(sarifs);
397-
List<Path> sonarIssuesJsonFiles = convertToPaths(sonarIssuesJsonFilePaths);
398-
List<Path> sonarHotspotJsonFiles = convertToPaths(sonarHotspotsJsonFilePaths);
390+
List<Path> sonarJsonFiles = convertToPaths(sonarJsons);
399391

400392
log.debug("Parsing SARIFs");
401393
Map<String, List<RuleSarif>> pathSarifMap =
@@ -412,8 +404,7 @@ public Integer call() throws IOException {
412404
filePaths,
413405
pathSarifMap,
414406
codemodParameters,
415-
sonarIssuesJsonFiles,
416-
sonarHotspotJsonFiles,
407+
sonarJsonFiles,
417408
defectDojoFindingsJsonFilePath,
418409
contrastVulnerabilitiesXmlFilePath);
419410
List<CodemodIdPair> codemods = loader.getCodemods();

framework/codemodder-base/src/main/java/io/codemodder/CodemodLoader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public CodemodLoader(
3030
final List<Path> includedFiles,
3131
final Map<String, List<RuleSarif>> ruleSarifByTool,
3232
final List<ParameterArgument> codemodParameters,
33-
final List<Path> sonarIssuesJsonFiles,
34-
final List<Path> sonarHotspotsJsonFiles,
33+
final List<Path> sonarJsonFiles,
3534
final Path defectDojoFindingsJsonFile,
3635
final Path contrastVulnerabilitiesXmlFilePath) {
3736

@@ -119,8 +118,7 @@ public CodemodLoader(
119118
pathExcludes,
120119
orderedCodemodTypes,
121120
allWantedSarifs,
122-
sonarIssuesJsonFiles,
123-
sonarHotspotsJsonFiles,
121+
sonarJsonFiles,
124122
defectDojoFindingsJsonFile,
125123
contrastVulnerabilitiesXmlFilePath);
126124
allModules.addAll(modules);

framework/codemodder-base/src/main/java/io/codemodder/CodemodProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public interface CodemodProvider {
2222
* their own analysis)
2323
* @param codemodTypes the codemod types that are being run
2424
* @param sarifs the SARIF output of tools that are being run
25-
* @param sonarIssuesJsonPaths the path to a Sonar issues JSON file retrieved from their web API
26-
* -- may be null
25+
* @param sonarJsonPaths the path to a Sonar issues/hotspots or combined JSON file retrieved from
26+
* their web API -- may be null
2727
* @param contrastFindingsJsonPath the path to a Contrast findings JSON file retrieved from their
2828
* web API -- may be null
2929
* @return a set of modules that perform dependency injection
@@ -35,15 +35,14 @@ Set<AbstractModule> getModules(
3535
List<String> pathExcludes,
3636
List<Class<? extends CodeChanger>> codemodTypes,
3737
List<RuleSarif> sarifs,
38-
List<Path> sonarIssuesJsonPaths,
39-
List<Path> sonarHotspotsJsonPaths,
38+
List<Path> sonarJsonPaths,
4039
Path defectDojoFindingsJsonPath,
4140
Path contrastFindingsJsonPath);
4241

4342
/**
4443
* Tools this provider is interested in processing the SARIF output of. Codemodder CLI will look
4544
* for the SARIF outputted by tools in this list in the repository root and then provide the
46-
* results to {@link #getModules(Path, List, List, List, List, List, List, List, Path, Path)} as a
45+
* results to {@link #getModules(Path, List, List, List, List, List, List, Path, Path)} as a
4746
* {@link List} of {@link RuleSarif}s.
4847
*
4948
* <p>By default, this returns an empty list.

framework/codemodder-base/src/test/java/io/codemodder/CodemodLoaderTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ void it_handles_codemod_orders(final @TempDir Path tmpDir) throws IOException {
257257
Files.list(tmpDir).toList(),
258258
Map.of(),
259259
List.of(),
260-
List.of(),
261260
null,
262261
null,
263262
null);
@@ -282,7 +281,6 @@ void it_handles_codemod_orders(final @TempDir Path tmpDir) throws IOException {
282281
Files.list(tmpDir).toList(),
283282
Map.of(),
284283
List.of(),
285-
List.of(),
286284
null,
287285
null,
288286
null);
@@ -510,7 +508,6 @@ private CodemodLoader createLoader(final Class<? extends CodeChanger> codemodTyp
510508
Files.list(dir).toList(),
511509
Map.of(),
512510
List.of(),
513-
List.of(),
514511
null,
515512
null,
516513
null);
@@ -527,7 +524,6 @@ private CodemodLoader createLoader(
527524
Files.list(dir).toList(),
528525
Map.of(),
529526
List.of(),
530-
List.of(),
531527
null,
532528
null,
533529
null);
@@ -549,7 +545,6 @@ private CodemodLoader createLoader(
549545
params,
550546
null,
551547
null,
552-
null,
553548
null);
554549
}
555550
}

framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ default Stream<DynamicTest> generateTestCases(@TempDir final Path tmpDir) throws
8383
metadata.doRetransformTest(),
8484
metadata.expectingFixesAtLines(),
8585
metadata.expectingFailedFixesAtLines(),
86-
metadata.sonarIssuesJsonFiles(),
87-
metadata.sonarHotspotsJsonFiles());
86+
metadata.sonarJsonFiles());
8887
};
8988

9089
final Predicate<String> displayNameFilter =
@@ -104,8 +103,7 @@ private void verifyCodemod(
104103
final boolean doRetransformTest,
105104
final int[] expectedFixLines,
106105
final int[] expectingFailedFixesAtLines,
107-
final String[] sonarIssuesJsonFiles,
108-
final String[] sonarHotspotsJsonFiles)
106+
final String[] sonarJsonFiles)
109107
throws IOException {
110108

111109
// create a copy of the test file in the temp directory to serve as our "repository"
@@ -125,10 +123,11 @@ private void verifyCodemod(
125123
pathToJavaFile = newPathToJavaFile;
126124
}
127125

128-
final List<Path> sonarIssuesJsonsPaths =
129-
buildSonarJsonPaths(testResourceDir, sonarIssuesJsonFiles, "sonar-issues.json");
130-
final List<Path> sonarHotspotsJsonPaths =
131-
buildSonarJsonPaths(testResourceDir, sonarHotspotsJsonFiles, "sonar-hotspots.json");
126+
final List<Path> sonarJsonsPaths =
127+
buildSonarJsonPaths(
128+
testResourceDir,
129+
sonarJsonFiles,
130+
List.of("sonar.json", "sonar-issues.json", "sonar-hotspots.json"));
132131

133132
// Check for any sarif files and build the RuleSarif map
134133
CodeDirectory codeDir = CodeDirectory.from(tmpDir);
@@ -155,8 +154,7 @@ private void verifyCodemod(
155154
List.of(pathToJavaFile),
156155
map,
157156
List.of(),
158-
sonarIssuesJsonsPaths,
159-
sonarHotspotsJsonPaths,
157+
sonarJsonsPaths,
160158
Files.exists(defectDojo) ? defectDojo : null,
161159
Files.exists(contrastXml) ? contrastXml : null);
162160

@@ -242,8 +240,7 @@ private void verifyCodemod(
242240
List.of(pathToJavaFile),
243241
map,
244242
List.of(),
245-
null,
246-
null,
243+
List.of(),
247244
null,
248245
null);
249246
CodemodIdPair codemod2 = loader2.getCodemods().get(0);
@@ -275,7 +272,7 @@ private void verifyCodemod(
275272
private List<Path> buildSonarJsonPaths(
276273
final Path testResourceDir,
277274
final String[] sonarJsonFiles,
278-
final String defaultSonarFilename) {
275+
final List<String> defaultSonarFilenames) {
279276
final List<String> sonarJsons =
280277
sonarJsonFiles != null ? Arrays.asList(sonarJsonFiles) : new ArrayList<>();
281278

@@ -286,9 +283,11 @@ private List<Path> buildSonarJsonPaths(
286283
.collect(Collectors.toList());
287284

288285
if (sonarIssuesJsonsPaths.isEmpty()) {
289-
Path defaultPath = testResourceDir.resolve(defaultSonarFilename);
290-
if (Files.exists(defaultPath)) {
291-
sonarIssuesJsonsPaths.add(defaultPath);
286+
for (String defaultSonarFilename : defaultSonarFilenames) {
287+
Path defaultPath = testResourceDir.resolve(defaultSonarFilename);
288+
if (Files.exists(defaultPath)) {
289+
sonarIssuesJsonsPaths.add(defaultPath);
290+
}
292291
}
293292
}
294293

0 commit comments

Comments
 (0)