]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19372 add default comment when missing
authorPierre <pierre.guillot@sonarsource.com>
Fri, 28 Jul 2023 14:46:18 +0000 (16:46 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 2 Aug 2023 20:03:03 +0000 (20:03 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/TransitionIssuesToAnticipatedStatesVisitor.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/TransitionIssuesToAnticipatedStatesVisitorTest.java

index f3add51501a766601964189cd825b684efd55f17..976ab894c2e547af83c5af37db1c8a8371e63007 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.issue;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import org.apache.logging.log4j.util.Strings;
 import org.sonar.ce.task.projectanalysis.component.Component;
 import org.sonar.core.issue.AnticipatedTransition;
 import org.sonar.core.issue.DefaultIssue;
@@ -38,7 +39,7 @@ public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
   private Collection<AnticipatedTransition> anticipatedTransitions;
   private final AnticipatedTransitionTracker<DefaultIssue, AnticipatedTransition> tracker = new AnticipatedTransitionTracker<>();
   private final IssueLifecycle issueLifecycle;
-  
+
   private final AnticipatedTransitionRepository anticipatedTransitionRepository;
 
   public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository, IssueLifecycle issueLifecycle) {
@@ -68,7 +69,9 @@ public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
     issue.setBeingClosed(true);
     issue.setAnticipatedTransitions(true);
     issueLifecycle.doManualTransition(issue, anticipatedTransition.getTransition(), anticipatedTransition.getUserUuid());
-    issueLifecycle.addComment(issue, anticipatedTransition.getComment(), anticipatedTransition.getUserUuid());
+    String transitionComment = anticipatedTransition.getComment();
+    String comment = Strings.isNotBlank(transitionComment) ? transitionComment : "Automatically transitioned from SonarLint";
+    issueLifecycle.addComment(issue, comment, anticipatedTransition.getUserUuid());
   }
 
 }
index 3039d15c295ea312ab6c2e1bb70980eda156b75b..b7b76f23a90fb418d37ffbe7b3d02504d908ff21 100644 (file)
@@ -78,6 +78,22 @@ public class TransitionIssuesToAnticipatedStatesVisitorTest {
     verifyNoInteractions(issueLifecycle);
   }
 
+  @Test
+  public void givenMatchingAnticipatedTransitionsWithEmptyComment_transitionsShouldBeAppliedToIssuesAndDefaultCommentApplied() {
+    Component component = getComponent(Component.Type.FILE);
+    when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitionsWithEmptyComment("projectKey", "fileName"));
+
+    DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
+
+    underTest.beforeComponent(component);
+    underTest.onIssue(component, issue);
+
+    assertThat(issue.isBeingClosed()).isTrue();
+    assertThat(issue.hasAnticipatedTransitions()).isTrue();
+    verify(issueLifecycle).doManualTransition(issue, "wontfix", "admin");
+    verify(issueLifecycle).addComment(issue, "Automatically transitioned from SonarLint", "admin");
+  }
+
   @Test
   public void givenAFileComponent_theRepositoryIsHitForFetchingAnticipatedTransitions() {
     Component component = getComponent(Component.Type.FILE);
@@ -102,6 +118,10 @@ public class TransitionIssuesToAnticipatedStatesVisitorTest {
     return Stream.of(new AnticipatedTransition(projecKey, null, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1, "abcdefghi", "wontfix", "doing the transition in an anticipated way")).collect(Collectors.toList());
   }
 
+  private Collection<AnticipatedTransition> getAnticipatedTransitionsWithEmptyComment(String projecKey, String fileName) {
+    return Stream.of(new AnticipatedTransition(projecKey, null, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1, "abcdefghi", "wontfix", null)).collect(Collectors.toList());
+  }
+
   private Component getComponent(Component.Type type) {
     ComponentImpl.Builder builder = ComponentImpl.builder(type)
       .setUuid("componentUuid")