Search in sources :

Example 31 with BranchDto

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

the class WebhookQGChangeEventListener method buildWebHookPayload.

private WebhookPayload buildWebHookPayload(DbSession dbSession, QGChangeEvent event, @Nullable EvaluatedQualityGate evaluatedQualityGate) {
    ProjectDto project = event.getProject();
    BranchDto branch = event.getBranch();
    SnapshotDto analysis = event.getAnalysis();
    Map<String, String> analysisProperties = dbClient.analysisPropertiesDao().selectByAnalysisUuid(dbSession, analysis.getUuid()).stream().collect(Collectors.toMap(AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue));
    ProjectAnalysis projectAnalysis = new ProjectAnalysis(new Project(project.getUuid(), project.getKey(), project.getName()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt(), analysis.getRevision()), new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())), evaluatedQualityGate, null, analysisProperties);
    return webhookPayloadFactory.create(projectAnalysis);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto)

Example 32 with BranchDto

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

the class AnalysisStatusAction method doHandle.

private void doHandle(Request request, Response response, String projectKey, @Nullable String branchKey, @Nullable String pullRequestKey) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
        userSession.checkProjectPermission(UserRole.USER, project);
        BranchDto branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchKey, pullRequestKey);
        AnalysisStatusWsResponse.Builder responseBuilder = AnalysisStatusWsResponse.newBuilder();
        CeActivityDto lastActivity = dbClient.ceActivityDao().selectLastByComponentUuidAndTaskType(dbSession, branch.getUuid(), CeTaskTypes.REPORT).orElse(null);
        responseBuilder.setComponent(formatComponent(dbSession, project, lastActivity, branchKey, pullRequestKey));
        writeProtobuf(responseBuilder.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) CeActivityDto(org.sonar.db.ce.CeActivityDto) AnalysisStatusWsResponse(org.sonarqube.ws.Ce.AnalysisStatusWsResponse)

Example 33 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 projectOrApp = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(projectOrApp);
        Collection<BranchDto> branches = dbClient.branchDao().selectByProject(dbSession, projectOrApp).stream().filter(b -> b.getBranchType() == BRANCH).collect(toList());
        List<String> branchUuids = branches.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder();
        branches.forEach(b -> addBranch(protobufResponse, b, qualityGateMeasuresByComponentUuids.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        WsUtils.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) ProjectBranches(org.sonarqube.ws.ProjectBranches) DbSession(org.sonar.db.DbSession) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) WebService(org.sonar.api.server.ws.WebService) Map(java.util.Map) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) Resources(com.google.common.io.Resources) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) WsUtils(org.sonar.server.ws.WsUtils) BranchesWs.addProjectParam(org.sonar.server.branch.ws.BranchesWs.addProjectParam) PARAM_PROJECT(org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_PROJECT) BRANCH(org.sonar.db.component.BranchType.BRANCH) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) List(java.util.List) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ACTION_LIST(org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_LIST) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto)

Example 34 with BranchDto

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

the class RenameAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    String newBranchName = request.mandatoryParam(PARAM_NAME);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), newBranchName);
        checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(), "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
        dbClient.branchDao().updateMainBranchName(dbSession, project.getUuid(), newBranchName);
        dbSession.commit();
        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) BranchesWs.addProjectParam(org.sonar.server.branch.ws.BranchesWs.addProjectParam) PARAM_PROJECT(org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_PROJECT) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) UserRole(org.sonar.api.web.UserRole) WebService(org.sonar.api.server.ws.WebService) PARAM_NAME(org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_NAME) Response(org.sonar.api.server.ws.Response) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) UserSession(org.sonar.server.user.UserSession) ACTION_RENAME(org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_RENAME) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto)

Example 35 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 branchKey = request.mandatoryParam(PARAM_BRANCH);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        BranchDto branch = checkFoundWithOptional(dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), branchKey), "Branch '%s' not found for project '%s'", branchKey, projectKey);
        if (branch.isMain()) {
            throw new IllegalArgumentException("Only non-main branches can be deleted");
        }
        componentCleanerService.deleteBranch(dbSession, branch);
        projectLifeCycleListeners.onProjectBranchesDeleted(singleton(from(project)));
        response.noContent();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto)

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