@Override protected Collection<String> getTagsToSet(Context context, Collection<String> tagsFromParams) { Set<String> newTags = new HashSet<>(context.issue().tags()); newTags.removeAll(tagsFromParams); return newTags; } }
@Override protected Collection<String> getTagsToSet(Context context, Collection<String> tagsFromParams) { Set<String> allTags = new HashSet<>(context.issue().tags()); allTags.addAll(tagsFromParams); return allTags; } }
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 should_fail_if_tag_is_not_valid() { throwable.expect(IllegalArgumentException.class); throwable.expectMessage("Tag 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'"); Map<String, Object> properties = newHashMap(); properties.put("tags", "th ag"); DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); Action.Context context = mock(Action.Context.class); when(context.issue()).thenReturn(issue); action.execute(properties, context); } }
@Test public void should_fail_if_tag_is_not_valid() { throwable.expect(IllegalArgumentException.class); throwable.expectMessage("Tag 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'"); Map<String, Object> properties = new HashMap<>(); properties.put("tags", "th ag"); DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); Action.Context context = mock(Action.Context.class); when(context.issue()).thenReturn(issue); underTest.execute(properties, context); } }
public void add(DefaultIssue issue) { boolean isOnLeak = onLeakPredicate.test(issue); distributions.get(RULE_TYPE).increment(issue.type().name(), isOnLeak); String componentUuid = issue.componentUuid(); if (componentUuid != null) { distributions.get(COMPONENT).increment(componentUuid, isOnLeak); } RuleKey ruleKey = issue.ruleKey(); if (ruleKey != null) { distributions.get(RULE).increment(ruleKey.toString(), isOnLeak); } String assigneeUuid = issue.assignee(); if (assigneeUuid != null) { distributions.get(ASSIGNEE).increment(assigneeUuid, isOnLeak); } for (String tag : issue.tags()) { distributions.get(TAG).increment(tag, isOnLeak); } Duration effort = issue.effort(); if (effort != null) { effortStats.add(effort.toMinutes(), isOnLeak); } }
@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 copy_tags_if_new_issue() { rule.setTags(Sets.newHashSet("bug", "performance")); issue.setNew(true); underTest.onIssue(mock(Component.class), issue); assertThat(issue.tags()).containsExactly("bug", "performance"); }
@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 load_external_issues_from_report() { when(sourceLinesHash.getLineHashesMatchingDBVersion(FILE)).thenReturn(Collections.singletonList("line")); ScannerReport.ExternalIssue reportIssue = ScannerReport.ExternalIssue.newBuilder() .setTextRange(TextRange.newBuilder().setStartLine(2).build()) .setMsg("the message") .setEngineId("eslint") .setRuleId("S001") .setSeverity(Constants.Severity.BLOCKER) .setEffort(20l) .setType(ScannerReport.IssueType.SECURITY_HOTSPOT) .build(); reportReader.putExternalIssues(FILE.getReportAttributes().getRef(), asList(reportIssue)); Input<DefaultIssue> input = underTest.create(FILE); Collection<DefaultIssue> issues = input.getIssues(); assertThat(issues).hasSize(1); DefaultIssue issue = Iterators.getOnlyElement(issues.iterator()); // fields set by analysis report assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("external_eslint", "S001")); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.line()).isEqualTo(2); assertThat(issue.effort()).isEqualTo(Duration.create(20l)); assertThat(issue.message()).isEqualTo("the message"); assertThat(issue.type()).isEqualTo(RuleType.SECURITY_HOTSPOT); // fields set by compute engine assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2)); assertThat(issue.tags()).isEmpty(); assertInitializedExternalIssue(issue); }
assertThat(issue.tags()).isEmpty(); assertInitializedIssue(issue); assertThat(issue.effort()).isNull();
@Test public void load_external_issues_from_report_with_default_effort() { when(sourceLinesHash.getLineHashesMatchingDBVersion(FILE)).thenReturn(Collections.singletonList("line")); ScannerReport.ExternalIssue reportIssue = ScannerReport.ExternalIssue.newBuilder() .setTextRange(TextRange.newBuilder().setStartLine(2).build()) .setMsg("the message") .setEngineId("eslint") .setRuleId("S001") .setSeverity(Constants.Severity.BLOCKER) .setType(ScannerReport.IssueType.BUG) .build(); reportReader.putExternalIssues(FILE.getReportAttributes().getRef(), asList(reportIssue)); Input<DefaultIssue> input = underTest.create(FILE); Collection<DefaultIssue> issues = input.getIssues(); assertThat(issues).hasSize(1); DefaultIssue issue = Iterators.getOnlyElement(issues.iterator()); // fields set by analysis report assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("external_eslint", "S001")); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.line()).isEqualTo(2); assertThat(issue.effort()).isEqualTo(Duration.create(0l)); assertThat(issue.message()).isEqualTo("the message"); // fields set by compute engine assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2)); assertThat(issue.tags()).isEmpty(); assertInitializedExternalIssue(issue); }
assertThat(raw.assignee()).isEqualTo("base assignee uuid"); assertThat(raw.authorLogin()).isEqualTo("base author"); assertThat(raw.tags()).containsOnly("base tag"); assertThat(raw.effort()).isEqualTo(DEFAULT_DURATION); assertThat(raw.isOnDisabledRule()).isTrue();
assertThat(raw.assignee()).isEqualTo("base assignee uuid"); assertThat(raw.authorLogin()).isEqualTo("base author"); assertThat(raw.tags()).containsOnly("base tag"); assertThat(raw.effort()).isEqualTo(DEFAULT_DURATION); assertThat(raw.isOnDisabledRule()).isTrue();
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()); } }
.setExternal(issue.isFromExternalRuleEngine()) .setIsFromHotspot(issue.isFromHotspot()) .setTags(issue.tags()) .setComponentUuid(issue.componentUuid()) .setComponentKey(issue.componentKey())
.setExternal(issue.isFromExternalRuleEngine()) .setIsFromHotspot(issue.isFromHotspot()) .setTags(issue.tags()) .setComponentUuid(issue.componentUuid()) .setComponentKey(issue.componentKey())