3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.projectanalysis.ws;
22 import java.util.function.Consumer;
23 import java.util.stream.Stream;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.util.Uuids;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.SnapshotDto;
32 import org.sonar.db.event.EventDto;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.user.UserSession;
36 import static com.google.common.base.Preconditions.checkArgument;
37 import static java.lang.String.format;
38 import static org.sonar.server.projectanalysis.ws.EventValidator.checkModifiable;
39 import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER;
40 import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION;
41 import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_EVENT;
43 public class DeleteEventAction implements ProjectAnalysesWsAction {
44 private final DbClient dbClient;
45 private final UserSession userSession;
47 public DeleteEventAction(DbClient dbClient, UserSession userSession) {
48 this.dbClient = dbClient;
49 this.userSession = userSession;
53 public void define(WebService.NewController context) {
54 WebService.NewAction action = context.createAction("delete_event")
55 .setDescription("Delete a project analysis event.<br>" +
56 "Only event of category '%s' and '%s' can be deleted.<br>" +
57 "Requires one of the following permissions:" +
59 " <li>'Administer System'</li>" +
60 " <li>'Administer' rights on the specified project</li>" +
62 VERSION.name(), OTHER.name())
67 action.createParam(PARAM_EVENT)
68 .setDescription("Event key")
69 .setExampleValue(Uuids.UUID_EXAMPLE_02)
74 public void handle(Request request, Response response) throws Exception {
75 String eventP = request.mandatoryParam(PARAM_EVENT);
76 try (DbSession dbSession = dbClient.openSession(false)) {
77 Stream.of(getEvent(dbSession, eventP))
78 .peek(checkPermissions())
79 .peek(checkModifiable())
80 .forEach(event -> deleteEvent(dbSession, event));
85 private EventDto getEvent(DbSession dbSession, String event) {
86 return dbClient.eventDao().selectByUuid(dbSession, event)
87 .orElseThrow(() -> new NotFoundException(format("Event '%s' not found", event)));
90 private void deleteEvent(DbSession dbSession, EventDto dbEvent) {
91 if (VERSION.getLabel().equals(dbEvent.getCategory())) {
92 SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid())
93 .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid())));
94 checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis");
95 analysis.setProjectVersion(null);
96 dbClient.snapshotDao().update(dbSession, analysis);
98 dbClient.eventDao().delete(dbSession, dbEvent.getUuid());
102 private Consumer<EventDto> checkPermissions() {
103 return event -> userSession.checkComponentUuidPermission(UserRole.ADMIN, event.getComponentUuid());