diff options
author | Alain Kermis <alain.kermis@sonarsource.com> | 2023-01-12 14:19:38 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-13 20:02:46 +0000 |
commit | 7166f4eb28690770382e442a4903801b96facd05 (patch) | |
tree | 1467de9f9ed09f4b62c498f201657d2b14427aee /server/sonar-server-common | |
parent | 27d238057534025c145e233de36f419f3dce66ed (diff) | |
download | sonarqube-7166f4eb28690770382e442a4903801b96facd05.tar.gz sonarqube-7166f4eb28690770382e442a4903801b96facd05.zip |
SONAR-18188 Share CAYC quality gate info via telemetry
Diffstat (limited to 'server/sonar-server-common')
3 files changed, 202 insertions, 101 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java index 61f4dc47e43..6daa1256f8a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java @@ -39,6 +39,7 @@ public class TelemetryData { private final Map<String, String> plugins; private final Database database; private final EditionProvider.Edition edition; + private final String defaultQualityGate; private final Long installationDate; private final String installationVersion; private final boolean inDocker; @@ -46,6 +47,7 @@ public class TelemetryData { private final List<UserTelemetryDto> users; private final List<Project> projects; private final List<ProjectStatistics> projectStatistics; + private final List<QualityGate> qualityGates; private final Boolean hasUnanalyzedC; private final Boolean hasUnanalyzedCpp; private final Set<String> customSecurityConfigs; @@ -57,6 +59,7 @@ public class TelemetryData { plugins = builder.plugins; database = builder.database; edition = builder.edition; + defaultQualityGate = builder.defaultQualityGate; installationDate = builder.installationDate; installationVersion = builder.installationVersion; inDocker = builder.inDocker; @@ -64,6 +67,7 @@ public class TelemetryData { users = builder.users; projects = builder.projects; projectStatistics = builder.projectStatistics; + qualityGates = builder.qualityGates; hasUnanalyzedC = builder.hasUnanalyzedC; hasUnanalyzedCpp = builder.hasUnanalyzedCpp; customSecurityConfigs = requireNonNullElse(builder.customSecurityConfigs, Set.of()); @@ -93,6 +97,10 @@ public class TelemetryData { return Optional.ofNullable(edition); } + public String getDefaultQualityGate() { + return defaultQualityGate; + } + public Long getInstallationDate() { return installationDate; } @@ -133,6 +141,10 @@ public class TelemetryData { return projectStatistics; } + public List<QualityGate> getQualityGates() { + return qualityGates; + } + static Builder builder() { return new Builder(); } @@ -144,6 +156,7 @@ public class TelemetryData { private Map<String, String> plugins; private Database database; private Edition edition; + private String defaultQualityGate; private Long installationDate; private String installationVersion; private boolean inDocker = false; @@ -154,6 +167,7 @@ public class TelemetryData { private List<UserTelemetryDto> users; private List<Project> projects; private List<ProjectStatistics> projectStatistics; + private List<QualityGate> qualityGates; private Builder() { // enforce static factory method @@ -189,6 +203,11 @@ public class TelemetryData { return this; } + Builder setDefaultQualityGate(String defaultQualityGate) { + this.defaultQualityGate = defaultQualityGate; + return this; + } + Builder setInstallationDate(@Nullable Long installationDate) { this.installationDate = installationDate; return this; @@ -244,6 +263,11 @@ public class TelemetryData { return this; } + Builder setQualityGates(List<QualityGate> qualityGates) { + this.qualityGates = qualityGates; + return this; + } + private static void requireNonNullValues(Object... values) { Arrays.stream(values).forEach(Objects::requireNonNull); } @@ -256,15 +280,9 @@ public class TelemetryData { record Project(String projectUuid, Long lastAnalysis, String language, Long loc) { } - record ProjectStatistics(String projectUuid, Long branchCount, Long pullRequestCount, String scm, String ci, String devopsPlatform) { - ProjectStatistics(String projectUuid, Long branchCount, Long pullRequestCount, - @Nullable String scm, @Nullable String ci, @Nullable String devopsPlatform) { - this.projectUuid = projectUuid; - this.branchCount = branchCount; - this.pullRequestCount = pullRequestCount; - this.scm = scm; - this.ci = ci; - this.devopsPlatform = devopsPlatform; - } + record ProjectStatistics(String projectUuid, Long branchCount, Long pullRequestCount, String qualityGate, String scm, String ci, String devopsPlatform) { + } + + record QualityGate(String uuid, boolean isCaycCompliant) { } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java index f5356ac46fa..116612e8b02 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java @@ -56,6 +56,7 @@ public class TelemetryDataJsonWriter { json.prop("messageSequenceNumber", statistics.getMessageSequenceNumber()); json.prop("localTimestamp", toUtc(system2.now())); statistics.getEdition().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH))); + json.prop("defaultQualityGate", statistics.getDefaultQualityGate()); json.name("database"); json.beginObject(); json.prop("name", statistics.getDatabase().name()); @@ -94,6 +95,7 @@ public class TelemetryDataJsonWriter { writeUserData(json, statistics); writeProjectData(json, statistics); writeProjectStatsData(json, statistics); + writeQualityGates(json, statistics); extensions.forEach(e -> e.write(json)); @@ -150,6 +152,7 @@ public class TelemetryDataJsonWriter { json.prop("projectUuid", project.projectUuid()); json.prop("branchCount", project.branchCount()); json.prop("pullRequestCount", project.pullRequestCount()); + json.prop("qualityGate", project.qualityGate()); json.prop("scm", project.scm()); json.prop("ci", project.ci()); json.prop("devopsPlatform", project.devopsPlatform()); @@ -159,6 +162,20 @@ public class TelemetryDataJsonWriter { } } + private static void writeQualityGates(JsonWriter json, TelemetryData statistics) { + if (statistics.getQualityGates() != null) { + json.name("quality-gates"); + json.beginArray(); + statistics.getQualityGates().forEach(qualityGate -> { + json.beginObject(); + json.prop("uuid", qualityGate.uuid()); + json.prop("isCaycCompliant", qualityGate.isCaycCompliant()); + json.endObject(); + }); + json.endArray(); + } + } + @NotNull private static String toUtc(long date) { return DateTimeFormatter.ofPattern(DATETIME_FORMAT) diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java index 1748571360b..d3dc46faa15 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java @@ -68,12 +68,13 @@ public class TelemetryDataJsonWriterTest { TelemetryData data = telemetryBuilder().build(); String json = writeTelemetryData(data); - - assertJson(json).isSimilarTo("{" + - " \"id\": \"" + data.getServerId() + "\"," + - " \"version\": \"" + data.getVersion() + "\"," + - " \"messageSequenceNumber\": " + data.getMessageSequenceNumber() + - "}"); + assertJson(json).isSimilarTo(""" + { + "id": "%s", + "version": "%s", + "messageSequenceNumber": %s + } + """.formatted(data.getServerId(), data.getVersion(), data.getMessageSequenceNumber())); } @Test @@ -93,10 +94,25 @@ public class TelemetryDataJsonWriterTest { .build(); String json = writeTelemetryData(data); + assertJson(json).isSimilarTo(""" + { + "edition": "%s" + } + """.formatted(edition.name().toLowerCase(Locale.ENGLISH))); + } + + @Test + public void writes_default_qg() { + TelemetryData data = telemetryBuilder() + .setDefaultQualityGate("default-qg") + .build(); - assertJson(json).isSimilarTo("{" + - " \"edition\": \"" + edition.name().toLowerCase(Locale.ENGLISH) + "\"" + - "}"); + String json = writeTelemetryData(data); + assertJson(json).isSimilarTo(""" + { + "defaultQualityGate": "%s" + } + """.formatted(data.getDefaultQualityGate())); } @Test @@ -108,13 +124,14 @@ public class TelemetryDataJsonWriterTest { .build(); String json = writeTelemetryData(data); - - assertJson(json).isSimilarTo("{" + - " \"database\": {" + - " \"name\": \"" + name + "\"," + - " \"version\": \"" + version + "\"" + - " }" + - "}"); + assertJson(json).isSimilarTo(""" + { + "database": { + "name": "%s", + "version": "%s" + } + } + """.formatted(name, version)); } @Test @@ -125,9 +142,11 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"plugins\": []" + - "}"); + assertJson(json).isSimilarTo(""" + { + "plugins": [] + } + """); } @Test @@ -140,13 +159,11 @@ public class TelemetryDataJsonWriterTest { .build(); String json = writeTelemetryData(data); - - assertJson(json).isSimilarTo("{" + - " \"plugins\": " + - "[" + - plugins.entrySet().stream().map(e -> "{\"name\":\"" + e.getKey() + "\",\"version\":\"" + e.getValue() + "\"}").collect(joining(",")) + - "]" + - "}"); + assertJson(json).isSimilarTo(""" + { + "plugins": [%s] + } + """.formatted(plugins.entrySet().stream().map(e -> "{\"name\":\"" + e.getKey() + "\",\"version\":\"" + e.getValue() + "\"}").collect(joining(",")))); } @Test @@ -168,9 +185,11 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"installationDate\":\"1970-01-01T00:00:01+0000\"," + - "}"); + assertJson(json).isSimilarTo(""" + { + "installationDate":"1970-01-01T00:00:01+0000" + } + """); } @Test @@ -192,10 +211,11 @@ public class TelemetryDataJsonWriterTest { .build(); String json = writeTelemetryData(data); - - assertJson(json).isSimilarTo("{" + - " \"installationVersion\":\"" + installationVersion + "\"" + - "}"); + assertJson(json).isSimilarTo(""" + { + "installationVersion": "%s" + } + """.formatted(installationVersion)); } @Test @@ -206,10 +226,11 @@ public class TelemetryDataJsonWriterTest { .build(); String json = writeTelemetryData(data); - - assertJson(json).isSimilarTo("{" + - " \"docker\":" + isInDocker + - "}"); + assertJson(json).isSimilarTo(""" + { + "docker": %s + } + """.formatted(isInDocker)); } @Test @@ -249,9 +270,11 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"customSecurityConfig\": [\"php\", \"java\"]" + - "}"); + assertJson(json).isSimilarTo(""" + { + "customSecurityConfig": ["php", "java"] + } + """); } @Test @@ -261,9 +284,11 @@ public class TelemetryDataJsonWriterTest { TelemetryData data = telemetryBuilder().build(); String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"localTimestamp\": \"1970-01-01T00:00:01+0000\"" + - "}"); + assertJson(json).isSimilarTo(""" + { + "localTimestamp": "1970-01-01T00:00:01+0000" + } + """); } @Test @@ -274,31 +299,34 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"users\": [" + - " {" + - " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-0") + "\"," + - " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"identityProvider\":\"gitlab\"," + - " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"status\":\"active\"" + - " }," + - " {" + - " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-1") + "\"," + - " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"identityProvider\":\"gitlab\"," + - " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"status\":\"inactive\"" + - " }," + - " {" + - " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-2") + "\"," + - " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"identityProvider\":\"gitlab\"," + - " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," + - " \"status\":\"active\"" + - " }" + - " ]" + - "}"); + assertJson(json).isSimilarTo(""" + { + "users": [ + { + "userUuid": "%s", + "status": "active", + "identityProvider": "gitlab", + "lastActivity": "1970-01-01T00:00:00+0000", + "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + }, + { + "userUuid": "%s", + "status": "inactive", + "identityProvider": "gitlab", + "lastActivity": "1970-01-01T00:00:00+0000", + "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + }, + { + "userUuid": "%s", + "status": "active", + "identityProvider": "gitlab", + "lastActivity": "1970-01-01T00:00:00+0000", + "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + } + ] + } + """ + .formatted(DigestUtils.sha3_224Hex("uuid-0"), DigestUtils.sha3_224Hex("uuid-1"), DigestUtils.sha3_224Hex("uuid-2"))); } @Test @@ -309,28 +337,30 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); - assertJson(json).isSimilarTo("{" + - " \"projects\": [" + - " {" + - " \"projectUuid\": \"uuid-0\"," + - " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," + - " \"language\": \"lang-0\"," + - " \"loc\": 2" + - " }," + - " {" + - " \"projectUuid\": \"uuid-1\"," + - " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," + - " \"language\": \"lang-1\"," + - " \"loc\": 4" + - " }," + - " {" + - " \"projectUuid\": \"uuid-2\"," + - " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," + - " \"language\": \"lang-2\"," + - " \"loc\": 6" + - " }" + - " ]" + - "}"); + assertJson(json).isSimilarTo(""" + { + "projects": [ + { + "projectUuid": "uuid-0", + "lastAnalysis": "1970-01-01T00:00:00+0000", + "language": "lang-0", + "loc": 2 + }, + { + "projectUuid": "uuid-1", + "lastAnalysis": "1970-01-01T00:00:00+0000", + "language": "lang-1", + "loc": 4 + }, + { + "projectUuid": "uuid-2", + "lastAnalysis": "1970-01-01T00:00:00+0000", + "language": "lang-2", + "loc": 6 + } + ] + } + """); } @Test @@ -348,6 +378,7 @@ public class TelemetryDataJsonWriterTest { "projectUuid": "uuid-0", "branchCount": 2, "pullRequestCount": 2, + "qualityGate": "qg-0" "scm": "scm-0", "ci": "ci-0", "devopsPlatform": "devops-0" @@ -356,6 +387,7 @@ public class TelemetryDataJsonWriterTest { "projectUuid": "uuid-1", "branchCount": 4, "pullRequestCount": 4, + "qualityGate": "qg-1" "scm": "scm-1", "ci": "ci-1", "devopsPlatform": "devops-1" @@ -364,6 +396,7 @@ public class TelemetryDataJsonWriterTest { "projectUuid": "uuid-2", "branchCount": 6, "pullRequestCount": 6, + "qualityGate": "qg-2" "scm": "scm-2", "ci": "ci-2", "devopsPlatform": "devops-2" @@ -384,6 +417,34 @@ public class TelemetryDataJsonWriterTest { assertThat(json).doesNotContain("hasUnanalyzedC", "hasUnanalyzedCpp"); } + @Test + public void writes_all_quality_gates() { + TelemetryData data = telemetryBuilder() + .setQualityGates(attachQualityGates()) + .build(); + + String json = writeTelemetryData(data); + assertJson(json).isSimilarTo(""" + { + "quality-gates": [ + { + "uuid": "uuid-0", + "isCaycCompliant": true + }, + { + "uuid": "uuid-1", + "isCaycCompliant": false + }, + { + "uuid": "uuid-2", + "isCaycCompliant": true + } + ] + } + """ + ); + } + private static TelemetryData.Builder telemetryBuilder() { return TelemetryData.builder() .setServerId("foo") @@ -406,7 +467,12 @@ public class TelemetryDataJsonWriterTest { } private List<TelemetryData.ProjectStatistics> attachProjectStats() { - return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.ProjectStatistics("uuid-" + i, (i + 1L) * 2L, (i + 1L) * 2L, "scm-" + i, "ci-" + i, "devops-" + i)) + return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.ProjectStatistics("uuid-" + i, (i + 1L) * 2L, (i + 1L) * 2L, "qg-" + i, "scm-" + i, "ci-" + i, "devops-" + i)) + .collect(Collectors.toList()); + } + + private List<TelemetryData.QualityGate> attachQualityGates() { + return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.QualityGate("uuid-" + i, i % 2 == 0)) .collect(Collectors.toList()); } |