Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MetadataPublisher.java 7.5KB

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