aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-12-07 14:27:04 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-12-08 14:19:49 +0100
commit637a056d414b8b93ccf82eae0ba3b303fa25675a (patch)
tree2b93d1133e70b8ddf3c85597acd98c7634373a54 /server
parente32724b8cb16b512a9564a9d67c5fca0e8ba66b2 (diff)
downloadsonarqube-637a056d414b8b93ccf82eae0ba3b303fa25675a.tar.gz
sonarqube-637a056d414b8b93ccf82eae0ba3b303fa25675a.zip
SONAR-8465 Create WS api/project_analyses/delete_event
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ProjectAnalysisModule.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java10
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteEventAction.java111
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ProjectAnalysisModuleTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteEventActionTest.java164
6 files changed, 286 insertions, 9 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ProjectAnalysisModule.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ProjectAnalysisModule.java
index 0c98286be87..61f5ecd3052 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ProjectAnalysisModule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ProjectAnalysisModule.java
@@ -22,6 +22,7 @@ package org.sonar.server.projectanalysis;
import org.sonar.core.platform.Module;
import org.sonar.server.projectanalysis.ws.CreateEventAction;
+import org.sonar.server.projectanalysis.ws.DeleteEventAction;
import org.sonar.server.projectanalysis.ws.ProjectAnalysesWs;
public class ProjectAnalysisModule extends Module {
@@ -31,7 +32,8 @@ public class ProjectAnalysisModule extends Module {
add(
ProjectAnalysesWs.class,
// actions
- CreateEventAction.class);
+ CreateEventAction.class,
+ DeleteEventAction.class);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java
index d2ea1f89d6a..96d068e8c27 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java
@@ -42,16 +42,16 @@ import org.sonar.server.user.UserSession;
import org.sonarqube.ws.ProjectAnalyses.CreateEventResponse;
import org.sonarqube.ws.ProjectAnalyses.Event;
import org.sonarqube.ws.client.projectanalysis.CreateEventRequest;
-import org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category;
+import org.sonarqube.ws.client.projectanalysis.EventCategory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
-import static org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category.OTHER;
-import static org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category.VERSION;
-import static org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category.fromLabel;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.OTHER;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.VERSION;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.fromLabel;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_CATEGORY;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_DESCRIPTION;
@@ -149,7 +149,7 @@ public class CreateEventAction implements ProjectAnalysesWsAction {
return CreateEventRequest.builder()
.setAnalysis(request.mandatoryParam(PARAM_ANALYSIS))
.setName(request.mandatoryParam(PARAM_NAME))
- .setCategory(request.mandatoryParamAsEnum(PARAM_CATEGORY, Category.class))
+ .setCategory(request.mandatoryParamAsEnum(PARAM_CATEGORY, EventCategory.class))
.setDescription(request.param(PARAM_DESCRIPTION))
.build();
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteEventAction.java
new file mode 100644
index 00000000000..ac6be518af1
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteEventAction.java
@@ -0,0 +1,111 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.projectanalysis.ws;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.event.EventDto;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.client.projectanalysis.DeleteEventRequest;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.lang.String.format;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.OTHER;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.VERSION;
+import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_EVENT;
+
+public class DeleteEventAction implements ProjectAnalysesWsAction {
+ private static final Set<String> AUTHORIZED_CATEGORIES = ImmutableSet.of(VERSION.getLabel(), OTHER.getLabel());
+ private static final String AUTHORIZED_CATEGORIES_INLINED = Joiner.on(", ").join(AUTHORIZED_CATEGORIES);
+
+ private final DbClient dbClient;
+ private final UserSession userSession;
+
+ public DeleteEventAction(DbClient dbClient, UserSession userSession) {
+ this.dbClient = dbClient;
+ this.userSession = userSession;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ WebService.NewAction action = context.createAction("delete_event")
+ .setDescription("Delete an analysis event.<br>" +
+ "Requires one of the following permissions:" +
+ "<ul>" +
+ " <li>'Administer System'</li>" +
+ " <li>'Administer' rights on the specified project</li>" +
+ "</ul>")
+ .setPost(true)
+ .setSince("6.3")
+ .setHandler(this);
+
+ action.createParam(PARAM_EVENT)
+ .setDescription("Event key")
+ .setRequired(true);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ DeleteEventRequest deleteEventRequest = toDeleteEventRequest(request);
+ doHandle(deleteEventRequest);
+ response.noContent();
+ }
+
+ private void doHandle(DeleteEventRequest request) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ EventDto dbEvent = dbClient.eventDao().selectByUuid(dbSession, request.getEvent())
+ .orElseThrow(() -> new NotFoundException(format("Event '%s' not found", request.getEvent())));
+ checkPermissions(dbEvent);
+ checkArgument(AUTHORIZED_CATEGORIES.contains(dbEvent.getCategory()), "Event of category '%s' cannot be deleted. Authorized categories: %s", dbEvent.getCategory(),
+ AUTHORIZED_CATEGORIES_INLINED);
+
+ deleteEvent(dbSession, dbEvent);
+ }
+ }
+
+ private void deleteEvent(DbSession dbSession, EventDto dbEvent) {
+ if (VERSION.getLabel().equals(dbEvent.getCategory())) {
+ SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid())
+ .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid())));
+ checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis");
+ dbClient.snapshotDao().updateVersion(dbSession, analysis.getUuid(), null);
+ }
+ dbClient.eventDao().delete(dbSession, dbEvent.getUuid());
+ dbSession.commit();
+ }
+
+ private void checkPermissions(EventDto event) {
+ userSession.checkComponentUuidPermission(UserRole.ADMIN, event.getComponentUuid());
+ }
+
+ private static DeleteEventRequest toDeleteEventRequest(Request httpRequest) {
+ return new DeleteEventRequest(httpRequest.mandatoryParam(PARAM_EVENT));
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ProjectAnalysisModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ProjectAnalysisModuleTest.java
index 40d45b0a602..1f81537fee8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ProjectAnalysisModuleTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ProjectAnalysisModuleTest.java
@@ -31,6 +31,6 @@ public class ProjectAnalysisModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new ProjectAnalysisModule().configure(container);
- assertThat(container.size()).isEqualTo(2 + 2);
+ assertThat(container.size()).isEqualTo(2 + 3);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java
index 1074e51306e..dd6ecaad45d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java
@@ -60,8 +60,8 @@ import static org.sonar.db.component.SnapshotTesting.newAnalysis;
import static org.sonar.db.component.SnapshotTesting.newSnapshot;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.WsRequest.Method.POST;
-import static org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category.OTHER;
-import static org.sonarqube.ws.client.projectanalysis.CreateEventRequest.Category.VERSION;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.OTHER;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.VERSION;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_CATEGORY;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_DESCRIPTION;
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteEventActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteEventActionTest.java
new file mode 100644
index 00000000000..dcd4ffd4f46
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteEventActionTest.java
@@ -0,0 +1,164 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.projectanalysis.ws;
+
+import java.util.List;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.event.EventDto;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.SnapshotTesting.newAnalysis;
+import static org.sonar.db.event.EventTesting.newEvent;
+import static org.sonarqube.ws.client.projectanalysis.EventCategory.VERSION;
+import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_EVENT;
+
+public class DeleteEventActionTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
+
+ private WsActionTester ws = new WsActionTester(new DeleteEventAction(db.getDbClient(), userSession));
+
+ @Test
+ public void delete_event() {
+ SnapshotDto analysis = db.components().insertProjectAndSnapshot(newProjectDto());
+ db.events().insertEvent(newEvent(analysis).setUuid("E1"));
+ db.events().insertEvent(newEvent(analysis).setUuid("E2"));
+
+ call("E2");
+
+ List<EventDto> events = db.getDbClient().eventDao().selectByAnalysisUuid(db.getSession(), analysis.getUuid());
+ assertThat(events).extracting(EventDto::getUuid).containsExactly("E1");
+ }
+
+ @Test
+ public void delete_version_event() {
+ ComponentDto project = db.components().insertProject();
+ SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setVersion("5.6.3").setLast(false));
+ db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
+
+ call("E1");
+
+ SnapshotDto newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()).get();
+ assertThat(newAnalysis.getVersion()).isNull();
+ }
+
+ @Test
+ public void delete_event_as_project_admin() {
+ SnapshotDto analysis = db.components().insertProjectAndSnapshot(newProjectDto("P1"));
+ db.events().insertEvent(newEvent(analysis).setUuid("E1"));
+ userSession.anonymous().addProjectUuidPermissions(UserRole.ADMIN, "P1");
+
+ call("E1");
+
+ assertThat(db.countRowsOfTable("events")).isEqualTo(0);
+ }
+
+ @Test
+ public void fail_if_version_for_last_analysis() {
+ ComponentDto project = db.components().insertProject();
+ SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setVersion("5.6.3").setLast(true));
+ db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory(VERSION.getLabel()));
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Cannot delete the version event of last analysis");
+
+ call("E1");
+ }
+
+ @Test
+ public void fail_if_category_different_than_other_and_version() {
+ SnapshotDto analysis = db.components().insertProjectAndSnapshot(newProjectDto("P1"));
+ db.events().insertEvent(newEvent(analysis).setUuid("E1").setCategory("Profile"));
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Event of category 'Profile' cannot be deleted. Authorized categories: Version, Other");
+
+ call("E1");
+ }
+
+ @Test
+ public void fail_if_event_does_not_exist() {
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("E42' not found");
+
+ call("E42");
+ }
+
+ @Test
+ public void fail_if_not_enough_permission() {
+ SnapshotDto analysis = db.components().insertProjectAndSnapshot(newProjectDto());
+ db.events().insertEvent(newEvent(analysis).setUuid("E1"));
+ userSession.anonymous();
+
+ expectedException.expect(ForbiddenException.class);
+
+ call("E1");
+ }
+
+ @Test
+ public void fail_if_event_not_provided() {
+ expectedException.expect(IllegalArgumentException.class);
+
+ call(null);
+ }
+
+ @Test
+ public void ws_definition() {
+ WebService.Action definition = ws.getDef();
+ assertThat(definition.key()).isEqualTo("delete_event");
+ assertThat(definition.isPost()).isTrue();
+ assertThat(definition.isInternal()).isFalse();
+ assertThat(definition.param(PARAM_EVENT).isRequired()).isTrue();
+ }
+
+ private void call(@Nullable String event) {
+ TestRequest request = ws.newRequest();
+ if (event != null) {
+ request.setParam(PARAM_EVENT, event);
+ }
+
+ request.execute();
+ }
+}