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.

CreateEventAction.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.projectanalysis.ws;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.function.Consumer;
  25. import java.util.function.Predicate;
  26. import org.sonar.api.resources.Qualifiers;
  27. import org.sonar.api.resources.Scopes;
  28. import org.sonar.api.server.ws.Request;
  29. import org.sonar.api.server.ws.Response;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.api.web.UserRole;
  33. import org.sonar.core.util.UuidFactory;
  34. import org.sonar.core.util.Uuids;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.component.SnapshotDto;
  39. import org.sonar.db.event.EventDto;
  40. import org.sonar.server.exceptions.NotFoundException;
  41. import org.sonar.server.user.UserSession;
  42. import org.sonarqube.ws.ProjectAnalyses.CreateEventResponse;
  43. import org.sonarqube.ws.ProjectAnalyses.Event;
  44. import static com.google.common.base.Preconditions.checkArgument;
  45. import static com.google.common.base.Preconditions.checkState;
  46. import static java.lang.String.format;
  47. import static java.util.Optional.ofNullable;
  48. import static org.sonar.db.event.EventValidator.MAX_NAME_LENGTH;
  49. import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER;
  50. import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
  51. import static org.sonar.server.projectanalysis.ws.EventCategory.fromLabel;
  52. import static org.sonar.server.projectanalysis.ws.EventValidator.checkVersionName;
  53. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
  54. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_CATEGORY;
  55. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME;
  56. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  57. public class CreateEventAction implements ProjectAnalysesWsAction {
  58. private static final Set<String> ALLOWED_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.APP);
  59. private final DbClient dbClient;
  60. private final UuidFactory uuidFactory;
  61. private final System2 system;
  62. private final UserSession userSession;
  63. public CreateEventAction(DbClient dbClient, UuidFactory uuidFactory, System2 system, UserSession userSession) {
  64. this.dbClient = dbClient;
  65. this.uuidFactory = uuidFactory;
  66. this.system = system;
  67. this.userSession = userSession;
  68. }
  69. @Override
  70. public void define(WebService.NewController context) {
  71. WebService.NewAction action = context.createAction("create_event")
  72. .setDescription("Create a project analysis event.<br>" +
  73. "Only event of category '%s' and '%s' can be created.<br>" +
  74. "Requires one of the following permissions:" +
  75. "<ul>" +
  76. " <li>'Administer System'</li>" +
  77. " <li>'Administer' rights on the specified project</li>" +
  78. "</ul>",
  79. VERSION.name(), OTHER.name())
  80. .setSince("6.3")
  81. .setPost(true)
  82. .setResponseExample(getClass().getResource("create_event-example.json"))
  83. .setHandler(this);
  84. action.createParam(PARAM_ANALYSIS)
  85. .setDescription("Analysis key")
  86. .setExampleValue(Uuids.UUID_EXAMPLE_01)
  87. .setRequired(true);
  88. action.createParam(PARAM_CATEGORY)
  89. .setDescription("Category")
  90. .setDefaultValue(OTHER)
  91. .setPossibleValues(VERSION, OTHER);
  92. action.createParam(PARAM_NAME)
  93. .setRequired(true)
  94. .setMaximumLength(MAX_NAME_LENGTH)
  95. .setDescription("Name")
  96. .setExampleValue("5.6");
  97. }
  98. @Override
  99. public void handle(Request httpRequest, Response httpResponse) throws Exception {
  100. CreateEventRequest request = toAddEventRequest(httpRequest);
  101. CreateEventResponse response = doHandle(request);
  102. writeProtobuf(response, httpRequest, httpResponse);
  103. }
  104. private CreateEventResponse doHandle(CreateEventRequest request) {
  105. try (DbSession dbSession = dbClient.openSession(false)) {
  106. SnapshotDto analysis = getAnalysis(dbSession, request);
  107. ComponentDto project = getProjectOrApplication(dbSession, analysis);
  108. checkRequest(request, project);
  109. checkExistingDbEvents(dbSession, request, analysis);
  110. EventDto dbEvent = insertDbEvent(dbSession, request, analysis);
  111. return toCreateEventResponse(dbEvent);
  112. }
  113. }
  114. private EventDto insertDbEvent(DbSession dbSession, CreateEventRequest request, SnapshotDto analysis) {
  115. EventDto dbEvent = dbClient.eventDao().insert(dbSession, toDbEvent(request, analysis));
  116. if (VERSION.equals(request.getCategory())) {
  117. analysis.setCodePeriodVersion(request.getName());
  118. dbClient.snapshotDao().update(dbSession, analysis);
  119. }
  120. dbSession.commit();
  121. return dbEvent;
  122. }
  123. private SnapshotDto getAnalysis(DbSession dbSession, CreateEventRequest request) {
  124. return dbClient.snapshotDao().selectByUuid(dbSession, request.getAnalysis())
  125. .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", request.getAnalysis())));
  126. }
  127. private ComponentDto getProjectOrApplication(DbSession dbSession, SnapshotDto analysis) {
  128. ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, analysis.getComponentUuid()).orElse(null);
  129. checkState(project != null, "Project of analysis '%s' is not found", analysis.getUuid());
  130. checkArgument(ALLOWED_QUALIFIERS.contains(project.qualifier()) && Scopes.PROJECT.equals(project.scope()),
  131. "An event must be created on a project or an application");
  132. return project;
  133. }
  134. private void checkRequest(CreateEventRequest request, ComponentDto component) {
  135. userSession.checkComponentPermission(UserRole.ADMIN, component);
  136. checkArgument(EventCategory.VERSION != request.getCategory() || Qualifiers.PROJECT.equals(component.qualifier()), "A version event must be created on a project");
  137. checkVersionName(request.getCategory(), request.getName());
  138. }
  139. private static CreateEventRequest toAddEventRequest(Request request) {
  140. return CreateEventRequest.builder()
  141. .setAnalysis(request.mandatoryParam(PARAM_ANALYSIS))
  142. .setName(request.mandatoryParam(PARAM_NAME))
  143. .setCategory(request.mandatoryParamAsEnum(PARAM_CATEGORY, EventCategory.class))
  144. .build();
  145. }
  146. private EventDto toDbEvent(CreateEventRequest request, SnapshotDto analysis) {
  147. return new EventDto()
  148. .setUuid(uuidFactory.create())
  149. .setAnalysisUuid(analysis.getUuid())
  150. .setComponentUuid(analysis.getComponentUuid())
  151. .setCategory(request.getCategory().getLabel())
  152. .setName(request.getName())
  153. .setCreatedAt(system.now())
  154. .setDate(analysis.getCreatedAt());
  155. }
  156. private static CreateEventResponse toCreateEventResponse(EventDto dbEvent) {
  157. Event.Builder wsEvent = Event.newBuilder()
  158. .setKey(dbEvent.getUuid())
  159. .setCategory(fromLabel(dbEvent.getCategory()).name())
  160. .setAnalysis(dbEvent.getAnalysisUuid())
  161. .setName(dbEvent.getName());
  162. ofNullable(dbEvent.getDescription()).ifPresent(wsEvent::setDescription);
  163. return CreateEventResponse.newBuilder().setEvent(wsEvent).build();
  164. }
  165. private void checkExistingDbEvents(DbSession dbSession, CreateEventRequest request, SnapshotDto analysis) {
  166. List<EventDto> dbEvents = dbClient.eventDao().selectByAnalysisUuid(dbSession, analysis.getUuid());
  167. Predicate<EventDto> similarEventExisting = filterSimilarEvents(request);
  168. dbEvents.stream()
  169. .filter(similarEventExisting)
  170. .findAny()
  171. .ifPresent(throwException(request));
  172. }
  173. private static Predicate<EventDto> filterSimilarEvents(CreateEventRequest request) {
  174. switch (request.getCategory()) {
  175. case VERSION:
  176. return dbEvent -> VERSION.getLabel().equals(dbEvent.getCategory());
  177. case OTHER:
  178. return dbEvent -> OTHER.getLabel().equals(dbEvent.getCategory()) && request.getName().equals(dbEvent.getName());
  179. default:
  180. throw new IllegalStateException("Event category not handled: " + request.getCategory());
  181. }
  182. }
  183. private static Consumer<EventDto> throwException(CreateEventRequest request) {
  184. switch (request.getCategory()) {
  185. case VERSION:
  186. return dbEvent -> {
  187. throw new IllegalArgumentException(format("A version event already exists on analysis '%s'", request.getAnalysis()));
  188. };
  189. case OTHER:
  190. return dbEvent -> {
  191. throw new IllegalArgumentException(format("An '%s' event with the same name already exists on analysis '%s'", OTHER.getLabel(), request.getAnalysis()));
  192. };
  193. default:
  194. throw new IllegalStateException("Event category not handled: " + request.getCategory());
  195. }
  196. }
  197. private static class CreateEventRequest {
  198. private final String analysis;
  199. private final EventCategory category;
  200. private final String name;
  201. private CreateEventRequest(Builder builder) {
  202. analysis = builder.analysis;
  203. category = builder.category;
  204. name = builder.name;
  205. }
  206. public String getAnalysis() {
  207. return analysis;
  208. }
  209. public EventCategory getCategory() {
  210. return category;
  211. }
  212. public String getName() {
  213. return name;
  214. }
  215. public static Builder builder() {
  216. return new Builder();
  217. }
  218. }
  219. private static class Builder {
  220. private String analysis;
  221. private EventCategory category = EventCategory.OTHER;
  222. private String name;
  223. private Builder() {
  224. // enforce static factory method
  225. }
  226. public Builder setAnalysis(String analysis) {
  227. this.analysis = analysis;
  228. return this;
  229. }
  230. public Builder setCategory(EventCategory category) {
  231. this.category = category;
  232. return this;
  233. }
  234. public Builder setName(String name) {
  235. this.name = name;
  236. return this;
  237. }
  238. public CreateEventRequest build() {
  239. checkArgument(analysis != null, "Analysis key is required");
  240. checkArgument(category != null, "Category is required");
  241. checkArgument(name != null, "Name is required");
  242. return new CreateEventRequest(this);
  243. }
  244. }
  245. }