@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);
  }
Exemplo n.º 2
0
  public DefaultIssue createManualIssue(
      String componentKey,
      RuleKey ruleKey,
      @Nullable Integer line,
      @Nullable String message,
      @Nullable String severity) {
    verifyLoggedIn();

    DbSession dbSession = dbClient.openSession(false);
    try {
      Optional<ComponentDto> componentOptional =
          dbClient.componentDao().selectByKey(dbSession, componentKey);
      if (!componentOptional.isPresent()) {
        throw new BadRequestException(
            String.format("Component with key '%s' not found", componentKey));
      }
      ComponentDto component = componentOptional.get();
      ComponentDto project =
          dbClient.componentDao().selectOrFailByUuid(dbSession, component.projectUuid());

      userSession.checkComponentPermission(UserRole.USER, project.getKey());
      if (!ruleKey.isManual()) {
        throw new IllegalArgumentException(
            "Issues can be created only on rules marked as 'manual': " + ruleKey);
      }
      Rule rule = getNullableRuleByKey(ruleKey);
      if (rule == null) {
        throw new IllegalArgumentException("Unknown rule: " + ruleKey);
      }

      DefaultIssue issue =
          new DefaultIssueBuilder()
              .componentKey(component.getKey())
              .projectKey(project.getKey())
              .line(line)
              .message(!Strings.isNullOrEmpty(message) ? message : rule.getName())
              .severity(Objects.firstNonNull(severity, Severity.MAJOR))
              .ruleKey(ruleKey)
              .reporter(userSession.getLogin())
              .assignee(findSourceLineUser(dbSession, component.uuid(), line))
              .build();

      Date now = new Date();
      issue.setCreationDate(now);
      issue.setUpdateDate(now);
      issueStorage.save(issue);
      return issue;
    } finally {
      dbSession.close();
    }
  }