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.

DeleteEventAction.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 java.util.function.Consumer;
  22. import java.util.stream.Stream;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.web.UserRole;
  27. import org.sonar.core.util.Uuids;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.SnapshotDto;
  31. import org.sonar.db.event.EventDto;
  32. import org.sonar.server.exceptions.NotFoundException;
  33. import org.sonar.server.user.UserSession;
  34. import static com.google.common.base.Preconditions.checkArgument;
  35. import static java.lang.String.format;
  36. import static org.sonar.server.projectanalysis.ws.EventValidator.checkModifiable;
  37. import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER;
  38. import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
  39. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_EVENT;
  40. public class DeleteEventAction implements ProjectAnalysesWsAction {
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. public DeleteEventAction(DbClient dbClient, UserSession userSession) {
  44. this.dbClient = dbClient;
  45. this.userSession = userSession;
  46. }
  47. @Override
  48. public void define(WebService.NewController context) {
  49. WebService.NewAction action = context.createAction("delete_event")
  50. .setDescription("Delete a project analysis event.<br>" +
  51. "Only event of category '%s' and '%s' can be deleted.<br>" +
  52. "Requires one of the following permissions:" +
  53. "<ul>" +
  54. " <li>'Administer System'</li>" +
  55. " <li>'Administer' rights on the specified project</li>" +
  56. "</ul>",
  57. VERSION.name(), OTHER.name())
  58. .setPost(true)
  59. .setSince("6.3")
  60. .setHandler(this);
  61. action.createParam(PARAM_EVENT)
  62. .setDescription("Event key")
  63. .setExampleValue(Uuids.UUID_EXAMPLE_02)
  64. .setRequired(true);
  65. }
  66. @Override
  67. public void handle(Request request, Response response) throws Exception {
  68. String eventP = request.mandatoryParam(PARAM_EVENT);
  69. try (DbSession dbSession = dbClient.openSession(false)) {
  70. Stream.of(getEvent(dbSession, eventP))
  71. .peek(checkPermissions())
  72. .peek(checkModifiable())
  73. .forEach(event -> deleteEvent(dbSession, event));
  74. }
  75. response.noContent();
  76. }
  77. private EventDto getEvent(DbSession dbSession, String event) {
  78. return dbClient.eventDao().selectByUuid(dbSession, event)
  79. .orElseThrow(() -> new NotFoundException(format("Event '%s' not found", event)));
  80. }
  81. private void deleteEvent(DbSession dbSession, EventDto dbEvent) {
  82. if (VERSION.getLabel().equals(dbEvent.getCategory())) {
  83. SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid())
  84. .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid())));
  85. checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis");
  86. analysis.setVersion(null);
  87. dbClient.snapshotDao().update(dbSession, analysis);
  88. }
  89. dbClient.eventDao().delete(dbSession, dbEvent.getUuid());
  90. dbSession.commit();
  91. }
  92. private Consumer<EventDto> checkPermissions() {
  93. return event -> userSession.checkComponentUuidPermission(UserRole.ADMIN, event.getComponentUuid());
  94. }
  95. }