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.

ScmPublisher.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.util.LinkedList;
  22. import java.util.List;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.CoreProperties;
  25. import org.sonar.api.batch.fs.FileSystem;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.fs.InputFile.Status;
  28. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  29. import org.sonar.api.batch.scm.ScmProvider;
  30. import org.sonar.api.notifications.AnalysisWarnings;
  31. import org.sonar.api.utils.log.Logger;
  32. import org.sonar.api.utils.log.Loggers;
  33. import org.sonar.scanner.protocol.output.ScannerReport;
  34. import org.sonar.scanner.protocol.output.ScannerReport.Changesets.Builder;
  35. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  36. import org.sonar.scanner.report.ReportPublisher;
  37. import org.sonar.scanner.repository.FileData;
  38. import org.sonar.scanner.repository.ProjectRepositories;
  39. import org.sonar.scanner.scan.branch.BranchConfiguration;
  40. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  41. public final class ScmPublisher {
  42. private static final Logger LOG = Loggers.get(ScmPublisher.class);
  43. private final ScmConfiguration configuration;
  44. private final ProjectRepositories projectRepositories;
  45. private final InputComponentStore componentStore;
  46. private final FileSystem fs;
  47. private final ScannerReportWriter writer;
  48. private AnalysisWarnings analysisWarnings;
  49. private final BranchConfiguration branchConfiguration;
  50. public ScmPublisher(ScmConfiguration configuration, ProjectRepositories projectRepositories, InputComponentStore componentStore, FileSystem fs,
  51. ReportPublisher reportPublisher, BranchConfiguration branchConfiguration, AnalysisWarnings analysisWarnings) {
  52. this.configuration = configuration;
  53. this.projectRepositories = projectRepositories;
  54. this.componentStore = componentStore;
  55. this.fs = fs;
  56. this.branchConfiguration = branchConfiguration;
  57. this.writer = reportPublisher.getWriter();
  58. this.analysisWarnings = analysisWarnings;
  59. }
  60. public void publish() {
  61. if (configuration.isDisabled()) {
  62. LOG.info("SCM Publisher is disabled");
  63. return;
  64. }
  65. ScmProvider provider = configuration.provider();
  66. if (provider == null) {
  67. LOG.info("SCM Publisher No SCM system was detected. You can use the '" + CoreProperties.SCM_PROVIDER_KEY + "' property to explicitly specify it.");
  68. return;
  69. }
  70. List<InputFile> filesToBlame = collectFilesToBlame(writer);
  71. if (!filesToBlame.isEmpty()) {
  72. String key = provider.key();
  73. LOG.info("SCM Publisher SCM provider for this project is: " + key);
  74. DefaultBlameOutput output = new DefaultBlameOutput(writer, analysisWarnings, filesToBlame);
  75. try {
  76. provider.blameCommand().blame(new DefaultBlameInput(fs, filesToBlame), output);
  77. } catch (Exception e) {
  78. output.finish(false);
  79. throw e;
  80. }
  81. output.finish(true);
  82. }
  83. }
  84. private List<InputFile> collectFilesToBlame(ScannerReportWriter writer) {
  85. if (configuration.forceReloadAll()) {
  86. LOG.warn("Forced reloading of SCM data for all files.");
  87. }
  88. List<InputFile> filesToBlame = new LinkedList<>();
  89. for (DefaultInputFile f : componentStore.allFilesToPublish()) {
  90. if (configuration.forceReloadAll() || f.status() != Status.SAME) {
  91. addIfNotEmpty(filesToBlame, f);
  92. } else if (!branchConfiguration.isPullRequest()) {
  93. FileData fileData = projectRepositories.fileData(componentStore.findModule(f).key(), f);
  94. if (fileData == null || StringUtils.isEmpty(fileData.revision())) {
  95. addIfNotEmpty(filesToBlame, f);
  96. } else {
  97. askToCopyDataFromPreviousAnalysis(f, writer);
  98. }
  99. }
  100. }
  101. return filesToBlame;
  102. }
  103. private static void askToCopyDataFromPreviousAnalysis(DefaultInputFile f, ScannerReportWriter writer) {
  104. Builder scmBuilder = ScannerReport.Changesets.newBuilder();
  105. scmBuilder.setComponentRef(f.scannerId());
  106. scmBuilder.setCopyFromPrevious(true);
  107. writer.writeComponentChangesets(scmBuilder.build());
  108. }
  109. private static void addIfNotEmpty(List<InputFile> filesToBlame, InputFile f) {
  110. if (!f.isEmpty()) {
  111. filesToBlame.add(f);
  112. }
  113. }
  114. }