From 81638702d68033ca95ddd406aa655fb2bfdab036 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 1 Apr 2019 11:28:19 +0200 Subject: SONAR-11856 Store the "true" target branch in the scanner report to display it properly --- .../scanner/report/ChangedLinesPublisher.java | 6 +-- .../sonar/scanner/report/MetadataPublisher.java | 4 ++ .../repository/DefaultQualityProfileLoader.java | 15 +++--- .../repository/ProjectRepositoriesProvider.java | 43 ------------------ .../repository/ProjectRepositoriesSupplier.java | 53 ++++++++++++++++++++++ .../scanner/repository/QualityProfileLoader.java | 2 - .../repository/QualityProfilesProvider.java | 12 +---- .../sonar/scanner/scan/ProjectScanContainer.java | 6 +-- .../scanner/scan/branch/BranchConfiguration.java | 15 +++--- .../scan/branch/DefaultBranchConfiguration.java | 2 +- .../scanner/scan/filesystem/StatusDetection.java | 29 +++++++----- .../org/sonar/scanner/scm/ScmChangedFiles.java | 14 +++++- .../sonar/scanner/scm/ScmChangedFilesProvider.java | 24 ++++------ .../java/org/sonar/scanner/scm/ScmPublisher.java | 17 ++++--- 14 files changed, 130 insertions(+), 112 deletions(-) delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesSupplier.java (limited to 'sonar-scanner-engine/src/main') 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 8bc978109b6..ff393c42773 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 @@ -57,8 +57,8 @@ public class ChangedLinesPublisher implements ReportPublisherStep { @Override public void publish(ScannerReportWriter writer) { - String targetScmBranch = branchConfiguration.targetScmBranch(); - if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetScmBranch == null) { + String targetBranchName = branchConfiguration.targetBranchName(); + if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetBranchName == null) { return; } @@ -68,7 +68,7 @@ public class ChangedLinesPublisher implements ReportPublisherStep { } Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - int count = writeChangedLines(provider, writer, targetScmBranch); + int count = writeChangedLines(provider, writer, targetBranchName); LOG.debug("SCM reported changed lines for {} {} in the branch", count, ScannerUtils.pluralize("file", count)); profiler.stopInfo(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java index 69891747295..9bf40f4a94e 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java @@ -160,6 +160,10 @@ public class MetadataPublisher implements ReportPublisherStep { if (referenceBranch != null) { builder.setMergeBranchName(referenceBranch); } + String targetBranchName = branchConfiguration.targetBranchName(); + if (targetBranchName != null) { + builder.setTargetBranchName(targetBranchName); + } if (branchType == BranchType.PULL_REQUEST) { builder.setPullRequestKey(branchConfiguration.pullRequestKey()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java index 6c3c2f93da2..361eeab3e6d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java @@ -50,24 +50,27 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { this.wsClient = wsClient; } - @Override - public List loadDefault() { + private List loadDefault() { StringBuilder url = new StringBuilder(WS_URL + "?defaults=true"); - return handleErrors(url, () -> "Failed to load the default quality profiles"); + return handleErrors(url, () -> "Failed to load the default quality profiles", false); } @Override public List load(String projectKey) { StringBuilder url = new StringBuilder(WS_URL + "?projectKey=").append(encodeForUrl(projectKey)); - return handleErrors(url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey)); + return handleErrors(url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey), true); } - private List handleErrors(StringBuilder url, Supplier errorMsg) { + private List handleErrors(StringBuilder url, Supplier errorMsg, boolean tryLoadDefault) { try { return doLoad(url); } catch (HttpException e) { if (e.code() == 404) { - throw MessageException.of(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); + if (tryLoadDefault) { + return loadDefault(); + } else { + throw MessageException.of(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); + } } throw new IllegalStateException(errorMsg.get() + ": " + ScannerWsClient.createErrorMessage(e)); } catch (MessageException e) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java deleted file mode 100644 index d2f7bfeb1af..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 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.repository; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.scanner.bootstrap.ProcessedScannerProperties; -import org.sonar.scanner.scan.branch.BranchConfiguration; - -public class ProjectRepositoriesProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(ProjectRepositoriesProvider.class); - private static final String LOG_MSG = "Load project repositories"; - private ProjectRepositories project = null; - - public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProcessedScannerProperties scannerProperties, BranchConfiguration branchConfig) { - if (project == null) { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - project = loader.load(scannerProperties.getKeyWithBranch(), branchConfig.longLivingSonarReferenceBranch()); - profiler.stopInfo(); - } - - return project; - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesSupplier.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesSupplier.java new file mode 100644 index 00000000000..f0fda872182 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesSupplier.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.repository; + +import java.util.function.Supplier; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; +import org.sonar.scanner.scan.branch.BranchConfiguration; + +public class ProjectRepositoriesSupplier implements Supplier { + private static final Logger LOG = Loggers.get(ProjectRepositoriesSupplier.class); + private static final String LOG_MSG = "Load project repositories"; + private final ProjectRepositoriesLoader loader; + private final ProcessedScannerProperties scannerProperties; + private final BranchConfiguration branchConfig; + + private ProjectRepositories project = null; + + public ProjectRepositoriesSupplier(ProjectRepositoriesLoader loader, ProcessedScannerProperties scannerProperties, BranchConfiguration branchConfig) { + this.loader = loader; + this.scannerProperties = scannerProperties; + this.branchConfig = branchConfig; + } + + public ProjectRepositories get() { + if (project == null) { + Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); + project = loader.load(scannerProperties.getKeyWithBranch(), branchConfig.longLivingSonarReferenceBranch()); + profiler.stopInfo(); + } + + return project; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileLoader.java index 202cc6389e1..df17a1f2247 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileLoader.java @@ -24,6 +24,4 @@ import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; public interface QualityProfileLoader { List load(String projectKey); - - List loadDefault(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java index 267d0c01729..e78e4d7457f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java @@ -19,31 +19,23 @@ */ package org.sonar.scanner.repository; -import java.util.List; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.scanner.bootstrap.ProcessedScannerProperties; import org.sonar.scanner.rule.QualityProfiles; -import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; public class QualityProfilesProvider extends ProviderAdapter { private static final Logger LOG = Loggers.get(QualityProfilesProvider.class); private static final String LOG_MSG = "Load quality profiles"; private QualityProfiles profiles = null; - public QualityProfiles provide(QualityProfileLoader loader, ProjectRepositories projectRepositories, ProcessedScannerProperties props) { + public QualityProfiles provide(QualityProfileLoader loader, ProcessedScannerProperties props) { if (this.profiles == null) { - List profileList; Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - if (!projectRepositories.exists()) { - profileList = loader.loadDefault(); - } else { - profileList = loader.load(props.getKeyWithBranch()); - } + profiles = new QualityProfiles(loader.load(props.getKeyWithBranch())); profiler.stopInfo(); - profiles = new QualityProfiles(profileList); } return profiles; 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 dc242a2b4bd..548a477c7d0 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 @@ -77,7 +77,7 @@ import org.sonar.scanner.repository.ContextPropertiesCache; import org.sonar.scanner.repository.DefaultProjectRepositoriesLoader; import org.sonar.scanner.repository.DefaultQualityProfileLoader; import org.sonar.scanner.repository.ProjectRepositoriesLoader; -import org.sonar.scanner.repository.ProjectRepositoriesProvider; +import org.sonar.scanner.repository.ProjectRepositoriesSupplier; import org.sonar.scanner.repository.QualityProfileLoader; import org.sonar.scanner.repository.QualityProfilesProvider; import org.sonar.scanner.repository.language.DefaultLanguagesRepository; @@ -157,7 +157,7 @@ public class ProjectScanContainer extends ComponentContainer { new ProjectBranchesProvider(), new ProjectPullRequestsProvider(), DefaultAnalysisMode.class, - new ProjectRepositoriesProvider(), + ProjectRepositoriesSupplier.class, new ProjectServerSettingsProvider(), // temp @@ -304,7 +304,7 @@ public class ProjectScanContainer extends ComponentContainer { BranchConfiguration branchConfig = getComponentByType(BranchConfiguration.class); if (branchConfig.branchType() == BranchType.PULL_REQUEST) { - LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetScmBranch()), branchConfig.branchName()); + LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetBranchName()), branchConfig.branchName()); } else if (branchConfig.branchName() != null) { LOG.info("Branch name: {}, type: {}", branchConfig.branchName(), branchTypeToDisplayName(branchConfig.branchType())); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java index 87dedb54d11..d3f51049c0d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java @@ -27,7 +27,6 @@ public interface BranchConfiguration { /** * The type of the branch we're on, determined by: - * * - If the specified branch exists on the server, then its type * - If the branch name matches the pattern of long-lived branches, then it's long-lived * - Otherwise it's short-lived @@ -43,7 +42,8 @@ public interface BranchConfiguration { /** * For long/short living branches, this is the value of sonar.branch.name, and fallback on the default branch name configured in SQ * For PR: the name of the branch containing PR changes (sonar.pullrequest.branch) - * Only @null if the branch feature is not available. + * + * @return null if the branch feature is not available or no branch was specified. */ @CheckForNull String branchName(); @@ -56,17 +56,20 @@ public interface BranchConfiguration { * transitively use its own target. * For PR, we look at sonar.pullrequest.base (default to default branch). If it exists but is a short living branch or PR, we will * transitively use its own target. If base is not analyzed, we will use default branch. - * Only @null if the branch feature is not available. + * + * @return null if the branch feature is not available or no branch was specified. */ @CheckForNull String longLivingSonarReferenceBranch(); /** - * Raw value of sonar.branch.target or sonar.pullrequest.base (fallback to the default branch), will be used by the SCM to compute changed files and changed lines. - * @null for long living branches and if the branch feature is not available + * Raw value of sonar.branch.target or sonar.pullrequest.base (fallback to the default branch). + * In the scanner side, it will be used by the SCM to compute changed files and changed lines. + * + * @return null if the branch feature is not available, the branch being analyzed is the main branch or no branch was specified. */ @CheckForNull - String targetScmBranch(); + String targetBranchName(); /** * The key of the pull request. diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java index 436afec909e..7314597a828 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java @@ -37,7 +37,7 @@ public class DefaultBranchConfiguration implements BranchConfiguration { @CheckForNull @Override - public String targetScmBranch() { + public String targetBranchName() { return null; } 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 8f33e02ea2c..09561999935 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 @@ -24,7 +24,7 @@ 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.repository.ProjectRepositoriesSupplier; import org.sonar.scanner.scm.ScmChangedFiles; import static org.sonar.api.batch.fs.InputFile.Status.ADDED; @@ -33,33 +33,40 @@ import static org.sonar.api.batch.fs.InputFile.Status.SAME; @Immutable public class StatusDetection { - private final ProjectRepositories projectRepositories; + private final ProjectRepositoriesSupplier projectSettingsSupplier; private final ScmChangedFiles scmChangedFiles; - public StatusDetection(ProjectRepositories projectSettings, ScmChangedFiles scmChangedFiles) { - this.projectRepositories = projectSettings; + public StatusDetection(ProjectRepositoriesSupplier projectSettingsSupplier, ScmChangedFiles scmChangedFiles) { + this.projectSettingsSupplier = projectSettingsSupplier; this.scmChangedFiles = scmChangedFiles; } InputFile.Status status(String moduleKeyWithBranch, DefaultInputFile inputFile, String hash) { - FileData fileDataPerPath = projectRepositories.fileData(moduleKeyWithBranch, inputFile); + if (scmChangedFiles.isValid()) { + return checkChangedWithScm(inputFile); + } + return checkChangedWithProjectRepositories(moduleKeyWithBranch, inputFile, hash); + } + + private InputFile.Status checkChangedWithProjectRepositories(String moduleKeyWithBranch, DefaultInputFile inputFile, String hash) { + FileData fileDataPerPath = projectSettingsSupplier.get().fileData(moduleKeyWithBranch, inputFile); if (fileDataPerPath == null) { - return checkChanged(ADDED, inputFile); + return ADDED; } String previousHash = fileDataPerPath.hash(); if (StringUtils.equals(hash, previousHash)) { return SAME; } if (StringUtils.isEmpty(previousHash)) { - return checkChanged(ADDED, inputFile); + return ADDED; } - return checkChanged(CHANGED, inputFile); + return CHANGED; } - private InputFile.Status checkChanged(InputFile.Status status, DefaultInputFile inputFile) { - if (!scmChangedFiles.verifyChanged(inputFile.path())) { + private InputFile.Status checkChangedWithScm(DefaultInputFile inputFile) { + if (!scmChangedFiles.isChanged(inputFile.path())) { return SAME; } - return status; + return CHANGED; } } 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 dfb078ecccb..380c65c32a2 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,6 +21,7 @@ package org.sonar.scanner.scm; import java.nio.file.Path; import java.util.Collection; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -33,10 +34,19 @@ public class ScmChangedFiles { this.fileCollection = changedFiles; } - public boolean verifyChanged(Path file) { - return fileCollection == null || fileCollection.contains(file); + public boolean isChanged(Path file) { + if (!isValid()) { + throw new IllegalStateException("Scm didn't provide valid data"); + } + + return fileCollection.contains(file); + } + + public boolean isValid() { + return fileCollection != null; } + @CheckForNull Collection 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 index 63f30c85f2c..b7fd4c8bf76 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,7 +22,6 @@ 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.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.scm.ScmProvider; @@ -38,19 +37,12 @@ public class ScmChangedFilesProvider extends ProviderAdapter { private ScmChangedFiles scmBranchChangedFiles; - /* - * ScmConfiguration is not available in issues mode - */ - public ScmChangedFiles provide(@Nullable ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, DefaultInputProject project) { + public ScmChangedFiles provide(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, DefaultInputProject project) { if (scmBranchChangedFiles == null) { - if (scmConfiguration == null) { - scmBranchChangedFiles = new ScmChangedFiles(null); - } else { - Path rootBaseDir = project.getBaseDir(); - Collection changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); - validatePaths(changedFiles); - scmBranchChangedFiles = new ScmChangedFiles(changedFiles); - } + Path rootBaseDir = project.getBaseDir(); + Collection changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir); + validatePaths(changedFiles); + scmBranchChangedFiles = new ScmChangedFiles(changedFiles); } return scmBranchChangedFiles; } @@ -63,12 +55,12 @@ public class ScmChangedFilesProvider extends ProviderAdapter { @CheckForNull private static Collection loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) { - String targetScmBranch = branchConfiguration.targetScmBranch(); - if (branchConfiguration.isShortOrPullRequest() && targetScmBranch != null) { + final String targetBranchName = branchConfiguration.targetBranchName(); + if (branchConfiguration.isShortOrPullRequest() && targetBranchName != null) { ScmProvider scmProvider = scmConfiguration.provider(); if (scmProvider != null) { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - Collection changedFiles = scmProvider.branchChangedFiles(targetScmBranch, rootBaseDir); + Collection changedFiles = scmProvider.branchChangedFiles(targetBranchName, rootBaseDir); profiler.stopInfo(); if (changedFiles != null) { LOG.debug("SCM reported {} {} changed in the branch", changedFiles.size(), ScannerUtils.pluralize("file", changedFiles.size())); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java index 4abb86427d5..819abc8adce 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java @@ -35,7 +35,7 @@ import org.sonar.scanner.protocol.output.ScannerReport.Changesets.Builder; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.ReportPublisher; import org.sonar.scanner.repository.FileData; -import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.repository.ProjectRepositoriesSupplier; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -44,16 +44,16 @@ public final class ScmPublisher { private static final Logger LOG = Loggers.get(ScmPublisher.class); private final ScmConfiguration configuration; - private final ProjectRepositories projectRepositories; + private final ProjectRepositoriesSupplier projectRepositoriesSupplier; private final InputComponentStore componentStore; private final FileSystem fs; private final ScannerReportWriter writer; private final BranchConfiguration branchConfiguration; - public ScmPublisher(ScmConfiguration configuration, ProjectRepositories projectRepositories, - InputComponentStore componentStore, FileSystem fs, ReportPublisher reportPublisher, BranchConfiguration branchConfiguration) { + public ScmPublisher(ScmConfiguration configuration, ProjectRepositoriesSupplier projectRepositoriesSupplier, + InputComponentStore componentStore, FileSystem fs, ReportPublisher reportPublisher, BranchConfiguration branchConfiguration) { this.configuration = configuration; - this.projectRepositories = projectRepositories; + this.projectRepositoriesSupplier = projectRepositoriesSupplier; this.componentStore = componentStore; this.fs = fs; this.branchConfiguration = branchConfiguration; @@ -96,12 +96,11 @@ public final class ScmPublisher { if (configuration.forceReloadAll() || f.status() != Status.SAME) { addIfNotEmpty(filesToBlame, f); } else if (!branchConfiguration.isShortOrPullRequest()) { - // File status is SAME so that mean fileData exists - FileData fileData = projectRepositories.fileData(componentStore.findModule(f).getKeyWithBranch(), f); - if (StringUtils.isEmpty(fileData.revision())) { + FileData fileData = projectRepositoriesSupplier.get().fileData(componentStore.findModule(f).getKeyWithBranch(), f); + if (fileData == null || StringUtils.isEmpty(fileData.revision())) { addIfNotEmpty(filesToBlame, f); } else { - askToCopyDataFromPreviousAnalysis((DefaultInputFile) f, writer); + askToCopyDataFromPreviousAnalysis(f, writer); } } } -- cgit v1.2.3