Skip to content

Commit

Permalink
SONAR-24046 Add telemetry 'aicaQualified' for a Quality Gate
Browse files Browse the repository at this point in the history
  • Loading branch information
dejan-milisavljevic-sonarsource authored and sonartech committed Dec 27, 2024
1 parent a648065 commit 670f4a2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class TelemetryDataLoaderImplIT {
@BeforeEach
void setUpBuiltInQualityGate() {
String builtInQgName = "Sonar way";
builtInDefaultQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName(builtInQgName).setBuiltIn(true));
builtInDefaultQualityGate =
db.qualityGates().insertQualityGate(qg -> qg.setName(builtInQgName).setBuiltIn(true).setAiCodeSupported(true));
when(qualityGateCaycChecker.checkCaycCompliant(any(), any(String.class))).thenReturn(NON_COMPLIANT);
db.qualityGates().setDefaultQualityGate(builtInDefaultQualityGate);

Expand Down Expand Up @@ -230,8 +231,8 @@ void send_telemetry_data() {
db.almSettings().insertGitlabProjectAlmSetting(gitHubAlmSetting, projectData2.getProjectDto(), true);

// quality gates
QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG1").setBuiltIn(true));
QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG2"));
QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG1").setBuiltIn(true).setAiCodeSupported(true));
QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG2").setAiCodeSupported(false));

QualityGateConditionDto condition1 = db.qualityGates().addCondition(qualityGate1, vulnerabilitiesDto, c -> c.setOperator("GT").setErrorThreshold("80"));
QualityGateConditionDto condition2 = db.qualityGates().addCondition(qualityGate2, securityHotspotsDto, c -> c.setOperator("LT").setErrorThreshold("2"));
Expand Down Expand Up @@ -323,14 +324,16 @@ void send_telemetry_data() {
tuple("branch", NewCodePeriodType.REFERENCE_BRANCH.name(), branch1.uuid()));

assertThat(data.getQualityGates())
.extracting(TelemetryData.QualityGate::uuid, TelemetryData.QualityGate::caycStatus,
.extracting(TelemetryData.QualityGate::uuid, TelemetryData.QualityGate::caycStatus, TelemetryData.QualityGate::aicaQualified,
qg -> qg.conditions().stream()
.map(condition -> tuple(condition.getMetricKey(), condition.getOperator().getDbValue(), condition.getErrorThreshold(), condition.isOnLeakPeriod()))
.toList())
.containsExactlyInAnyOrder(
tuple(builtInDefaultQualityGate.getUuid(), "non-compliant", Collections.emptyList()),
tuple(qualityGate1.getUuid(), "non-compliant", List.of(tuple(vulnerabilitiesDto.getKey(), condition1.getOperator(), condition1.getErrorThreshold(), false))),
tuple(qualityGate2.getUuid(), "non-compliant", List.of(tuple(securityHotspotsDto.getKey(), condition2.getOperator(), condition2.getErrorThreshold(), false))));
tuple(builtInDefaultQualityGate.getUuid(), "non-compliant", true, Collections.emptyList()),
tuple(qualityGate1.getUuid(), "non-compliant", true,
List.of(tuple(vulnerabilitiesDto.getKey(), condition1.getOperator(), condition1.getErrorThreshold(), false))),
tuple(qualityGate2.getUuid(), "non-compliant", false,
List.of(tuple(securityHotspotsDto.getKey(), condition2.getOperator(), condition2.getErrorThreshold(), false))));

assertThat(data.getQualityProfiles())
.extracting(TelemetryData.QualityProfile::uuid, TelemetryData.QualityProfile::isBuiltIn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ record Branch(String projectUuid, String branchUuid, int ncdId, int greenQuality
record Project(String projectUuid, Long lastAnalysis, String language, String qualityProfile, Long loc) {
}

record QualityGate(String uuid, String caycStatus, List<Condition> conditions) {
record QualityGate(String uuid, String caycStatus, boolean aicaQualified, List<Condition> conditions) {
}

public record QualityProfile(String uuid, @Nullable String parentUuid, String language, boolean isDefault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private static void writeQualityGates(JsonWriter json, TelemetryData telemetryDa
json.beginObject();
json.prop("uuid", qualityGate.uuid());
json.prop("caycStatus", qualityGate.caycStatus());
json.prop("aicaQualified", qualityGate.aicaQualified());
json.name("conditions");
json.beginArray();
qualityGate.conditions().forEach(condition -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.telemetry.legacy;

import jakarta.inject.Inject;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -34,7 +35,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import jakarta.inject.Inject;
import org.sonar.api.config.Configuration;
import org.sonar.api.platform.Server;
import org.sonar.api.server.ServerSide;
Expand Down Expand Up @@ -407,7 +407,7 @@ private void resolveQualityGates(TelemetryData.Builder data, DbSession dbSession
List<Condition> conditions = conditionsMap.getOrDefault(qualityGateUuid, Collections.emptyList());
qualityGates.add(
new TelemetryData.QualityGate(qualityGateDto.getUuid(), qualityGateCaycChecker.checkCaycCompliant(dbSession,
qualityGateDto.getUuid()).toString(), conditions));
qualityGateDto.getUuid()).toString(), qualityGateDto.isAiCodeSupported(), conditions));
}

data.setQualityGates(qualityGates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ void writes_all_quality_gates() {
{
"uuid": "uuid-0",
"caycStatus": "non-compliant",
"aicaQualified": true,
"conditions": [
{
"metric": "new_coverage",
Expand All @@ -560,6 +561,7 @@ void writes_all_quality_gates() {
{
"uuid": "uuid-1",
"caycStatus": "compliant",
"aicaQualified": true,
"conditions": [
{
"metric": "new_coverage",
Expand All @@ -576,6 +578,7 @@ void writes_all_quality_gates() {
{
"uuid": "uuid-2",
"caycStatus": "over-compliant",
"aicaQualified": false,
"conditions": [
{
"metric": "new_coverage",
Expand Down Expand Up @@ -758,9 +761,9 @@ private static TelemetryData.ProjectStatistics.Builder getProjectStatisticsWithM

private List<TelemetryData.QualityGate> attachQualityGates() {
List<Condition> qualityGateConditions = attachQualityGateConditions();
return List.of(new TelemetryData.QualityGate("uuid-0", "non-compliant", qualityGateConditions),
new TelemetryData.QualityGate("uuid-1", "compliant", qualityGateConditions),
new TelemetryData.QualityGate("uuid-2", "over-compliant", qualityGateConditions));
return List.of(new TelemetryData.QualityGate("uuid-0", "non-compliant", true, qualityGateConditions),
new TelemetryData.QualityGate("uuid-1", "compliant", true, qualityGateConditions),
new TelemetryData.QualityGate("uuid-2", "over-compliant", false, qualityGateConditions));
}

private List<Condition> attachQualityGateConditions() {
Expand Down

0 comments on commit 670f4a2

Please sign in to comment.