public boolean setTags(DefaultIssue issue, Collection<String> tags, IssueChangeContext context) { Set<String> newTags = RuleTagFormat.validate(tags); Set<String> oldTags = new HashSet<>(issue.tags()); if (!oldTags.equals(newTags)) { issue.setFieldChange(context, TAGS, oldTags.isEmpty() ? null : CHANGELOG_TAG_JOINER.join(oldTags), newTags.isEmpty() ? null : CHANGELOG_TAG_JOINER.join(newTags)); issue.setTags(newTags); issue.setUpdateDate(context.date()); issue.setChanged(true); issue.setSendNotifications(true); return true; } return false; }
@Test @SuppressWarnings("unchecked") public void should_execute() { Map<String, Object> properties = new HashMap<>(); properties.put("tags", "tag2,tag3"); DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); Action.Context context = mock(Action.Context.class, Mockito.RETURNS_DEEP_STUBS); when(context.issue()).thenReturn(issue); underTest.execute(properties, context); verify(issue).setTags(ImmutableSet.of("tag1", "tag2", "tag3")); }
@Test @SuppressWarnings("unchecked") public void should_execute() { Map<String, Object> properties = newHashMap(); properties.put("tags", "tag2,tag3"); DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); Action.Context context = mock(Action.Context.class, Mockito.RETURNS_DEEP_STUBS); when(context.issue()).thenReturn(issue); action.execute(properties, context); verify(issue).setTags(ImmutableSet.of("tag1")); }
@Override public void onIssue(Component component, DefaultIssue issue) { if (issue.isNew()) { // analyzer can provide some tags. They must be merged with rule tags Rule rule = ruleRepository.getByKey(issue.ruleKey()); if (!rule.isExternal()) { issue.setTags(union(issue.tags(), rule.getTags())); } } } }
@Test public void do_not_copy_tags_if_existing_issue() { rule.setTags(Sets.newHashSet("bug", "performance")); issue.setNew(false).setTags(asList("misra")); underTest.onIssue(mock(Component.class), issue); assertThat(issue.tags()).containsExactly("misra"); }
@Test public void do_not_copy_tags_if_existing_issue_without_tags() { rule.setTags(Sets.newHashSet("bug", "performance")); issue.setNew(false).setTags(Collections.emptyList()); underTest.onIssue(mock(Component.class), issue); assertThat(issue.tags()).isEmpty(); } }
@Test public void add_does_not_count_tags_if_empty_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(Collections.emptyList()).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
@Test public void add_counts_issue_per_tags_off_leak_globally_and_per_assignee() { List<String> tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(false)); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> tags.forEach(tag -> assertStats(distribution, tag, 0, 1, 1))); }
@Test public void add_counts_issue_per_tags_on_leak_globally_and_per_assignee() { List<String> tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(true)); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> tags.forEach(tag -> assertStats(distribution, tag, 1, 0, 1))); }
@Test public void add_issues_with_correct_global_statistics() { DefaultIssue issue = new DefaultIssue() .setAssigneeUuid("maynard") .setComponentUuid("file-uuid") .setNew(true) .setType(RuleType.BUG) .setRuleKey(RuleKey.of("SonarQube", "rule-the-world")) .setTags(Lists.newArrayList("bug", "owasp")) .setEffort(Duration.create(5L)); underTest.add(issue); underTest.add(issue.setAssigneeUuid("james")); underTest.add(issue.setAssigneeUuid("keenan")); assertThat(countDistributionTotal(Metric.ASSIGNEE, "maynard")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "james")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "keenan")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "wrong.login")).isNull(); assertThat(countDistributionTotal(Metric.COMPONENT, "file-uuid")).isEqualTo(3); assertThat(countDistributionTotal(Metric.COMPONENT, "wrong-uuid")).isNull(); assertThat(countDistributionTotal(Metric.RULE_TYPE, RuleType.BUG.name())).isEqualTo(3); assertThat(countDistributionTotal(Metric.RULE_TYPE, RuleType.CODE_SMELL.name())).isNull(); assertThat(countDistributionTotal(Metric.TAG, "owasp")).isEqualTo(3); assertThat(countDistributionTotal(Metric.TAG, "wrong-tag")).isNull(); assertThat(countDistributionTotal(Metric.RULE, "SonarQube:rule-the-world")).isEqualTo(3); assertThat(countDistributionTotal(Metric.RULE, "SonarQube:has-a-fake-rule")).isNull(); assertThat(underTest.globalStatistics().effort().getTotal()).isEqualTo(15L); assertThat(underTest.globalStatistics().hasIssues()).isTrue(); assertThat(underTest.hasIssues()).isTrue(); assertThat(underTest.getAssigneesStatistics().get("maynard").hasIssues()).isTrue(); }
.setAssigneeUuid("base assignee uuid") .setAuthorLogin("base author") .setTags(newArrayList("base tag")) .setOnDisabledRule(true) .setSelectedAt(1000L)
.setAssigneeUuid("base assignee uuid") .setAuthorLogin("base author") .setTags(newArrayList("base tag")) .setOnDisabledRule(true) .setSelectedAt(1000L)
.setAssigneeUuid("base assignee uuid") .setAuthorLogin("base author") .setTags(newArrayList("base tag")) .setSelectedAt(1000L) .setLine(10)
.setType(randomRuleTypeExceptHotspot) .setComponentUuid(componentUuid) .setTags(ImmutableSet.of(tag)) .setAssigneeUuid(assignee) .setRuleKey(ruleKey)
private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) { toIssue.setType(fromIssue.type()); toIssue.setCreationDate(fromIssue.creationDate()); toIssue.setUpdateDate(fromIssue.updateDate()); toIssue.setCloseDate(fromIssue.closeDate()); toIssue.setResolution(fromIssue.resolution()); toIssue.setStatus(fromIssue.status()); toIssue.setAssigneeUuid(fromIssue.assignee()); toIssue.setAuthorLogin(fromIssue.authorLogin()); toIssue.setTags(fromIssue.tags()); toIssue.setAttributes(fromIssue.attributes()); toIssue.setEffort(debtCalculator.calculate(toIssue)); toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule()); toIssue.setSelectedAt(fromIssue.selectedAt()); } }
issue.setManualSeverity(manualSeverity); issue.setRuleKey(getRuleKey()); issue.setTags(getTags()); issue.setLanguage(language); issue.setAuthorLogin(authorLogin);
public boolean setTags(DefaultIssue issue, Collection<String> tags, IssueChangeContext context) { Set<String> newTags = tags.stream() .filter(Objects::nonNull) .filter(tag -> !tag.isEmpty()) .map(tag -> RuleTagFormat.validate(tag.toLowerCase(Locale.ENGLISH))) .collect(MoreCollectors.toSet()); Set<String> oldTags = new HashSet<>(issue.tags()); if (!oldTags.equals(newTags)) { issue.setFieldChange(context, TAGS, oldTags.isEmpty() ? null : CHANGELOG_TAG_JOINER.join(oldTags), newTags.isEmpty() ? null : CHANGELOG_TAG_JOINER.join(newTags)); issue.setTags(newTags); issue.setUpdateDate(context.date()); issue.setChanged(true); issue.setSendNotifications(true); return true; } return false; }
@Override public void onIssue(Component component, DefaultIssue issue) { if (issue.isNew()) { // analyzer can provide some tags. They must be merged with rule tags Rule rule = ruleRepository.getByKey(issue.ruleKey()); if (!rule.isExternal()) { issue.setTags(union(issue.tags(), rule.getTags())); } } } }
private void copyFields(DefaultIssue toIssue, DefaultIssue fromIssue) { toIssue.setType(fromIssue.type()); toIssue.setCreationDate(fromIssue.creationDate()); toIssue.setUpdateDate(fromIssue.updateDate()); toIssue.setCloseDate(fromIssue.closeDate()); toIssue.setResolution(fromIssue.resolution()); toIssue.setStatus(fromIssue.status()); toIssue.setAssigneeUuid(fromIssue.assignee()); toIssue.setAuthorLogin(fromIssue.authorLogin()); toIssue.setTags(fromIssue.tags()); toIssue.setAttributes(fromIssue.attributes()); toIssue.setEffort(debtCalculator.calculate(toIssue)); toIssue.setOnDisabledRule(fromIssue.isOnDisabledRule()); toIssue.setSelectedAt(fromIssue.selectedAt()); } }
issue.setManualSeverity(manualSeverity); issue.setRuleKey(getRuleKey()); issue.setTags(getTags()); issue.setLanguage(language); issue.setAuthorLogin(authorLogin);