From 49e4d796f49c3dd5619cf084ae8779b501a2f865 Mon Sep 17 00:00:00 2001 From: Arshan Dabirsiaghi Date: Tue, 23 Apr 2024 17:26:16 -0400 Subject: [PATCH] Add file searching ability to `CodeDirectory` (#363) Analyzing binary result from multiple tools shows we'll need this functionality in multiple places, so we're adding it to the generally available `CodeDirectory` type. --- .../src/main/java/io/codemodder/CLI.java | 3 +- .../java/io/codemodder/CodeDirectory.java | 12 ++++ .../io/codemodder/DefaultCodeDirectory.java | 34 ++++++++++ .../io/codemodder/DefaultSarifParser.java | 13 ++-- .../java/io/codemodder/RuleSarifFactory.java | 3 +- .../main/java/io/codemodder/SarifParser.java | 2 +- .../codemodder/DefaultCodeDirectoryTest.java | 68 +++++++++++++++++++ .../testutils/CodemodTestMixin.java | 3 +- .../testutils/RawFileCodemodTest.java | 3 +- .../sarif/appscan/AppScanRuleSarif.java | 32 ++------- .../appscan/AppScanRuleSarifFactory.java | 9 ++- .../sarif/appscan/AppScanModuleTest.java | 3 +- .../appscan/AppScanRuleSarifFactoryTest.java | 3 +- .../sarif/codeql/CodeQLRuleSarif.java | 5 +- .../sarif/codeql/CodeQLRuleSarifFactory.java | 9 ++- .../semgrep/SemgrepRuleSarifFactory.java | 9 ++- .../sarif/semgrep/SingleSemgrepRuleSarif.java | 4 +- .../sarif/semgrep/SemgrepModuleTest.java | 2 +- 18 files changed, 161 insertions(+), 56 deletions(-) create mode 100644 framework/codemodder-base/src/test/java/io/codemodder/DefaultCodeDirectoryTest.java diff --git a/framework/codemodder-base/src/main/java/io/codemodder/CLI.java b/framework/codemodder-base/src/main/java/io/codemodder/CLI.java index 4c61ada36..3b7bb7432 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/CLI.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/CLI.java @@ -377,10 +377,11 @@ public Integer call() throws IOException { } // create the loader + CodeDirectory codeDirectory = new DefaultCodeDirectory(projectPath); List sarifFiles = sarifs != null ? sarifs.stream().map(Path::of).collect(Collectors.toList()) : List.of(); Map> pathSarifMap = - SarifParser.create().parseIntoMap(sarifFiles, projectPath); + SarifParser.create().parseIntoMap(sarifFiles, codeDirectory); List codemodParameters = createFromParameterStrings(this.codemodParameters); CodemodLoader loader = diff --git a/framework/codemodder-base/src/main/java/io/codemodder/CodeDirectory.java b/framework/codemodder-base/src/main/java/io/codemodder/CodeDirectory.java index 8a1fd9b44..79e036c36 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/CodeDirectory.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/CodeDirectory.java @@ -1,10 +1,22 @@ package io.codemodder; +import java.io.IOException; import java.nio.file.Path; +import java.util.Optional; /** Holds a code directory (e.g., a repository root). */ public interface CodeDirectory { /** The filesystem directory path we are running against. */ Path asPath(); + + /** + * Find a file with the given trailing path. This is useful for situations in which you only know + * the last part of the path for a file within the project. + */ + Optional findFilesWithTrailingPath(final String path) throws IOException; + + static CodeDirectory from(final Path projectDir) { + return new DefaultCodeDirectory(projectDir); + } } diff --git a/framework/codemodder-base/src/main/java/io/codemodder/DefaultCodeDirectory.java b/framework/codemodder-base/src/main/java/io/codemodder/DefaultCodeDirectory.java index 68aff5634..b77eb2e66 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/DefaultCodeDirectory.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/DefaultCodeDirectory.java @@ -1,8 +1,15 @@ package io.codemodder; +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; final class DefaultCodeDirectory implements CodeDirectory { @@ -26,4 +33,31 @@ final class DefaultCodeDirectory implements CodeDirectory { public Path asPath() { return repositoryDir; } + + @Override + public Optional findFilesWithTrailingPath(final String path) throws IOException { + // find the files with the trailing path + AtomicReference found = new AtomicReference<>(); + + final String cleanPath = + path.trim() + .replace("\\\\", "\\") + .replace("//", "/") + .replace('\\', File.separatorChar) + .replace('/', File.separatorChar); + + Files.walkFileTree( + repositoryDir, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) { + if (file.toString().endsWith(cleanPath)) { + found.set(file); + return FileVisitResult.TERMINATE; + } + return FileVisitResult.CONTINUE; + } + }); + return Optional.ofNullable(found.get()); + } } diff --git a/framework/codemodder-base/src/main/java/io/codemodder/DefaultSarifParser.java b/framework/codemodder-base/src/main/java/io/codemodder/DefaultSarifParser.java index dfd089b86..6d27e5b68 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/DefaultSarifParser.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/DefaultSarifParser.java @@ -30,10 +30,10 @@ private Optional> tryToBuild( final String toolName, final String rule, final SarifSchema210 sarif, - final Path repositoryRoot, + final CodeDirectory codeDirectory, final List factories) { for (final var factory : factories) { - final var maybeRuleSarif = factory.build(toolName, rule, sarif, repositoryRoot); + final var maybeRuleSarif = factory.build(toolName, rule, sarif, codeDirectory); if (maybeRuleSarif.isPresent()) { return Optional.of(Map.entry(toolName, maybeRuleSarif.get())); } @@ -64,7 +64,7 @@ private String extractRuleId(final Result result, final Run run) { } private Stream> fromSarif( - final Run run, final SarifSchema210 sarif, final Path repositoryRoot) { + final Run run, final SarifSchema210 sarif, final CodeDirectory codeDirectory) { // driver name final var toolName = run.getTool().getDriver().getName(); final List factories = @@ -81,20 +81,21 @@ private Stream> fromSarif( : Stream.empty(); return allResults.flatMap( - rule -> tryToBuild(toolName, rule, sarif, repositoryRoot, factories).stream()); + rule -> tryToBuild(toolName, rule, sarif, codeDirectory, factories).stream()); } /** * Parse a list of SARIF files and organize the obtained {@link RuleSarif}s by tool name with a * map . */ + @Override public Map> parseIntoMap( - final List sarifFiles, final Path repositoryRoot) { + final List sarifFiles, final CodeDirectory codeDirectory) { final var map = new HashMap>(); sarifFiles.stream() .flatMap(f -> readSarifFile(f).stream()) .flatMap( - sarif -> sarif.getRuns().stream().flatMap(run -> fromSarif(run, sarif, repositoryRoot))) + sarif -> sarif.getRuns().stream().flatMap(run -> fromSarif(run, sarif, codeDirectory))) .forEach( p -> map.merge( diff --git a/framework/codemodder-base/src/main/java/io/codemodder/RuleSarifFactory.java b/framework/codemodder-base/src/main/java/io/codemodder/RuleSarifFactory.java index 0f590716d..f43cb0049 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/RuleSarifFactory.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/RuleSarifFactory.java @@ -1,7 +1,6 @@ package io.codemodder; import com.contrastsecurity.sarif.SarifSchema210; -import java.nio.file.Path; import java.util.Optional; /** Builds {@link RuleSarif}s. */ @@ -9,5 +8,5 @@ public interface RuleSarifFactory { /** Builds {@link RuleSarif}s if it supports {@code toolName}. */ Optional build( - String toolName, String rule, SarifSchema210 sarif, Path repositoryRoot); + String toolName, String rule, SarifSchema210 sarif, CodeDirectory codeDirectory); } diff --git a/framework/codemodder-base/src/main/java/io/codemodder/SarifParser.java b/framework/codemodder-base/src/main/java/io/codemodder/SarifParser.java index b4239bafe..1f9e4fe73 100644 --- a/framework/codemodder-base/src/main/java/io/codemodder/SarifParser.java +++ b/framework/codemodder-base/src/main/java/io/codemodder/SarifParser.java @@ -14,7 +14,7 @@ public interface SarifParser { * Given a list of sarif {@link Path}s, organize them into a {@link Map} containing {@link * RuleSarif}s organized by tool name. */ - Map> parseIntoMap(List sarifFiles, Path repositoryRoot); + Map> parseIntoMap(List sarifFiles, CodeDirectory codeDirectory); static SarifParser create() { return new DefaultSarifParser(); diff --git a/framework/codemodder-base/src/test/java/io/codemodder/DefaultCodeDirectoryTest.java b/framework/codemodder-base/src/test/java/io/codemodder/DefaultCodeDirectoryTest.java new file mode 100644 index 000000000..247683736 --- /dev/null +++ b/framework/codemodder-base/src/test/java/io/codemodder/DefaultCodeDirectoryTest.java @@ -0,0 +1,68 @@ +package io.codemodder; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +final class DefaultCodeDirectoryTest { + + private DefaultCodeDirectory codeDirectory; + private Path repoDir; + + @BeforeEach + void setup(@TempDir final Path repoDir) throws IOException { + Path testFile1 = repoDir.resolve("my/other/test/file1.java"); + Path srcFile1 = repoDir.resolve("src/main/file1.java"); + Path srcFile2 = repoDir.resolve("src/main/file2.java"); + + Files.createDirectories(testFile1.getParent()); + Files.createFile(testFile1); + + Files.createDirectories(srcFile1.getParent()); + Files.createFile(srcFile1); + + Files.createDirectories(srcFile2.getParent()); + Files.createFile(srcFile2); + + Files.writeString(testFile1, "test file 1"); + Files.writeString(srcFile1, "src file 1"); + Files.writeString(srcFile2, "src file 2"); + + this.repoDir = repoDir; + codeDirectory = new DefaultCodeDirectory(repoDir); + } + + @ParameterizedTest + @MethodSource("fileTests") + void it_finds_files(final String givenPath, final String expectedPath) throws IOException { + Optional filesWithTrailingPath = codeDirectory.findFilesWithTrailingPath(givenPath); + + if (expectedPath == null) { + assertThat(filesWithTrailingPath).isEmpty(); + } else { + assertThat(filesWithTrailingPath).isPresent(); + Path expected = repoDir.resolve(expectedPath); + assertThat(filesWithTrailingPath.get()).isEqualTo(expected); + } + } + + private static Stream fileTests() { + return Stream.of( + Arguments.of("file1.java", "my/other/test/file1.java"), + Arguments.of("main/file1.java", "src/main/file1.java"), + Arguments.of("main//file1.java", "src/main/file1.java"), + Arguments.of("main\\file1.java", "src/main/file1.java"), + Arguments.of("src\\\\main\\file1.java", "src/main/file1.java"), + Arguments.of("file2.java", "src/main/file2.java"), + Arguments.of("file3.java", null)); + } +} diff --git a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java index 1f2097e8d..826c7f90e 100644 --- a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java +++ b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java @@ -115,11 +115,12 @@ private void verifyCodemod( Path sonarJson = testResourceDir.resolve("sonar-issues.json"); // Check for any sarif files and build the RuleSarif map + CodeDirectory codeDir = CodeDirectory.from(tmpDir); List allSarifs = new ArrayList<>(); Files.newDirectoryStream(testResourceDir, "*.sarif") .iterator() .forEachRemaining(allSarifs::add); - Map> map = SarifParser.create().parseIntoMap(allSarifs, tmpDir); + Map> map = SarifParser.create().parseIntoMap(allSarifs, codeDir); // Check for any a defectdojo Path defectDojo = testResourceDir.resolve("defectdojo.json"); diff --git a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/RawFileCodemodTest.java b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/RawFileCodemodTest.java index 651c894da..ccff64cde 100644 --- a/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/RawFileCodemodTest.java +++ b/framework/codemodder-testutils/src/main/java/io/codemodder/testutils/RawFileCodemodTest.java @@ -127,8 +127,9 @@ private void verifyCodemod( files.filter(file -> file.getFileName().toString().endsWith(".sarif")).toList(); } + final CodeDirectory codeDirectory = CodeDirectory.from(tmpDir); final Map> map = - SarifParser.create().parseIntoMap(allSarifFiles, tmpDir); + SarifParser.create().parseIntoMap(allSarifFiles, codeDirectory); // grab all the .before and .after files in the dir final List allBeforeFiles; diff --git a/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarif.java b/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarif.java index d25b203e0..e66ccf3c4 100644 --- a/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarif.java +++ b/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarif.java @@ -1,16 +1,12 @@ package io.codemodder.providers.sarif.appscan; import com.contrastsecurity.sarif.*; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import java.io.IOException; import java.io.UncheckedIOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; /** A {@link RuleSarif} for AppScan results. */ final class AppScanRuleSarif implements RuleSarif { @@ -18,7 +14,6 @@ final class AppScanRuleSarif implements RuleSarif { private final SarifSchema210 sarif; private final String ruleId; private final Map> resultsCache; - private final Path repositoryRoot; private final List locations; /** A map of a AppScan SARIF "location" URIs mapped to their respective file paths. */ @@ -28,11 +23,10 @@ final class AppScanRuleSarif implements RuleSarif { * Creates an {@link AppScanRuleSarif} that has already done the work of mapping AppScan SARIF * locations, which are strange combinations of class name and file path, into predictable paths. */ - public AppScanRuleSarif( - final String ruleId, final SarifSchema210 sarif, final Path repositoryRoot) { + AppScanRuleSarif( + final String ruleId, final SarifSchema210 sarif, final CodeDirectory codeDirectory) { this.sarif = Objects.requireNonNull(sarif); this.ruleId = Objects.requireNonNull(ruleId); - this.repositoryRoot = repositoryRoot; this.resultsCache = new HashMap<>(); this.locations = sarif.getRuns().get(0).getArtifacts().stream() @@ -49,7 +43,7 @@ public AppScanRuleSarif( // we have a real but partial path, now we have to find it in the repository Optional existingRealPath; try { - existingRealPath = findFileWithTrailingPath(path); + existingRealPath = codeDirectory.findFilesWithTrailingPath(path); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -61,24 +55,6 @@ public AppScanRuleSarif( this.artifactLocationIndices = Map.copyOf(artifactLocationIndicesMap); } - private Optional findFileWithTrailingPath(final String path) throws IOException { - // find the files with the trailing path - AtomicReference found = new AtomicReference<>(); - Files.walkFileTree( - repositoryRoot, - new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) { - if (file.toString().endsWith(path)) { - found.set(file); - return FileVisitResult.TERMINATE; - } - return FileVisitResult.CONTINUE; - } - }); - return Optional.ofNullable(found.get()); - } - @Override public List getRegionsFromResultsByRule(final Path path) { List resultsByLocationPath = getResultsByLocationPath(path); diff --git a/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactory.java b/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactory.java index 8119fa6d6..0f994151a 100644 --- a/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactory.java +++ b/plugins/codemodder-plugin-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactory.java @@ -1,9 +1,9 @@ package io.codemodder.providers.sarif.appscan; import com.contrastsecurity.sarif.SarifSchema210; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import io.codemodder.RuleSarifFactory; -import java.nio.file.Path; import java.util.Optional; /** A factory for building {@link AppScanRuleSarif}s. */ @@ -11,9 +11,12 @@ public final class AppScanRuleSarifFactory implements RuleSarifFactory { @Override public Optional build( - String toolName, String rule, SarifSchema210 sarif, Path repositoryRoot) { + final String toolName, + final String rule, + final SarifSchema210 sarif, + final CodeDirectory codeDirectory) { if (AppScanRuleSarif.toolName.equals(toolName)) { - return Optional.of(new AppScanRuleSarif(rule, sarif, repositoryRoot)); + return Optional.of(new AppScanRuleSarif(rule, sarif, codeDirectory)); } return Optional.empty(); } diff --git a/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanModuleTest.java b/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanModuleTest.java index ef487c76e..840af3120 100644 --- a/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanModuleTest.java +++ b/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanModuleTest.java @@ -85,7 +85,8 @@ void it_works_with_appscan_sarif(@TempDir final Path repoDir) throws IOException new ObjectMapper().readValue(AppScanModuleTest.emptySarif, SarifSchema210.class); AppScanRuleSarifFactory ruleSarifFactory = new AppScanRuleSarifFactory(); Optional ruleSarif = - ruleSarifFactory.build("HCL AppScan Static Analyzer", "SA2813462719", rawSarif, repoDir); + ruleSarifFactory.build( + "HCL AppScan Static Analyzer", "SA2813462719", rawSarif, CodeDirectory.from(repoDir)); assertThat(ruleSarif.isPresent(), is(true)); AppScanModule module = new AppScanModule(List.of(AppScanSarifTestCodemod.class), List.of(ruleSarif.get())); diff --git a/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactoryTest.java b/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactoryTest.java index 0b9b25902..c9d08a44a 100644 --- a/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactoryTest.java +++ b/plugins/codemodder-plugin-appscan/src/test/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactoryTest.java @@ -6,6 +6,7 @@ import com.contrastsecurity.sarif.Result; import com.contrastsecurity.sarif.SarifSchema210; import com.fasterxml.jackson.databind.ObjectMapper; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import java.io.File; import java.io.IOException; @@ -42,7 +43,7 @@ void it_parses_sarif_and_maps_java_locations(@TempDir final Path tmpDir) throws new File("src/test/resources/webgoat_2023_8_binary.sarif"), SarifSchema210.class); Optional sarifRef = appScanRuleSarifFactory.build( - "HCL AppScan Static Analyzer", "SA2813462719", rawSarif, tmpDir); + "HCL AppScan Static Analyzer", "SA2813462719", rawSarif, CodeDirectory.from(tmpDir)); assertThat(sarifRef.isPresent()).isTrue(); RuleSarif ruleSarif = sarifRef.get(); diff --git a/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarif.java b/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarif.java index e068bc060..0b89ba5eb 100644 --- a/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarif.java +++ b/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarif.java @@ -4,6 +4,7 @@ import com.contrastsecurity.sarif.Result; import com.contrastsecurity.sarif.Run; import com.contrastsecurity.sarif.SarifSchema210; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import java.io.IOException; import java.nio.file.Files; @@ -25,10 +26,10 @@ public final class CodeQLRuleSarif implements RuleSarif { private final Path repositoryRoot; public CodeQLRuleSarif( - final String ruleId, final SarifSchema210 sarif, final Path repositoryRoot) { + final String ruleId, final SarifSchema210 sarif, final CodeDirectory codeDirectory) { this.sarif = Objects.requireNonNull(sarif); this.ruleId = Objects.requireNonNull(ruleId); - this.repositoryRoot = repositoryRoot; + this.repositoryRoot = codeDirectory.asPath(); this.resultsCache = new HashMap<>(); } diff --git a/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarifFactory.java b/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarifFactory.java index 4f59f5dca..e2c9df159 100644 --- a/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarifFactory.java +++ b/plugins/codemodder-plugin-codeql/src/main/java/io/codemodder/providers/sarif/codeql/CodeQLRuleSarifFactory.java @@ -1,9 +1,9 @@ package io.codemodder.providers.sarif.codeql; import com.contrastsecurity.sarif.SarifSchema210; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import io.codemodder.RuleSarifFactory; -import java.nio.file.Path; import java.util.Optional; /** A factory for building {@link CodeQLRuleSarif}s. */ @@ -11,9 +11,12 @@ public final class CodeQLRuleSarifFactory implements RuleSarifFactory { @Override public Optional build( - String toolName, String rule, SarifSchema210 sarif, Path repositoryRoot) { + final String toolName, + final String rule, + final SarifSchema210 sarif, + final CodeDirectory codeDirectory) { if (CodeQLRuleSarif.toolName.equals(toolName)) { - return Optional.of(new CodeQLRuleSarif(rule, sarif, repositoryRoot)); + return Optional.of(new CodeQLRuleSarif(rule, sarif, codeDirectory)); } return Optional.empty(); } diff --git a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SemgrepRuleSarifFactory.java b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SemgrepRuleSarifFactory.java index 457376085..036ffd71a 100644 --- a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SemgrepRuleSarifFactory.java +++ b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SemgrepRuleSarifFactory.java @@ -1,9 +1,9 @@ package io.codemodder.providers.sarif.semgrep; import com.contrastsecurity.sarif.SarifSchema210; +import io.codemodder.CodeDirectory; import io.codemodder.RuleSarif; import io.codemodder.RuleSarifFactory; -import java.nio.file.Path; import java.util.Optional; /** A factory for building {@link SingleSemgrepRuleSarif}s. */ @@ -11,9 +11,12 @@ public class SemgrepRuleSarifFactory implements RuleSarifFactory { @Override public Optional build( - String toolName, String rule, SarifSchema210 sarif, Path repositoryRoot) { + final String toolName, + final String rule, + final SarifSchema210 sarif, + final CodeDirectory codeDirectory) { if (SingleSemgrepRuleSarif.toolName.equalsIgnoreCase(toolName)) { - return Optional.of(new SingleSemgrepRuleSarif(rule, sarif, repositoryRoot)); + return Optional.of(new SingleSemgrepRuleSarif(rule, sarif, codeDirectory.asPath())); } return Optional.empty(); } diff --git a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java index d0ef157ba..25cf1ff99 100644 --- a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java +++ b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java @@ -28,10 +28,10 @@ final class SingleSemgrepRuleSarif implements RuleSarif { private final Path repositoryRoot; SingleSemgrepRuleSarif( - final String ruleId, final SarifSchema210 sarif, final Path repositoryRoot) { + final String ruleId, final SarifSchema210 sarif, final Path codeDirectory) { this.sarif = Objects.requireNonNull(sarif); this.ruleId = Objects.requireNonNull(ruleId); - this.repositoryRoot = Objects.requireNonNull(repositoryRoot); + this.repositoryRoot = Objects.requireNonNull(codeDirectory); this.resultsCache = new HashMap<>(); } diff --git a/plugins/codemodder-plugin-semgrep/src/test/java/io/codemodder/providers/sarif/semgrep/SemgrepModuleTest.java b/plugins/codemodder-plugin-semgrep/src/test/java/io/codemodder/providers/sarif/semgrep/SemgrepModuleTest.java index bb3a0789f..f5bd8d469 100644 --- a/plugins/codemodder-plugin-semgrep/src/test/java/io/codemodder/providers/sarif/semgrep/SemgrepModuleTest.java +++ b/plugins/codemodder-plugin-semgrep/src/test/java/io/codemodder/providers/sarif/semgrep/SemgrepModuleTest.java @@ -240,7 +240,7 @@ void it_works_with_offline_semgrep(@TempDir Path tmpDir) throws IOException { SarifParser.create() .parseIntoMap( List.of(Path.of("src/test/resources/semgrep_with_reflection_injection.sarif")), - tmpDir); + CodeDirectory.from(tmpDir)); SemgrepModule module = new SemgrepModule( tmpDir,