From 30e65beada07ec62d2d7f9d5efa806ad69a80699 Mon Sep 17 00:00:00 2001 From: OrlovAlexander <35396155+OrlovAlexander85@users.noreply.github.com> Date: Fri, 24 May 2024 10:20:53 +0200 Subject: [PATCH] SONAR-22252 Update IssueDto, ActiveRuleDto and corresponding mappers --- .../java/org/sonar/db/issue/IssueDto.java | 11 ++++ .../db/qualityprofile/ActiveRuleDto.java | 11 +++- .../org/sonar/db/issue/IssueMapper.xml | 26 +++++--- .../db/qualityprofile/ActiveRuleMapper.xml | 8 ++- .../java/org/sonar/db/issue/IssueDtoTest.java | 3 + .../org/sonar/core/issue/DefaultIssue.java | 13 +++- .../sonar/core/issue/DefaultIssueTest.java | 66 +++++++++++-------- 7 files changed, 97 insertions(+), 41 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java index dfa4c7e74e6..22226973246 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java @@ -87,6 +87,7 @@ public final class IssueDto implements Serializable { private boolean quickFixAvailable; private boolean isNewCodeReferenceIssue; private String ruleDescriptionContextKey; + private boolean prioritizedRule; // functional dates stored as Long private Long issueCreationDate; @@ -862,6 +863,15 @@ public final class IssueDto implements Serializable { .collect(toUnmodifiableMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)); } + public boolean isPrioritizedRule() { + return prioritizedRule; + } + + public IssueDto setPrioritizedRule(boolean isBlockerRule) { + this.prioritizedRule = isBlockerRule; + return this; + } + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); @@ -880,6 +890,7 @@ public final class IssueDto implements Serializable { issue.setLine(line); issue.setChecksum(checksum); issue.setSeverity(severity); + issue.setPrioritizedRule(prioritizedRule); issue.setAssigneeUuid(assigneeUuid); issue.setAssigneeLogin(assigneeLogin); issue.setComponentKey(componentKey); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index 005dff79be5..c69dea3971a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -41,9 +41,9 @@ public class ActiveRuleDto { private String ruleUuid; private Integer severity; private String inheritance; - private long createdAt; private long updatedAt; + private boolean prioritizedRule; // These fields do not exists in db, it's only retrieve by joins private String repository; @@ -170,6 +170,15 @@ public class ActiveRuleDto { return this; } + public boolean isPrioritizedRule(){ + return prioritizedRule; + } + + public ActiveRuleDto setPrioritizedRule(boolean isBlockerRule){ + this.prioritizedRule = isBlockerRule; + return this; + } + public static ActiveRuleDto createFor(QProfileDto profile, RuleDto ruleDto) { requireNonNull(profile.getRulesProfileUuid(), "Profile is not persisted"); requireNonNull(ruleDto.getUuid(), "Rule is not persisted"); 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 12aafee88dd..0a9a7730ce4 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 @@ -42,6 +42,7 @@ i.issue_type as type, i.quick_fix_available as quickFixAvailable, i.code_variants as codeVariantsString, + i.prioritized_rule as prioritizedRule, @@ -75,7 +76,8 @@ i.issue_type, i.quick_fix_available, i.code_variants, - i.clean_code_attribute + i.clean_code_attribute, + i.prioritized_rule @@ -131,7 +133,7 @@ + javaType="java.util.Set" ofType="Impact"> @@ -142,7 +144,7 @@ message, message_formattings, line, locations, gap, effort, status, tags, rule_description_context_key, resolution, checksum, assignee, author_login, issue_creation_date, issue_update_date, issue_close_date, created_at, updated_at, component_uuid, project_uuid, issue_type, quick_fix_available, code_variants, - clean_code_attribute) + clean_code_attribute, prioritized_rule) VALUES ( #{kee,jdbcType=VARCHAR}, #{ruleUuid,jdbcType=VARCHAR}, @@ -164,7 +166,8 @@ #{componentUuid,jdbcType=VARCHAR}, #{projectUuid,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{quickFixAvailable, jdbcType=BOOLEAN}, #{codeVariantsString,jdbcType=VARCHAR}, - #{effectiveCleanCodeAttribute,jdbcType=VARCHAR}) + #{effectiveCleanCodeAttribute,jdbcType=VARCHAR}, + #{prioritizedRule,jdbcType=BOOLEAN}) @@ -183,12 +186,12 @@ #{dto.severity,jdbcType=VARCHAR}) - + DELETE FROM new_code_reference_issues where issue_key = #{issueKey, jdbcType=VARCHAR} - + DELETE FROM issues_impacts where issue_key = #{issueUuid, jdbcType=VARCHAR} @@ -220,7 +223,8 @@ updated_at=#{updatedAt,jdbcType=BIGINT}, issue_type=#{type,jdbcType=INTEGER}, code_variants=#{codeVariantsString,jdbcType=VARCHAR}, - clean_code_attribute=#{effectiveCleanCodeAttribute,jdbcType=VARCHAR} + clean_code_attribute=#{effectiveCleanCodeAttribute,jdbcType=VARCHAR}, + prioritized_rule=#{prioritizedRule,jdbcType=BOOLEAN} where kee = #{kee} @@ -252,7 +256,8 @@ updated_at=#{updatedAt,jdbcType=BIGINT}, issue_type=#{type,jdbcType=INTEGER}, code_variants=#{codeVariantsString,jdbcType=VARCHAR}, - clean_code_attribute=#{effectiveCleanCodeAttribute,jdbcType=VARCHAR} + clean_code_attribute=#{effectiveCleanCodeAttribute,jdbcType=VARCHAR}, + prioritized_rule=#{prioritizedRule,jdbcType=BOOLEAN} where kee = #{kee} and updated_at <= #{selectedAt} @@ -271,7 +276,8 @@ where i.kee=#{kee,jdbcType=VARCHAR} - select from issues i @@ -362,6 +368,7 @@ r.security_standards as securityStandards, c.qualifier, i.code_variants as codeVariants, + i.prioritized_rule as prioritizedRule, @@ -634,6 +641,7 @@ i.assignee as assigneeUuid, u.login as assigneeLogin, i.rule_description_context_key as ruleDescriptionContextKey, + i.prioritized_rule as prioritizedRule, i.clean_code_attribute as cleanCodeAttribute, diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 96c6fd22f17..3facaac4dba 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -13,7 +13,8 @@ r.plugin_name as "repository", rp.uuid as "ruleProfileUuid", a.created_at as "createdAt", - a.updated_at as "updatedAt" + a.updated_at as "updatedAt", + a.prioritized_rule as "prioritizedRule" @@ -22,6 +23,7 @@ a.rule_uuid as "ruleUuid", a.failure_level as "severity", a.inheritance as "inheritance", + a.prioritized_rule as "prioritizedRule", r.plugin_rule_key as "rulefield", r.plugin_name as "repository", r.security_standards as "securityStandards", @@ -44,6 +46,7 @@ rule_uuid, failure_level, inheritance, + prioritized_rule, created_at, updated_at ) values ( @@ -52,6 +55,7 @@ #{ruleUuid, jdbcType=VARCHAR}, #{severity, jdbcType=INTEGER}, #{inheritance, jdbcType=VARCHAR}, + #{prioritizedRule, jdbcType=BOOLEAN}, #{createdAt, jdbcType=BIGINT}, #{updatedAt, jdbcType=BIGINT} ) @@ -62,6 +66,7 @@ set failure_level = #{severity, jdbcType=INTEGER}, inheritance = #{inheritance, jdbcType=VARCHAR}, + prioritized_rule = #{prioritizedRule, jdbcType=BOOLEAN}, updated_at = #{updatedAt, jdbcType=BIGINT} where uuid = #{uuid, jdbcType=BIGINT} @@ -306,6 +311,7 @@ ar.uuid as "uuid", ar.failure_level as "severity", ar.inheritance as "inheritance", + ar.prioritized_rule as "prioritizedRule", r.uuid as "ruleUuid", r.plugin_name as "repository", r.plugin_rule_key as "key", diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java index 84508faee34..a320ae28522 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java @@ -81,6 +81,7 @@ class IssueDtoTest { .setEffort(10L) .setLine(6) .setSeverity("BLOCKER") + .setPrioritizedRule(true) .setMessage("message") .setMessageFormattings(EXAMPLE_MESSAGE_FORMATTINGS) .setManualSeverity(true) @@ -107,6 +108,7 @@ class IssueDtoTest { .setEffort(Duration.create(10L)) .setLine(6) .setSeverity("BLOCKER") + .setPrioritizedRule(true) .setMessage("message") .setMessageFormattings(DbIssues.MessageFormattings.parseFrom(EXAMPLE_MESSAGE_FORMATTINGS.toByteArray())) .setManualSeverity(true) @@ -377,6 +379,7 @@ class IssueDtoTest { .setResolution(Issue.RESOLUTION_FALSE_POSITIVE) .setStatus(Issue.STATUS_CLOSED) .setSeverity("BLOCKER") + .setPrioritizedRule(true) .setManualSeverity(true) .setChecksum("123") .setAssigneeUuid("123") diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java index 7afc0784a49..83427a1a193 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java @@ -81,6 +81,7 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure. private List comments = null; private Set tags = null; private Set codeVariants = null; + private boolean prioritizedRule = false; // temporarily an Object as long as DefaultIssue is used by sonar-batch private Object locations = null; @@ -536,7 +537,8 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure. return this; } - public DefaultIssue setFieldChange(IssueChangeContext context, String field, @Nullable Serializable oldValue, @Nullable Serializable newValue) { + public DefaultIssue setFieldChange(IssueChangeContext context, String field, @Nullable Serializable oldValue, + @Nullable Serializable newValue) { if (!Objects.equals(oldValue, newValue)) { if (currentChange == null) { currentChange = new FieldDiffs(); @@ -726,6 +728,15 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure. return this; } + public boolean isPrioritizedRule() { + return prioritizedRule; + } + + public DefaultIssue setPrioritizedRule(boolean isBlockerRule) { + this.prioritizedRule = isBlockerRule; + return this; + } + @Nullable public CleanCodeAttribute getCleanCodeAttribute() { return cleanCodeAttribute; diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java index a550be9ca87..a50efdc2ef4 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java @@ -22,7 +22,7 @@ package org.sonar.core.issue; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.sonar.api.issue.Issue; import org.sonar.api.issue.IssueStatus; import org.sonar.api.issue.impact.Severity; @@ -35,12 +35,12 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class DefaultIssueTest { +class DefaultIssueTest { private final DefaultIssue issue = new DefaultIssue(); @Test - public void set_empty_dates() { + void set_empty_dates() { issue .setCreationDate(null) .setUpdateDate(null) @@ -54,7 +54,7 @@ public class DefaultIssueTest { } @Test - public void fail_on_empty_status() { + void fail_on_empty_status() { try { issue.setStatus(""); fail(); @@ -64,7 +64,7 @@ public class DefaultIssueTest { } @Test - public void fail_on_bad_severity() { + void fail_on_bad_severity() { try { issue.setSeverity("FOO"); fail(); @@ -74,19 +74,19 @@ public class DefaultIssueTest { } @Test - public void message_should_be_abbreviated_if_too_long() { + void message_should_be_abbreviated_if_too_long() { issue.setMessage(StringUtils.repeat("a", 5_000)); assertThat(issue.message()).hasSize(1_333); } @Test - public void message_could_be_null() { + void message_could_be_null() { issue.setMessage(null); assertThat(issue.message()).isNull(); } @Test - public void test_nullable_fields() { + void test_nullable_fields() { issue.setGap(null).setSeverity(null).setLine(null); assertThat(issue.gap()).isNull(); assertThat(issue.severity()).isNull(); @@ -94,7 +94,7 @@ public class DefaultIssueTest { } @Test - public void test_equals_and_hashCode() { + void test_equals_and_hashCode() { DefaultIssue a1 = new DefaultIssue().setKey("AAA"); DefaultIssue a2 = new DefaultIssue().setKey("AAA"); DefaultIssue b = new DefaultIssue().setKey("BBB"); @@ -106,7 +106,7 @@ public class DefaultIssueTest { } @Test - public void comments_should_not_be_modifiable() { + void comments_should_not_be_modifiable() { DefaultIssue issue = new DefaultIssue().setKey("AAA"); List comments = issue.defaultIssueComments(); @@ -123,7 +123,7 @@ public class DefaultIssueTest { } @Test - public void all_changes_contain_current_change() { + void all_changes_contain_current_change() { IssueChangeContext issueChangeContext = mock(IssueChangeContext.class); when(issueChangeContext.getExternalUser()).thenReturn("toto"); when(issueChangeContext.getWebhookSource()).thenReturn("github"); @@ -139,7 +139,7 @@ public class DefaultIssueTest { } @Test - public void setFieldChange_whenAddingChange_shouldUpdateCurrentChange() { + void setFieldChange_whenAddingChange_shouldUpdateCurrentChange() { IssueChangeContext issueChangeContext = mock(IssueChangeContext.class); DefaultIssue issue = new DefaultIssue().setKey("AAA"); @@ -158,7 +158,7 @@ public class DefaultIssueTest { } @Test - public void adding_null_change_has_no_effect() { + void adding_null_change_has_no_effect() { DefaultIssue issue = new DefaultIssue(); issue.addChange(null); @@ -167,7 +167,7 @@ public class DefaultIssueTest { } @Test - public void test_isToBeMigratedAsNewCodeReferenceIssue_is_correctly_calculated() { + void test_isToBeMigratedAsNewCodeReferenceIssue_is_correctly_calculated() { issue.setKey("ABCD") .setIsOnChangedLine(true) .setIsNewCodeReferenceIssue(false) @@ -219,7 +219,7 @@ public class DefaultIssueTest { } @Test - public void isQuickFixAvailable_givenQuickFixAvailable_returnTrue() { + void isQuickFixAvailable_givenQuickFixAvailable_returnTrue() { DefaultIssue defaultIssue = new DefaultIssue(); defaultIssue.setQuickFixAvailable(true); @@ -232,7 +232,7 @@ public class DefaultIssueTest { } @Test - public void setLine_whenLineIsNegative_shouldThrowException() { + void setLine_whenLineIsNegative_shouldThrowException() { int anyNegativeValue = Integer.MIN_VALUE; assertThatThrownBy(() -> issue.setLine(anyNegativeValue)) .isInstanceOf(IllegalArgumentException.class) @@ -240,14 +240,14 @@ public class DefaultIssueTest { } @Test - public void setLine_whenLineIsZero_shouldThrowException() { + void setLine_whenLineIsZero_shouldThrowException() { assertThatThrownBy(() -> issue.setLine(0)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Line must be null or greater than zero (got 0)"); } @Test - public void setGap_whenGapIsNegative_shouldThrowException() { + void setGap_whenGapIsNegative_shouldThrowException() { Double anyNegativeValue = -1.0; assertThatThrownBy(() -> issue.setGap(anyNegativeValue)) .isInstanceOf(IllegalArgumentException.class) @@ -255,41 +255,41 @@ public class DefaultIssueTest { } @Test - public void setGap_whenGapIsZero_shouldWork() { + void setGap_whenGapIsZero_shouldWork() { issue.setGap(0.0); assertThat(issue.gap()).isEqualTo(0.0); } @Test - public void effortInMinutes_shouldConvertEffortToMinutes() { + void effortInMinutes_shouldConvertEffortToMinutes() { issue.setEffort(Duration.create(60)); assertThat(issue.effortInMinutes()).isEqualTo(60L); } @Test - public void effortInMinutes_whenNull_shouldReturnNull() { + void effortInMinutes_whenNull_shouldReturnNull() { issue.setEffort(null); assertThat(issue.effortInMinutes()).isNull(); } @Test - public void tags_whenNull_shouldReturnEmptySet() { + void tags_whenNull_shouldReturnEmptySet() { assertThat(issue.tags()).isEmpty(); } @Test - public void codeVariants_whenNull_shouldReturnEmptySet() { + void codeVariants_whenNull_shouldReturnEmptySet() { assertThat(issue.codeVariants()).isEmpty(); } @Test - public void issueByDefault_shouldNotHaveAppliedAnticipatedTransitions() { + void issueByDefault_shouldNotHaveAppliedAnticipatedTransitions() { DefaultIssue defaultIssue = new DefaultIssue(); assertThat(defaultIssue.getAnticipatedTransitionUuid()).isNotPresent(); } @Test - public void anticipatedTransitions_WhenSetTrue_shouldReturnTrue() { + void anticipatedTransitions_WhenSetTrue_shouldReturnTrue() { DefaultIssue defaultIssue = new DefaultIssue(); defaultIssue.setAnticipatedTransitionUuid("uuid"); assertThat(defaultIssue.getAnticipatedTransitionUuid()).isPresent(); @@ -297,15 +297,16 @@ public class DefaultIssueTest { } @Test - public void getImpacts_whenAddingNewImpacts_shouldReturnListOfImpacts() { + void getImpacts_whenAddingNewImpacts_shouldReturnListOfImpacts() { issue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); issue.addImpact(SoftwareQuality.RELIABILITY, Severity.LOW); - assertThat(issue.impacts()).containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, SoftwareQuality.RELIABILITY, Severity.LOW)); + assertThat(issue.impacts()).containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, + SoftwareQuality.RELIABILITY, Severity.LOW)); } @Test - public void getIssueStatus_shouldReturnExpectedStatus() { + void getIssueStatus_shouldReturnExpectedStatus() { issue.setStatus(Issue.STATUS_RESOLVED); issue.setResolution(Issue.RESOLUTION_FIXED); @@ -313,7 +314,7 @@ public class DefaultIssueTest { } @Test - public void replaceImpacts_shouldReplaceExistingImpacts() { + void replaceImpacts_shouldReplaceExistingImpacts() { issue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); issue.addImpact(SoftwareQuality.RELIABILITY, Severity.LOW); @@ -321,4 +322,11 @@ public class DefaultIssueTest { assertThat(issue.impacts()).containsExactlyEntriesOf(Map.of(SoftwareQuality.SECURITY, Severity.LOW)); } + + @Test + void prioritizedRule_shouldHaveCorrectDefaultValue() { + assertThat(issue.isPrioritizedRule()).isFalse(); + issue.setPrioritizedRule(true); + assertThat(issue.isPrioritizedRule()).isTrue(); + } } -- 2.39.5