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.

SubmitAction.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.server.ce.ws;
  21. import java.io.BufferedInputStream;
  22. import java.io.InputStream;
  23. import java.util.LinkedHashMap;
  24. import java.util.Map;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.ce.task.CeTask;
  30. import org.sonar.db.ce.CeTaskCharacteristicDto;
  31. import org.sonar.server.ce.queue.ReportSubmitter;
  32. import org.sonar.server.organization.DefaultOrganizationProvider;
  33. import org.sonar.server.ws.WsUtils;
  34. import org.sonarqube.ws.Ce;
  35. import static org.apache.commons.lang.StringUtils.abbreviate;
  36. import static org.apache.commons.lang.StringUtils.defaultIfBlank;
  37. import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
  38. import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
  39. import static org.sonar.server.ws.WsUtils.checkRequest;
  40. public class SubmitAction implements CeWsAction {
  41. private static final String PARAM_ORGANIZATION_KEY = "organization";
  42. private static final String PARAM_PROJECT_KEY = "projectKey";
  43. private static final String PARAM_PROJECT_NAME = "projectName";
  44. private static final String PARAM_REPORT_DATA = "report";
  45. private static final String PARAM_ANALYSIS_CHARACTERISTIC = "characteristic";
  46. private final ReportSubmitter reportSubmitter;
  47. private final DefaultOrganizationProvider defaultOrganizationProvider;
  48. public SubmitAction(ReportSubmitter reportSubmitter, DefaultOrganizationProvider defaultOrganizationProvider) {
  49. this.reportSubmitter = reportSubmitter;
  50. this.defaultOrganizationProvider = defaultOrganizationProvider;
  51. }
  52. @Override
  53. public void define(WebService.NewController controller) {
  54. WebService.NewAction action = controller.createAction("submit")
  55. .setDescription("Submits a scanner report to the queue. Report is processed asynchronously. Requires analysis permission. " +
  56. "If the project does not exist, then the provisioning permission is also required.")
  57. .setPost(true)
  58. .setInternal(true)
  59. .setSince("5.2")
  60. .setHandler(this)
  61. .setResponseExample(getClass().getResource("submit-example.json"));
  62. action.createParam(PARAM_ORGANIZATION_KEY)
  63. .setDescription("Key of the organization the project belongs to")
  64. .setExampleValue("my-org")
  65. .setSince("6.3")
  66. .setInternal(true);
  67. action
  68. .createParam(PARAM_PROJECT_KEY)
  69. .setRequired(true)
  70. .setMaximumLength(MAX_COMPONENT_KEY_LENGTH)
  71. .setDescription("Key of project")
  72. .setExampleValue("my_project");
  73. action
  74. .createParam(PARAM_PROJECT_NAME)
  75. .setRequired(false)
  76. .setDescription("Optional name of the project, used only if the project does not exist yet. If name is longer than %d, it is abbreviated.", MAX_COMPONENT_NAME_LENGTH)
  77. .setExampleValue("My Project");
  78. action
  79. .createParam(PARAM_REPORT_DATA)
  80. .setRequired(true)
  81. .setDescription("Report file. Format is not an API, it changes among SonarQube versions.");
  82. action
  83. .createParam(PARAM_ANALYSIS_CHARACTERISTIC)
  84. .setRequired(false)
  85. .setDescription("Optional characteristic of the analysis. Can be repeated to define multiple characteristics.")
  86. .setExampleValue("branchType=long")
  87. .setSince("6.6");
  88. }
  89. @Override
  90. public void handle(Request wsRequest, Response wsResponse) throws Exception {
  91. String organizationKey = wsRequest.getParam(PARAM_ORGANIZATION_KEY)
  92. .emptyAsNull()
  93. .or(defaultOrganizationProvider.get()::getKey);
  94. String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY);
  95. String projectName = abbreviate(defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey), MAX_COMPONENT_NAME_LENGTH);
  96. Map<String, String> characteristics = parseTaskCharacteristics(wsRequest);
  97. try (InputStream report = new BufferedInputStream(wsRequest.mandatoryParamAsPart(PARAM_REPORT_DATA).getInputStream())) {
  98. CeTask task = reportSubmitter.submit(organizationKey, projectKey, projectName, characteristics, report);
  99. Ce.SubmitResponse submitResponse = Ce.SubmitResponse.newBuilder()
  100. .setTaskId(task.getUuid())
  101. .setProjectId(task.getComponent().get().getUuid())
  102. .build();
  103. WsUtils.writeProtobuf(submitResponse, wsRequest, wsResponse);
  104. }
  105. }
  106. private static Map<String, String> parseTaskCharacteristics(Request wsRequest) {
  107. Map<String, String> characteristics = new LinkedHashMap<>();
  108. for (String param : wsRequest.multiParam(PARAM_ANALYSIS_CHARACTERISTIC)) {
  109. String[] pair = StringUtils.split(param, "=", 2);
  110. checkRequest(pair.length == 2, "Parameter '%s' must be a key-value pair with the format 'key=value'.", PARAM_ANALYSIS_CHARACTERISTIC);
  111. checkRequest(!characteristics.containsKey(pair[0]), "Key '%s' was provided twice with parameters '%s'", pair[0], PARAM_ANALYSIS_CHARACTERISTIC);
  112. if (CeTaskCharacteristicDto.SUPPORTED_KEYS.contains(pair[0])) {
  113. characteristics.put(pair[0], pair[1]);
  114. }
  115. }
  116. return characteristics;
  117. }
  118. }