@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization); writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response); } }
@Override public QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto project, BranchDto branch) { if (branch.getBranchType() == BranchType.SHORT || branch.getBranchType() == BranchType.PULL_REQUEST) { return ShortLivingBranchQualityGate.GATE; } ComponentDto mainProject = project.getMainBranchProjectUuid() == null ? project : dbClient.componentDao().selectOrFailByKey(dbSession, project.getKey()); QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject) .orElseThrow(() -> new IllegalStateException(format("Quality Gate not found for project %s", mainProject.getKey()))) .getQualityGate(); Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getId()); Set<Integer> metricIds = conditionDtos.stream().map(c -> (int) c.getMetricId()) .collect(toHashSet(conditionDtos.size())); Map<Integer, MetricDto> metricsById = dbClient.metricDao().selectByIds(dbSession, metricIds).stream() .collect(uniqueIndex(MetricDto::getId)); Set<Condition> conditions = conditionDtos.stream().map(conditionDto -> { String metricKey = metricsById.get((int) conditionDto.getMetricId()).getKey(); Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator()); return new Condition(metricKey, operator, conditionDto.getErrorThreshold()); }).collect(toHashSet(conditionDtos.size())); return new QualityGate(String.valueOf(gateDto.getId()), gateDto.getName(), conditions); }
private void init(Page... pages) { PluginRepository pluginRepository = mock(PluginRepository.class); when(pluginRepository.hasPlugin(any())).thenReturn(true); when(pluginRepository.getPluginInfo(any())).thenReturn(new PluginInfo("unused").setVersion(Version.create("1.0"))); CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); when(coreExtensionRepository.isInstalled(any())).thenReturn(false); PageRepository pageRepository = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {context -> { for (Page page : pages) { context.addPage(page); } }}); pageRepository.start(); ws = new WsActionTester( new ComponentAction(dbClient, pageRepository, resourceTypes, userSession, new ComponentFinder(dbClient, resourceTypes), new QualityGateFinder(dbClient), billingValidations)); }
@Override public void handle(Request request, Response response) { long qualityGateId = request.mandatoryParamAsLong(QualityGatesWsParameters.PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId); QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); checkArgument(!defaultQualityGate.getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); wsSupport.checkCanEdit(qualityGate); dbClient.propertiesDao().deleteByKeyAndValue(dbSession, SONAR_QUALITYGATE_PROPERTY, String.valueOf(qualityGate.getId())); dbClient.qualityGateDao().delete(qualityGate, dbSession); dbSession.commit(); response.noContent(); } }
private QualityGateDto getByNameOrId(DbSession dbSession, OrganizationDto organization, @Nullable String name, @Nullable Long id) { if (name != null) { return checkFound(dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name), "No quality gate has been found for name %s", name); } if (id != null) { return qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); } throw new IllegalArgumentException("No parameter has been set to identify a quality gate"); }
@Test public void ensure_only_that_builtin_is_set_as_default_when_no_default_quality_gate() { insertMetrics(); QualityGateDto builtInQualityGate = db.qualityGates().insertBuiltInQualityGate(); underTest.start(); assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession)).isNotNull(); assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession).getId()).isEqualTo(builtInQualityGate.getId()); }
private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, long id, String name) { QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); wsSupport.checkCanEdit(qualityGate); checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name"); checkNotAlreadyExists(dbSession, organization, qualityGate, name); qualityGate.setName(name); dbClient.qualityGateDao().update(qualityGate, dbSession); dbSession.commit(); return qualityGate; }
private void writeQualityGate(JsonWriter json, DbSession session, OrganizationDto organization, ComponentDto component) { QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component) .orElseThrow(() -> new NotFoundException(format("Quality Gate not found for %s", component.getKey()))); QualityGateDto qualityGateDto = qualityGateData.getQualityGate(); json.name("qualityGate").beginObject() .prop("key", qualityGateDto.getId()) .prop("name", qualityGateDto.getName()) .prop("isDefault", qualityGateData.isDefault()) .endObject(); }
@Override public void handle(Request request, Response response) { long qualityGateId = request.mandatoryParamAsLong(QualityGatesWsParameters.PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, qualityGateId); QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); checkArgument(!defaultQualityGate.getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); wsSupport.checkCanEdit(qualityGate); dbClient.propertiesDao().deleteByKeyAndValue(dbSession, SONAR_QUALITYGATE_PROPERTY, String.valueOf(qualityGate.getId())); dbClient.qualityGateDao().delete(qualityGate, dbSession); dbSession.commit(); response.noContent(); } }
@Override public void handle(Request request, Response response) { Long id = parseId(request, PARAM_ID); String destinationName = request.mandatoryParam(PARAM_NAME); checkArgument(!destinationName.isEmpty(), "The 'name' parameter is empty"); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); userSession.checkPermission(ADMINISTER_QUALITY_GATES, organization); QualityGateDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); QualityGateDto copy = qualityGateUpdater.copy(dbSession, organization, qualityGate, destinationName); dbSession.commit(); writeProtobuf(newBuilder() .setId(copy.getId()) .setName(copy.getName()) .build(), request, response); } } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); ComponentDto project = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_PROJECT)); // As ComponentFinder doesn't handle organization yet, we only check here that the project belongs to the organization wsSupport.checkProjectBelongsToOrganization(organization, project); if (!userSession.hasComponentPermission(USER, project) && !userSession.hasComponentPermission(ADMIN, project)) { throw insufficientPrivilegesException(); } QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project) .orElseThrow(() -> new NotFoundException(format("Quality gate not found for project %s", project.getKey()))); writeProtobuf(buildResponse(data), request, response); } }
@Override public void handle(Request request, Response response) { Long id = request.paramAsLong(PARAM_ID); String name = request.param(PARAM_NAME); checkOneOfIdOrNamePresent(id, name); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QualityGateDto qualityGate = getByNameOrId(dbSession, organization, name, id); Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate); Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions); QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession, organization); writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate, conditions, metricsById), request, response); } }
@Override public void handle(Request request, Response response) { Long id = parseId(request, PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); userSession.checkPermission(ADMINISTER_QUALITY_GATES, organization); QualityGateDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); organization.setDefaultQualityGateUuid(qualityGate.getUuid()); dbClient.organizationDao().update(dbSession, organization); dbSession.commit(); } response.noContent(); }
@Test public void return_default_quality_gate_for_project() { ComponentDto project = db.components().insertPrivateProject(); QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); assertThat(result).isPresent(); assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); assertThat(result.get().isDefault()).isTrue(); }
@Test public void fail_when_default_quality_gate_does_not_exists() { QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); expectedException.expect(IllegalStateException.class); expectedException.expectMessage(format("Default quality gate [%s] is missing on organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid())); underTest.getDefault(dbSession, db.getDefaultOrganization()); }
@Override public void handle(Request request, Response response) { long gateId = request.mandatoryParamAsLong(PARAM_GATE_ID); String projectId = request.param(PARAM_PROJECT_ID); String projectKey = request.param(PARAM_PROJECT_KEY); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, gateId); ComponentDto project = getProject(dbSession, organization, projectId, projectKey); wsSupport.checkCanAdminProject(organization, project); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(SONAR_QUALITYGATE_PROPERTY) .setResourceId(project.getId()) .setValue(String.valueOf(qualityGate.getId()))); dbSession.commit(); } response.noContent(); }
@Test public void return_project_quality_gate_over_default() { ComponentDto project = db.components().insertPrivateProject(); db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(),qg -> qg.setName("Sonar way")); QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); assertThat(result).isPresent(); assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); assertThat(result.get().isDefault()).isFalse(); }
@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization); writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response); } }
@Override public void handle(Request request, Response response) { int gateId = request.mandatoryParamAsInt(PARAM_GATE_ID); String metric = request.mandatoryParam(PARAM_METRIC); String operator = request.mandatoryParam(PARAM_OPERATOR); String warning = request.param(PARAM_WARNING); String error = request.param(PARAM_ERROR); Integer period = request.paramAsInt(PARAM_PERIOD); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, gateId); wsSupport.checkCanEdit(qualityGate); QualityGateConditionDto condition = qualityGateConditionsUpdater.createCondition(dbSession, qualityGate, metric, operator, emptyToNull(warning), emptyToNull(error), period); CreateConditionResponse.Builder createConditionResponse = CreateConditionResponse.newBuilder() .setId(condition.getId()) .setMetric(condition.getMetricKey()) .setOp(condition.getOperator()); setNullable(condition.getWarningThreshold(), createConditionResponse::setWarning); setNullable(condition.getErrorThreshold(), createConditionResponse::setError); setNullable(condition.getPeriod(), createConditionResponse::setPeriod); writeProtobuf(createConditionResponse.build(), request, response); dbSession.commit(); } }
@Test public void fail_when_default_qgate_defined_does_not_exists() { ComponentDto project = db.components().insertPrivateProject(); QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); db.commit(); assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty(); }