您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MetadataPublisher.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.report;
  21. import java.io.File;
  22. import java.nio.file.Path;
  23. import java.time.Instant;
  24. import java.util.LinkedList;
  25. import java.util.Map.Entry;
  26. import java.util.regex.Pattern;
  27. import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
  28. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  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.ProjectInfo;
  33. import org.sonar.scanner.bootstrap.ScannerPlugin;
  34. import org.sonar.scanner.bootstrap.ScannerPluginRepository;
  35. import org.sonar.scanner.cpd.CpdSettings;
  36. import org.sonar.scanner.fs.InputModuleHierarchy;
  37. import org.sonar.scanner.protocol.output.ScannerReport;
  38. import org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType;
  39. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  40. import org.sonar.scanner.repository.ForkDateSupplier;
  41. import org.sonar.scanner.rule.QProfile;
  42. import org.sonar.scanner.rule.QualityProfiles;
  43. import org.sonar.scanner.scan.ScanProperties;
  44. import org.sonar.scanner.scan.branch.BranchConfiguration;
  45. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  46. import org.sonar.scanner.scm.ScmConfiguration;
  47. import org.sonar.scanner.scm.ScmRevision;
  48. public class MetadataPublisher implements ReportPublisherStep {
  49. private static final Logger LOG = Loggers.get(MetadataPublisher.class);
  50. private final ScanProperties properties;
  51. private final QualityProfiles qProfiles;
  52. private final ProjectInfo projectInfo;
  53. private final InputModuleHierarchy moduleHierarchy;
  54. private final CpdSettings cpdSettings;
  55. private final ScannerPluginRepository pluginRepository;
  56. private final BranchConfiguration branchConfiguration;
  57. private final ScmRevision scmRevision;
  58. private final ForkDateSupplier forkDateSupplier;
  59. private final InputComponentStore componentStore;
  60. private final ScmConfiguration scmConfiguration;
  61. public MetadataPublisher(ProjectInfo projectInfo, InputModuleHierarchy moduleHierarchy, ScanProperties properties,
  62. QualityProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration,
  63. ScmRevision scmRevision, ForkDateSupplier forkDateSupplier, InputComponentStore componentStore, ScmConfiguration scmConfiguration) {
  64. this.projectInfo = projectInfo;
  65. this.moduleHierarchy = moduleHierarchy;
  66. this.properties = properties;
  67. this.qProfiles = qProfiles;
  68. this.cpdSettings = cpdSettings;
  69. this.pluginRepository = pluginRepository;
  70. this.branchConfiguration = branchConfiguration;
  71. this.scmRevision = scmRevision;
  72. this.forkDateSupplier = forkDateSupplier;
  73. this.componentStore = componentStore;
  74. this.scmConfiguration = scmConfiguration;
  75. }
  76. @Override
  77. public void publish(ScannerReportWriter writer) {
  78. AbstractProjectOrModule rootProject = moduleHierarchy.root();
  79. ScannerReport.Metadata.Builder builder = ScannerReport.Metadata.newBuilder()
  80. .setAnalysisDate(projectInfo.getAnalysisDate().getTime())
  81. // Here we want key without branch
  82. .setProjectKey(rootProject.key())
  83. .setCrossProjectDuplicationActivated(cpdSettings.isCrossProjectDuplicationEnabled())
  84. .setRootComponentRef(rootProject.scannerId());
  85. projectInfo.getProjectVersion().ifPresent(builder::setProjectVersion);
  86. projectInfo.getBuildString().ifPresent(builder::setBuildString);
  87. properties.organizationKey().ifPresent(builder::setOrganizationKey);
  88. if (branchConfiguration.branchName() != null) {
  89. addBranchInformation(builder);
  90. }
  91. addScmInformation(builder);
  92. addForkPoint(builder);
  93. addNotAnalyzedFileCountsByLanguage(builder);
  94. for (QProfile qp : qProfiles.findAll()) {
  95. builder.putQprofilesPerLanguage(qp.getLanguage(), ScannerReport.Metadata.QProfile.newBuilder()
  96. .setKey(qp.getKey())
  97. .setLanguage(qp.getLanguage())
  98. .setName(qp.getName())
  99. .setRulesUpdatedAt(qp.getRulesUpdatedAt().getTime()).build());
  100. }
  101. for (Entry<String, ScannerPlugin> pluginEntry : pluginRepository.getPluginsByKey().entrySet()) {
  102. builder.putPluginsByKey(pluginEntry.getKey(), ScannerReport.Metadata.Plugin.newBuilder()
  103. .setKey(pluginEntry.getKey())
  104. .setUpdatedAt(pluginEntry.getValue().getUpdatedAt()).build());
  105. }
  106. addModulesRelativePaths(builder);
  107. writer.writeMetadata(builder.build());
  108. }
  109. private void addForkPoint(ScannerReport.Metadata.Builder builder) {
  110. Instant date = forkDateSupplier.get();
  111. if (date != null) {
  112. builder.setForkDate(date.toEpochMilli());
  113. }
  114. }
  115. private void addModulesRelativePaths(ScannerReport.Metadata.Builder builder) {
  116. LinkedList<DefaultInputModule> queue = new LinkedList<>();
  117. queue.add(moduleHierarchy.root());
  118. while (!queue.isEmpty()) {
  119. DefaultInputModule module = queue.removeFirst();
  120. queue.addAll(moduleHierarchy.children(module));
  121. String relativePath = moduleHierarchy.relativePathToRoot(module);
  122. if (relativePath != null) {
  123. builder.putModulesProjectRelativePathByKey(module.key(), relativePath);
  124. }
  125. }
  126. ScmProvider scmProvider = scmConfiguration.provider();
  127. if (scmProvider == null) {
  128. return;
  129. }
  130. Path projectBasedir = moduleHierarchy.root().getBaseDir();
  131. try {
  132. builder.setRelativePathFromScmRoot(toSonarQubePath(scmProvider.relativePathFromScmRoot(projectBasedir)));
  133. } catch (UnsupportedOperationException e) {
  134. LOG.debug(e.getMessage());
  135. }
  136. }
  137. private void addScmInformation(ScannerReport.Metadata.Builder builder) {
  138. try {
  139. scmRevision.get().ifPresent(revisionId -> {
  140. LOG.debug("SCM revision ID '{}'", revisionId);
  141. builder.setScmRevisionId(revisionId);
  142. });
  143. } catch (UnsupportedOperationException e) {
  144. LOG.debug(e.getMessage());
  145. }
  146. }
  147. private void addNotAnalyzedFileCountsByLanguage(ScannerReport.Metadata.Builder builder) {
  148. builder.putAllNotAnalyzedFilesByLanguage(componentStore.getNotAnalysedFilesByLanguage());
  149. }
  150. private void addBranchInformation(ScannerReport.Metadata.Builder builder) {
  151. builder.setBranchName(branchConfiguration.branchName());
  152. BranchType branchType = toProtobufBranchType(branchConfiguration.branchType());
  153. builder.setBranchType(branchType);
  154. String referenceBranch = branchConfiguration.referenceBranchName();
  155. if (referenceBranch != null) {
  156. builder.setReferenceBranchName(referenceBranch);
  157. }
  158. String targetBranchName = branchConfiguration.targetBranchName();
  159. if (targetBranchName != null) {
  160. builder.setTargetBranchName(targetBranchName);
  161. }
  162. if (branchType == BranchType.PULL_REQUEST) {
  163. builder.setPullRequestKey(branchConfiguration.pullRequestKey());
  164. }
  165. }
  166. private static BranchType toProtobufBranchType(org.sonar.scanner.scan.branch.BranchType branchType) {
  167. if (branchType == org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST) {
  168. return BranchType.PULL_REQUEST;
  169. }
  170. return BranchType.BRANCH;
  171. }
  172. private static String toSonarQubePath(Path path) {
  173. String pathAsString = path.toString();
  174. char sonarQubeSeparatorChar = '/';
  175. if (File.separatorChar != sonarQubeSeparatorChar) {
  176. return pathAsString.replaceAll(Pattern.quote(File.separator), String.valueOf(sonarQubeSeparatorChar));
  177. }
  178. return pathAsString;
  179. }
  180. }