use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.
the class LiveQualityGateComputerImplTest method refreshGateStatus_generates_gate_related_measures.
@Test
public void refreshGateStatus_generates_gate_related_measures() {
ComponentDto project = ComponentTesting.newPublicProjectDto();
MetricDto conditionMetric = newMetricDto();
MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10");
QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList());
EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix, configuration);
QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
assertThat(measures.get(conditionMetric.getKey())).isEmpty();
assertThat(result.getStatus()).isEqualTo(Metric.Level.OK);
assertThat(result.getEvaluatedConditions()).extracting(EvaluatedCondition::getStatus).containsExactly(EvaluatedCondition.EvaluationStatus.OK);
assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name());
assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString()).isNotEmpty().startsWith("{").endsWith("}");
}
use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.
the class LiveQualityGateComputerImplTest method refreshGateStatus_provides_measures_to_evaluator.
@Test
public void refreshGateStatus_provides_measures_to_evaluator() {
ComponentDto project = ComponentTesting.newPublicProjectDto();
MetricDto numericMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name());
MetricDto numericNewMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()).setKey("new_metric");
MetricDto stringMetric = newMetricDto().setValueType(Metric.ValueType.STRING.name());
MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
QualityGate gate = new QualityGate("1", "foo", Collections.emptySet());
LiveMeasureDto numericMeasure = new LiveMeasureDto().setMetricUuid(numericMetric.getUuid()).setValue(1.23).setVariation(4.56).setComponentUuid(project.uuid());
LiveMeasureDto numericNewMeasure = new LiveMeasureDto().setMetricUuid(numericNewMetric.getUuid()).setValue(7.8).setVariation(8.9).setComponentUuid(project.uuid());
LiveMeasureDto stringMeasure = new LiveMeasureDto().setMetricUuid(stringMetric.getUuid()).setData("bar").setComponentUuid(project.uuid());
MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric), asList(numericMeasure, numericNewMeasure, stringMeasure));
underTest.refreshGateStatus(project, gate, matrix, configuration);
QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
QualityGateEvaluator.Measure loadedStringMeasure = measures.get(stringMetric.getKey()).get();
assertThat(loadedStringMeasure.getStringValue()).hasValue("bar");
assertThat(loadedStringMeasure.getValue()).isEmpty();
assertThat(loadedStringMeasure.getType()).isEqualTo(Metric.ValueType.STRING);
QualityGateEvaluator.Measure loadedNumericMeasure = measures.get(numericMetric.getKey()).get();
assertThat(loadedNumericMeasure.getStringValue()).isEmpty();
assertThat(loadedNumericMeasure.getValue()).hasValue(1.23);
assertThat(loadedNumericMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
QualityGateEvaluator.Measure loadedNumericNewMeasure = measures.get(numericNewMetric.getKey()).get();
assertThat(loadedNumericNewMeasure.getStringValue()).isEmpty();
assertThat(loadedNumericNewMeasure.getNewMetricValue()).hasValue(8.9);
assertThat(loadedNumericNewMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
}
use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.
the class ProjectAnalysisTest method defines_hashcode_based_on_all_fields.
@Test
public void defines_hashcode_based_on_all_fields() {
assertThat(underTest).hasSameHashCodeAs(underTest).hasSameHashCodeAs(new ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, 1L, properties));
assertThat(underTest.hashCode()).isNotEqualTo(new Object().hashCode()).isNotEqualTo(new ProjectAnalysis(project, new CeTask("2", CeTask.Status.SUCCESS), analysis, branch, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(new Project("A", "B", "C"), ceTask, analysis, branch, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(new Project("A", "B", "C"), ceTask, analysis, branch, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, null, null, null, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, null, null, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, new Analysis("foo", 1_500L, "sha1"), null, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, null, qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, new Branch(false, "B", Branch.Type.BRANCH), qualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, null, 1L, properties).hashCode());
EvaluatedQualityGate otherQualityGate = EvaluatedQualityGate.newBuilder().setQualityGate(new QualityGate("A", "B", emptySet())).setStatus(Metric.Level.ERROR).build();
assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, otherQualityGate, 1L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, this.qualityGate, null, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, this.qualityGate, 2L, properties).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, this.qualityGate, 1L, emptyMap()).hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, this.qualityGate, 1L, ImmutableMap.of("B", "C")).hashCode());
}
use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.
the class LiveMeasureComputerImpl method refreshComponentsOnSameProject.
private Optional<QGChangeEvent> refreshComponentsOnSameProject(DbSession dbSession, List<ComponentDto> touchedComponents) {
// load all the components to be refreshed, including their ancestors
List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
ComponentDto branchComponent = findBranchComponent(components);
BranchDto branch = loadBranch(dbSession, branchComponent);
ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
if (!lastAnalysis.isPresent()) {
return Optional.empty();
}
QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
Map<String, MetricDto> metricsPerId = metrics.stream().collect(uniqueIndex(MetricDto::getUuid));
List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerId.keySet());
// previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);
Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, branchComponent);
DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
components.forEach(c -> {
IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeak));
for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
// use formulas when the leak period is defined, it's a PR, or the formula is not about the leak period
if (shouldUseLeakFormulas(lastAnalysis.get(), branch) || !formula.isOnLeak()) {
context.change(c, formula);
try {
formula.compute(context, issueCounter);
} catch (RuntimeException e) {
throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
}
}
}
});
EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
// persist the measures that have been created or updated
matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE).forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m));
projectIndexer.commitAndIndexComponents(dbSession, singleton(branchComponent), ProjectIndexer.Cause.MEASURE_CHANGE);
return Optional.of(new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
}
use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.
the class LiveQualityGateComputerImpl method loadQualityGate.
@Override
public QualityGate loadQualityGate(DbSession dbSession, ProjectDto project, BranchDto branch) {
QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, project).getQualityGate();
Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getUuid());
Set<String> metricUuids = conditionDtos.stream().map(QualityGateConditionDto::getMetricUuid).collect(toHashSet(conditionDtos.size()));
Map<String, MetricDto> metricsByUuid = dbClient.metricDao().selectByUuids(dbSession, metricUuids).stream().collect(uniqueIndex(MetricDto::getUuid));
Stream<Condition> conditions = conditionDtos.stream().map(conditionDto -> {
String metricKey = metricsByUuid.get(conditionDto.getMetricUuid()).getKey();
Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator());
return new Condition(metricKey, operator, conditionDto.getErrorThreshold());
});
if (branch.getBranchType() == BranchType.PULL_REQUEST) {
conditions = conditions.filter(Condition::isOnLeakPeriod);
}
return new QualityGate(String.valueOf(gateDto.getUuid()), gateDto.getName(), conditions.collect(toHashSet(conditionDtos.size())));
}
Aggregations