Skip to content

Commit 32727dd

Browse files
authored
Update SonarModule to receive a list of sonar hotspot files (#375)
Refactor for future configuration of the sonar hotspots
1 parent 8025db2 commit 32727dd

File tree

19 files changed

+169
-97
lines changed

19 files changed

+169
-97
lines changed

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

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void it_fixes_jsp(
5050
List.of(jsp),
5151
Map.of(),
5252
List.of(),
53+
List.of(),
5354
null,
5455
null,
5556
null);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void it_removes_verb_tampering(
6060
List.of(webxml),
6161
Map.of(),
6262
List.of(),
63+
List.of(),
6364
null,
6465
null,
6566
null);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ final class CLI implements Callable<Integer> {
133133
description =
134134
"comma-separated set of path(s) to file(s) containing the result of a call to the Sonar Web API Hotspots endpoint",
135135
split = ",")
136-
private List<Path> sonarHotspotsJsonFilePaths;
136+
private List<String> sonarHotspotsJsonFilePaths;
137137

138138
@CommandLine.Option(
139139
names = {"--contrast-vulnerabilities-xml"},
@@ -382,6 +382,7 @@ public Integer call() throws IOException {
382382
CodeDirectory codeDirectory = new DefaultCodeDirectory(projectPath);
383383
List<Path> sarifFiles = convertToPaths(sarifs);
384384
List<Path> sonarIssuesJsonFiles = convertToPaths(sonarIssuesJsonFilePaths);
385+
List<Path> sonarHotspotJsonFiles = convertToPaths(sonarHotspotsJsonFilePaths);
385386
Map<String, List<RuleSarif>> pathSarifMap =
386387
SarifParser.create().parseIntoMap(sarifFiles, codeDirectory);
387388
List<ParameterArgument> codemodParameters =
@@ -397,6 +398,7 @@ public Integer call() throws IOException {
397398
pathSarifMap,
398399
codemodParameters,
399400
sonarIssuesJsonFiles,
401+
sonarHotspotJsonFiles,
400402
defectDojoFindingsJsonFilePath,
401403
contrastVulnerabilitiesXmlFilePath);
402404
List<CodemodIdPair> codemods = loader.getCodemods();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public CodemodLoader(
2929
final Map<String, List<RuleSarif>> ruleSarifByTool,
3030
final List<ParameterArgument> codemodParameters,
3131
final List<Path> sonarIssuesJsonFiles,
32+
final List<Path> sonarHotspotsJsonFiles,
3233
final Path defectDojoFindingsJsonFile,
3334
final Path contrastVulnerabilitiesXmlFilePath) {
3435

@@ -101,6 +102,7 @@ public CodemodLoader(
101102
orderedCodemodTypes,
102103
allWantedSarifs,
103104
sonarIssuesJsonFiles,
105+
sonarHotspotsJsonFiles,
104106
defectDojoFindingsJsonFile,
105107
contrastVulnerabilitiesXmlFilePath);
106108
allModules.addAll(modules);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ Set<AbstractModule> getModules(
3636
List<Class<? extends CodeChanger>> codemodTypes,
3737
List<RuleSarif> sarifs,
3838
List<Path> sonarIssuesJsonPaths,
39+
List<Path> sonarHotspotsJsonPaths,
3940
Path defectDojoFindingsJsonPath,
4041
Path contrastFindingsJsonPath);
4142

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ private CodemodLoader createLoader(final Class<? extends CodeChanger> codemodTyp
409409
Files.list(dir).toList(),
410410
Map.of(),
411411
List.of(),
412+
List.of(),
412413
null,
413414
null,
414415
null);
@@ -425,6 +426,7 @@ private CodemodLoader createLoader(
425426
Files.list(dir).toList(),
426427
Map.of(),
427428
List.of(),
429+
List.of(),
428430
null,
429431
null,
430432
null);
@@ -446,6 +448,7 @@ private CodemodLoader createLoader(
446448
params,
447449
null,
448450
null,
451+
null,
449452
null);
450453
}
451454
}

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ default Stream<DynamicTest> generateTestCases(@TempDir final Path tmpDir) throws
7878
metadata.doRetransformTest(),
7979
metadata.expectingFixesAtLines(),
8080
metadata.expectingFailedFixesAtLines(),
81-
metadata.sonarIssuesJsonFiles());
81+
metadata.sonarIssuesJsonFiles(),
82+
metadata.sonarHotspotsJsonFiles());
8283

8384
return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
8485
}
@@ -94,7 +95,8 @@ private void verifyCodemod(
9495
final boolean doRetransformTest,
9596
final int[] expectedFixLines,
9697
final int[] expectingFailedFixesAtLines,
97-
final String[] sonarIssuesJsonFiles)
98+
final String[] sonarIssuesJsonFiles,
99+
final String[] sonarHotspotsJsonFiles)
98100
throws IOException {
99101

100102
// create a copy of the test file in the temp directory to serve as our "repository"
@@ -114,21 +116,10 @@ private void verifyCodemod(
114116
pathToJavaFile = newPathToJavaFile;
115117
}
116118

117-
final List<String> sonarJsons =
118-
sonarIssuesJsonFiles != null ? Arrays.asList(sonarIssuesJsonFiles) : new ArrayList<>();
119-
120-
final List<Path> sonarJsonsPaths =
121-
sonarJsons.stream()
122-
.map(testResourceDir::resolve)
123-
.filter(Files::exists)
124-
.collect(Collectors.toList());
125-
126-
if (sonarJsonsPaths.isEmpty()) {
127-
Path defaultPath = testResourceDir.resolve("sonar-issues.json");
128-
if (Files.exists(defaultPath)) {
129-
sonarJsonsPaths.add(defaultPath);
130-
}
131-
}
119+
final List<Path> sonarIssuesJsonsPaths =
120+
buildSonarJsonPaths(testResourceDir, sonarIssuesJsonFiles, "sonar-issues.json");
121+
final List<Path> sonarHotspotsJsonPaths =
122+
buildSonarJsonPaths(testResourceDir, sonarHotspotsJsonFiles, "sonar-hotspots.json");
132123

133124
// Check for any sarif files and build the RuleSarif map
134125
CodeDirectory codeDir = CodeDirectory.from(tmpDir);
@@ -155,7 +146,8 @@ private void verifyCodemod(
155146
List.of(pathToJavaFile),
156147
map,
157148
List.of(),
158-
sonarJsonsPaths,
149+
sonarIssuesJsonsPaths,
150+
sonarHotspotsJsonPaths,
159151
Files.exists(defectDojo) ? defectDojo : null,
160152
Files.exists(contrastXml) ? contrastXml : null);
161153

@@ -243,6 +235,7 @@ private void verifyCodemod(
243235
List.of(),
244236
null,
245237
null,
238+
null,
246239
null);
247240
CodemodIdPair codemod2 = loader2.getCodemods().get(0);
248241
CodemodExecutor executor2 =
@@ -270,6 +263,29 @@ private void verifyCodemod(
270263
assertThat(codeAfterFirstTransform, equalTo(codeAfterSecondTransform));
271264
}
272265

266+
private List<Path> buildSonarJsonPaths(
267+
final Path testResourceDir,
268+
final String[] sonarJsonFiles,
269+
final String defaultSonarFilename) {
270+
final List<String> sonarJsons =
271+
sonarJsonFiles != null ? Arrays.asList(sonarJsonFiles) : new ArrayList<>();
272+
273+
final List<Path> sonarIssuesJsonsPaths =
274+
sonarJsons.stream()
275+
.map(testResourceDir::resolve)
276+
.filter(Files::exists)
277+
.collect(Collectors.toList());
278+
279+
if (sonarIssuesJsonsPaths.isEmpty()) {
280+
Path defaultPath = testResourceDir.resolve(defaultSonarFilename);
281+
if (Files.exists(defaultPath)) {
282+
sonarIssuesJsonsPaths.add(defaultPath);
283+
}
284+
}
285+
286+
return sonarIssuesJsonsPaths;
287+
}
288+
273289
/**
274290
* A hook for verifying the before and after files. By default, this method will compare the
275291
* contents of the two files for exact equality.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@
5151

5252
/** Sonar issues file names for testing multiple json files */
5353
String[] sonarIssuesJsonFiles() default {};
54+
55+
/** Sonar hotspots file names for testing multiple json files */
56+
String[] sonarHotspotsJsonFiles() default {};
5457
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void verifySingleCase(
7171
List.of(),
7272
null,
7373
null,
74+
null,
7475
null);
7576
List<CodemodIdPair> codemods = loader.getCodemods();
7677
assertThat("Only expecting 1 codemod per test", codemods.size(), equalTo(1));

0 commit comments

Comments
 (0)