Search in sources :

Example 1 with NewCodePeriodType

use of org.sonar.db.newcodeperiod.NewCodePeriodType in project sonarqube by SonarSource.

the class SetAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);
    String branchKey = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);
    if (projectKey == null && branchKey != null) {
        throw new IllegalArgumentException("If branch key is specified, project key needs to be specified too");
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
        String typeStr = request.mandatoryParam(PARAM_TYPE);
        String valueStr = request.getParam(PARAM_VALUE).emptyAsNull().or(() -> null);
        boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent();
        NewCodePeriodType type = validateType(typeStr, projectKey == null, branchKey != null || isCommunityEdition);
        NewCodePeriodDto dto = new NewCodePeriodDto();
        dto.setType(type);
        ProjectDto project = null;
        BranchDto branch = null;
        if (projectKey != null) {
            project = getProject(dbSession, projectKey);
            userSession.checkProjectPermission(UserRole.ADMIN, project);
            if (branchKey != null) {
                branch = getBranch(dbSession, project, branchKey);
                dto.setBranchUuid(branch.getUuid());
            } else if (isCommunityEdition) {
                // in CE set main branch value instead of project value
                branch = getMainBranch(dbSession, project);
                dto.setBranchUuid(branch.getUuid());
            }
            dto.setProjectUuid(project.getUuid());
        } else {
            userSession.checkIsSystemAdministrator();
        }
        setValue(dbSession, dto, type, project, branch, valueStr);
        newCodePeriodDao.upsert(dbSession, dto);
        dbSession.commit();
    }
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentFinder(org.sonar.server.component.ComponentFinder) NewCodePeriodDao(org.sonar.db.newcodeperiod.NewCodePeriodDao) NewCodePeriodParser(org.sonar.db.newcodeperiod.NewCodePeriodParser) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) WebService(org.sonar.api.server.ws.WebService) Locale(java.util.Locale) Response(org.sonar.api.server.ws.Response) NUMBER_OF_DAYS(org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS) NewCodePeriodType(org.sonar.db.newcodeperiod.NewCodePeriodType) EditionProvider(org.sonar.core.platform.EditionProvider) EnumSet(java.util.EnumSet) Nullable(javax.annotation.Nullable) PREVIOUS_VERSION(org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION) REFERENCE_BRANCH(org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH) PlatformEditionProvider(org.sonar.core.platform.PlatformEditionProvider) Set(java.util.Set) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) UserRole(org.sonar.api.web.UserRole) SPECIFIC_ANALYSIS(org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS) ProjectDto(org.sonar.db.project.ProjectDto) Preconditions(com.google.common.base.Preconditions) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) BranchDto(org.sonar.db.component.BranchDto) NewCodePeriodType(org.sonar.db.newcodeperiod.NewCodePeriodType)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 String.format (java.lang.String.format)1 EnumSet (java.util.EnumSet)1 Locale (java.util.Locale)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 WebService (org.sonar.api.server.ws.WebService)1 UserRole (org.sonar.api.web.UserRole)1 EditionProvider (org.sonar.core.platform.EditionProvider)1 PlatformEditionProvider (org.sonar.core.platform.PlatformEditionProvider)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1 BranchDto (org.sonar.db.component.BranchDto)1 SnapshotDto (org.sonar.db.component.SnapshotDto)1 NewCodePeriodDao (org.sonar.db.newcodeperiod.NewCodePeriodDao)1 NewCodePeriodDto (org.sonar.db.newcodeperiod.NewCodePeriodDto)1 NewCodePeriodParser (org.sonar.db.newcodeperiod.NewCodePeriodParser)1