use of org.sonar.server.qualitygate.changeevent.QGChangeEvent 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.changeevent.QGChangeEvent in project sonarqube by SonarSource.
the class WebhookQGChangeEventListenerTest method onIssueChanges_calls_webhook_for_changeEvent_with_webhook_enabled.
@Test
@UseDataProvider("newQGorNot")
public void onIssueChanges_calls_webhook_for_changeEvent_with_webhook_enabled(@Nullable EvaluatedQualityGate newQualityGate) {
ProjectAndBranch projectBranch = insertBranch(BRANCH, "foo");
SnapshotDto analysis = insertAnalysisTask(projectBranch);
Configuration configuration = mock(Configuration.class);
mockPayloadSupplierConsumedByWebhooks();
Map<String, String> properties = new HashMap<>();
properties.put("sonar.analysis.test1", randomAlphanumeric(50));
properties.put("sonar.analysis.test2", randomAlphanumeric(5000));
insertPropertiesFor(analysis.getUuid(), properties);
QGChangeEvent qualityGateEvent = newQGChangeEvent(projectBranch, analysis, configuration, newQualityGate);
mockWebhookEnabled(qualityGateEvent.getProject());
underTest.onIssueChanges(qualityGateEvent, CHANGED_ISSUES_ARE_IGNORED);
ProjectAnalysis projectAnalysis = verifyWebhookCalledAndExtractPayloadFactoryArgument(projectBranch, analysis, qualityGateEvent.getProject());
assertThat(projectAnalysis).isEqualTo(new ProjectAnalysis(new Project(projectBranch.project.getUuid(), projectBranch.project.getKey(), projectBranch.project.getName()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt(), analysis.getRevision()), new Branch(false, "foo", Branch.Type.BRANCH), newQualityGate, null, properties));
}
use of org.sonar.server.qualitygate.changeevent.QGChangeEvent in project sonarqube by SonarSource.
the class WebhookQGChangeEventListenerTest method onIssueChanges_has_no_effect_if_no_webhook_is_configured.
@Test
@UseDataProvider("allCombinationsOfStatuses")
public void onIssueChanges_has_no_effect_if_no_webhook_is_configured(Metric.Level previousStatus, Metric.Level newStatus) {
Configuration configuration1 = mock(Configuration.class);
when(newQualityGate.getStatus()).thenReturn(newStatus);
QGChangeEvent qualityGateEvent = newQGChangeEvent(configuration1, previousStatus, newQualityGate);
mockWebhookDisabled(qualityGateEvent.getProject());
mockedUnderTest.onIssueChanges(qualityGateEvent, CHANGED_ISSUES_ARE_IGNORED);
verify(webHooks).isEnabled(qualityGateEvent.getProject());
verifyZeroInteractions(webhookPayloadFactory, mockedDbClient);
}
use of org.sonar.server.qualitygate.changeevent.QGChangeEvent in project sonarqube by SonarSource.
the class WebhookQGChangeEventListenerTest method onIssueChanges_calls_webhook_on_main_branch.
@Test
@UseDataProvider("newQGorNot")
public void onIssueChanges_calls_webhook_on_main_branch(@Nullable EvaluatedQualityGate newQualityGate) {
ProjectAndBranch mainBranch = insertMainBranch();
SnapshotDto analysis = insertAnalysisTask(mainBranch);
Configuration configuration = mock(Configuration.class);
QGChangeEvent qualityGateEvent = newQGChangeEvent(mainBranch, analysis, configuration, newQualityGate);
mockWebhookEnabled(qualityGateEvent.getProject());
underTest.onIssueChanges(qualityGateEvent, CHANGED_ISSUES_ARE_IGNORED);
verifyWebhookCalled(mainBranch, analysis, qualityGateEvent.getProject());
}
use of org.sonar.server.qualitygate.changeevent.QGChangeEvent in project sonarqube by SonarSource.
the class LiveMeasureComputerImplTest method variation_is_refreshed_when_int_value_is_changed.
@Test
public void variation_is_refreshed_when_int_value_is_changed() {
markProjectAsAnalyzed(project);
// value is:
// 42 on last analysis
// 42-12=30 on beginning of leak period
db.measures().insertLiveMeasure(project, intMetric, m -> m.setValue(42.0).setVariation(12.0));
// new value is 44, so variation on leak period is 44-30=14
List<QGChangeEvent> result = run(file1, newIntConstantFormula(44.0));
LiveMeasureDto measure = assertThatIntMeasureHasValue(project, 44.0);
assertThat(measure.getVariation()).isEqualTo(14.0);
assertThatProjectChanged(result, project);
}
Aggregations