diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2023-05-08 10:58:39 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-05-08 20:03:47 +0000 |
commit | bc1cd787ad0d474bfec5614eb0ca8ab5b1d9fc3f (patch) | |
tree | 3c34297f64885b03c51fc49737b4440702ffa10a /sonar-scanner-engine | |
parent | 204d9b7f21fb8807c8a53756d1234a00023a9206 (diff) | |
download | sonarqube-bc1cd787ad0d474bfec5614eb0ca8ab5b1d9fc3f.tar.gz sonarqube-bc1cd787ad0d474bfec5614eb0ca8ab5b1d9fc3f.zip |
SONAR-18210 not computing the whole file metadata when only status is needed.
Co-authored-by: Duarte Meneses <duarte.meneses@sonarsource.com>
Diffstat (limited to 'sonar-scanner-engine')
5 files changed, 32 insertions, 15 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index e07679e003f..2d177d4edee 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -68,6 +68,7 @@ public class FileIndexer { private final InputComponentStore componentStore; private final SensorStrategy sensorStrategy; private final LanguageDetection langDetection; + private final StatusDetection statusDetection; private final ScmChangedFiles scmChangedFiles; private boolean warnInclusionsAlreadyLogged; @@ -78,7 +79,7 @@ public class FileIndexer { public FileIndexer(DefaultInputProject project, ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, ProjectExclusionFilters projectExclusionFilters, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, IssueExclusionsLoader issueExclusionsLoader, MetadataGenerator metadataGenerator, SensorStrategy sensorStrategy, LanguageDetection languageDetection, AnalysisWarnings analysisWarnings, ScanProperties properties, - InputFileFilter[] filters, ScmChangedFiles scmChangedFiles) { + InputFileFilter[] filters, ScmChangedFiles scmChangedFiles, StatusDetection statusDetection) { this.project = project; this.scannerComponentIdGenerator = scannerComponentIdGenerator; this.componentStore = componentStore; @@ -92,6 +93,7 @@ public class FileIndexer { this.filters = filters; this.projectExclusionFilters = projectExclusionFilters; this.scmChangedFiles = scmChangedFiles; + this.statusDetection = statusDetection; } void indexFile(DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, @@ -145,7 +147,8 @@ public class FileIndexer { scmChangedFiles.getOldRelativeFilePath(realAbsoluteFile) ); - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding())); + DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()), + f -> f.setStatus(statusDetection.findStatusFromScm(f))); if (language != null && language.isPublishAllFiles()) { inputFile.setPublished(true); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/MetadataGenerator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/MetadataGenerator.java index 627cb3adc17..562fcbe338a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/MetadataGenerator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/MetadataGenerator.java @@ -63,7 +63,9 @@ public class MetadataGenerator { inputFile.setCharset(charset); Metadata metadata = fileMetadata.readMetadata(is, charset, inputFile.absolutePath(), exclusionsScanner.createCharHandlerFor(inputFile)); inputFile.setMetadata(metadata); - inputFile.setStatus(statusDetection.status(moduleKeyWithBranch, inputFile, metadata.hash())); + if(!inputFile.isStatusSet()) { + inputFile.setStatus(statusDetection.status(moduleKeyWithBranch, inputFile, metadata.hash())); + } LOG.debug("'{}' generated metadata{} with charset '{}'", inputFile, inputFile.type() == Type.TEST ? " as test " : "", charset); } catch (Exception e) { throw new IllegalStateException(e); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java index 8c7d8af91da..65349df4120 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java @@ -41,13 +41,25 @@ public class StatusDetection { this.scmChangedFiles = scmChangedFiles; } + public boolean isScmStatusAvailable() { + return scmChangedFiles.isValid(); + } + InputFile.Status status(String moduleKeyWithBranch, DefaultInputFile inputFile, String hash) { - if (scmChangedFiles.isValid()) { - return checkChangedWithScm(inputFile); + InputFile.Status statusFromScm = findStatusFromScm(inputFile); + if (statusFromScm != null) { + return statusFromScm; } return checkChangedWithProjectRepositories(moduleKeyWithBranch, inputFile, hash); } + InputFile.Status findStatusFromScm(DefaultInputFile inputFile) { + if (isScmStatusAvailable()) { + return checkChangedWithScm(inputFile); + } + return null; + } + private InputFile.Status checkChangedWithProjectRepositories(String moduleKeyWithBranch, DefaultInputFile inputFile, String hash) { FileData fileDataPerPath = projectRepositories.fileData(moduleKeyWithBranch, inputFile); if (fileDataPerPath == null) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java index 2b305833b52..6883d28e7ef 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RegionMapperTest.java @@ -48,8 +48,8 @@ import static org.mockito.Mockito.when; public class RegionMapperTest { private static final int LINE_END_OFFSET = 10; private static final DefaultInputFile INPUT_FILE = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "relative/path", null), - f -> f.setMetadata(generateMetadata())); - + f -> f.setMetadata(generateMetadata()), f -> { + }); private static Metadata generateMetadata() { Metadata metadata = mock(Metadata.class); @@ -97,9 +97,9 @@ public class RegionMapperTest { @DataProvider public static Object[][] index() { - return new Object[][]{ - {1,3, 0,2}, - {5,8, 4,7} + return new Object[][] { + {1, 3, 0, 2}, + {5, 8, 4, 7} }; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/ChangedFileTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/ChangedFileTest.java index 1b7219ca85d..adaf4ed24a9 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/ChangedFileTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/ChangedFileTest.java @@ -30,11 +30,10 @@ import static org.apache.commons.lang.RandomStringUtils.random; import static org.apache.commons.lang.RandomStringUtils.randomNumeric; import static org.assertj.core.api.Assertions.assertThat; - public class ChangedFileTest { @Test - public void test_unMovedFile(){ + public void test_unMovedFile() { Path absolutePath = Path.of("/absolutePath"); ChangedFile changedFile = ChangedFile.of(absolutePath); @@ -44,7 +43,7 @@ public class ChangedFileTest { } @Test - public void test_movedFile(){ + public void test_movedFile() { Path absolutePath = Path.of("/absolutePath"); ChangedFile changedFile = ChangedFile.of(absolutePath, "/oldRelativePath"); @@ -54,7 +53,7 @@ public class ChangedFileTest { } @Test - public void test_equalsAndHashCode(){ + public void test_equalsAndHashCode() { Path absolutePath = Path.of("/absolutePath"); ChangedFile changedFile1 = ChangedFile.of(absolutePath, "/oldRelativePath"); ChangedFile changedFile2 = ChangedFile.of(absolutePath, "/oldRelativePath"); @@ -76,7 +75,8 @@ public class ChangedFileTest { private DefaultInputFile composeDefaultInputFile(Path path, String oldRelativeReference) { DefaultIndexedFile indexedFile = composeDefaultIndexFile(path, oldRelativeReference); - return new DefaultInputFile(indexedFile, f -> f.setPublished(true)); + return new DefaultInputFile(indexedFile, f -> f.setPublished(true), f -> { + }); } private DefaultIndexedFile composeDefaultIndexFile(Path path, String oldRelativePath) { |