aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2022-11-23 15:57:02 +0100
committersonartech <sonartech@sonarsource.com>2022-12-01 20:03:11 +0000
commit66b8bff0dcc778eb93da92cd408214bbfca82c2a (patch)
treee60388bdf0bdbc60434d00eae81b8e6dffe39fad /server/sonar-server-common
parent24d4519ea1b8a4426dd90e0bb80b2b09bd7a1c12 (diff)
downloadsonarqube-66b8bff0dcc778eb93da92cd408214bbfca82c2a.tar.gz
sonarqube-66b8bff0dcc778eb93da92cd408214bbfca82c2a.zip
SONAR-17592 Persist Message formatting from plugin api
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/issue/IssueFieldsSetter.java42
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/issue/IssueFieldsSetterTest.java72
2 files changed, 111 insertions, 3 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 1b7afe26d32..a6d2b6e0024 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
@@ -288,10 +288,48 @@ public class IssueFieldsSetter {
return false;
}
- public boolean setPastMessage(DefaultIssue issue, @Nullable String previousMessage, IssueChangeContext context) {
+ public boolean setMessageFormattings(DefaultIssue issue, @Nullable Object issueMessageFormattings, IssueChangeContext context) {
+ if (!messageFormattingsEqualsIgnoreHashes(issueMessageFormattings, issue.getMessageFormattings())) {
+ issue.setMessageFormattings(issueMessageFormattings);
+ issue.setUpdateDate(context.date());
+ issue.setChanged(true);
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean messageFormattingsEqualsIgnoreHashes(@Nullable Object l1, @Nullable DbIssues.MessageFormattings l2) {
+ if (l1 == null && l2 == null) {
+ return true;
+ }
+
+ if (l2 == null || !(l1 instanceof DbIssues.MessageFormattings)) {
+ return false;
+ }
+
+ DbIssues.MessageFormattings l1c = (DbIssues.MessageFormattings) l1;
+
+ if (!Objects.equals(l1c.getMessageFormattingCount(), l2.getMessageFormattingCount())) {
+ return false;
+ }
+
+ 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()) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public boolean setPastMessage(DefaultIssue issue, @Nullable String previousMessage, @Nullable Object previousMessageFormattings, IssueChangeContext context) {
String currentMessage = issue.message();
+ DbIssues.MessageFormattings currentMessageFormattings = issue.getMessageFormattings();
issue.setMessage(previousMessage);
- return setMessage(issue, currentMessage, context);
+ issue.setMessageFormattings(previousMessageFormattings);
+ boolean changed = setMessage(issue, currentMessage, context);
+ return setMessageFormattings(issue, currentMessageFormattings, context) || changed;
}
public void addComment(DefaultIssue issue, String text, IssueChangeContext context) {
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 a2a0053159a..4c434971b47 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
@@ -21,6 +21,7 @@ package org.sonar.server.issue;
import java.util.Calendar;
import java.util.Date;
+import java.util.List;
import java.util.Random;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
@@ -30,11 +31,13 @@ import org.sonar.core.issue.FieldDiffs;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.db.protobuf.DbCommons;
import org.sonar.db.protobuf.DbIssues;
+import org.sonar.db.protobuf.DbIssues.MessageFormattingType;
import org.sonar.db.user.UserDto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
+import static org.sonar.db.protobuf.DbIssues.MessageFormattingType.CODE;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.server.issue.IssueFieldsSetter.ASSIGNEE;
import static org.sonar.server.issue.IssueFieldsSetter.RESOLUTION;
@@ -500,7 +503,7 @@ public class IssueFieldsSetterTest {
@Test
public void set_past_message() {
issue.setMessage("new message");
- boolean updated = underTest.setPastMessage(issue, "past message", context);
+ boolean updated = underTest.setPastMessage(issue, "past message", null, context);
assertThat(updated).isTrue();
assertThat(issue.message()).isEqualTo("new message");
@@ -510,6 +513,73 @@ public class IssueFieldsSetterTest {
}
@Test
+ public void set_past_message_formatting() {
+ issue.setMessage("past message");
+ DbIssues.MessageFormattings newFormatting = formattings(formatting(0, 3, CODE));
+ DbIssues.MessageFormattings pastFormatting = formattings(formatting(0, 7, CODE));
+ issue.setMessageFormattings(newFormatting);
+ boolean updated = underTest.setPastMessage(issue, "past message", pastFormatting, context);
+ assertThat(updated).isTrue();
+ assertThat(issue.message()).isEqualTo("past message");
+ assertThat((DbIssues.MessageFormattings) issue.getMessageFormattings()).isEqualTo(newFormatting);
+
+ // do not save change
+ assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
+ }
+
+ @Test
+ public void set_past_message_formatting_no_changes() {
+ issue.setMessage("past message");
+ DbIssues.MessageFormattings sameFormatting = formattings(formatting(0, 3, CODE));
+ issue.setMessageFormattings(sameFormatting);
+ boolean updated = underTest.setPastMessage(issue, "past message", sameFormatting, context);
+ assertThat(updated).isFalse();
+ assertThat(issue.message()).isEqualTo("past message");
+ assertThat((DbIssues.MessageFormattings) issue.getMessageFormattings()).isEqualTo(sameFormatting);
+
+ // do not save change
+ assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
+ }
+
+ @Test
+ public void message_formatting_different_size_is_changed(){
+ issue.setMessageFormattings(formattings(formatting(0,3,CODE)));
+ boolean updated = underTest.setLocations(issue, formattings(formatting(0,3,CODE), formatting(4,6,CODE)));
+ assertThat(updated).isTrue();
+ }
+
+ @Test
+ public void message_formatting_different_start_is_changed(){
+ issue.setMessageFormattings(formattings(formatting(0,3,CODE)));
+ boolean updated = underTest.setLocations(issue, formattings(formatting(1,3,CODE)));
+ assertThat(updated).isTrue();
+ }
+
+ @Test
+ public void message_formatting_different_end_is_changed(){
+ issue.setMessageFormattings(formattings(formatting(0,3,CODE)));
+ boolean updated = underTest.setLocations(issue, formattings(formatting(0,4,CODE)));
+ assertThat(updated).isTrue();
+ }
+
+ private static DbIssues.MessageFormatting formatting(int start, int end, MessageFormattingType type) {
+ return DbIssues.MessageFormatting
+ .newBuilder()
+ .setStart(start)
+ .setEnd(end)
+ .setType(type)
+ .build();
+ }
+
+ private static DbIssues.MessageFormattings formattings(DbIssues.MessageFormatting... messageFormatting) {
+ return DbIssues.MessageFormattings.newBuilder()
+ .addAllMessageFormatting(List.of(messageFormatting))
+ .build();
+ }
+
+ @Test
public void set_author() {
boolean updated = underTest.setAuthorLogin(issue, "eric", context);
assertThat(updated).isTrue();