aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorDejan Milisavljevic <dejan.milisavljevic@sonarsource.com>2024-12-27 11:56:39 +0100
committersonartech <sonartech@sonarsource.com>2024-12-27 20:03:07 +0000
commit025e1a6f122bd3cb714483effa485bd0dbfd48e5 (patch)
treece23ef755c42ce859dcb1699b8b979b4fb891a21 /server/sonar-server-common
parentaf48441fd5a4c6ac9033017fbd88d6fd3e932af1 (diff)
downloadsonarqube-025e1a6f122bd3cb714483effa485bd0dbfd48e5.tar.gz
sonarqube-025e1a6f122bd3cb714483effa485bd0dbfd48e5.zip
SONAR-24042 add telemetry 'project_uses_aica_quality_gate'
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/it/java/org/sonar/server/qualitygate/QualityGateFinderIT.java33
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java32
2 files changed, 62 insertions, 3 deletions
diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/qualitygate/QualityGateFinderIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/qualitygate/QualityGateFinderIT.java
index 5ff55d9a39e..b154c3d0754 100644
--- a/server/sonar-server-common/src/it/java/org/sonar/server/qualitygate/QualityGateFinderIT.java
+++ b/server/sonar-server-common/src/it/java/org/sonar/server/qualitygate/QualityGateFinderIT.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.qualitygate;
+import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
@@ -26,9 +27,11 @@ import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.tuple;
public class QualityGateFinderIT {
@@ -44,7 +47,7 @@ public class QualityGateFinderIT {
ProjectDto project = db.components().insertPrivateProject().getProjectDto();
QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
- QualityGateFinder.QualityGateData result = underTest.getEffectiveQualityGate(dbSession, project);
+ QualityGateData result = underTest.getEffectiveQualityGate(dbSession, project);
assertThat(result.getUuid()).isEqualTo(dbQualityGate.getUuid());
assertThat(result.isDefault()).isTrue();
@@ -57,13 +60,39 @@ public class QualityGateFinderIT {
QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG"));
db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
- QualityGateFinder.QualityGateData result = underTest.getEffectiveQualityGate(dbSession, project);
+ QualityGateData result = underTest.getEffectiveQualityGate(dbSession, project);
assertThat(result.getUuid()).isEqualTo(dbQualityGate.getUuid());
assertThat(result.isDefault()).isFalse();
}
@Test
+ public void return_quality_gates_for_all_projects() {
+ QualityGateDto defaultQualityGate = db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
+
+ ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
+ QualityGateDto dbQualityGate1 = db.qualityGates().insertQualityGate(qg -> qg.setName("My team QG").setAiCodeSupported(true));
+ db.qualityGates().associateProjectToQualityGate(project1, dbQualityGate1);
+
+ ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
+ QualityGateDto dbQualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setName("Another QG").setAiCodeSupported(false));
+ db.qualityGates().associateProjectToQualityGate(project2, dbQualityGate2);
+
+ ProjectDto project3 = db.components().insertPrivateProject().getProjectDto();
+
+ Map<String, QualityGateData> result = underTest.getEffectiveQualityGateForAllProjects(dbSession);
+
+ assertThat(result).containsOnlyKeys(project1.getUuid(), project2.getUuid(), project3.getUuid())
+ .extractingByKeys(project1.getUuid(), project2.getUuid(), project3.getUuid())
+ .extracting(QualityGateData::getUuid, QualityGateData::getName, QualityGateData::isBuiltIn, QualityGateData::isDefault, QualityGateData::isAiCodeSupported)
+ .containsExactly(
+ tuple(dbQualityGate1.getUuid(), dbQualityGate1.getName(), dbQualityGate1.isBuiltIn(), false, dbQualityGate1.isAiCodeSupported()),
+ tuple(dbQualityGate2.getUuid(), dbQualityGate2.getName(), dbQualityGate2.isBuiltIn(), false, dbQualityGate2.isAiCodeSupported()),
+ tuple(defaultQualityGate.getUuid(), defaultQualityGate.getName(), defaultQualityGate.isBuiltIn(), true, defaultQualityGate.isAiCodeSupported())
+ );
+ }
+
+ @Test
public void fail_when_default_qgate_defined_does_not_exist() {
ProjectDto project = db.components().insertPrivateProject().getProjectDto();
QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(qg -> qg.setName("Sonar way"));
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
index 4deee6f148b..94bbb3d0e97 100644
--- a/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
@@ -19,10 +19,15 @@
*/
package org.sonar.server.qualitygate;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
+import java.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.entity.EntityDto;
import org.sonar.db.project.ProjectDto;
+import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.builtin.SonarWayQualityGate;
@@ -48,6 +53,25 @@ public class QualityGateFinder {
return new QualityGateData(defaultQualityGate, true);
}
+ public Map<String, QualityGateData> getEffectiveQualityGateForAllProjects(DbSession dbSession) {
+ List<ProjectDto> allProjects = dbClient.projectDao().selectAll(dbSession);
+ Map<String, String> qgUuidPerProjectUuid =
+ dbClient.projectQgateAssociationDao().selectAll(dbSession).stream()
+ .filter(qga -> qga.getGateUuid() != null)
+ .collect(Collectors.toMap(ProjectQgateAssociationDto::getUuid, ProjectQgateAssociationDto::getGateUuid));
+ Map<String, QualityGateDto> qualityGates =
+ dbClient.qualityGateDao().selectAll(dbSession).stream().collect(Collectors.toMap(QualityGateDto::getUuid, qg -> qg));
+
+ Map<String, QualityGateData> qualityGatesPerProjectUuid =
+ qgUuidPerProjectUuid.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
+ e -> new QualityGateData(qualityGates.get(e.getValue()), false)));
+
+ QualityGateData defaultQualityGateData = new QualityGateData(getDefault(dbSession), true);
+
+ return allProjects.stream().collect(Collectors.toMap(EntityDto::getUuid,
+ p -> Optional.ofNullable(qualityGatesPerProjectUuid.get(p.getUuid())).orElse(defaultQualityGateData)));
+ }
+
private Optional<QualityGateData> getQualityGateForProject(DbSession dbSession, String projectUuid) {
return dbClient.projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, projectUuid)
.map(qualityGateUuid -> dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid))
@@ -68,12 +92,14 @@ public class QualityGateFinder {
private final String name;
private final boolean isDefault;
private final boolean builtIn;
+ private final boolean aiCodeSupported;
- private QualityGateData(QualityGateDto qualityGate, boolean isDefault) {
+ QualityGateData(QualityGateDto qualityGate, boolean isDefault) {
this.uuid = qualityGate.getUuid();
this.name = qualityGate.getName();
this.isDefault = isDefault;
this.builtIn = qualityGate.isBuiltIn();
+ this.aiCodeSupported = qualityGate.isAiCodeSupported();
}
public boolean isBuiltIn() {
@@ -91,6 +117,10 @@ public class QualityGateFinder {
public boolean isDefault() {
return isDefault;
}
+
+ public boolean isAiCodeSupported() {
+ return aiCodeSupported;
+ }
}
}