use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class QualityGates method listConditions.
public Collection<QualityGateConditionDto> listConditions(long qGateId) {
try (DbSession dbSession = dbClient.openSession(false)) {
Collection<QualityGateConditionDto> conditionsForGate = conditionDao.selectForQualityGate(dbSession, qGateId);
for (QualityGateConditionDto condition : conditionsForGate) {
Metric metric = metricFinder.findById((int) condition.getMetricId());
if (metric == null) {
throw new IllegalStateException("Could not find metric with id " + condition.getMetricId());
}
condition.setMetricKey(metric.getKey());
}
return conditionsForGate;
}
}
use of org.sonar.api.measures.Metric 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());
}
}
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class RegisterMetricsTest method insert_new_metrics.
/**
* Insert new metrics, including custom metrics
*/
@Test
public void insert_new_metrics() {
dbTester.prepareDbUnit(getClass(), "insert_new_metrics.xml");
Metric m1 = new Metric.Builder("m1", "One", Metric.ValueType.FLOAT).setDescription("desc1").setDirection(1).setQualitative(true).setDomain("domain1").setUserManaged(false).create();
Metric custom = new Metric.Builder("custom", "Custom", Metric.ValueType.FLOAT).setDescription("This is a custom metric").setUserManaged(true).create();
RegisterMetrics register = new RegisterMetrics(dbClient);
register.register(asList(m1, custom));
dbTester.assertDbUnit(getClass(), "insert_new_metrics-result.xml", "metrics");
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class RegisterMetricsTest method enable_disabled_metrics.
@Test
public void enable_disabled_metrics() {
dbTester.prepareDbUnit(getClass(), "enable_disabled_metric.xml");
RegisterMetrics register = new RegisterMetrics(dbClient);
Metric m1 = new Metric.Builder("m1", "New name", Metric.ValueType.FLOAT).setDescription("new description").setDirection(-1).setQualitative(true).setDomain("new domain").setUserManaged(false).setHidden(true).create();
register.register(asList(m1));
dbTester.assertDbUnit(getClass(), "enable_disabled_metric-result.xml", "metrics");
}
use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.
the class DefaultI18nTest method all_core_metrics_are_in_core_bundle.
@Test
public void all_core_metrics_are_in_core_bundle() {
List<Metric> coreMetrics = CoreMetrics.getMetrics();
List<String> incorrectMetricDefinitions = new ArrayList<>();
for (Metric metric : coreMetrics) {
if (metric.isHidden()) {
continue;
}
String metricNamePropertyKey = "metric." + metric.getKey() + ".name";
String l10nMetricName = underTest.message(Locale.ENGLISH, metricNamePropertyKey, null);
if (l10nMetricName == null) {
incorrectMetricDefinitions.add(metricNamePropertyKey + "=" + metric.getName());
} else if (!l10nMetricName.equals(metric.getName())) {
incorrectMetricDefinitions.add(metricNamePropertyKey + " is not consistent in core bundle and CoreMetrics");
}
String metricDescriptionPropertyKey = "metric." + metric.getKey() + ".description";
String l10nMetricDescription = underTest.message(Locale.ENGLISH, metricDescriptionPropertyKey, null);
if (l10nMetricDescription == null) {
incorrectMetricDefinitions.add(metricDescriptionPropertyKey + "=" + metric.getDescription());
} else if (!l10nMetricDescription.equals(metric.getDescription())) {
incorrectMetricDefinitions.add(metricDescriptionPropertyKey + " is not consistent in core bundle and CoreMetrics");
}
}
assertThat(incorrectMetricDefinitions).as("Metric definitions to fix in core bundle", incorrectMetricDefinitions.size()).isEmpty();
}
Aggregations