From b34f03299ce0849cc832467a0ab4073c01e1d6b2 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 13 Dec 2016 17:34:51 +0100 Subject: [PATCH] SONAR-8468 Create WS api/project_analyses/delete --- .../ProjectAnalysisModule.java | 2 + .../projectanalysis/ws/CreateEventAction.java | 3 +- .../projectanalysis/ws/DeleteAction.java | 104 +++++++++++++ .../projectanalysis/ws/DeleteEventAction.java | 3 +- .../projectanalysis/ws/UpdateEventAction.java | 3 +- .../ProjectAnalysisModuleTest.java | 2 +- .../projectanalysis/ws/DeleteActionTest.java | 143 ++++++++++++++++++ .../org/sonar/db/component/SnapshotDao.java | 5 +- .../sonar/db/component/SnapshotMapper.java | 3 +- .../org/sonar/db/component/SnapshotMapper.xml | 7 +- .../sonar/db/component/SnapshotDaoTest.java | 16 +- .../client/projectanalysis/DeleteRequest.java | 35 +++++ 12 files changed, 311 insertions(+), 15 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteAction.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteActionTest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalysis/DeleteRequest.java 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 08de37424ed..dfee6a111a3 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.DeleteAction; import org.sonar.server.projectanalysis.ws.DeleteEventAction; import org.sonar.server.projectanalysis.ws.ProjectAnalysesWs; import org.sonar.server.projectanalysis.ws.SearchAction; @@ -37,6 +38,7 @@ public class ProjectAnalysisModule extends Module { CreateEventAction.class, UpdateEventAction.class, DeleteEventAction.class, + DeleteAction.class, SearchAction.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 cc5a422051f..affcfb36e93 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 @@ -129,7 +129,8 @@ public class CreateEventAction implements ProjectAnalysesWsAction { private EventDto insertDbEvent(DbSession dbSession, CreateEventRequest request, SnapshotDto analysis) { EventDto dbEvent = dbClient.eventDao().insert(dbSession, toDbEvent(request, analysis)); if (VERSION.equals(request.getCategory())) { - dbClient.snapshotDao().updateVersion(dbSession, analysis.getUuid(), request.getName()); + analysis.setVersion(request.getName()); + dbClient.snapshotDao().update(dbSession, analysis); } dbSession.commit(); return dbEvent; diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteAction.java new file mode 100644 index 00000000000..9e709934987 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/DeleteAction.java @@ -0,0 +1,104 @@ +/* + * 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.function.Consumer; +import java.util.function.Function; +import java.util.stream.Stream; +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.core.util.Uuids; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.component.SnapshotDto; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.user.UserSession; +import org.sonarqube.ws.client.projectanalysis.DeleteRequest; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.lang.String.format; +import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED; +import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_ANALYSIS; + +public class DeleteAction implements ProjectAnalysesWsAction { + private final DbClient dbClient; + private final UserSession userSession; + + public DeleteAction(DbClient dbClient, UserSession userSession) { + this.dbClient = dbClient; + this.userSession = userSession; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction("delete") + .setDescription("Delete an analysis.
" + + "Requires one of the following permissions:" + + "") + .setSince("6.3") + .setPost(true) + .setHandler(this); + + action.createParam(PARAM_ANALYSIS) + .setExampleValue(Uuids.UUID_EXAMPLE_04) + .setRequired(true); + } + + @Override + public void handle(Request request, Response response) throws Exception { + Stream.of(request) + .map(toWsRequest()) + .forEach(deleteAnalysis()); + + response.noContent(); + + } + + private static Function toWsRequest() { + return request -> new DeleteRequest(request.mandatoryParam(PARAM_ANALYSIS)); + } + + private Consumer deleteAnalysis() { + return request -> { + try (DbSession dbSession = dbClient.openSession(false)) { + SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, request.getAnalysis()) + .orElseThrow(() -> analysisNotFoundException(request.getAnalysis())); + if (STATUS_UNPROCESSED.equals(analysis.getStatus())) { + throw analysisNotFoundException(request.getAnalysis()); + } + userSession.checkComponentUuidPermission(UserRole.ADMIN, analysis.getComponentUuid()); + checkArgument(!analysis.getLast(), "The last analysis '%s' cannot be deleted", request.getAnalysis()); + analysis.setStatus(STATUS_UNPROCESSED); + dbClient.snapshotDao().update(dbSession, analysis); + dbSession.commit(); + } + }; + } + + private static NotFoundException analysisNotFoundException(String analysis) { + return new NotFoundException(format("Analysis '%s' not found", analysis)); + } +} 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 index 1fe95314717..69da18c6798 100644 --- 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 @@ -88,7 +88,8 @@ public class DeleteEventAction implements ProjectAnalysesWsAction { 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); + analysis.setVersion(null); + dbClient.snapshotDao().update(dbSession, analysis); } dbClient.eventDao().delete(dbSession, dbEvent.getUuid()); dbSession.commit(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java index 823c057f60a..652e02f00c6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java @@ -113,7 +113,8 @@ public class UpdateEventAction implements ProjectAnalysesWsAction { dbClient.eventDao().update(dbSession, event.getUuid(), event.getName(), event.getDescription()); if (VERSION.getLabel().equals(event.getCategory())) { SnapshotDto analysis = getAnalysis(dbSession, event); - dbClient.snapshotDao().updateVersion(dbSession, analysis.getUuid(), event.getName()); + analysis.setVersion(event.getName()); + dbClient.snapshotDao().update(dbSession, analysis); } dbSession.commit(); }; 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 b7841ce9ec2..0a73927f7a3 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 + 5); + assertThat(container.size()).isEqualTo(2 + 6); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteActionTest.java new file mode 100644 index 00000000000..917ae8947eb --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/DeleteActionTest.java @@ -0,0 +1,143 @@ +/* + * 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 org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.server.ws.WebService; +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.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsActionTester; + +import static com.google.common.collect.Lists.newArrayList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED; +import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED; +import static org.sonar.db.component.SnapshotTesting.newAnalysis; +import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_ANALYSIS; + +public class DeleteActionTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public UserSessionRule userSession = UserSessionRule.standalone().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + @Rule + public DbTester db = DbTester.create(); + private DbClient dbClient = db.getDbClient(); + private DbSession dbSession = db.getSession(); + + private WsActionTester ws = new WsActionTester(new DeleteAction(dbClient, userSession)); + + @Test + public void delete_as_global_admin() { + ComponentDto project = db.components().insertProject(); + db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false).setStatus(STATUS_PROCESSED)); + db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setLast(true).setStatus(STATUS_PROCESSED)); + + call("A1"); + + db.commit(); + assertThat(dbClient.snapshotDao().selectByUuids(dbSession, newArrayList("A1", "A2"))).extracting(SnapshotDto::getUuid, SnapshotDto::getStatus).containsExactly( + tuple("A1", STATUS_UNPROCESSED), + tuple("A2", STATUS_PROCESSED)); + } + + @Test + public void delete_as_project_admin() { + ComponentDto project = db.components().insertProject(); + db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false).setStatus(STATUS_PROCESSED)); + db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setLast(true).setStatus(STATUS_PROCESSED)); + userSession.anonymous().addProjectUuidPermissions(UserRole.ADMIN, project.uuid()); + + call("A1"); + + db.commit(); + assertThat(dbClient.snapshotDao().selectByUuids(dbSession, newArrayList("A1", "A2"))).extracting(SnapshotDto::getUuid, SnapshotDto::getStatus).containsExactly( + tuple("A1", STATUS_UNPROCESSED), + tuple("A2", STATUS_PROCESSED)); + } + + @Test + public void definition() { + WebService.Action definition = ws.getDef(); + + assertThat(definition.key()).isEqualTo("delete"); + assertThat(definition.isPost()).isTrue(); + assertThat(definition.param("analysis").isRequired()).isTrue(); + } + + @Test + public void fail_when_last_analysis() { + ComponentDto project = db.components().insertProject(); + db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(true)); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The last analysis 'A1' cannot be deleted"); + + call("A1"); + } + + @Test + public void fail_when_analysis_not_found() { + expectedException.expect(NotFoundException.class); + expectedException.expectMessage("Analysis 'A42' not found"); + + call("A42"); + } + + @Test + public void fail_when_analysis_is_unprocessed() { + ComponentDto project = db.components().insertProject(); + db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false).setStatus(STATUS_UNPROCESSED)); + + expectedException.expect(NotFoundException.class); + expectedException.expectMessage("Analysis 'A1' not found"); + + call("A1"); + } + + @Test + public void fail_when_not_enough_permission() { + userSession.anonymous(); + ComponentDto project = db.components().insertProject(); + db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setLast(false)); + + expectedException.expect(ForbiddenException.class); + + call("A1"); + } + + private void call(String analysis) { + ws.newRequest() + .setParam(PARAM_ANALYSIS, analysis) + .execute(); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java index db3f6384066..bde593592a5 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -138,8 +138,8 @@ public class SnapshotDao implements Dao { insert(session, Lists.asList(item, others)); } - public void updateVersion(DbSession dbSession, String analysisUuid, @Nullable String version) { - mapper(dbSession).updateVersion(analysisUuid, version); + public void update(DbSession dbSession, SnapshotDto analysis) { + mapper(dbSession).update(analysis); } /** @@ -152,6 +152,7 @@ public class SnapshotDao implements Dao { .orNull(); } + private static SnapshotMapper mapper(DbSession session) { return session.getMapper(SnapshotMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java index af570aedbee..56c4b28bd38 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -22,7 +22,6 @@ package org.sonar.db.component; import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; -import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.RowBounds; @@ -57,5 +56,5 @@ public interface SnapshotMapper { void setIsLastFlagForAnalysisUuid(@Param("analysisUuid") String analysisUuid); - void updateVersion(@Param("analysisUuid") String analysisUuid, @Param("version") @Nullable String version); + void update(SnapshotDto analysis); } diff --git a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index b27c1e31abf..102c9497a51 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -177,10 +177,11 @@ where uuid = #{analysisUuid} - + update snapshots - set version = #{version, jdbcType=VARCHAR} - where uuid = #{analysisUuid} + set version = #{version, jdbcType=VARCHAR}, + status = #{status, jdbcType=VARCHAR} + where uuid = #{uuid} diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index 5f957466a87..be6a98a301c 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -36,6 +36,7 @@ import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED; +import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED; import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC; @@ -298,22 +299,29 @@ public class SnapshotDaoTest { } @Test - public void updateVersion() { - insertAnalysis("P1", "A1", STATUS_PROCESSED, true); + public void update() { + SnapshotDto analysis = insertAnalysis("P1", "A1", STATUS_PROCESSED, false); db.commit(); + analysis + .setComponentUuid("P42") + .setVersion("5.6.3") + .setStatus(STATUS_UNPROCESSED); - underTest.updateVersion(dbSession, "A1", "5.6.3"); + underTest.update(dbSession, analysis); SnapshotDto result = underTest.selectByUuid(dbSession, "A1").get(); assertThat(result.getVersion()).isEqualTo("5.6.3"); + assertThat(result.getStatus()).isEqualTo(STATUS_UNPROCESSED); + assertThat(result.getComponentUuid()).isEqualTo("P1"); } - private void insertAnalysis(String projectUuid, String uuid, String status, boolean isLastFlag) { + private SnapshotDto insertAnalysis(String projectUuid, String uuid, String status, boolean isLastFlag) { SnapshotDto snapshot = SnapshotTesting.newAnalysis(ComponentTesting.newProjectDto(projectUuid)) .setLast(isLastFlag) .setStatus(status) .setUuid(uuid); underTest.insert(db.getSession(), snapshot); + return snapshot; } private void verifyStatusAndIsLastFlag(String uuid, String expectedStatus, boolean expectedLastFlag) { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalysis/DeleteRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalysis/DeleteRequest.java new file mode 100644 index 00000000000..e8474110534 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalysis/DeleteRequest.java @@ -0,0 +1,35 @@ +/* + * 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.sonarqube.ws.client.projectanalysis; + +import static java.util.Objects.requireNonNull; + +public class DeleteRequest { + private final String analysis; + + public DeleteRequest(String analysis) { + this.analysis = requireNonNull(analysis); + } + + public String getAnalysis() { + return analysis; + } +} -- 2.39.5