You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScmChangedFilesProvider.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scm;
  21. import java.nio.file.Path;
  22. import java.util.Collection;
  23. import javax.annotation.CheckForNull;
  24. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  25. import org.sonar.api.batch.scm.ScmProvider;
  26. import org.sonar.api.impl.utils.ScannerUtils;
  27. import org.sonar.api.utils.log.Logger;
  28. import org.sonar.api.utils.log.Loggers;
  29. import org.sonar.api.utils.log.Profiler;
  30. import org.sonar.scanner.scan.branch.BranchConfiguration;
  31. import org.springframework.context.annotation.Bean;
  32. public class ScmChangedFilesProvider {
  33. private static final Logger LOG = Loggers.get(ScmChangedFilesProvider.class);
  34. private static final String LOG_MSG = "SCM collecting changed files in the branch";
  35. @Bean("ScmChangedFiles")
  36. public ScmChangedFiles provide(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, DefaultInputProject project) {
  37. Path rootBaseDir = project.getBaseDir();
  38. Collection<Path> changedFiles = loadChangedFilesIfNeeded(scmConfiguration, branchConfiguration, rootBaseDir);
  39. validatePaths(changedFiles);
  40. return new ScmChangedFiles(changedFiles);
  41. }
  42. private static void validatePaths(@javax.annotation.Nullable Collection<Path> paths) {
  43. if (paths != null && paths.stream().anyMatch(p -> !p.isAbsolute())) {
  44. throw new IllegalStateException("SCM provider returned a changed file with a relative path but paths must be absolute. Please fix the provider.");
  45. }
  46. }
  47. @CheckForNull
  48. private static Collection<Path> loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) {
  49. final String targetBranchName = branchConfiguration.targetBranchName();
  50. if (branchConfiguration.isPullRequest() && targetBranchName != null) {
  51. ScmProvider scmProvider = scmConfiguration.provider();
  52. if (scmProvider != null) {
  53. Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
  54. Collection<Path> changedFiles = scmProvider.branchChangedFiles(targetBranchName, rootBaseDir);
  55. profiler.stopInfo();
  56. if (changedFiles != null) {
  57. LOG.debug("SCM reported {} {} changed in the branch", changedFiles.size(), ScannerUtils.pluralize("file", changedFiles.size()));
  58. return changedFiles;
  59. }
  60. }
  61. LOG.debug("SCM information about changed files in the branch is not available");
  62. }
  63. return null;
  64. }
  65. }