public boolean setSeverity(DefaultIssue issue, String severity, IssueChangeContext context) { if (issue.manualSeverity()) { throw new IllegalStateException("Severity can't be changed"); } if (!Objects.equals(severity, issue.severity())) { issue.setFieldChange(context, SEVERITY, issue.severity(), severity); issue.setSeverity(severity); issue.setUpdateDate(context.date()); issue.setChanged(true); return true; } return false; }
public boolean setManualSeverity( DefaultIssue issue, String severity, IssueChangeContext context) { if (!issue.manualSeverity() || !Objects.equals(severity, issue.severity())) { issue.setFieldChange(context, SEVERITY, issue.severity(), severity); issue.setSeverity(severity); issue.setManualSeverity(true); issue.setUpdateDate(context.date()); issue.setChanged(true); issue.setSendNotifications(true); return true; } return false; }
@Test public void severity_changed_by_user_should_be_kept() { DefaultIssue issue = new DefaultIssue() .setKey("ABCDE") .setRuleKey(RuleKey.of("squid", "AvoidCycles")) .setComponentKey("struts:org.apache.struts.Action") .setNew(false) .setStatus(Issue.STATUS_OPEN); // Changed by scan issue.setSeverity(Severity.BLOCKER); issue.setManualSeverity(false); // Issue as seen and changed by end-user IssueDto dbIssue = new IssueDto() .setKee("ABCDE") .setStatus(Issue.STATUS_OPEN) .setSeverity(Severity.INFO) .setManualSeverity(true); new UpdateConflictResolver().mergeFields(dbIssue, issue); assertThat(issue.severity()).isEqualTo(Severity.INFO); assertThat(issue.manualSeverity()).isTrue(); }
@Test public void load_issues_from_report() throws Exception { when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(true); fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;"); ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder() .setTextRange(TextRange.newBuilder().setStartLine(2).build()) .setMsg("the message") .setRuleRepository("java") .setRuleKey("S001") .setSeverity(Constants.Severity.BLOCKER) .setGap(3.14) .build(); reportReader.putIssues(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("java", "S001")); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.line()).isEqualTo(2); assertThat(issue.effortToFix()).isEqualTo(3.14); assertThat(issue.gap()).isEqualTo(3.14); assertThat(issue.message()).isEqualTo("the message"); // fields set by compute engine assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2)); assertThat(issue.tags()).isEmpty(); assertInitializedIssue(issue); }
@Test public void load_issues() throws Exception { reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;"); BatchReport.Issue reportIssue = BatchReport.Issue.newBuilder() .setLine(2) .setMsg("the message") .setRuleRepository("java") .setRuleKey("S001") .setSeverity(Constants.Severity.BLOCKER) .setEffortToFix(3.14) .build(); reportReader.putIssues(FILE.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("java", "S001")); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.line()).isEqualTo(2); assertThat(issue.effortToFix()).isEqualTo(3.14); assertThat(issue.message()).isEqualTo("the message"); // fields set by compute engine assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2)); assertThat(issue.tags()).isEmpty(); assertInitializedIssue(issue); }
@Test public void should_keep_changes_made_by_user() { DefaultIssue issue = new DefaultIssue() .setKey("ABCDE") .setRuleKey(RuleKey.of("squid", "AvoidCycles")) .setComponentKey("struts:org.apache.struts.Action") .setNew(false); // Before starting scan issue.setAssignee(null); issue.setCreationDate(DateUtils.parseDate("2012-01-01")); issue.setUpdateDate(DateUtils.parseDate("2012-02-02")); // Changed by scan issue.setLine(200); issue.setSeverity(Severity.BLOCKER); issue.setManualSeverity(false); issue.setAuthorLogin("simon"); issue.setChecksum("CHECKSUM-ABCDE"); issue.setResolution(null); issue.setStatus(Issue.STATUS_REOPENED); // Issue as seen and changed by end-user IssueDto dbIssue = new IssueDto() .setKee("ABCDE") .setRuleId(10) .setRuleKey("squid", "AvoidCycles") .setComponentUuid("100") .setComponentKey("struts:org.apache.struts.Action") .setLine(10) .setResolution(Issue.RESOLUTION_FALSE_POSITIVE) .setStatus(Issue.STATUS_RESOLVED) .setAssignee("arthur") .setSeverity(Severity.MAJOR) .setManualSeverity(false); new UpdateConflictResolver().mergeFields(dbIssue, issue); assertThat(issue.key()).isEqualTo("ABCDE"); assertThat(issue.componentKey()).isEqualTo("struts:org.apache.struts.Action"); // Scan wins on : assertThat(issue.line()).isEqualTo(200); assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); assertThat(issue.manualSeverity()).isFalse(); // User wins on : assertThat(issue.assignee()).isEqualTo("arthur"); assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE); assertThat(issue.status()).isEqualTo(Issue.STATUS_RESOLVED); }
public boolean setPastSeverity( DefaultIssue issue, @Nullable String previousSeverity, IssueChangeContext context) { String currentSeverity = issue.severity(); issue.setSeverity(previousSeverity); return setSeverity(issue, currentSeverity, context); }
@Test public void set_issue_fields() { Date createdAt = DateUtils.addDays(new Date(), -5); Date updatedAt = DateUtils.addDays(new Date(), -3); Date closedAt = DateUtils.addDays(new Date(), -1); IssueDto dto = new IssueDto() .setKee("100") .setType(RuleType.VULNERABILITY) .setRuleId(1) .setRuleKey("squid", "AvoidCycle") .setLanguage("xoo") .setComponentKey("org.sonar.sample:Sample") .setComponentUuid("CDEF") .setProjectUuid("GHIJ") .setModuleUuid("BCDE") .setModuleUuidPath("ABCD.BCDE.") .setProjectKey("org.sonar.sample") .setStatus(Issue.STATUS_CLOSED) .setResolution(Issue.RESOLUTION_FALSE_POSITIVE) .setGap(15.0) .setEffort(10L) .setLine(6) .setSeverity("BLOCKER") .setMessage("message") .setManualSeverity(true) .setAssignee("perceval") .setIssueAttributes("key=value") .setAuthorLogin("pierre") .setIssueCreationDate(createdAt) .setIssueUpdateDate(updatedAt) .setIssueCloseDate(closedAt); DefaultIssue issue = dto.toDefaultIssue(); assertThat(issue.key()).isEqualTo("100"); assertThat(issue.type()).isEqualTo(RuleType.VULNERABILITY); assertThat(issue.ruleKey().toString()).isEqualTo("squid:AvoidCycle"); assertThat(issue.language()).isEqualTo("xoo"); assertThat(issue.componentUuid()).isEqualTo("CDEF"); assertThat(issue.projectUuid()).isEqualTo("GHIJ"); assertThat(issue.componentKey()).isEqualTo("org.sonar.sample:Sample"); assertThat(issue.moduleUuid()).isEqualTo("BCDE"); assertThat(issue.moduleUuidPath()).isEqualTo("ABCD.BCDE."); assertThat(issue.projectKey()).isEqualTo("org.sonar.sample"); assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED); assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE); assertThat(issue.gap()).isEqualTo(15.0); assertThat(issue.effort()).isEqualTo(Duration.create(10L)); assertThat(issue.line()).isEqualTo(6); assertThat(issue.severity()).isEqualTo("BLOCKER"); assertThat(issue.message()).isEqualTo("message"); assertThat(issue.manualSeverity()).isTrue(); assertThat(issue.assignee()).isEqualTo("perceval"); assertThat(issue.attribute("key")).isEqualTo("value"); assertThat(issue.authorLogin()).isEqualTo("pierre"); assertThat(issue.creationDate()).isEqualTo(DateUtils.truncate(createdAt, Calendar.SECOND)); assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(updatedAt, Calendar.SECOND)); assertThat(issue.closeDate()).isEqualTo(DateUtils.truncate(closedAt, Calendar.SECOND)); assertThat(issue.isNew()).isFalse(); }