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.

MetadataPublisher.java 7.7KB

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