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.8KB

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