From b4216e6b0edef15492dd5d3d595e19fd33bb3459 Mon Sep 17 00:00:00 2001 From: Benjamin Campomenosi Date: Thu, 20 Oct 2022 16:29:28 +0200 Subject: SONAR-13579 Overall coverage improvements --- .../scanner/report/ChangedLinesPublisher.java | 26 ++++++++++++++-- .../sonar/scanner/report/ComponentsPublisher.java | 5 +-- .../sonar/scanner/scan/filesystem/FileIndexer.java | 2 +- .../org/sonar/scanner/scm/ScmChangedFiles.java | 33 +++++++++++--------- .../sonar/scanner/scm/ScmChangedFilesProvider.java | 36 +++++++++++++++++----- 5 files changed, 75 insertions(+), 27 deletions(-) (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner') diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java index 56737d1e068..1d434618862 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java @@ -20,12 +20,13 @@ package org.sonar.scanner.report; import java.nio.file.Path; +import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import javax.annotation.CheckForNull; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.scm.ScmProvider; @@ -39,9 +40,12 @@ import org.sonar.scanner.repository.ReferenceBranchSupplier; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scm.ScmConfiguration; +import org.sonar.scm.git.ChangedFile; import org.sonar.scm.git.GitScmProvider; import static java.util.Optional.empty; +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.toMap; public class ChangedLinesPublisher implements ReportPublisherStep { private static final Logger LOG = Loggers.get(ChangedLinesPublisher.class); @@ -88,9 +92,9 @@ public class ChangedLinesPublisher implements ReportPublisherStep { private int writeChangedLines(ScmProvider provider, ScannerReportWriter writer, String targetScmBranch) { Path rootBaseDir = project.getBaseDir(); Map changedFiles = StreamSupport.stream(inputComponentStore.allChangedFilesToPublish().spliterator(), false) - .collect(Collectors.toMap(DefaultInputFile::path, f -> f)); + .collect(toMap(DefaultInputFile::path, identity())); - Map> pathSetMap = ((GitScmProvider) provider).branchChangedLines(targetScmBranch, rootBaseDir, changedFiles); // TODO: Extend ScmProvider abstract + Map> pathSetMap = getBranchChangedLinesByScm(provider, targetScmBranch, rootBaseDir, toChangedFilesByPathMap(changedFiles.values())); int count = 0; if (pathSetMap == null) { @@ -126,4 +130,20 @@ public class ChangedLinesPublisher implements ReportPublisherStep { builder.addAllLine(changedLines); writer.writeComponentChangedLines(fileRef, builder.build()); } + + @CheckForNull + private static Map> getBranchChangedLinesByScm(ScmProvider scmProvider, String targetScmBranch, Path rootBaseDir, Map changedFiles) { + if (scmProvider instanceof GitScmProvider) { + return ((GitScmProvider) scmProvider).branchChangedLinesWithFileMovementDetection(targetScmBranch, rootBaseDir, changedFiles); + } + + return scmProvider.branchChangedLines(targetScmBranch, rootBaseDir, changedFiles.keySet()); + } + + private static Map toChangedFilesByPathMap(Collection files) { + return files + .stream() + .map(ChangedFile::of) + .collect(toMap(ChangedFile::getAbsolutFilePath, identity())); + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java index b21da5f2f94..404d646e191 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java @@ -68,8 +68,9 @@ public class ComponentsPublisher implements ReportPublisherStep { fileBuilder.setStatus(convert(file.status())); fileBuilder.setMarkedAsUnchanged(file.isMarkedAsUnchanged()); - if (file.isMovedFile()) { - fileBuilder.setOldRelativeFilePath(file.oldPath()); + String oldRelativePath = file.oldRelativePath(); + if (oldRelativePath != null) { + fileBuilder.setOldRelativeFilePath(oldRelativePath); } String lang = getLanguageKey(file); 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 4653c24d171..f62617d166a 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 @@ -136,7 +136,7 @@ public class FileIndexer { language != null ? language.key() : null, scannerComponentIdGenerator.getAsInt(), sensorStrategy, - scmChangedFiles.getFileOldPath(realAbsoluteFile) + scmChangedFiles.getOldRelativeFilePath(realAbsoluteFile) ); DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding())); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java index 010f2de4cbf..a26f57ce2dd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java @@ -21,21 +21,26 @@ package org.sonar.scanner.scm; import java.nio.file.Path; import java.util.Collection; -import java.util.List; +import java.util.Map; import java.util.Optional; -import java.util.function.Predicate; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.scm.git.ChangedFile; +import static java.util.Collections.emptyMap; +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.toMap; + @Immutable public class ScmChangedFiles { @Nullable private final Collection changedFiles; + private final Map changedFilesByPath; public ScmChangedFiles(@Nullable Collection changedFiles) { this.changedFiles = changedFiles; + this.changedFilesByPath = toChangedFilesByPathMap(changedFiles); } public boolean isChanged(Path file) { @@ -43,7 +48,7 @@ public class ScmChangedFiles { throw new IllegalStateException("Scm didn't provide valid data"); } - return this.findFile(file).isPresent(); + return this.getChangedFile(file).isPresent(); } public boolean isValid() { @@ -56,20 +61,20 @@ public class ScmChangedFiles { } @CheckForNull - public String getFileOldPath(Path absoluteFilePath) { - return this.findFile(absoluteFilePath) - .filter(ChangedFile::isMoved) - .map(ChangedFile::getOldFilePath) + public String getOldRelativeFilePath(Path absoluteFilePath) { + return this.getChangedFile(absoluteFilePath) + .filter(ChangedFile::isMovedFile) + .map(ChangedFile::getOldRelativeFilePathReference) .orElse(null); } - private Optional findFile(Path absoluteFilePath) { - Predicate isTargetFile = file -> file.getAbsolutFilePath().equals(absoluteFilePath); + private Optional getChangedFile(Path absoluteFilePath) { + return Optional.ofNullable(changedFilesByPath.get(absoluteFilePath)); + } - return Optional.ofNullable(this.get()) - .orElseGet(List::of) - .stream() - .filter(isTargetFile) - .findFirst(); + private static Map toChangedFilesByPathMap(@Nullable Collection changedFiles) { + return Optional.ofNullable(changedFiles) + .map(files -> files.stream().collect(toMap(ChangedFile::getAbsolutFilePath, identity()))) + .orElse(emptyMap()); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java index 392996d7e74..4ec314a4566 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java @@ -22,9 +22,10 @@ package org.sonar.scanner.scm; import java.nio.file.Path; import java.util.Collection; import java.util.Set; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.api.batch.fs.internal.DefaultInputProject; +import org.sonar.api.batch.scm.ScmProvider; import org.sonar.api.impl.utils.ScannerUtils; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -34,6 +35,8 @@ import org.sonar.scm.git.ChangedFile; import org.sonar.scm.git.GitScmProvider; import org.springframework.context.annotation.Bean; +import static java.util.stream.Collectors.toSet; + public class ScmChangedFilesProvider { private static final Logger LOG = Loggers.get(ScmChangedFilesProvider.class); private static final String LOG_MSG = "SCM collecting changed files in the branch"; @@ -44,33 +47,33 @@ public class ScmChangedFilesProvider { Collection changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); if (changedFiles != null) { - validatePaths(getFilePaths(changedFiles)); + validatePaths(getAbsoluteFilePaths(changedFiles)); } return new ScmChangedFiles(changedFiles); } private static void validatePaths(Set changedFilePaths) { - if (changedFilePaths != null && changedFilePaths.stream().anyMatch(p -> !p.isAbsolute())) { + if (changedFilePaths.stream().anyMatch(p -> !p.isAbsolute())) { throw new IllegalStateException("SCM provider returned a changed file with a relative path but paths must be absolute. Please fix the provider."); } } - private static Set getFilePaths(Collection changedFiles) { + private static Set getAbsoluteFilePaths(Collection changedFiles) { return changedFiles .stream() .map(ChangedFile::getAbsolutFilePath) - .collect(Collectors.toSet()); + .collect(toSet()); } @CheckForNull private static Collection loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) { final String targetBranchName = branchConfiguration.targetBranchName(); if (branchConfiguration.isPullRequest() && targetBranchName != null) { - GitScmProvider scmProvider = (GitScmProvider) scmConfiguration.provider(); + ScmProvider scmProvider = scmConfiguration.provider(); if (scmProvider != null) { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - Collection changedFiles = scmProvider.branchModifiedFiles(targetBranchName, rootBaseDir); + Collection changedFiles = getChangedFilesByScm(scmProvider, targetBranchName, rootBaseDir); profiler.stopInfo(); if (changedFiles != null) { LOG.debug("SCM reported {} {} changed in the branch", changedFiles.size(), ScannerUtils.pluralize("file", changedFiles.size())); @@ -83,4 +86,23 @@ public class ScmChangedFilesProvider { return null; } + private static Collection getChangedFilesByScm(ScmProvider scmProvider, String targetBranchName, Path rootBaseDir) { + if (scmProvider instanceof GitScmProvider) { + return ((GitScmProvider) scmProvider).branchChangedFilesWithFileMovementDetection(targetBranchName, rootBaseDir); + } + + return toChangedFiles(scmProvider.branchChangedFiles(targetBranchName, rootBaseDir)); + } + + @CheckForNull + private static Collection toChangedFiles(@Nullable Set changedPaths) { + if (changedPaths == null) { + return null; + } + + return changedPaths + .stream() + .map(ChangedFile::of) + .collect(toSet()); + } } -- cgit v1.2.3