Search in sources :

Example 21 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class QualityGateConditionsUpdater method createCondition.

public QualityGateConditionDto createCondition(DbSession dbSession, long qGateId, String metricKey, String operator, @Nullable String warningThreshold, @Nullable String errorThreshold, @Nullable Integer period) {
    getNonNullQgate(dbSession, qGateId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(getConditions(dbSession, qGateId, null), metric, period);
    QualityGateConditionDto newCondition = new QualityGateConditionDto().setQualityGateId(qGateId).setMetricId(metric.getId()).setMetricKey(metric.getKey()).setOperator(operator).setWarningThreshold(warningThreshold).setErrorThreshold(errorThreshold).setPeriod(period);
    dbClient.gateConditionDao().insert(newCondition, dbSession);
    return newCondition;
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto)

Example 22 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class QualityGateConditionsUpdater method updateCondition.

public QualityGateConditionDto updateCondition(DbSession dbSession, long condId, String metricKey, String operator, @Nullable String warningThreshold, @Nullable String errorThreshold, @Nullable Integer period) {
    QualityGateConditionDto condition = getNonNullCondition(dbSession, condId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(getConditions(dbSession, condition.getQualityGateId(), condition.getId()), metric, period);
    condition.setMetricId(metric.getId()).setMetricKey(metric.getKey()).setOperator(operator).setWarningThreshold(warningThreshold).setErrorThreshold(errorThreshold).setPeriod(period);
    dbClient.gateConditionDao().update(condition, dbSession);
    return condition;
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto)

Example 23 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class RegisterMetrics method save.

private void save(DbSession session, Iterable<Metric> metrics) {
    Map<String, MetricDto> basesByKey = new HashMap<>();
    for (MetricDto base : from(dbClient.metricDao().selectAll(session)).toList()) {
        basesByKey.put(base.getKey(), base);
    }
    for (Metric metric : metrics) {
        MetricDto dto = MetricToDto.INSTANCE.apply(metric);
        MetricDto base = basesByKey.get(metric.getKey());
        if (base == null) {
            // new metric, never installed
            dbClient.metricDao().insert(session, dto);
        } else if (!base.isUserManaged()) {
            // existing metric, update changes. Existing custom metrics are kept without applying changes.
            dto.setId(base.getId());
            dbClient.metricDao().update(session, dto);
        }
        basesByKey.remove(metric.getKey());
    }
    for (MetricDto nonUpdatedBase : basesByKey.values()) {
        if (!nonUpdatedBase.isUserManaged() && dbClient.metricDao().disableCustomByKey(session, nonUpdatedBase.getKey())) {
            LOG.info("Disable metric {} [{}]", nonUpdatedBase.getShortName(), nonUpdatedBase.getKey());
        }
    }
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) HashMap(java.util.HashMap) Metric(org.sonar.api.measures.Metric)

Example 24 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class UpdateActionTest method fail_if_insufficient_privileges.

@Test
public void fail_if_insufficient_privileges() throws Exception {
    userSessionRule.logIn();
    MetricDto metric = MetricTesting.newMetricDto().setEnabled(true).setValueType(ValueType.STRING.name());
    dbClient.metricDao().insert(dbSession, metric);
    ComponentDto component = ComponentTesting.newProjectDto(db.getDefaultOrganization(), "project-uuid");
    dbClient.componentDao().insert(dbSession, component);
    CustomMeasureDto customMeasure = newCustomMeasureDto().setMetricId(metric.getId()).setComponentUuid(component.uuid()).setCreatedAt(system.now()).setDescription("custom-measure-description").setTextValue("text-measure-value");
    dbClient.customMeasureDao().insert(dbSession, customMeasure);
    dbSession.commit();
    expectedException.expect(ForbiddenException.class);
    ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION).setParam(PARAM_ID, String.valueOf(customMeasure.getId())).setParam(PARAM_DESCRIPTION, "new-custom-measure-description").setParam(PARAM_VALUE, "1984").execute();
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) ComponentDto(org.sonar.db.component.ComponentDto) CustomMeasureTesting.newCustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto) CustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureDto) Test(org.junit.Test)

Example 25 with MetricDto

use of org.sonar.db.metric.MetricDto in project sonarqube by SonarSource.

the class UpdateActionTest method returns_full_object_in_response.

@Test
public void returns_full_object_in_response() throws Exception {
    MetricDto metric = MetricTesting.newMetricDto().setEnabled(true).setValueType(ValueType.STRING.name()).setKey("metric-key");
    dbClient.metricDao().insert(dbSession, metric);
    OrganizationDto organizationDto = db.organizations().insert();
    ComponentDto component = ComponentTesting.newProjectDto(organizationDto, "project-uuid").setKey("project-key");
    dbClient.componentDao().insert(dbSession, component);
    CustomMeasureDto customMeasure = newCustomMeasure(component, metric).setCreatedAt(100_000_000L).setDescription("custom-measure-description").setTextValue("text-measure-value");
    dbClient.customMeasureDao().insert(dbSession, customMeasure);
    dbSession.commit();
    when(system.now()).thenReturn(123_456_789L);
    logInAsProjectAdministrator(component);
    WsTester.Result response = ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION).setParam(PARAM_ID, String.valueOf(customMeasure.getId())).setParam(PARAM_DESCRIPTION, "new-custom-measure-description").setParam(PARAM_VALUE, "new-text-measure-value").execute();
    response.assertJson(getClass(), "custom-measure.json");
    String responseAsString = response.outputAsString();
    assertThat(responseAsString).matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", customMeasure.getId()));
    assertThat(responseAsString).matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", metric.getId()));
    assertThat(responseAsString).matches(".*createdAt.*updatedAt.*");
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) WsTester(org.sonar.server.ws.WsTester) ComponentDto(org.sonar.db.component.ComponentDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) CustomMeasureTesting.newCustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto) CustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureDto) Test(org.junit.Test)

Aggregations

MetricDto (org.sonar.db.metric.MetricDto)137 Test (org.junit.Test)83 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)61 ComponentDto (org.sonar.db.component.ComponentDto)49 SnapshotDto (org.sonar.db.component.SnapshotDto)33 CustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureDto)26 DbSession (org.sonar.db.DbSession)11 ComponentTreeWsResponse (org.sonarqube.ws.WsMeasures.ComponentTreeWsResponse)11 CustomMeasureTesting.newCustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto)10 MeasureDto (org.sonar.db.measure.MeasureDto)9 OrganizationDto (org.sonar.db.organization.OrganizationDto)8 ProjectMeasures (org.sonar.db.measure.ProjectMeasuresIndexerIterator.ProjectMeasures)7 QualityGateConditionDto (org.sonar.db.qualitygate.QualityGateConditionDto)6 JsonWriter (org.sonar.api.utils.text.JsonWriter)5 WsTester (org.sonar.server.ws.WsTester)5 SearchWsResponse (org.sonarqube.ws.WsMeasures.SearchWsResponse)5 MeasureQuery (org.sonar.db.measure.MeasureQuery)3 UserDto (org.sonar.db.user.UserDto)3 WsMeasures (org.sonarqube.ws.WsMeasures)3 ComponentWsResponse (org.sonarqube.ws.WsMeasures.ComponentWsResponse)3