From cfbb6db160f244c46239da80cedd35a0bbfa4b2c Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 30 May 2013 14:38:41 +0200 Subject: [PATCH] SONAR-4310 update issue message during tracking --- .../core/issue/IssueTrackingDecorator.java | 11 ++-- .../org/sonar/core/issue/IssueUpdater.java | 33 +++++++++- .../sonar/core/issue/IssueUpdaterTest.java | 64 +++++++++++++++++++ 3 files changed, 98 insertions(+), 10 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java index fb1297aa31f..68a90e91629 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java @@ -149,14 +149,11 @@ public class IssueTrackingDecorator implements Decorator { issue.setManualSeverity(true); issue.setSeverity(ref.getSeverity()); } else { - // Emulate change of severity in the current scan. - String severity = issue.severity(); - issue.setSeverity(ref.getSeverity()); - updater.setSeverity(issue, severity, changeContext); + updater.setPastSeverity(issue, ref.getSeverity(), changeContext); } - Integer line = issue.line(); - issue.setLine(ref.getLine()); - updater.setLine(issue, line); + updater.setPastLine(issue, ref.getLine()); + updater.setPastMessage(issue, ref.getMessage(), changeContext); + updater.setPastEffortToFix(issue, ref.getEffortToFix(), changeContext); } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java index e080d39eec9..ab393473128 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java @@ -46,6 +46,12 @@ public class IssueUpdater implements BatchComponent, ServerComponent { return false; } + public boolean setPastSeverity(DefaultIssue issue, @Nullable String previousSeverity, IssueChangeContext context) { + String currentSeverity = issue.severity(); + issue.setSeverity(previousSeverity); + return setSeverity(issue, currentSeverity, context); + } + public boolean setManualSeverity(DefaultIssue issue, String severity, IssueChangeContext context) { if (!issue.manualSeverity() || !Objects.equal(severity, issue.severity())) { issue.setFieldDiff(context, "severity", issue.severity(), severity); @@ -79,6 +85,12 @@ public class IssueUpdater implements BatchComponent, ServerComponent { return false; } + public boolean setPastLine(DefaultIssue issue, @Nullable Integer previousLine) { + Integer currentLine = issue.line(); + issue.setLine(previousLine); + return setLine(issue, currentLine); + } + public boolean setResolution(DefaultIssue issue, @Nullable String resolution, IssueChangeContext context) { if (!Objects.equal(resolution, issue.resolution())) { issue.setFieldDiff(context, "resolution", issue.resolution(), resolution); @@ -101,22 +113,31 @@ public class IssueUpdater implements BatchComponent, ServerComponent { return false; } - public void setAuthorLogin(DefaultIssue issue, @Nullable String authorLogin, IssueChangeContext context) { + public boolean setAuthorLogin(DefaultIssue issue, @Nullable String authorLogin, IssueChangeContext context) { if (!Objects.equal(authorLogin, issue.authorLogin())) { issue.setFieldDiff(context, "author", issue.authorLogin(), authorLogin); issue.setAuthorLogin(authorLogin); issue.setUpdateDate(context.date()); issue.setChanged(true); + return true; } + return false; } - public void setMessage(DefaultIssue issue, @Nullable String s, IssueChangeContext context) { + public boolean setMessage(DefaultIssue issue, @Nullable String s, IssueChangeContext context) { if (!Objects.equal(s, issue.message())) { - issue.setFieldDiff(context, "message", issue.message(), s); issue.setMessage(s); issue.setUpdateDate(context.date()); issue.setChanged(true); + return true; } + return false; + } + + public boolean setPastMessage(DefaultIssue issue, @Nullable String previousMessage, IssueChangeContext context) { + String currentMessage = issue.message(); + issue.setMessage(previousMessage); + return setMessage(issue, currentMessage, context); } public void addComment(DefaultIssue issue, String text, IssueChangeContext context) { @@ -143,6 +164,12 @@ public class IssueUpdater implements BatchComponent, ServerComponent { return false; } + public boolean setPastEffortToFix(DefaultIssue issue, @Nullable Double previousEffort, IssueChangeContext context) { + Double currentEffort = issue.effortToFix(); + issue.setEffortToFix(previousEffort); + return setEffortToFix(issue, currentEffort, context); + } + public boolean setAttribute(DefaultIssue issue, String key, @Nullable String value, IssueChangeContext context) { String oldValue = issue.attribute(key); if (!Objects.equal(oldValue, value)) { diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueUpdaterTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueUpdaterTest.java index b9db96810f4..01e3c3b34c9 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueUpdaterTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueUpdaterTest.java @@ -84,6 +84,18 @@ public class IssueUpdaterTest { assertThat(diff.newValue()).isEqualTo("BLOCKER"); } + @Test + public void should_set_past_severity() throws Exception { + issue.setSeverity("BLOCKER"); + boolean updated = updater.setPastSeverity(issue, "INFO", context); + assertThat(updated).isTrue(); + assertThat(issue.severity()).isEqualTo("BLOCKER"); + + FieldDiffs.Diff diff = issue.diffs().get("severity"); + assertThat(diff.oldValue()).isEqualTo("INFO"); + assertThat(diff.newValue()).isEqualTo("BLOCKER"); + } + @Test public void should_update_severity() throws Exception { issue.setSeverity("BLOCKER"); @@ -145,6 +157,17 @@ public class IssueUpdaterTest { assertThat(issue.diffs()).isNull(); } + @Test + public void should_set_past_line() throws Exception { + issue.setLine(42); + boolean updated = updater.setPastLine(issue, 123); + assertThat(updated).isTrue(); + assertThat(issue.line()).isEqualTo(42); + + // do not save change + assertThat(issue.diffs()).isNull(); + } + @Test public void should_not_change_line() throws Exception { issue.setLine(123); @@ -249,4 +272,45 @@ public class IssueUpdaterTest { assertThat(issue.isChanged()).isFalse(); assertThat(issue.effortToFix()).isEqualTo(3.14); } + + @Test + public void should_set_past_effort() throws Exception { + issue.setEffortToFix(3.14); + boolean updated = updater.setPastEffortToFix(issue, 1.0, context); + assertThat(updated).isTrue(); + assertThat(issue.effortToFix()).isEqualTo(3.14); + + // do not save change + assertThat(issue.diffs()).isNull(); + } + + @Test + public void should_set_message() throws Exception { + boolean updated = updater.setMessage(issue, "the message", context); + assertThat(updated).isTrue(); + assertThat(issue.isChanged()).isTrue(); + assertThat(issue.message()).isEqualTo("the message"); + } + + @Test + public void should_set_past_message() throws Exception { + issue.setMessage("new message"); + boolean updated = updater.setPastMessage(issue, "past message", context); + assertThat(updated).isTrue(); + assertThat(issue.message()).isEqualTo("new message"); + + // do not save change + assertThat(issue.diffs()).isNull(); + } + + @Test + public void should_set_author() throws Exception { + boolean updated = updater.setAuthorLogin(issue, "eric", context); + assertThat(updated).isTrue(); + assertThat(issue.authorLogin()).isEqualTo("eric"); + + FieldDiffs.Diff diff = issue.diffs().get("author"); + assertThat(diff.oldValue()).isNull(); + assertThat(diff.newValue()).isEqualTo("eric"); + } } -- 2.39.5