From d088cb12d8c61d794e809c6151c3cb2c9ca75f0f Mon Sep 17 00:00:00 2001 From: Eric Giffon Date: Tue, 28 Feb 2023 10:45:39 +0100 Subject: [PATCH] SONAR-18267 Fix duplicate issue changes --- .../org/sonar/core/issue/DefaultIssue.java | 2 +- .../sonar/core/issue/DefaultIssueTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 931746fa44f..08b92350a10 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 @@ -503,10 +503,10 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure. currentChange.setCreationDate(context.date()); currentChange.setWebhookSource(context.getWebhookSource()); currentChange.setExternalUser(context.getExternalUser()); + addChange(currentChange); } currentChange.setDiff(field, oldValue, newValue); } - addChange(currentChange); return this; } 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 f295cdf7cc6..763be4b20ad 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 @@ -206,6 +206,25 @@ public class DefaultIssueTest { assertThat(actualDiffs.webhookSource()).contains(issueChangeContext.getWebhookSource()); } + @Test + public void setFieldChange_whenAddingChange_shouldUpdateCurrentChange() { + IssueChangeContext issueChangeContext = mock(IssueChangeContext.class); + DefaultIssue issue = new DefaultIssue().setKey("AAA"); + + issue.setFieldChange(issueChangeContext, "actionPlan", "1.0", "1.1"); + assertThat(issue.changes()).hasSize(1); + FieldDiffs currentChange = issue.currentChange(); + assertThat(currentChange).isNotNull(); + assertThat(currentChange.get("actionPlan")).isNotNull(); + assertThat(currentChange.get("authorLogin")).isNull(); + + issue.setFieldChange(issueChangeContext, "authorLogin", null, "testuser"); + assertThat(issue.changes()).hasSize(1); + assertThat(currentChange.get("actionPlan")).isNotNull(); + assertThat(currentChange.get("authorLogin")).isNotNull(); + assertThat(currentChange.get("authorLogin").newValue()).isEqualTo("testuser"); + } + @Test public void adding_null_change_has_no_effect() { DefaultIssue issue = new DefaultIssue(); -- 2.39.5