From bb784c0d359bdb37286c77d25897728b8ea85563 Mon Sep 17 00:00:00 2001 From: Dimitris Kavvathas Date: Thu, 20 Jul 2023 15:41:51 +0200 Subject: [PATCH] SONAR-19372 add api/issues/anticipated_transitions endpoint --- .../db/issue/AnticipatedTransitionDaoIT.java | 59 +++++ .../db/issue/AnticipatedTransitionDao.java | 23 ++ .../db/issue/AnticipatedTransitionDto.java | 15 ++ .../db/issue/AnticipatedTransitionMapper.java | 2 + .../db/issue/AnticipatedTransitionMapper.xml | 4 + .../AnticipatedTransitionsActionIT.java | 240 ++++++++++++++++++ .../sonar/server/issue/ws/IssueWsModule.java | 11 +- .../AnticipatedTransitionHandler.java | 69 +++++ .../AnticipatedTransitionParser.java | 56 ++++ .../AnticipatedTransitionsAction.java | 105 ++++++++ ...AnticipatedTransitionsActionValidator.java | 69 +++++ .../GsonAnticipatedTransition.java | 28 ++ .../AnticipatedTransitionHandlerTest.java | 111 ++++++++ .../AnticipatedTransitionParserTest.java | 108 ++++++++ ...cipatedTransitionsActionValidatorTest.java | 137 ++++++++++ .../request-with-transitions.json | 20 ++ .../core/issue/AnticipatedTransition.java | 150 +++++++++++ .../core/issue/AnticipatedTransitionTest.java | 74 ++++++ .../ws/client/issue/IssuesWsParameters.java | 1 + 19 files changed, 1281 insertions(+), 1 deletion(-) create mode 100644 server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionsActionIT.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionHandler.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionParser.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionsAction.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionsActionValidator.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/anticipatedtransition/GsonAnticipatedTransition.java create mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionHandlerTest.java create mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionParserTest.java create mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/anticipatedtransition/AnticipatedTransitionsActionValidatorTest.java create mode 100644 server/sonar-webserver-webapi/src/test/resources/org/sonar/server/issue/ws/anticipatedtransition/request-with-transitions.json create mode 100644 sonar-core/src/main/java/org/sonar/core/issue/AnticipatedTransition.java create mode 100644 sonar-core/src/test/java/org/sonar/core/issue/AnticipatedTransitionTest.java diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/issue/AnticipatedTransitionDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/issue/AnticipatedTransitionDaoIT.java index 417455f9b65..a47126da22d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/issue/AnticipatedTransitionDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/issue/AnticipatedTransitionDaoIT.java @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.db.issue; import org.junit.Rule; @@ -45,4 +64,44 @@ public class AnticipatedTransitionDaoIT { var anticipatedTransitionDtosDeleted = underTest.selectByProjectUuid(db.getSession(), projectUuid); assertThat(anticipatedTransitionDtosDeleted).isEmpty(); } + + @Test + public void deleteByProjectAndUser_shouldDeleteAllRelatedRecords() { + // given + final String projectUuid1 = "project1"; + final String projectUuid2 = "project2"; + String userUuid1 = "user1"; + String userUuid2 = "user2"; + + generateAndInsertAnticipatedTransition("uuid1", projectUuid1, userUuid1); // should be deleted + generateAndInsertAnticipatedTransition("uuid2", projectUuid1, userUuid1); // should be deleted + generateAndInsertAnticipatedTransition("uuid3", projectUuid2, userUuid1); + generateAndInsertAnticipatedTransition("uuid4", projectUuid1, userUuid2); + generateAndInsertAnticipatedTransition("uuid5", projectUuid2, userUuid2); + generateAndInsertAnticipatedTransition("uuid6", projectUuid1, userUuid1); // should be deleted + + // when + underTest.deleteByProjectAndUser(db.getSession(), projectUuid1, userUuid1); + + // then + assertThat(underTest.selectByProjectUuid(db.getSession(), projectUuid1)).hasSize(1); + assertThat(underTest.selectByProjectUuid(db.getSession(), projectUuid2)).hasSize(2); + } + + private void generateAndInsertAnticipatedTransition(String uuid, String projectUuid1, String userUuid1) { + AnticipatedTransitionDto transition = new AnticipatedTransitionDto( + uuid, + projectUuid1, + userUuid1, + "transition", + "status", + "comment", + 1, + "message", + "lineHash", + "ruleKey"); + + // insert one + underTest.insert(db.getSession(), transition); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDao.java index 6deb8bea985..78c3b787c67 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDao.java @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.db.issue; import java.util.List; @@ -14,6 +33,10 @@ public class AnticipatedTransitionDao implements Dao { mapper(session).delete(uuid); } + public void deleteByProjectAndUser(DbSession dbSession, String projectUuid, String userUuid) { + mapper(dbSession).deleteByProjectAndUser(projectUuid, userUuid); + } + public List selectByProjectUuid(DbSession session, String projectUuid) { return mapper(session).selectByProjectUuid(projectUuid); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDto.java index 7611d8dd8bd..7991e06401d 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionDto.java @@ -20,6 +20,7 @@ package org.sonar.db.issue; import javax.annotation.Nullable; +import org.sonar.core.issue.AnticipatedTransition; public class AnticipatedTransitionDto { private String uuid; @@ -138,4 +139,18 @@ public class AnticipatedTransitionDto { public void setRuleKey(String ruleKey) { this.ruleKey = ruleKey; } + + public static AnticipatedTransitionDto toDto(AnticipatedTransition anticipatedTransition, String uuid, String projectUuid) { + return new AnticipatedTransitionDto( + uuid, + projectUuid, + anticipatedTransition.getUserUuid(), + anticipatedTransition.getTransition(), + anticipatedTransition.getStatus(), + anticipatedTransition.getComment(), + anticipatedTransition.getLine(), + anticipatedTransition.getMessage(), + anticipatedTransition.getLineHash(), + anticipatedTransition.getRuleKey().toString()); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionMapper.java index dc8383557ee..7459f316536 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/AnticipatedTransitionMapper.java @@ -27,5 +27,7 @@ public interface AnticipatedTransitionMapper { void delete(@Param("uuid") String uuid); + void deleteByProjectAndUser(@Param("projectUuid") String projectUuid, @Param("userUuid") String userUuid); + List selectByProjectUuid(@Param("projectUuid") String projectUuid); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/AnticipatedTransitionMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/AnticipatedTransitionMapper.xml index 3160d2d164a..89a8c9e2181 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/AnticipatedTransitionMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/AnticipatedTransitionMapper.xml @@ -30,6 +30,10 @@ delete from anticipated_transitions where uuid=#{uuid} + + delete from anticipated_transitions where project_uuid=#{projectUuid,jdbcType=VARCHAR} and user_uuid=#{userUuid,jdbcType=VARCHAR} + +