Search in sources :

Example 76 with IssueDto

use of org.sonar.db.issue.IssueDto in project sonarqube by SonarSource.

the class IssueIteratorFactoryTest method extract_file_path.

@Test
public void extract_file_path() {
    RuleDefinitionDto rule = dbTester.rules().insert();
    ComponentDto project = dbTester.components().insertPrivateProject();
    ComponentDto module = dbTester.components().insertComponent(newModuleDto(project));
    ComponentDto fileInRootDir = dbTester.components().insertComponent(newFileDto(module).setPath("pom.xml"));
    ComponentDto fileInSubDir = dbTester.components().insertComponent(newFileDto(module).setPath("src/main/java/Action.java"));
    IssueDto projectIssue = dbTester.issues().insert(rule, project, project);
    IssueDto fileInSubDirIssue = dbTester.issues().insert(rule, project, fileInSubDir);
    IssueDto fileInRootDirIssue = dbTester.issues().insert(rule, project, fileInRootDir);
    IssueDto moduleIssue = dbTester.issues().insert(rule, project, module);
    Map<String, IssueDoc> issuesByKey = issuesByKey();
    assertThat(issuesByKey).hasSize(4);
    assertThat(issuesByKey.get(fileInSubDirIssue.getKey()).filePath()).isEqualTo("src/main/java/Action.java");
    assertThat(issuesByKey.get(fileInRootDirIssue.getKey()).filePath()).isEqualTo("pom.xml");
    assertThat(issuesByKey.get(moduleIssue.getKey()).filePath()).isNull();
    assertThat(issuesByKey.get(projectIssue.getKey()).filePath()).isNull();
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 77 with IssueDto

use of org.sonar.db.issue.IssueDto in project sonarqube by SonarSource.

the class IssueIteratorFactoryTest method iterator_over_one_issue.

@Test
public void iterator_over_one_issue() {
    RuleDefinitionDto rule = dbTester.rules().insert();
    ComponentDto project = dbTester.components().insertPrivateProject();
    ComponentDto file = dbTester.components().insertComponent(newFileDto(project).setPath("src/main/java/Action.java"));
    IssueDto expected = dbTester.issues().insert(rule, project, file, t -> t.setResolution("FIXED").setStatus("RESOLVED").setSeverity("BLOCKER").setManualSeverity(false).setAssigneeUuid("uuid-of-guy1").setAuthorLogin("guy2").setChecksum("FFFFF").setGap(2D).setEffort(10L).setMessage(null).setLine(444).setRuleUuid(rule.getUuid()).setTags(List.of("tag1", "tag2", "tag3")).setCreatedAt(1400000000000L).setUpdatedAt(1400000000000L).setIssueCreationDate(new Date(1115848800000L)).setIssueUpdateDate(new Date(1356994800000L)).setIssueCloseDate(null).setType(2));
    Map<String, IssueDoc> issuesByKey = issuesByKey();
    assertThat(issuesByKey).hasSize(1);
    IssueDoc issue = issuesByKey.get(expected.getKey());
    assertThat(issue.key()).isEqualTo(expected.getKey());
    assertThat(issue.resolution()).isEqualTo("FIXED");
    assertThat(issue.status()).isEqualTo("RESOLVED");
    assertThat(issue.severity()).isEqualTo("BLOCKER");
    assertThat(issue.assigneeUuid()).isEqualTo("uuid-of-guy1");
    assertThat(issue.authorLogin()).isEqualTo("guy2");
    assertThat(issue.line()).isEqualTo(444);
    assertThat(issue.ruleUuid()).isEqualTo(rule.getUuid());
    assertThat(issue.componentUuid()).isEqualTo(file.uuid());
    assertThat(issue.projectUuid()).isEqualTo(file.projectUuid());
    assertThat(issue.moduleUuid()).isEqualTo(file.projectUuid());
    assertThat(issue.modulePath()).isEqualTo(file.moduleUuidPath());
    assertThat(issue.directoryPath()).isEqualTo("src/main/java");
    assertThat(issue.filePath()).isEqualTo("src/main/java/Action.java");
    assertThat(issue.getTags()).containsOnly("tag1", "tag2", "tag3");
    assertThat(issue.effort().toMinutes()).isPositive();
    assertThat(issue.type().getDbConstant()).isEqualTo(2);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Date(java.util.Date) Test(org.junit.Test)

Example 78 with IssueDto

use of org.sonar.db.issue.IssueDto in project sonarqube by SonarSource.

the class IssuesAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto component = loadComponent(dbSession, request);
        userSession.checkComponentPermission(USER, component);
        Map<String, String> keysByUUid = keysByUUid(dbSession, component);
        ScannerInput.ServerIssue.Builder responseBuilder = ScannerInput.ServerIssue.newBuilder();
        response.stream().setMediaType(MediaTypes.PROTOBUF);
        OutputStream output = response.stream().output();
        List<IssueDto> issueDtos = new ArrayList<>();
        switch(component.scope()) {
            case Scopes.PROJECT:
                issueDtos.addAll(dbClient.issueDao().selectNonClosedByModuleOrProjectExcludingExternalsAndSecurityHotspots(dbSession, component));
                break;
            case Scopes.FILE:
                issueDtos.addAll(dbClient.issueDao().selectNonClosedByComponentUuidExcludingExternalsAndSecurityHotspots(dbSession, component.uuid()));
                break;
            default:
                // only projects, modules and files are supported. Other types of components are not allowed.
                throw new IllegalArgumentException(format("Component of scope '%s' is not allowed", component.scope()));
        }
        List<String> usersUuids = issueDtos.stream().filter(issue -> issue.getAssigneeUuid() != null).map(IssueDto::getAssigneeUuid).collect(toList());
        Map<String, String> userLoginsByUserUuids = dbClient.userDao().selectByUuids(dbSession, usersUuids).stream().collect(toMap(UserDto::getUuid, UserDto::getLogin));
        issueDtos.forEach(issue -> {
            issue.setAssigneeUuid(userLoginsByUserUuids.get(issue.getAssigneeUuid()));
            handleIssue(issue, responseBuilder, keysByUUid, output);
        });
    }
}
Also used : DbSession(org.sonar.db.DbSession) OutputStream(java.io.OutputStream) ComponentDto(org.sonar.db.component.ComponentDto) ArrayList(java.util.ArrayList) IssueDto(org.sonar.db.issue.IssueDto)

Example 79 with IssueDto

use of org.sonar.db.issue.IssueDto in project sonarqube by SonarSource.

the class AssignAction method assign.

private SearchResponseData assign(String issueKey, @Nullable String login) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        IssueDto issueDto = issueFinder.getByKey(dbSession, issueKey);
        DefaultIssue issue = issueDto.toDefaultIssue();
        UserDto user = getUser(dbSession, login);
        IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid());
        if (issueFieldsSetter.assign(issue, user, context)) {
            return issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, issue, context, false);
        }
        return new SearchResponseData(issueDto);
    }
}
Also used : DbSession(org.sonar.db.DbSession) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) UserDto(org.sonar.db.user.UserDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Example 80 with IssueDto

use of org.sonar.db.issue.IssueDto in project sonarqube by SonarSource.

the class IssueUpdater method doSaveIssue.

private IssueDto doSaveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, Optional<RuleDefinitionDto> rule, ComponentDto project, BranchDto branchDto) {
    IssueDto issueDto = issueStorage.save(session, singletonList(issue)).iterator().next();
    if (// since this method is called after an update of the issue, date should never be null
    issue.updateDate() == null || // name of rule is displayed in notification, rule must therefor be present
    !rule.isPresent() || // notification are not supported on PRs
    !hasNotificationSupport(branchDto)) {
        return issueDto;
    }
    Optional<UserDto> assignee = Optional.ofNullable(issue.assignee()).map(assigneeUuid -> dbClient.userDao().selectByUuid(session, assigneeUuid));
    UserDto author = Optional.ofNullable(context.userUuid()).map(authorUuid -> dbClient.userDao().selectByUuid(session, authorUuid)).orElseThrow(() -> new IllegalStateException("Can not find dto for change author " + context.userUuid()));
    IssuesChangesNotificationBuilder notificationBuilder = new IssuesChangesNotificationBuilder(singleton(new ChangedIssue.Builder(issue.key()).setNewResolution(issue.resolution()).setNewStatus(issue.status()).setAssignee(assignee.map(assigneeDto -> new User(assigneeDto.getUuid(), assigneeDto.getLogin(), assigneeDto.getName())).orElse(null)).setRule(rule.map(r -> new Rule(r.getKey(), RuleType.valueOfNullable(r.getType()), r.getName())).get()).setProject(new Project.Builder(project.uuid()).setKey(project.getKey()).setProjectName(project.name()).setBranchName(branchDto.isMain() ? null : branchDto.getKey()).build()).build()), new UserChange(issue.updateDate().getTime(), new User(author.getUuid(), author.getLogin(), author.getName())));
    notificationService.scheduleForSending(notificationSerializer.serialize(notificationBuilder));
    return issueDto;
}
Also used : BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) RuleStatus(org.sonar.api.rule.RuleStatus) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) RuleType(org.sonar.api.rules.RuleType) Collections.singletonList(java.util.Collections.singletonList) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collections.singleton(java.util.Collections.singleton) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) DefaultIssue(org.sonar.core.issue.DefaultIssue) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DbClient(org.sonar.db.DbClient) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) RuleKey(org.sonar.api.rule.RuleKey) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) UserDto(org.sonar.db.user.UserDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule)

Aggregations

IssueDto (org.sonar.db.issue.IssueDto)478 Test (org.junit.Test)425 ComponentDto (org.sonar.db.component.ComponentDto)324 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)213 UserDto (org.sonar.db.user.UserDto)136 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)77 TestRequest (org.sonar.server.ws.TestRequest)66 Date (java.util.Date)60 Hotspots (org.sonarqube.ws.Hotspots)57 DefaultIssue (org.sonar.core.issue.DefaultIssue)55 IssueIndexer (org.sonar.server.issue.index.IssueIndexer)53 NotFoundException (org.sonar.server.exceptions.NotFoundException)51 DbClient (org.sonar.db.DbClient)50 List (java.util.List)49 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)49 Rule (org.junit.Rule)49 System2 (org.sonar.api.utils.System2)49 DbTester (org.sonar.db.DbTester)49 RuleDto (org.sonar.db.rule.RuleDto)49 IntStream (java.util.stream.IntStream)48