aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner
diff options
context:
space:
mode:
authorBenjamin Campomenosi <benjamin.campomenosi@sonarsource.com>2022-10-20 16:29:28 +0200
committersonartech <sonartech@sonarsource.com>2022-10-26 20:03:10 +0000
commitb4216e6b0edef15492dd5d3d595e19fd33bb3459 (patch)
treeed48e865c3a5ead39f6c0bb7471beedd8b4d36c6 /sonar-scanner-engine/src/main/java/org/sonar/scanner
parent342f7a1caf70e329f92466f07424406fbc424ee9 (diff)
downloadsonarqube-b4216e6b0edef15492dd5d3d595e19fd33bb3459.tar.gz
sonarqube-b4216e6b0edef15492dd5d3d595e19fd33bb3459.zip
SONAR-13579 Overall coverage improvements
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java26
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java5
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java33
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java36
5 files changed, 75 insertions, 27 deletions
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<Path, DefaultInputFile> changedFiles = StreamSupport.stream(inputComponentStore.allChangedFilesToPublish().spliterator(), false)
- .collect(Collectors.toMap(DefaultInputFile::path, f -> f));
+ .collect(toMap(DefaultInputFile::path, identity()));
- Map<Path, Set<Integer>> pathSetMap = ((GitScmProvider) provider).branchChangedLines(targetScmBranch, rootBaseDir, changedFiles); // TODO: Extend ScmProvider abstract
+ Map<Path, Set<Integer>> 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<Path, Set<Integer>> getBranchChangedLinesByScm(ScmProvider scmProvider, String targetScmBranch, Path rootBaseDir, Map<Path, ChangedFile> changedFiles) {
+ if (scmProvider instanceof GitScmProvider) {
+ return ((GitScmProvider) scmProvider).branchChangedLinesWithFileMovementDetection(targetScmBranch, rootBaseDir, changedFiles);
+ }
+
+ return scmProvider.branchChangedLines(targetScmBranch, rootBaseDir, changedFiles.keySet());
+ }
+
+ private static Map<Path, ChangedFile> toChangedFilesByPathMap(Collection<DefaultInputFile> 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<ChangedFile> changedFiles;
+ private final Map<Path, ChangedFile> changedFilesByPath;
public ScmChangedFiles(@Nullable Collection<ChangedFile> 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<ChangedFile> findFile(Path absoluteFilePath) {
- Predicate<ChangedFile> isTargetFile = file -> file.getAbsolutFilePath().equals(absoluteFilePath);
+ private Optional<ChangedFile> getChangedFile(Path absoluteFilePath) {
+ return Optional.ofNullable(changedFilesByPath.get(absoluteFilePath));
+ }
- return Optional.ofNullable(this.get())
- .orElseGet(List::of)
- .stream()
- .filter(isTargetFile)
- .findFirst();
+ private static Map<Path, ChangedFile> toChangedFilesByPathMap(@Nullable Collection<ChangedFile> 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<ChangedFile> changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir);
if (changedFiles != null) {
- validatePaths(getFilePaths(changedFiles));
+ validatePaths(getAbsoluteFilePaths(changedFiles));
}
return new ScmChangedFiles(changedFiles);
}
private static void validatePaths(Set<Path> 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<Path> getFilePaths(Collection<ChangedFile> changedFiles) {
+ private static Set<Path> getAbsoluteFilePaths(Collection<ChangedFile> changedFiles) {
return changedFiles
.stream()
.map(ChangedFile::getAbsolutFilePath)
- .collect(Collectors.toSet());
+ .collect(toSet());
}
@CheckForNull
private static Collection<ChangedFile> 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<ChangedFile> changedFiles = scmProvider.branchModifiedFiles(targetBranchName, rootBaseDir);
+ Collection<ChangedFile> 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<ChangedFile> 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<ChangedFile> toChangedFiles(@Nullable Set<Path> changedPaths) {
+ if (changedPaths == null) {
+ return null;
+ }
+
+ return changedPaths
+ .stream()
+ .map(ChangedFile::of)
+ .collect(toSet());
+ }
}