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.

ComponentsPublisher.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.util.Map;
  22. import javax.annotation.CheckForNull;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.CoreProperties;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.InputFile.Status;
  27. import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
  28. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  29. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  30. import org.sonar.scanner.protocol.output.ScannerReport;
  31. import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
  32. import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus;
  33. import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink;
  34. import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType;
  35. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  36. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  37. /**
  38. * Adds components and analysis metadata to output report
  39. */
  40. public class ComponentsPublisher implements ReportPublisherStep {
  41. private final InputComponentStore inputComponentStore;
  42. private final DefaultInputProject project;
  43. public ComponentsPublisher(DefaultInputProject project, InputComponentStore inputComponentStore) {
  44. this.project = project;
  45. this.inputComponentStore = inputComponentStore;
  46. }
  47. @Override
  48. public void publish(ScannerReportWriter writer) {
  49. ScannerReport.Component.Builder projectBuilder = prepareProjectBuilder();
  50. ScannerReport.Component.Builder fileBuilder = ScannerReport.Component.newBuilder();
  51. for (DefaultInputFile file : inputComponentStore.allFilesToPublish()) {
  52. projectBuilder.addChildRef(file.scannerId());
  53. fileBuilder.clear();
  54. // non-null fields
  55. fileBuilder.setRef(file.scannerId());
  56. fileBuilder.setType(ComponentType.FILE);
  57. fileBuilder.setIsTest(file.type() == InputFile.Type.TEST);
  58. fileBuilder.setLines(file.lines());
  59. fileBuilder.setStatus(convert(file.status()));
  60. String lang = getLanguageKey(file);
  61. if (lang != null) {
  62. fileBuilder.setLanguage(lang);
  63. }
  64. fileBuilder.setProjectRelativePath(file.getProjectRelativePath());
  65. writer.writeComponent(fileBuilder.build());
  66. }
  67. writer.writeComponent(projectBuilder.build());
  68. }
  69. private ScannerReport.Component.Builder prepareProjectBuilder() {
  70. ScannerReport.Component.Builder projectBuilder = ScannerReport.Component.newBuilder();
  71. projectBuilder.setRef(project.scannerId());
  72. projectBuilder.setType(ComponentType.PROJECT);
  73. // Here we want key without branch
  74. projectBuilder.setKey(project.key());
  75. // protocol buffers does not accept null values
  76. String name = getName(project);
  77. if (name != null) {
  78. projectBuilder.setName(name);
  79. }
  80. String description = getDescription(project);
  81. if (description != null) {
  82. projectBuilder.setDescription(description);
  83. }
  84. writeLinks(project, projectBuilder);
  85. return projectBuilder;
  86. }
  87. private static FileStatus convert(Status status) {
  88. switch (status) {
  89. case ADDED:
  90. return FileStatus.ADDED;
  91. case CHANGED:
  92. return FileStatus.CHANGED;
  93. case SAME:
  94. return FileStatus.SAME;
  95. default:
  96. throw new IllegalArgumentException("Unexpected status: " + status);
  97. }
  98. }
  99. private static void writeLinks(DefaultInputProject project, ScannerReport.Component.Builder builder) {
  100. ComponentLink.Builder linkBuilder = ComponentLink.newBuilder();
  101. writeProjectLink(builder, project.properties(), linkBuilder, CoreProperties.LINKS_HOME_PAGE, ComponentLinkType.HOME);
  102. writeProjectLink(builder, project.properties(), linkBuilder, CoreProperties.LINKS_CI, ComponentLinkType.CI);
  103. writeProjectLink(builder, project.properties(), linkBuilder, CoreProperties.LINKS_ISSUE_TRACKER, ComponentLinkType.ISSUE);
  104. writeProjectLink(builder, project.properties(), linkBuilder, CoreProperties.LINKS_SOURCES, ComponentLinkType.SCM);
  105. }
  106. private static void writeProjectLink(ScannerReport.Component.Builder componentBuilder, Map<String, String> properties, ComponentLink.Builder linkBuilder, String linkProp,
  107. ComponentLinkType linkType) {
  108. String link = properties.get(linkProp);
  109. if (StringUtils.isNotBlank(link)) {
  110. linkBuilder.setType(linkType);
  111. linkBuilder.setHref(link);
  112. componentBuilder.addLink(linkBuilder.build());
  113. linkBuilder.clear();
  114. }
  115. }
  116. @CheckForNull
  117. private static String getLanguageKey(InputFile file) {
  118. return file.language();
  119. }
  120. @CheckForNull
  121. private static String getName(AbstractProjectOrModule module) {
  122. return module.definition().getOriginalName();
  123. }
  124. @CheckForNull
  125. private static String getDescription(AbstractProjectOrModule module) {
  126. return module.definition().getDescription();
  127. }
  128. }