]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12423 do not display component with zero issues
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 27 Aug 2019 15:09:04 +0000 (17:09 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 30 Aug 2019 18:21:02 +0000 (20:21 +0200)
in "new issues" and "my new issues" email notifications

server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java
server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java

index 13bdb7b353d0423e0b56d73850309c643d693bc0..aac875a40f5ab1c3878c17d5f2e9751385078e3b 100644 (file)
@@ -227,6 +227,7 @@ public class NewIssuesNotification extends Notification {
     return distributedMetricStatsInt.getForLabels()
       .entrySet()
       .stream()
+      .filter(i -> biggerCriteria.applyAsInt(i.getValue()) > 0)
       .sorted(comparator.reversed())
       .limit(5)
       .collect(MoreCollectors.toList(5));
index c145f574d73bf4a02ab712dd9b774dd470fcdb01..0beecfe0f3e5131da7fe2d4d7cf2f5d556d32c3e 100644 (file)
@@ -148,6 +148,7 @@ public class NewIssuesNotificationTest {
     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
     IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
     IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
+
     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
@@ -178,6 +179,91 @@ public class NewIssuesNotificationTest {
     assertThat(underTest.getDefaultMessage()).startsWith("8 new issues on " + project.longName());
   }
 
+  @Test
+  public void set_statistics_when_no_issues_created_in_current_analysis() {
+    UserDto maynard = db.users().insertUser(u -> u.setLogin("maynard"));
+    UserDto keenan = db.users().insertUser(u -> u.setLogin("keenan"));
+    ComponentDto project = db.components().insertPrivateProject();
+    ComponentDto directory = db.components().insertComponent(newDirectory(project, "path"));
+    ComponentDto file = db.components().insertComponent(newFileDto(directory));
+    RuleDefinitionDto rule1 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-world").setName("Rule the World").setLanguage("Java"));
+    RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
+    IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
+    IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
+
+    NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> false);
+    IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
+    IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
+    mockDetailsSupplierComponents(project, directory, file);
+    mockDetailsSupplierRules(rule1, rule2);
+    mockDetailsSupplierAssignees(maynard, keenan);
+
+    underTest.setStatistics(project.longName(), stats);
+
+    assertThat(underTest.getFieldValue(RULE_TYPE + ".BUG.count")).isEqualTo("0");
+    assertThat(underTest.getFieldValue(RULE_TYPE + ".CODE_SMELL.count")).isEqualTo("0");
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isNull();
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isNull();
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".1.label")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".1.count")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".1.label")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".1.count")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".2.count")).isNull();
+    assertThat(underTest.getDefaultMessage()).startsWith("0 new issues on " + project.longName());
+  }
+
+  @Test
+  public void set_statistics_when_some_issues_are_no_created_in_current_analysis() {
+
+    UserDto maynard = db.users().insertUser(u -> u.setLogin("maynard"));
+    UserDto keenan = db.users().insertUser(u -> u.setLogin("keenan"));
+    ComponentDto project = db.components().insertPrivateProject();
+    ComponentDto directory = db.components().insertComponent(newDirectory(project, "path"));
+    ComponentDto file = db.components().insertComponent(newFileDto(directory));
+    RuleDefinitionDto rule1 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-world").setName("Rule the World").setLanguage("Java"));
+    RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
+    IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
+    IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
+
+    NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> i.key().equals(issue2.getKey()));
+    IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
+    IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
+    mockDetailsSupplierComponents(project, directory, file);
+    mockDetailsSupplierRules(rule1, rule2);
+    mockDetailsSupplierAssignees(maynard, keenan);
+
+    underTest.setStatistics(project.longName(), stats);
+
+    assertThat(underTest.getFieldValue(RULE_TYPE + ".BUG.count")).isEqualTo("0");
+    assertThat(underTest.getFieldValue(RULE_TYPE + ".CODE_SMELL.count")).isEqualTo("3");
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(keenan.getName());
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("3");
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".1.label")).isEqualTo("owasp");
+    assertThat(underTest.getFieldValue(TAG + ".1.count")).isEqualTo("3");
+    assertThat(underTest.getFieldValue(TAG + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(TAG + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isEqualTo(directory.name());
+    assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isEqualTo("3");
+    assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".1.label")).isEqualTo("Rule the Universe (Clojure)");
+    assertThat(underTest.getFieldValue(RULE + ".1.count")).isEqualTo("3");
+    assertThat(underTest.getFieldValue(RULE + ".2.label")).isNull();
+    assertThat(underTest.getFieldValue(RULE + ".2.count")).isNull();
+    assertThat(underTest.getDefaultMessage()).startsWith("3 new issues on " + project.longName());
+  }
+
   private void mockDetailsSupplierAssignees(UserDto... users) {
     for (UserDto user : users) {
       when(detailsSupplier.getUserNameByUuid(user.getUuid())).thenReturn(Optional.of(user.getName()));