diff options
Diffstat (limited to 'server/sonar-server-common')
2 files changed, 56 insertions, 17 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/IssueFieldsSetter.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/IssueFieldsSetter.java index 7e9e63543af..482fc0791a0 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/IssueFieldsSetter.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/IssueFieldsSetter.java @@ -23,22 +23,19 @@ import com.google.common.base.Joiner; import java.time.temporal.ChronoUnit; import java.util.Collection; import java.util.Date; -import java.util.EnumMap; import java.util.HashSet; -import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.issue.IssueStatus; -import org.sonar.api.issue.impact.Severity; -import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.server.ServerSide; import org.sonar.api.server.rule.RuleTagFormat; import org.sonar.api.utils.Duration; +import org.sonar.core.issue.DefaultImpact; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.IssueChangeContext; @@ -262,7 +259,7 @@ public class IssueFieldsSetter { public boolean setIssueStatus(DefaultIssue issue, @Nullable IssueStatus previousIssueStatus, @Nullable IssueStatus newIssueStatus, IssueChangeContext context) { if (!Objects.equals(newIssueStatus, previousIssueStatus)) { - //Currently, issue status is not persisted in database, but is considered as an issue change + // Currently, issue status is not persisted in database, but is considered as an issue change issue.setFieldChange(context, ISSUE_STATUS, previousIssueStatus, issue.issueStatus()); return true; } @@ -346,8 +343,8 @@ public class IssueFieldsSetter { for (int i = 0; i < l1c.getMessageFormattingCount(); i++) { if (l1c.getMessageFormatting(i).getStart() != l2.getMessageFormatting(i).getStart() - || l1c.getMessageFormatting(i).getEnd() != l2.getMessageFormatting(i).getEnd() - || l1c.getMessageFormatting(i).getType() != l2.getMessageFormatting(i).getType()) { + || l1c.getMessageFormatting(i).getEnd() != l2.getMessageFormatting(i).getEnd() + || l1c.getMessageFormatting(i).getType() != l2.getMessageFormatting(i).getType()) { return false; } } @@ -372,7 +369,7 @@ public class IssueFieldsSetter { public void setPrioritizedRule(DefaultIssue issue, boolean prioritizedRule, IssueChangeContext context) { if (!Objects.equals(prioritizedRule, issue.isPrioritizedRule())) { issue.setPrioritizedRule(prioritizedRule); - if (!issue.isNew()){ + if (!issue.isNew()) { issue.setUpdateDate(context.date()); issue.setChanged(true); } @@ -463,14 +460,17 @@ public class IssueFieldsSetter { return false; } - public boolean setImpacts(DefaultIssue issue, Map<SoftwareQuality, Severity> previousImpacts, IssueChangeContext context) { - Map<SoftwareQuality, Severity> currentImpacts = new EnumMap<>(issue.impacts()); - if (!previousImpacts.equals(currentImpacts)) { - issue.replaceImpacts(currentImpacts); + public boolean setImpacts(DefaultIssue issue, Set<DefaultImpact> previousImpacts, IssueChangeContext context) { + previousImpacts + .stream().filter(DefaultImpact::manualSeverity) + .forEach(i -> issue.addImpact(i.softwareQuality(), i.severity(), true)); + + if (!previousImpacts.equals(issue.getImpacts())) { issue.setUpdateDate(context.date()); issue.setChanged(true); return true; } + return false; } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/IssueFieldsSetterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/IssueFieldsSetterTest.java index 85e08425108..6d80dba6d82 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/IssueFieldsSetterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/IssueFieldsSetterTest.java @@ -24,7 +24,6 @@ import java.util.Calendar; import java.util.Date; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Random; import java.util.Set; import org.apache.commons.lang3.time.DateUtils; @@ -36,6 +35,7 @@ import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.utils.Duration; +import org.sonar.core.issue.DefaultImpact; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.FieldDiffs; import org.sonar.core.issue.IssueChangeContext; @@ -577,13 +577,52 @@ class IssueFieldsSetterTest { @Test void setImpacts_whenImpactAdded_shouldBeUpdated() { - Map<SoftwareQuality, Severity> currentImpacts = Map.of(SoftwareQuality.RELIABILITY, Severity.LOW); - Map<SoftwareQuality, Severity> newImpacts = Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); + Set<DefaultImpact> currentImpacts = Set.of(new DefaultImpact(SoftwareQuality.RELIABILITY, Severity.LOW, false)); + Set<DefaultImpact> newImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, false)); - issue.replaceImpacts(newImpacts); + newImpacts + .forEach(e -> issue.addImpact(e.softwareQuality(), e.severity(), e.manualSeverity())); boolean updated = underTest.setImpacts(issue, currentImpacts, context); assertThat(updated).isTrue(); - assertThat(issue.impacts()).isEqualTo(newImpacts); + assertThat(issue.getImpacts()).isEqualTo(newImpacts); + } + + @Test + void setImpacts_whenImpactExists_shouldNotBeUpdated() { + Set<DefaultImpact> currentImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.LOW, false)); + Set<DefaultImpact> newImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.LOW, false)); + + newImpacts + .forEach(e -> issue.addImpact(e.softwareQuality(), e.severity(), e.manualSeverity())); + boolean updated = underTest.setImpacts(issue, currentImpacts, context); + assertThat(updated).isFalse(); + assertThat(issue.getImpacts()).isEqualTo(newImpacts); + } + + @Test + void setImpacts_whenImpactExistsWithManualSeverity_shouldNotBeUpdated() { + Set<DefaultImpact> currentImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, true)); + Set<DefaultImpact> newImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.LOW, false)); + + newImpacts + .forEach(e -> issue.addImpact(e.softwareQuality(), e.severity(), e.manualSeverity())); + boolean updated = underTest.setImpacts(issue, currentImpacts, context); + assertThat(updated).isFalse(); + assertThat(issue.getImpacts()).isEqualTo(currentImpacts); + } + + @Test + void setImpacts_whenImpactExistsWithManualSeverityAndNewImpact_shouldBeUpdated() { + Set<DefaultImpact> currentImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, true)); + Set<DefaultImpact> newImpacts = Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.LOW, false), + new DefaultImpact(SoftwareQuality.RELIABILITY, Severity.HIGH, false)); + + newImpacts + .forEach(e -> issue.addImpact(e.softwareQuality(), e.severity(), e.manualSeverity())); + boolean updated = underTest.setImpacts(issue, currentImpacts, context); + assertThat(updated).isTrue(); + assertThat(issue.getImpacts()).isEqualTo(Set.of(new DefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, true), + new DefaultImpact(SoftwareQuality.RELIABILITY, Severity.HIGH, false))); } @Test |