public boolean setManualSeverity(DefaultIssue issue, String severity, IssueChangeContext context) {
   if (!issue.manualSeverity() || !Objects.equal(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;
 }
예제 #2
0
 public boolean setSeverity(DefaultIssue issue, String severity, IssueChangeContext context) {
   if (issue.manualSeverity()) {
     throw new IllegalStateException("Severity can't be changed");
   }
   if (!Objects.equal(severity, issue.severity())) {
     issue.setFieldChange(context, SEVERITY, issue.severity(), severity);
     issue.setSeverity(severity);
     issue.setUpdateDate(context.date());
     issue.setChanged(true);
     return true;
   }
   return false;
 }
  @Test
  public void severity_changed_by_user_should_be_kept() throws Exception {
    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 should_keep_changes_made_by_user() throws Exception {
    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.setActionPlanKey("PLAN-1");
    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")
            .setActionPlanKey("PLAN-2")
            .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);
    assertThat(issue.actionPlanKey()).isEqualTo("PLAN-2");
  }
예제 #5
0
  @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")
            .setRuleId(1)
            .setRuleKey_unit_test_only("squid", "AvoidCycle")
            .setComponentKey_unit_test_only("org.sonar.sample:Sample")
            .setRootComponentKey_unit_test_only("org.sonar.sample")
            .setComponentId(1l)
            .setRootComponentId(1l)
            .setStatus(Issue.STATUS_CLOSED)
            .setResolution(Issue.RESOLUTION_FALSE_POSITIVE)
            .setEffortToFix(15.0)
            .setTechnicalDebt(101010L)
            .setLine(6)
            .setSeverity("BLOCKER")
            .setMessage("message")
            .setManualSeverity(true)
            .setReporter("arthur")
            .setAssignee("perceval")
            .setIssueAttributes("key=value")
            .setAuthorLogin("pierre")
            .setIssueCreationDate(createdAt)
            .setIssueUpdateDate(updatedAt)
            .setIssueCloseDate(closedAt);

    DefaultIssue issue = dto.toDefaultIssue();
    assertThat(issue.key()).isEqualTo("100");
    assertThat(issue.ruleKey().toString()).isEqualTo("squid:AvoidCycle");
    assertThat(issue.componentKey()).isEqualTo("org.sonar.sample:Sample");
    assertThat(issue.projectKey()).isEqualTo("org.sonar.sample");
    assertThat(issue.status()).isEqualTo(Issue.STATUS_CLOSED);
    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
    assertThat(issue.effortToFix()).isEqualTo(15.0);
    assertThat(issue.technicalDebt()).isEqualTo(WorkDayDuration.of(10, 10, 10));
    assertThat(issue.line()).isEqualTo(6);
    assertThat(issue.severity()).isEqualTo("BLOCKER");
    assertThat(issue.message()).isEqualTo("message");
    assertThat(issue.manualSeverity()).isTrue();
    assertThat(issue.reporter()).isEqualTo("arthur");
    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();
  }
  @Test
  public void not_set_default_severity() {
    DefaultIssue issue =
        (DefaultIssue)
            new DefaultIssueBuilder()
                .componentKey("Action.java")
                .projectKey("org.apache.struts")
                .ruleKey(RuleKey.of("squid", "NullDereference"))
                .build();

    assertThat(issue.severity()).isNull();
  }
예제 #7
0
 private void updateIssue(DefaultIssue issue, Rule rule, ActiveRule activeRule) {
   if (Strings.isNullOrEmpty(issue.message())) {
     issue.setMessage(rule.name());
   }
   issue.setCreationDate(project.getAnalysisDate());
   issue.setUpdateDate(project.getAnalysisDate());
   if (issue.severity() == null) {
     issue.setSeverity(activeRule.severity());
   }
   DebtRemediationFunction function = rule.debtRemediationFunction();
   if (rule.debtSubCharacteristic() != null && function != null) {
     issue.setDebt(calculateDebt(function, issue.effortToFix(), rule.key()));
   }
 }
예제 #8
0
  private void writeIssue(IssueQueryResult result, DefaultIssue issue, JsonWriter json) {
    Component component = result.component(issue);
    Component project = result.project(issue);
    String actionPlanKey = issue.actionPlanKey();
    WorkDayDuration technicalDebt = issue.technicalDebt();
    Date updateDate = issue.updateDate();
    Date closeDate = issue.closeDate();

    json.prop("key", issue.key())
        .prop("component", issue.componentKey())
        .prop("componentLongName", component != null ? component.longName() : null)
        .prop("componentQualifier", component != null ? component.qualifier() : null)
        .prop("project", issue.projectKey())
        .prop("projectLongName", project != null ? project.longName() : null)
        .prop("rule", issue.ruleKey().toString())
        .prop("ruleName", result.rule(issue).getName())
        .prop("line", issue.line())
        .prop("message", issue.message())
        .prop("resolution", issue.resolution())
        .prop("status", issue.status())
        .prop("severity", issue.severity())
        .prop("author", issue.authorLogin())
        .prop("actionPlan", actionPlanKey)
        .prop(
            "debt",
            technicalDebt != null
                ? technicalDebtFormatter.format(UserSession.get().locale(), technicalDebt)
                : null)
        .prop("actionPlanName", actionPlanKey != null ? result.actionPlan(issue).name() : null)
        .prop("creationDate", DateUtils.formatDateTime(issue.creationDate()))
        .prop("fCreationDate", formatDate(issue.creationDate()))
        .prop("updateDate", updateDate != null ? DateUtils.formatDateTime(updateDate) : null)
        .prop("fUpdateDate", formatDate(updateDate))
        .prop("fUpdateAge", formatAgeDate(updateDate))
        .prop("closeDate", closeDate != null ? DateUtils.formatDateTime(closeDate) : null)
        .prop("fCloseDate", formatDate(issue.closeDate()));

    addUserWithLabel(result, issue.assignee(), "assignee", json);
    addUserWithLabel(result, issue.reporter(), "reporter", json);
  }
예제 #9
0
  private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) {
    // TODO fail fast : if rule does not exist

    ActiveRule activeRule =
        qProfile.getActiveRule(issue.ruleKey().repository(), issue.ruleKey().rule());
    if (activeRule == null || activeRule.getRule() == null) {
      // rule does not exist or is not enabled -> ignore the issue
      return false;
    }
    issue.setCreationDate(project.getAnalysisDate());
    issue.setUpdateDate(project.getAnalysisDate());
    if (issue.severity() == null) {
      issue.setSeverity(activeRule.getSeverity().name());
    }
    issue.setRemediationCost(technicalDebtCalculator.cost(issue));

    if (filters.accept(issue, violation)) {
      cache.put(issue);
      return true;
    }
    return false;
  }
  @Test
  public void build_new_issue() throws Exception {
    String componentKey = "org.apache.struts:struts-core:Action.java";
    String projectKey = "org.apache.struts";
    DefaultIssue issue =
        (DefaultIssue)
            new DefaultIssueBuilder()
                .componentKey(componentKey)
                .projectKey(projectKey)
                .message("the message")
                .line(123)
                .effortToFix(10000.0)
                .ruleKey(RuleKey.of("squid", "NullDereference"))
                .severity(Severity.CRITICAL)
                .attribute("JIRA", "FOO-123")
                .attribute("YOUTRACK", "YT-123")
                .build();

    assertThat(issue).isNotNull();
    assertThat(issue.key()).isNotNull();
    assertThat(issue.effortToFix()).isEqualTo(10000.0);
    assertThat(issue.componentKey()).isEqualTo(componentKey);
    assertThat(issue.projectKey()).isEqualTo(projectKey);
    assertThat(issue.message()).isEqualTo("the message");
    assertThat(issue.line()).isEqualTo(123);
    assertThat(issue.ruleKey().repository()).isEqualTo("squid");
    assertThat(issue.ruleKey().rule()).isEqualTo("NullDereference");
    assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
    assertThat(issue.assignee()).isNull();
    assertThat(issue.isNew()).isTrue();
    assertThat(issue.resolution()).isNull();
    assertThat(issue.status()).isEqualTo(Issue.STATUS_OPEN);
    assertThat(issue.attribute("JIRA")).isEqualTo("FOO-123");
    assertThat(issue.attribute("YOUTRACK")).isEqualTo("YT-123");
    assertThat(issue.attributes()).hasSize(2);
  }
예제 #11
0
 public boolean setPastSeverity(
     DefaultIssue issue, @Nullable String previousSeverity, IssueChangeContext context) {
   String currentSeverity = issue.severity();
   issue.setSeverity(previousSeverity);
   return setSeverity(issue, currentSeverity, context);
 }