Search in sources :

Example 81 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class WebhookQGChangeEventListenerTest method insertBranch.

public ProjectAndBranch insertBranch(BranchType type, String branchKey) {
    ProjectDto project = dbTester.components().insertPrivateProjectDto();
    BranchDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey(branchKey).setBranchType(type));
    return new ProjectAndBranch(project, branch);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto)

Example 82 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class WebhookQGChangeEventListenerTest method insertMainBranch.

public ProjectAndBranch insertMainBranch() {
    ProjectDto project = dbTester.components().insertPrivateProjectDto();
    BranchDto branch = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), project.getUuid()).get();
    dbTester.commit();
    return new ProjectAndBranch(project, branch);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto)

Example 83 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class AsyncIssueIndexingImpl method triggerOnIndexCreation.

@Override
public void triggerOnIndexCreation() {
    try (DbSession dbSession = dbClient.openSession(false)) {
        // remove already existing indexation task, if any
        removeExistingIndexationTasks(dbSession);
        dbClient.branchDao().updateAllNeedIssueSync(dbSession);
        List<BranchDto> branchInNeedOfIssueSync = dbClient.branchDao().selectBranchNeedingIssueSync(dbSession);
        LOG.info("{} branch found in need of issue sync.", branchInNeedOfIssueSync.size());
        if (branchInNeedOfIssueSync.isEmpty()) {
            return;
        }
        List<String> projectUuids = branchInNeedOfIssueSync.stream().map(BranchDto::getProjectUuid).distinct().collect(Collectors.toList());
        LOG.info("{} projects found in need of issue sync.", projectUuids.size());
        sortProjectUuids(dbSession, projectUuids);
        Map<String, List<BranchDto>> branchesByProject = branchInNeedOfIssueSync.stream().collect(Collectors.groupingBy(BranchDto::getProjectUuid));
        List<CeTaskSubmit> tasks = new ArrayList<>();
        for (String projectUuid : projectUuids) {
            List<BranchDto> branches = branchesByProject.get(projectUuid);
            for (BranchDto branch : branches) {
                tasks.add(buildTaskSubmit(branch));
            }
        }
        ceQueue.massSubmit(tasks);
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CeTaskSubmit(org.sonar.ce.queue.CeTaskSubmit)

Example 84 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class DeleteAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    String pullRequestId = request.mandatoryParam(PARAM_PULL_REQUEST);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        BranchDto pullRequest = dbClient.branchDao().selectByPullRequestKey(dbSession, project.getUuid(), pullRequestId).filter(branch -> branch.getBranchType() == PULL_REQUEST).orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' is not found for project '%s'", pullRequestId, projectKey)));
        componentCleanerService.deleteBranch(dbSession, pullRequest);
        response.noContent();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) NewController(org.sonar.api.server.ws.WebService.NewController) BranchDto(org.sonar.db.component.BranchDto) ComponentFinder(org.sonar.server.component.ComponentFinder) PARAM_PROJECT(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT) PullRequestsWs.addPullRequestParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addPullRequestParam) DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) PARAM_PULL_REQUEST(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PULL_REQUEST) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) UserRole(org.sonar.api.web.UserRole) WebService(org.sonar.api.server.ws.WebService) PullRequestsWs.addProjectParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam) ACTION_DELETE(org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_DELETE) Response(org.sonar.api.server.ws.Response) ProjectDto(org.sonar.db.project.ProjectDto) UserSession(org.sonar.server.user.UserSession) ComponentCleanerService(org.sonar.server.component.ComponentCleanerService) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) NotFoundException(org.sonar.server.exceptions.NotFoundException)

Example 85 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ListAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        List<BranchDto> pullRequests = dbClient.branchDao().selectByProject(dbSession, project).stream().filter(b -> b.getBranchType() == PULL_REQUEST).collect(toList());
        List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao().selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())).stream().collect(uniqueIndex(BranchDto::getUuid));
        Map<String, PrStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream().collect(uniqueIndex(PrStatistics::getBranchUuid, Function.identity()));
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
        pullRequests.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        writeProtobuf(protobufResponse.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) AbstractUserSession.insufficientPrivilegesException(org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException) ComponentFinder(org.sonar.server.component.ComponentFinder) StringUtils(org.apache.commons.lang.StringUtils) ProjectPullRequests(org.sonarqube.ws.ProjectPullRequests) GlobalPermission(org.sonar.db.permission.GlobalPermission) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) DbProjectBranches(org.sonar.db.protobuf.DbProjectBranches) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) IssueIndex(org.sonar.server.issue.index.IssueIndex) PullRequestsWs.addProjectParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) PrStatistics(org.sonar.server.issue.index.PrStatistics) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) PARAM_PROJECT(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) PrStatistics(org.sonar.server.issue.index.PrStatistics) DbSession(org.sonar.db.DbSession) Objects(java.util.Objects)

Aggregations

BranchDto (org.sonar.db.component.BranchDto)111 Test (org.junit.Test)62 ComponentDto (org.sonar.db.component.ComponentDto)52 ProjectDto (org.sonar.db.project.ProjectDto)42 DbSession (org.sonar.db.DbSession)31 SnapshotDto (org.sonar.db.component.SnapshotDto)22 List (java.util.List)15 ComponentTesting.newBranchDto (org.sonar.db.component.ComponentTesting.newBranchDto)13 DbClient (org.sonar.db.DbClient)12 MetricDto (org.sonar.db.metric.MetricDto)12 Map (java.util.Map)9 Optional (java.util.Optional)9 Nullable (javax.annotation.Nullable)9 WebService (org.sonar.api.server.ws.WebService)9 System2 (org.sonar.api.utils.System2)9 NotFoundException (org.sonar.server.exceptions.NotFoundException)9 Collection (java.util.Collection)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Rule (org.junit.Rule)8 Request (org.sonar.api.server.ws.Request)8