diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-19 17:25:57 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-28 09:14:43 +0200 |
commit | e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f (patch) | |
tree | 9091cd9ea21ae09da8666488724a9edc6a24f2a2 /sonar-scanner-engine/src/main/java | |
parent | 22c8ebe741fb7ff7c6f18f95c7b74aa0104f4620 (diff) | |
download | sonarqube-e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f.tar.gz sonarqube-e19f3f56fe2d8bc2101086b166ec4de92a5c3c8f.zip |
SONAR-9837 Detect files changed in the current branch with SCM
Diffstat (limited to 'sonar-scanner-engine/src/main/java')
8 files changed, 144 insertions, 13 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index b9c7d779257..14e403f2a01 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -119,7 +119,6 @@ public class ModuleScanContainer extends ComponentContainer { ExclusionFilters.class, new MetadataGeneratorProvider(), FileMetadata.class, - StatusDetectionFactory.class, LanguageDetection.class, FileIndexer.class, InputFileBuilder.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 7deb3166880..fc34c441582 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -93,9 +93,11 @@ import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.branch.ProjectBranchesProvider; import org.sonar.scanner.scan.filesystem.BatchIdGenerator; import org.sonar.scanner.scan.filesystem.InputComponentStoreProvider; +import org.sonar.scanner.scan.filesystem.StatusDetectionFactory; import org.sonar.scanner.scan.measure.DefaultMetricFinder; import org.sonar.scanner.scan.measure.DeprecatedMetricFinder; import org.sonar.scanner.scan.measure.MeasureCache; +import org.sonar.scanner.scm.ScmChangedFilesProvider; import org.sonar.scanner.storage.Storages; public class ProjectScanContainer extends ComponentContainer { @@ -157,6 +159,8 @@ public class ProjectScanContainer extends ComponentContainer { new InputModuleHierarchyProvider(), DefaultComponentTree.class, BatchIdGenerator.class, + new ScmChangedFilesProvider(), + StatusDetectionFactory.class, // rules new ActiveRulesProvider(), 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 f0bfe220d08..3ad3c9960ad 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 @@ -62,15 +62,15 @@ class MetadataGenerator { if (charsetDetector.run()) { charset = charsetDetector.charset(); } else { - LOG.debug("Failed to detect a valid charset for file '{}'. Using default charset.", inputFile.relativePath()); + LOG.debug("Failed to detect a valid charset for file '{}'. Using default charset.", inputFile); charset = defaultEncoding; } InputStream is = charsetDetector.inputStream(); inputFile.setCharset(charset); Metadata metadata = fileMetadata.readMetadata(is, charset, inputFile.absolutePath(), exclusionsScanner.createCharHandlerFor(inputFile.key())); inputFile.setMetadata(metadata); - inputFile.setStatus(statusDetection.status(inputModule.definition().getKeyWithBranch(), inputFile.relativePath(), metadata.hash())); - LOG.debug("'{}' generated metadata {} with charset '{}'", inputFile.relativePath(), inputFile.type() == Type.TEST ? "as test " : "", charset); + inputFile.setStatus(statusDetection.status(inputModule.definition().getKeyWithBranch(), 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 79f3576e67b..b91a8165734 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 @@ -20,23 +20,26 @@ package org.sonar.scanner.scan.filesystem; import javax.annotation.concurrent.Immutable; - import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.scanner.repository.FileData; import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scm.ScmChangedFiles; @Immutable class StatusDetection { private final ProjectRepositories projectRepositories; + private final ScmChangedFiles scmChangedFiles; - StatusDetection(ProjectRepositories projectSettings) { + StatusDetection(ProjectRepositories projectSettings, ScmChangedFiles scmChangedFiles) { this.projectRepositories = projectSettings; + this.scmChangedFiles = scmChangedFiles; } - InputFile.Status status(String projectKeyWithBranch, String relativePath, String hash) { - FileData fileDataPerPath = projectRepositories.fileData(projectKeyWithBranch, relativePath); + InputFile.Status status(String projectKeyWithBranch, DefaultInputFile inputFile, String hash) { + FileData fileDataPerPath = projectRepositories.fileData(projectKeyWithBranch, inputFile.relativePath()); if (fileDataPerPath == null) { return InputFile.Status.ADDED; } @@ -47,6 +50,9 @@ class StatusDetection { if (StringUtils.isEmpty(previousHash)) { return InputFile.Status.ADDED; } + if (!scmChangedFiles.confirmChanged(inputFile.path())) { + return InputFile.Status.SAME; + } return InputFile.Status.CHANGED; } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactory.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactory.java index e4eabe778d4..b1cdf614e6b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactory.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetectionFactory.java @@ -21,17 +21,20 @@ package org.sonar.scanner.scan.filesystem; import org.sonar.api.batch.ScannerSide; import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scm.ScmChangedFiles; @ScannerSide public class StatusDetectionFactory { private final ProjectRepositories projectReferentials; + private final ScmChangedFiles scmChangedFiles; - public StatusDetectionFactory(ProjectRepositories projectReferentials) { + public StatusDetectionFactory(ProjectRepositories projectReferentials, ScmChangedFiles scmChangedFiles) { this.projectReferentials = projectReferentials; + this.scmChangedFiles = scmChangedFiles; } StatusDetection create() { - return new StatusDetection(projectReferentials); + return new StatusDetection(projectReferentials, scmChangedFiles); } } 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 new file mode 100644 index 00000000000..d4dcdd9ac00 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFiles.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scm; + +import java.nio.file.Path; +import java.util.Collection; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +@Immutable +public class ScmChangedFiles { + @Nullable + private final Collection<Path> fileCollection; + + public ScmChangedFiles(@Nullable Collection<Path> changedFiles) { + this.fileCollection = changedFiles; + } + + public boolean verifyChanged(Path file) { + return fileCollection == null || fileCollection.contains(file); + } + + Collection<Path> get() { + return fileCollection; + } +} 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 new file mode 100644 index 00000000000..60b87a1fc0a --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scm; + +import java.nio.file.Path; +import java.util.Collection; +import javax.annotation.CheckForNull; +import org.picocontainer.annotations.Nullable; +import org.picocontainer.injectors.ProviderAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.scm.ScmBranchProvider; +import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.scanner.scan.branch.BranchConfiguration; + +public class ScmChangedFilesProvider extends ProviderAdapter { + private static final Logger LOG = LoggerFactory.getLogger(ScmChangedFilesProvider.class); + + private ScmChangedFiles scmBranchChangedFiles; + + /* + * ScmConfiguration is not available in issues mode + */ + public ScmChangedFiles provide(@Nullable ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, InputModuleHierarchy inputModuleHierarchy) { + if (scmBranchChangedFiles == null) { + if (scmConfiguration == null) { + scmBranchChangedFiles = new ScmChangedFiles(null); + } else { + Path rootBaseDir = inputModuleHierarchy.root().getBaseDir(); + Collection<Path> changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); + scmBranchChangedFiles = new ScmChangedFiles(changedFiles); + } + } + return scmBranchChangedFiles; + } + + @CheckForNull + private static Collection<Path> loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) { + if (branchConfiguration.isShortLivingBranch()) { + ScmProvider scmProvider = scmConfiguration.provider(); + if (scmProvider != null && (scmProvider instanceof ScmBranchProvider)) { + ScmBranchProvider scmBranchProvider = (ScmBranchProvider) scmProvider; + Collection<Path> changedFiles = scmBranchProvider.branchChangedFiles(branchConfiguration.branchTarget(), rootBaseDir); + if (changedFiles != null) { + LOG.debug("SCM reported {} files changed in the branch", changedFiles.size()); + return changedFiles; + } + } + + LOG.debug("SCM information about changed files in the branch is not available"); + } + return null; + } + +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmConfiguration.java index ef380a0c74e..f34abc0eba6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmConfiguration.java @@ -19,9 +19,10 @@ */ package org.sonar.scanner.scm; -import com.google.common.base.Joiner; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Collectors; +import javax.annotation.CheckForNull; import org.apache.commons.lang.StringUtils; import org.picocontainer.Startable; import org.sonar.api.CoreProperties; @@ -51,7 +52,7 @@ import org.sonar.api.utils.log.Loggers; }) @InstantiationStrategy(InstantiationStrategy.PER_BATCH) @ScannerSide -public final class ScmConfiguration implements Startable { +public class ScmConfiguration implements Startable { private static final Logger LOG = Loggers.get(ScmConfiguration.class); public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll"; @@ -103,7 +104,8 @@ public final class ScmConfiguration implements Startable { if (providerPerKey.containsKey(forcedProviderKey)) { this.provider = providerPerKey.get(forcedProviderKey); } else { - String supportedProviders = providerPerKey.isEmpty() ? "No SCM provider installed" : ("Supported SCM providers are " + Joiner.on(",").join(providerPerKey.keySet())); + String supportedProviders = providerPerKey.isEmpty() ? "No SCM provider installed" + : ("Supported SCM providers are " + providerPerKey.keySet().stream().collect(Collectors.joining(","))); throw new IllegalArgumentException("SCM provider was set to \"" + forcedProviderKey + "\" but no SCM provider found for this key. " + supportedProviders); } } @@ -132,6 +134,7 @@ public final class ScmConfiguration implements Startable { } } + @CheckForNull public ScmProvider provider() { return provider; } |