Search in sources :

Example 1 with PARAM_PROJECT

use of org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT 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)

Example 2 with PARAM_PROJECT

use of org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT 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)

Aggregations

Request (org.sonar.api.server.ws.Request)2 Response (org.sonar.api.server.ws.Response)2 WebService (org.sonar.api.server.ws.WebService)2 UserRole (org.sonar.api.web.UserRole)2 DbClient (org.sonar.db.DbClient)2 DbSession (org.sonar.db.DbSession)2 BranchDto (org.sonar.db.component.BranchDto)2 PULL_REQUEST (org.sonar.db.component.BranchType.PULL_REQUEST)2 ProjectDto (org.sonar.db.project.ProjectDto)2 PullRequestsWs.addProjectParam (org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam)2 PARAM_PROJECT (org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT)2 ComponentFinder (org.sonar.server.component.ComponentFinder)2 UserSession (org.sonar.server.user.UserSession)2 Strings.emptyToNull (com.google.common.base.Strings.emptyToNull)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1