aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorBelen Pruvost <belen.pruvost@sonarsource.com>2022-01-13 16:42:20 +0100
committersonartech <sonartech@sonarsource.com>2022-01-21 20:03:22 +0000
commit573f47d57bc4cc52235a22066d81b6be7dee4f3c (patch)
tree59444eac11c580e23bf1c22e8a2f2d61428d879a /server/sonar-db-dao
parent3ec97d1a4368fdd3212d524aacfc73403ed709ce (diff)
downloadsonarqube-573f47d57bc4cc52235a22066d81b6be7dee4f3c.tar.gz
sonarqube-573f47d57bc4cc52235a22066d81b6be7dee4f3c.zip
SONAR-14929 - Save a new issue as new on branch using reference branch
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDao.java4
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueMapper.java4
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/issue/NewCodeReferenceIssueDto.java69
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml34
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/issue/NewCodeReferenceIssueDtoTest.java48
6 files changed, 159 insertions, 2 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
index 691ce4c137b..19916bb3e24 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
@@ -73,6 +73,7 @@ import org.sonar.db.issue.IssueChangeDto;
import org.sonar.db.issue.IssueChangeMapper;
import org.sonar.db.issue.IssueDto;
import org.sonar.db.issue.IssueMapper;
+import org.sonar.db.issue.NewCodeReferenceIssueDto;
import org.sonar.db.issue.PrIssueDto;
import org.sonar.db.mapping.ProjectMappingDto;
import org.sonar.db.mapping.ProjectMappingsMapper;
@@ -197,6 +198,7 @@ public class MyBatis implements Startable {
confBuilder.loadAlias("IssueChange", IssueChangeDto.class);
confBuilder.loadAlias("KeyLongValue", KeyLongValue.class);
confBuilder.loadAlias("Issue", IssueDto.class);
+ confBuilder.loadAlias("NewCodeReferenceIssue", NewCodeReferenceIssueDto.class);
confBuilder.loadAlias("Measure", MeasureDto.class);
confBuilder.loadAlias("NotificationQueue", NotificationQueueDto.class);
confBuilder.loadAlias("PermissionTemplateCharacteristic", PermissionTemplateCharacteristicDto.class);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDao.java
index 52ef7a4f10c..bb9a136b987 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDao.java
@@ -58,6 +58,10 @@ public class IssueDao implements Dao {
return executeLargeInputs(keys, mapper(session)::selectByKeys);
}
+ public boolean isNewCodeOnReferencedBranch(DbSession session, String issueKey) {
+ return mapper(session).isNewCodeOnReferencedBranch(issueKey);
+ }
+
public Set<String> selectIssueKeysByComponentUuid(DbSession session, String componentUuid) {
return mapper(session).selectIssueKeysByComponentUuid(componentUuid);
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueMapper.java
index 80ea1be570c..49399d49cf9 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueMapper.java
@@ -37,6 +37,8 @@ public interface IssueMapper {
List<IssueDto> selectByKeys(List<String> keys);
+ boolean isNewCodeOnReferencedBranch(@Param("issueKey") String issueKey);
+
Set<String> selectIssueKeysByComponentUuid(@Param("componentUuid") String componentUuid);
List<IssueDto> selectByComponentUuidPaginated(@Param("componentUuid") String componentUuid,
@@ -50,6 +52,8 @@ public interface IssueMapper {
int update(IssueDto issue);
+ void insertAsNewOnReferenceBranch(NewCodeReferenceIssueDto issue);
+
int updateIfBeforeSelectedDate(IssueDto issue);
void scrollNonClosedByComponentUuid(@Param("componentUuid") String componentUuid, ResultHandler<IssueDto> handler);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/NewCodeReferenceIssueDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/NewCodeReferenceIssueDto.java
new file mode 100644
index 00000000000..3b870154465
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/NewCodeReferenceIssueDto.java
@@ -0,0 +1,69 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.io.Serializable;
+import org.sonar.core.util.UuidFactory;
+
+public final class NewCodeReferenceIssueDto implements Serializable {
+ private String uuid;
+ private String issueKey;
+
+ // technical date
+ private Long createdAt;
+
+ public NewCodeReferenceIssueDto() {
+ // nothing to do
+ }
+
+ public String getUuid() {
+ return uuid;
+ }
+
+ public NewCodeReferenceIssueDto setUuid(String uuid) {
+ this.uuid = uuid;
+ return this;
+ }
+
+ public String getIssueKey() {
+ return issueKey;
+ }
+
+ public NewCodeReferenceIssueDto setIssueKey(String issueKey) {
+ this.issueKey = issueKey;
+ return this;
+ }
+
+ public Long getCreatedAt() {
+ return createdAt;
+ }
+
+ public NewCodeReferenceIssueDto setCreatedAt(Long createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public static NewCodeReferenceIssueDto fromIssueDto(IssueDto issue, long now, UuidFactory uuidFactory) {
+ return new NewCodeReferenceIssueDto()
+ .setUuid(uuidFactory.create())
+ .setIssueKey(issue.getKey())
+ .setCreatedAt(now);
+ }
+}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
index 877acd0fcb7..8e84d87f0f7 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
@@ -118,6 +118,14 @@
#{quickFixAvailable, jdbcType=BOOLEAN})
</insert>
+ <insert id="insertAsNewOnReferenceBranch" parameterType="NewCodeReferenceIssue" useGeneratedKeys="false">
+ INSERT INTO new_code_reference_issues (uuid, issue_key, created_at)
+ VALUES (
+ #{uuid,jdbcType=VARCHAR},
+ #{issueKey,jdbcType=VARCHAR},
+ #{createdAt,jdbcType=BIGINT})
+ </insert>
+
<!--
IMPORTANT - invariant columns can't be updated. See IssueDto#toDtoForUpdate()
-->
@@ -185,7 +193,29 @@
where i.kee=#{kee,jdbcType=VARCHAR}
</select>
- <select id="scrollNonClosedByComponentUuid" parameterType="String" resultType="Issue" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+ <sql id="isNewCodeOnReferencedBranchSql">
+ select
+ case when exists
+ (
+ select i.uuid from new_code_reference_issues i
+ where i.issue_key = #{issueKey,jdbcType=VARCHAR}
+ )
+ then 1
+ else 0
+ end
+ </sql>
+
+ <select id="isNewCodeOnReferencedBranch" parameterType="String" resultType="boolean">
+ <include refid="isNewCodeOnReferencedBranchSql"/>
+ </select>
+
+ <select id="isNewCodeOnReferencedBranch" parameterType="String" resultType="boolean" databaseId="oracle">
+ <include refid="isNewCodeOnReferencedBranchSql"/>
+ from dual
+ </select>
+
+ <select id="scrollNonClosedByComponentUuid" parameterType="String" resultType="Issue" fetchSize="${_scrollFetchSize}"
+ resultSetType="FORWARD_ONLY">
select
<include refid="issueColumns"/>
from issues i
@@ -384,7 +414,7 @@
from
(select
row_number() over(order by i.issue_creation_date ASC) as row_number,
- <include refid="issueColumnsInInnerQuery" />
+ <include refid="issueColumnsInInnerQuery"/>
from issues i
where i.project_uuid=#{componentUuid,jdbcType=VARCHAR}
order by row_number asc
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/NewCodeReferenceIssueDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/NewCodeReferenceIssueDtoTest.java
new file mode 100644
index 00000000000..17bf644b99e
--- /dev/null
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/NewCodeReferenceIssueDtoTest.java
@@ -0,0 +1,48 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.Test;
+import org.sonar.core.util.UuidFactory;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class NewCodeReferenceIssueDtoTest {
+
+ private static final IssueDto ISSUE_DTO = mock(IssueDto.class);
+ private static final String KEY = "issue-key";
+ private static final String UUID = "uuid";
+ private static final UuidFactory UUID_FACTORY = mock(UuidFactory.class);
+
+ @Test
+ public void create_from_issue_dto() {
+ when(ISSUE_DTO.getKey()).thenReturn(KEY);
+ when(UUID_FACTORY.create()).thenReturn(UUID);
+ long now = System.currentTimeMillis();
+
+ NewCodeReferenceIssueDto dto = NewCodeReferenceIssueDto.fromIssueDto(ISSUE_DTO, now, UUID_FACTORY);
+
+ assertThat(dto.getUuid()).isEqualTo(UUID);
+ assertThat(dto.getIssueKey()).isEqualTo(KEY);
+ assertThat(dto.getCreatedAt()).isNotNull();
+ }
+}