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.

ReportPublisher.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.base.Throwables;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.UnsupportedEncodingException;
  27. import java.io.Writer;
  28. import java.net.URL;
  29. import java.nio.charset.StandardCharsets;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.util.LinkedHashMap;
  33. import java.util.Map;
  34. import javax.annotation.Nullable;
  35. import okhttp3.HttpUrl;
  36. import org.apache.commons.io.FileUtils;
  37. import org.picocontainer.Startable;
  38. import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
  39. import org.sonar.api.platform.Server;
  40. import org.sonar.api.utils.MessageException;
  41. import org.sonar.api.utils.TempFolder;
  42. import org.sonar.api.utils.ZipUtils;
  43. import org.sonar.api.utils.log.Logger;
  44. import org.sonar.api.utils.log.Loggers;
  45. import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
  46. import org.sonar.scanner.bootstrap.ScannerWsClient;
  47. import org.sonar.scanner.protocol.output.ScannerReportReader;
  48. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  49. import org.sonar.scanner.scan.ScanProperties;
  50. import org.sonar.scanner.scan.branch.BranchConfiguration;
  51. import org.sonarqube.ws.Ce;
  52. import org.sonarqube.ws.MediaTypes;
  53. import org.sonarqube.ws.client.HttpException;
  54. import org.sonarqube.ws.client.PostRequest;
  55. import org.sonarqube.ws.client.WsResponse;
  56. import static java.net.URLEncoder.encode;
  57. import static org.apache.commons.lang.StringUtils.EMPTY;
  58. import static org.apache.commons.lang.StringUtils.isBlank;
  59. import static org.sonar.core.util.FileUtils.deleteQuietly;
  60. import static org.sonar.scanner.scan.branch.BranchType.LONG;
  61. import static org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST;
  62. import static org.sonar.scanner.scan.branch.BranchType.SHORT;
  63. public class ReportPublisher implements Startable {
  64. private static final Logger LOG = Loggers.get(ReportPublisher.class);
  65. private static final String CHARACTERISTIC = "characteristic";
  66. private static final String DASHBOARD = "dashboard";
  67. private static final String BRANCH = "branch";
  68. private static final String ID = "id";
  69. private static final String RESOLVED = "resolved";
  70. private final ScannerWsClient wsClient;
  71. private final AnalysisContextReportPublisher contextPublisher;
  72. private final InputModuleHierarchy moduleHierarchy;
  73. private final GlobalAnalysisMode analysisMode;
  74. private final TempFolder temp;
  75. private final ReportPublisherStep[] publishers;
  76. private final Server server;
  77. private final BranchConfiguration branchConfiguration;
  78. private final ScanProperties properties;
  79. private Path reportDir;
  80. private ScannerReportWriter writer;
  81. private ScannerReportReader reader;
  82. public ReportPublisher(ScanProperties properties, ScannerWsClient wsClient, Server server, AnalysisContextReportPublisher contextPublisher,
  83. InputModuleHierarchy moduleHierarchy, GlobalAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers, BranchConfiguration branchConfiguration) {
  84. this.wsClient = wsClient;
  85. this.server = server;
  86. this.contextPublisher = contextPublisher;
  87. this.moduleHierarchy = moduleHierarchy;
  88. this.analysisMode = analysisMode;
  89. this.temp = temp;
  90. this.publishers = publishers;
  91. this.branchConfiguration = branchConfiguration;
  92. this.properties = properties;
  93. }
  94. @Override
  95. public void start() {
  96. reportDir = moduleHierarchy.root().getWorkDir().resolve("scanner-report");
  97. writer = new ScannerReportWriter(reportDir.toFile());
  98. reader = new ScannerReportReader(reportDir.toFile());
  99. contextPublisher.init(writer);
  100. if (!analysisMode.isMediumTest()) {
  101. String publicUrl = server.getPublicRootUrl();
  102. if (HttpUrl.parse(publicUrl) == null) {
  103. throw MessageException.of("Failed to parse public URL set in SonarQube server: " + publicUrl);
  104. }
  105. }
  106. }
  107. @Override
  108. public void stop() {
  109. if (!properties.shouldKeepReport()) {
  110. deleteQuietly(reportDir);
  111. }
  112. }
  113. public Path getReportDir() {
  114. return reportDir;
  115. }
  116. public ScannerReportWriter getWriter() {
  117. return writer;
  118. }
  119. public ScannerReportReader getReader() {
  120. return reader;
  121. }
  122. public void execute() {
  123. String taskId = null;
  124. File report = generateReportFile();
  125. if (properties.shouldKeepReport()) {
  126. LOG.info("Analysis report generated in " + reportDir);
  127. }
  128. if (!analysisMode.isMediumTest()) {
  129. taskId = upload(report);
  130. }
  131. logSuccess(taskId);
  132. }
  133. private File generateReportFile() {
  134. try {
  135. long startTime = System.currentTimeMillis();
  136. for (ReportPublisherStep publisher : publishers) {
  137. publisher.publish(writer);
  138. }
  139. long stopTime = System.currentTimeMillis();
  140. LOG.info("Analysis report generated in {}ms, dir size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir.toFile())));
  141. startTime = System.currentTimeMillis();
  142. File reportZip = temp.newFile("scanner-report", ".zip");
  143. ZipUtils.zipDir(reportDir.toFile(), reportZip);
  144. stopTime = System.currentTimeMillis();
  145. LOG.info("Analysis report compressed in {}ms, zip size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip)));
  146. return reportZip;
  147. } catch (IOException e) {
  148. throw new IllegalStateException("Unable to prepare analysis report", e);
  149. }
  150. }
  151. /**
  152. * Uploads the report file to server and returns the generated task id
  153. */
  154. @VisibleForTesting
  155. String upload(File report) {
  156. LOG.debug("Upload report");
  157. long startTime = System.currentTimeMillis();
  158. PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report);
  159. PostRequest post = new PostRequest("api/ce/submit")
  160. .setMediaType(MediaTypes.PROTOBUF)
  161. .setParam("organization", properties.organizationKey().orElse(null))
  162. .setParam("projectKey", moduleHierarchy.root().key())
  163. .setParam("projectName", moduleHierarchy.root().getOriginalName())
  164. .setParam("projectBranch", moduleHierarchy.root().getBranch())
  165. .setPart("report", filePart);
  166. String branchName = branchConfiguration.branchName();
  167. if (branchName != null) {
  168. if (branchConfiguration.branchType() != PULL_REQUEST) {
  169. post.setParam(CHARACTERISTIC, "branch=" + branchName);
  170. post.setParam(CHARACTERISTIC, "branchType=" + branchConfiguration.branchType().name());
  171. } else {
  172. post.setParam(CHARACTERISTIC, "pullRequest=" + branchConfiguration.pullRequestKey());
  173. }
  174. }
  175. WsResponse response;
  176. try {
  177. response = wsClient.call(post).failIfNotSuccessful();
  178. } catch (HttpException e) {
  179. throw MessageException.of(String.format("Failed to upload report - %s", ScannerWsClient.createErrorMessage(e)));
  180. }
  181. try (InputStream protobuf = response.contentStream()) {
  182. return Ce.SubmitResponse.parser().parseFrom(protobuf).getTaskId();
  183. } catch (Exception e) {
  184. throw Throwables.propagate(e);
  185. } finally {
  186. long stopTime = System.currentTimeMillis();
  187. LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms");
  188. }
  189. }
  190. @VisibleForTesting
  191. void logSuccess(@Nullable String taskId) {
  192. if (taskId == null) {
  193. LOG.info("ANALYSIS SUCCESSFUL");
  194. } else {
  195. Map<String, String> metadata = new LinkedHashMap<>();
  196. String effectiveKey = moduleHierarchy.root().getKeyWithBranch();
  197. properties.organizationKey().ifPresent(org -> metadata.put("organization", org));
  198. metadata.put("projectKey", effectiveKey);
  199. metadata.put("serverUrl", server.getPublicRootUrl());
  200. metadata.put("serverVersion", server.getVersion());
  201. properties.branch().ifPresent(branch -> metadata.put("branch", branch));
  202. URL dashboardUrl = buildDashboardUrl(server.getPublicRootUrl(), effectiveKey);
  203. metadata.put("dashboardUrl", dashboardUrl.toExternalForm());
  204. URL taskUrl = HttpUrl.parse(server.getPublicRootUrl()).newBuilder()
  205. .addPathSegment("api").addPathSegment("ce").addPathSegment("task")
  206. .addQueryParameter(ID, taskId)
  207. .build()
  208. .url();
  209. metadata.put("ceTaskId", taskId);
  210. metadata.put("ceTaskUrl", taskUrl.toExternalForm());
  211. LOG.info("ANALYSIS SUCCESSFUL, you can browse {}", dashboardUrl);
  212. LOG.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report");
  213. LOG.info("More about the report processing at {}", taskUrl);
  214. dumpMetadata(metadata);
  215. }
  216. }
  217. private URL buildDashboardUrl(String publicUrl, String effectiveKey) {
  218. HttpUrl httpUrl = HttpUrl.parse(publicUrl);
  219. if (onPullRequest(branchConfiguration)) {
  220. return httpUrl.newBuilder()
  221. .addPathSegment(DASHBOARD)
  222. .addEncodedQueryParameter(ID, encoded(effectiveKey))
  223. .addEncodedQueryParameter("pullRequest", encoded(branchConfiguration.pullRequestKey()))
  224. .build()
  225. .url();
  226. }
  227. if (onLongLivingBranch(branchConfiguration)) {
  228. return httpUrl.newBuilder()
  229. .addPathSegment(DASHBOARD)
  230. .addEncodedQueryParameter(ID, encoded(effectiveKey))
  231. .addEncodedQueryParameter(BRANCH, encoded(branchConfiguration.branchName()))
  232. .build()
  233. .url();
  234. }
  235. if (onShortLivingBranch(branchConfiguration)) {
  236. return httpUrl.newBuilder()
  237. .addPathSegment(DASHBOARD)
  238. .addEncodedQueryParameter(ID, encoded(effectiveKey))
  239. .addEncodedQueryParameter(BRANCH, encoded(branchConfiguration.branchName()))
  240. .addQueryParameter(RESOLVED, "false")
  241. .build()
  242. .url();
  243. }
  244. if (onMainBranch(branchConfiguration)) {
  245. return httpUrl.newBuilder()
  246. .addPathSegment(DASHBOARD)
  247. .addEncodedQueryParameter(ID, encoded(effectiveKey))
  248. .build()
  249. .url();
  250. }
  251. return httpUrl.newBuilder().build().url();
  252. }
  253. private static boolean onPullRequest(BranchConfiguration branchConfiguration) {
  254. return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == PULL_REQUEST);
  255. }
  256. private static boolean onShortLivingBranch(BranchConfiguration branchConfiguration) {
  257. return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == SHORT);
  258. }
  259. private static boolean onLongLivingBranch(BranchConfiguration branchConfiguration) {
  260. return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == LONG);
  261. }
  262. private static boolean onMainBranch(BranchConfiguration branchConfiguration) {
  263. return branchConfiguration.branchName() == null;
  264. }
  265. private static String encoded(@Nullable String queryParameter) {
  266. if (isBlank(queryParameter)) {
  267. return EMPTY;
  268. }
  269. try {
  270. return encode(queryParameter, "UTF-8");
  271. } catch (UnsupportedEncodingException e) {
  272. throw new IllegalStateException("Unable to urlencode " + queryParameter, e);
  273. }
  274. }
  275. private void dumpMetadata(Map<String, String> metadata) {
  276. Path file = properties.metadataFilePath();
  277. try {
  278. Files.createDirectories(file.getParent());
  279. try (Writer output = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
  280. for (Map.Entry<String, String> entry : metadata.entrySet()) {
  281. output.write(entry.getKey());
  282. output.write("=");
  283. output.write(entry.getValue());
  284. output.write("\n");
  285. }
  286. }
  287. LOG.debug("Report metadata written to {}", file);
  288. } catch (IOException e) {
  289. throw new IllegalStateException("Unable to dump " + file, e);
  290. }
  291. }
  292. }