diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2023-10-03 16:01:34 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-10-05 20:02:47 +0000 |
commit | 5f4872096f1b4cfaa8df11ecc28c895fba323ffb (patch) | |
tree | cbf93f5d5eae46968d32e6ac54017ef617e7306e | |
parent | 5410c2b5221ad4a93346fe48b36a0eaf66ee40f4 (diff) | |
download | sonarqube-5f4872096f1b4cfaa8df11ecc28c895fba323ffb.tar.gz sonarqube-5f4872096f1b4cfaa8df11ecc28c895fba323ffb.zip |
SONAR-20630 Add creationMethod to project telemetry payload.
10 files changed, 85 insertions, 70 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java index b7f294ccb30..1b64206178a 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java @@ -353,19 +353,6 @@ public class ProjectDaoIT { } @Test - public void selectAllProjectUuids_shouldOnlyReturnProjectWithTRKQualifier() { - ProjectData application = db.components().insertPrivateApplication(); - ProjectData project = db.components().insertPrivateProject(); - ProjectData project2 = db.components().insertPrivateProject(); - db.components().addApplicationProject(application, project, project2); - - List<String> projectUuids = projectDao.selectAllProjectUuids(db.getSession()); - - assertThat(projectUuids).containsExactlyInAnyOrder(project.projectUuid(), project2.projectUuid()); - } - - // methodName_when<conditionInCamelCase>_should<assertionInCamelCase> - @Test public void selectByUuids_whenUuidsAreEmptyWithPagination_shouldReturnEmptyList() { db.components().insertPublicProject(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index e10f753f9c2..885b044abf3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -132,10 +132,6 @@ public class ProjectDao implements Dao { return session.getMapper(ProjectMapper.class); } - public List<String> selectAllProjectUuids(DbSession session) { - return mapper(session).selectAllProjectUuids(); - } - public Set<String> selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(DbSession session, String language) { Set<String> languageFilters = Set.of(language + "=%", "%;" + language + "=%"); return mapper(session).selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(languageFilters); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java index cc4c34a050e..b00fe460ed2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java @@ -66,8 +66,6 @@ public interface ProjectMapper { @CheckForNull ProjectDto selectByBranchUuid(String branchUuid); - List<String> selectAllProjectUuids(); - Set<String> selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(@Param("languageFilters") Set<String> languageFilters); void updateNcloc(@Param("projectUuid") String projectUuid, @Param("ncloc") long ncloc); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml index cf0d09cf5e6..aba8dbdf0b2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml @@ -15,13 +15,6 @@ p.updated_at as updatedAt </sql> - <select id="selectAllProjectUuids" resultType="String"> - SELECT - p.uuid as uuid - FROM projects p - where p.qualifier = 'TRK' - </select> - <select id="selectByUuid" parameterType="String" resultType="Project"> SELECT <include refid="projectColumns"/> diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index a6df26605b6..b9aa5d50735 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -80,6 +80,11 @@ public class ComponentDbTester { defaults(), defaults(), defaults()); } + public ProjectData insertPrivateProjectWithCreationMethod(CreationMethod creationMethod) { + return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, + defaults(), defaults(), projectDto -> projectDto.setCreationMethod(creationMethod)); + } + public BranchDto getBranchDto(ComponentDto branch) { return db.getDbClient().branchDao().selectByUuid(dbSession, branch.uuid()) .orElseThrow(() -> new IllegalStateException("Project has invalid configuration")); 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 ee42c111c68..6ec247b38b7 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 @@ -29,6 +29,7 @@ import java.util.Set; import javax.annotation.Nullable; import org.sonar.core.platform.EditionProvider; import org.sonar.core.platform.EditionProvider.Edition; +import org.sonar.db.project.CreationMethod; import org.sonar.db.user.UserTelemetryDto; import static java.util.Objects.requireNonNullElse; @@ -393,6 +394,8 @@ public class TelemetryData { private final int ncdId; private final Long externalSecurityReportExportedAt; + private final CreationMethod creationMethod; + ProjectStatistics(Builder builder) { this.projectUuid = builder.projectUuid; this.branchCount = builder.branchCount; @@ -408,6 +411,7 @@ public class TelemetryData { this.developmentCost = builder.developmentCost; this.ncdId = builder.ncdId; this.externalSecurityReportExportedAt = builder.externalSecurityReportExportedAt; + this.creationMethod = builder.creationMethod; } public int getNcdId() { @@ -466,6 +470,10 @@ public class TelemetryData { return Optional.ofNullable(externalSecurityReportExportedAt); } + public CreationMethod getCreationMethod() { + return creationMethod; + } + static class Builder { private String projectUuid; private Long branchCount; @@ -481,6 +489,7 @@ public class TelemetryData { private Long developmentCost; private int ncdId; private Long externalSecurityReportExportedAt; + private CreationMethod creationMethod; public Builder setProjectUuid(String projectUuid) { this.projectUuid = projectUuid; @@ -552,6 +561,11 @@ public class TelemetryData { return this; } + public Builder setCreationMethod(CreationMethod creationMethod) { + this.creationMethod = creationMethod; + return this; + } + public ProjectStatistics build() { return new ProjectStatistics(this); } 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 3e61c0036c0..6d467a50146 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 @@ -201,6 +201,7 @@ public class TelemetryDataJsonWriter { json.prop("ci", project.getCi()); json.prop("devopsPlatform", project.getDevopsPlatform()); json.prop(NCD_ID, project.getNcdId()); + json.prop("creationMethod", project.getCreationMethod().name()); project.getBugs().ifPresent(bugs -> json.prop("bugs", bugs)); project.getVulnerabilities().ifPresent(vulnerabilities -> json.prop("vulnerabilities", vulnerabilities)); project.getSecurityHotspots().ifPresent(securityHotspots -> json.prop("securityHotspots", securityHotspots)); 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 ad47094834d..d23d0a2c5e7 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 @@ -40,6 +40,7 @@ import org.sonar.api.utils.System2; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.platform.EditionProvider; import org.sonar.core.telemetry.TelemetryExtension; +import org.sonar.db.project.CreationMethod; import org.sonar.db.user.UserTelemetryDto; import static java.util.stream.Collectors.joining; @@ -64,10 +65,8 @@ public class TelemetryDataJsonWriterTest { private static final int NCD_ID = 12345; - private static final TelemetryData.NewCodeDefinition NCD_INSTANCE = - new TelemetryData.NewCodeDefinition(PREVIOUS_VERSION.name(), "", "instance"); - private static final TelemetryData.NewCodeDefinition NCD_PROJECT = - new TelemetryData.NewCodeDefinition(NUMBER_OF_DAYS.name(), "30", "project"); + private static final TelemetryData.NewCodeDefinition NCD_INSTANCE = new TelemetryData.NewCodeDefinition(PREVIOUS_VERSION.name(), "", "instance"); + private static final TelemetryData.NewCodeDefinition NCD_PROJECT = new TelemetryData.NewCodeDefinition(NUMBER_OF_DAYS.name(), "30", "project"); @Test public void write_server_id_version_and_sequence() { @@ -149,7 +148,7 @@ public class TelemetryDataJsonWriterTest { String json = writeTelemetryData(data); assertJson(json).isSimilarTo(""" - { + { "plugins": [] } """); @@ -447,7 +446,8 @@ public class TelemetryDataJsonWriterTest { "technicalDebt": 60, "developmentCost": 30, "ncdId": 12345, - "externalSecurityReportExportedAt": 1500000 + "externalSecurityReportExportedAt": 1500000, + "creationMethod": "LOCAL_API" }, { "projectUuid": "uuid-1", @@ -463,7 +463,8 @@ public class TelemetryDataJsonWriterTest { "technicalDebt": 120, "developmentCost": 60, "ncdId": 12345, - "externalSecurityReportExportedAt": 1500001 + "externalSecurityReportExportedAt": 1500001, + "creationMethod": "LOCAL_API" }, { "projectUuid": "uuid-2", @@ -479,12 +480,12 @@ public class TelemetryDataJsonWriterTest { "technicalDebt": 180, "developmentCost": 90, "ncdId": 12345, - "externalSecurityReportExportedAt": 1500002 + "externalSecurityReportExportedAt": 1500002, + "creationMethod": "LOCAL_API" } ] } - """ - ); + """); } @Test @@ -530,8 +531,7 @@ public class TelemetryDataJsonWriterTest { } ] } - """ - ); + """); } @Test @@ -564,8 +564,7 @@ public class TelemetryDataJsonWriterTest { "builtIn": true } ]} - """ - ); + """); } @Test @@ -659,7 +658,7 @@ public class TelemetryDataJsonWriterTest { } private static List<TelemetryData.Project> attachProjects() { - return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.Project("uuid-" + i, 1L, "lang-" + i, "qprofile-" + i, (i + 1L) * 2L)).toList(); + return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.Project("uuid-" + i, 1L, "lang-" + i, "qprofile-" + i, (i + 1L) * 2)).toList(); } private static List<TelemetryData.ProjectStatistics> attachProjectStatsWithMetrics() { @@ -678,7 +677,8 @@ public class TelemetryDataJsonWriterTest { .setQG("qg-" + i).setCi("ci-" + i) .setScm("scm-" + i) .setDevops("devops-" + i) - .setNcdId(NCD_ID); + .setNcdId(NCD_ID) + .setCreationMethod(CreationMethod.LOCAL_API); } private static TelemetryData.ProjectStatistics.Builder getProjectStatisticsWithMetricBuilder(int i) { @@ -688,7 +688,8 @@ public class TelemetryDataJsonWriterTest { .setSecurityHotspots((i + 1L) * 4) .setDevelopmentCost((i + 1L) * 30d) .setTechnicalDebt((i + 1L) * 60d) - .setExternalSecurityReportExportedAt(1_500_000L + i); + .setExternalSecurityReportExportedAt(1_500_000L + i) + .setCreationMethod(CreationMethod.LOCAL_API); } private List<TelemetryData.QualityGate> attachQualityGates() { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/it/java/org/sonar/server/telemetry/TelemetryDataLoaderImplIT.java index d7494fa47cb..d467e152eaf 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/it/java/org/sonar/server/telemetry/TelemetryDataLoaderImplIT.java @@ -51,6 +51,7 @@ import org.sonar.db.component.ProjectData; import org.sonar.db.component.SnapshotDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.newcodeperiod.NewCodePeriodType; +import org.sonar.db.project.CreationMethod; import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.db.qualityprofile.QProfileDto; @@ -101,8 +102,9 @@ import static org.sonar.server.qualitygate.QualityGateCaycStatus.NON_COMPLIANT; import static org.sonar.server.telemetry.TelemetryDataLoaderImpl.EXTERNAL_SECURITY_REPORT_EXPORTED_AT; @RunWith(DataProviderRunner.class) -public class TelemetryDataLoaderImplTest { +public class TelemetryDataLoaderImplIT { private final static Long NOW = 100_000_000L; + public static final String SERVER_ID = "AU-TpxcB-iU5OvuD2FL7"; private final TestSystem2 system2 = new TestSystem2().setNow(NOW); @Rule @@ -149,12 +151,11 @@ public class TelemetryDataLoaderImplTest { @Test public void send_telemetry_data() { - String serverId = "AU-TpxcB-iU5OvuD2FL7"; String version = "7.5.4"; Long analysisDate = 1L; Long lastConnectionDate = 5L; - server.setId(serverId); + server.setId(SERVER_ID); server.setVersion(version); List<PluginInfo> plugins = asList(newPlugin("java", "4.12.0.11033"), newPlugin("scmgit", "1.2"), new PluginInfo("other")); when(pluginRepository.getPluginInfos()).thenReturn(plugins); @@ -185,7 +186,7 @@ public class TelemetryDataLoaderImplTest { db.measures().insertLiveMeasure(mainBranch1, securityHotspotsDto, m -> m.setValue(1d).setData((String) null)); db.measures().insertLiveMeasure(mainBranch1, developmentCostDto, m -> m.setData("50").setValue(null)); db.measures().insertLiveMeasure(mainBranch1, technicalDebtDto, m -> m.setValue(5d).setData((String) null)); - //Measures on other branches + // Measures on other branches db.measures().insertLiveMeasure(branch1, technicalDebtDto, m -> m.setValue(6d).setData((String) null)); db.measures().insertLiveMeasure(branch2, technicalDebtDto, m -> m.setValue(7d).setData((String) null)); @@ -218,7 +219,7 @@ public class TelemetryDataLoaderImplTest { QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG1").setBuiltIn(true)); QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(qg -> qg.setName("QG2")); - //quality profiles + // quality profiles QProfileDto javaQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("java")); QProfileDto kotlinQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("kotlin")); QProfileDto jsQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("js")); @@ -240,7 +241,7 @@ public class TelemetryDataLoaderImplTest { var branchNcdId = new NewCodeDefinition(NewCodePeriodType.REFERENCE_BRANCH.name(), branch1.uuid(), "branch").hashCode(); TelemetryData data = communityUnderTest.load(); - assertThat(data.getServerId()).isEqualTo(serverId); + assertThat(data.getServerId()).isEqualTo(SERVER_ID); assertThat(data.getVersion()).isEqualTo(version); assertThat(data.getEdition()).contains(DEVELOPER); assertThat(data.getDefaultQualityGate()).isEqualTo(builtInDefaultQualityGate.getUuid()); @@ -297,8 +298,7 @@ public class TelemetryDataLoaderImplTest { .containsExactlyInAnyOrder( tuple(builtInDefaultQualityGate.getUuid(), "non-compliant"), tuple(qualityGate1.getUuid(), "non-compliant"), - tuple(qualityGate2.getUuid(), "non-compliant") - ); + tuple(qualityGate2.getUuid(), "non-compliant")); assertThat(data.getQualityProfiles()) .extracting(TelemetryData.QualityProfile::uuid, TelemetryData.QualityProfile::isBuiltIn) @@ -307,8 +307,7 @@ public class TelemetryDataLoaderImplTest { tuple(qualityProfile2.getKee(), qualityProfile2.isBuiltIn()), tuple(jsQP.getKee(), jsQP.isBuiltIn()), tuple(javaQP.getKee(), javaQP.isBuiltIn()), - tuple(kotlinQP.getKee(), kotlinQP.isBuiltIn()) - ); + tuple(kotlinQP.getKee(), kotlinQP.isBuiltIn())); } @@ -376,7 +375,7 @@ public class TelemetryDataLoaderImplTest { @Test public void take_largest_branch_snapshot_project_data() { - server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + server.setId(SERVER_ID).setVersion("7.5.4"); MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY)); MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY)); @@ -423,7 +422,7 @@ public class TelemetryDataLoaderImplTest { @Test public void load_shouldProvideQualityProfileInProjectSection() { - server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + server.setId(SERVER_ID).setVersion("7.5.4"); MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY)); MetricDto nclocDistrib = db.measures().insertMetric(m -> m.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY)); @@ -432,12 +431,11 @@ public class TelemetryDataLoaderImplTest { // default quality profile QProfileDto javaQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("java")); QProfileDto kotlinQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("kotlin")); - db.qualityProfiles().setAsDefault(javaQP,kotlinQP); + db.qualityProfiles().setAsDefault(javaQP, kotlinQP); // selected quality profile QProfileDto jsQP = db.qualityProfiles().insert(qProfileDto -> qProfileDto.setLanguage("js")); db.qualityProfiles().associateWithProject(projectData.getProjectDto(), jsQP); - ComponentDto mainBranch = projectData.getMainBranchComponent(); db.measures().insertLiveMeasure(mainBranch, ncloc, m -> m.setValue(110d)); db.measures().insertLiveMeasure(mainBranch, nclocDistrib, m -> m.setValue(null).setData("java=70;js=30;kotlin=10")); @@ -461,8 +459,31 @@ public class TelemetryDataLoaderImplTest { } @Test + public void load_shouldProvideCreationMethodInProjectStatisticsSection() { + server.setId(SERVER_ID).setVersion("7.5.4"); + + ProjectData projectData1 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.LOCAL_API); + ProjectData projectData2 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.LOCAL_BROWSER); + ProjectData projectData3 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.UNKNOWN); + ProjectData projectData4 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.SCANNER_API); + ProjectData projectData5 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.ALM_IMPORT_BROWSER); + ProjectData projectData6 = db.components().insertPrivateProjectWithCreationMethod(CreationMethod.ALM_IMPORT_API); + + TelemetryData data = communityUnderTest.load(); + + assertThat(data.getProjectStatistics()).extracting(TelemetryData.ProjectStatistics::getProjectUuid, TelemetryData.ProjectStatistics::getCreationMethod) + .containsExactlyInAnyOrder( + tuple(projectData1.projectUuid(), CreationMethod.LOCAL_API), + tuple(projectData2.projectUuid(), CreationMethod.LOCAL_BROWSER), + tuple(projectData3.projectUuid(), CreationMethod.UNKNOWN), + tuple(projectData4.projectUuid(), CreationMethod.SCANNER_API), + tuple(projectData5.projectUuid(), CreationMethod.ALM_IMPORT_BROWSER), + tuple(projectData6.projectUuid(), CreationMethod.ALM_IMPORT_API)); + } + + @Test public void test_ncd_on_community_edition() { - server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + server.setId(SERVER_ID).setVersion("7.5.4"); when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY)); ProjectData project = db.components().insertPublicProject(); @@ -613,7 +634,7 @@ public class TelemetryDataLoaderImplTest { @Test public void undetected_alm_ci_slm_data() { - server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + server.setId(SERVER_ID).setVersion("7.5.4"); db.components().insertPublicProject().getMainBranchComponent(); TelemetryData data = communityUnderTest.load(); assertThat(data.getProjectStatistics()) @@ -623,7 +644,7 @@ public class TelemetryDataLoaderImplTest { @Test public void givenExistingExternalSecurityReport_whenTelemetryIsGenerated_payloadShouldContainLastUsageDate() { - server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + server.setId(SERVER_ID).setVersion("7.5.4"); ProjectData projectData = db.components().insertPublicProject(); db.getDbClient().propertiesDao().saveProperty(new PropertyDto().setKey(EXTERNAL_SECURITY_REPORT_EXPORTED_AT).setEntityUuid(projectData.projectUuid()).setValue("1")); @@ -692,7 +713,7 @@ public class TelemetryDataLoaderImplTest { @DataProvider public static Object[][] getManagedInstanceData() { - return new Object[][]{ + return new Object[][] { {true, "scim"}, {true, "github"}, {true, "gitlab"}, diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index b10220642d8..db30d30bec9 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -19,7 +19,6 @@ */ package org.sonar.server.telemetry; -import com.google.common.annotations.VisibleForTesting; import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.util.ArrayList; @@ -54,6 +53,7 @@ import org.sonar.db.measure.ProjectLocDistributionDto; import org.sonar.db.measure.ProjectMainBranchLiveMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.newcodeperiod.NewCodePeriodDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.db.qualitygate.ProjectQgateAssociationDto; @@ -90,8 +90,6 @@ import static org.sonar.server.telemetry.TelemetryDaemon.I_PROP_MESSAGE_SEQUENCE @ServerSide public class TelemetryDataLoaderImpl implements TelemetryDataLoader { - @VisibleForTesting - static final String SCIM_PROPERTY_ENABLED = "sonar.scim.enabled"; private static final String UNDETECTED = "undetected"; public static final String EXTERNAL_SECURITY_REPORT_EXPORTED_AT = "project.externalSecurityReportExportedAt"; @@ -169,10 +167,11 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { data.setNewCodeDefinitions(newCodeDefinitions); String defaultQualityGateUuid = qualityGateFinder.getDefault(dbSession).getUuid(); + List<ProjectDto> projects = dbClient.projectDao().selectProjects(dbSession); data.setDefaultQualityGate(defaultQualityGateUuid); resolveUnanalyzedLanguageCode(data, dbSession); - resolveProjectStatistics(data, dbSession, defaultQualityGateUuid); + resolveProjectStatistics(data, dbSession, defaultQualityGateUuid, projects); resolveProjects(data, dbSession); resolveBranches(data, branchMeasuresDtos); resolveQualityGates(data, dbSession); @@ -284,8 +283,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { return internalProperties.read(I_PROP_MESSAGE_SEQUENCE).map(Long::parseLong).orElse(0L); } - private void resolveProjectStatistics(TelemetryData.Builder data, DbSession dbSession, String defaultQualityGateUuid) { - List<String> projectUuids = dbClient.projectDao().selectAllProjectUuids(dbSession); + private void resolveProjectStatistics(TelemetryData.Builder data, DbSession dbSession, String defaultQualityGateUuid, List<ProjectDto> projects) { Map<String, String> scmByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDSCM); Map<String, String> ciByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDCI); Map<String, ProjectAlmKeyAndProject> almAndUrlByProject = getAlmAndUrlByProject(dbSession); @@ -298,7 +296,8 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { Map<String, Long> securityReportExportedAtByProjectUuid = getSecurityReportExportedAtDateByProjectUuid(dbSession); List<TelemetryData.ProjectStatistics> projectStatistics = new ArrayList<>(); - for (String projectUuid : projectUuids) { + for (ProjectDto project : projects) { + String projectUuid = project.getUuid(); Map<String, Number> metrics = metricsByProject.getOrDefault(projectUuid, Collections.emptyMap()); Optional<PrBranchAnalyzedLanguageCountByProjectDto> counts = ofNullable(prAndBranchCountByProject.get(projectUuid)); @@ -317,6 +316,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { .setTechnicalDebt(metrics.getOrDefault("sqale_index", null)) .setNcdId(ncdByProject.getOrDefault(projectUuid, instanceNcd).hashCode()) .setExternalSecurityReportExportedAt(securityReportExportedAtByProjectUuid.get(projectUuid)) + .setCreationMethod(project.getCreationMethod()) .build(); projectStatistics.add(stats); } @@ -350,8 +350,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { data.setProjects(buildProjectsList(branchesWithLargestNcloc, latestSnapshotMap)); } - private List<TelemetryData.Project> buildProjectsList(List<ProjectLocDistributionDto> branchesWithLargestNcloc, - Map<String, Long> latestSnapshotMap) { + private List<TelemetryData.Project> buildProjectsList(List<ProjectLocDistributionDto> branchesWithLargestNcloc, Map<String, Long> latestSnapshotMap) { return branchesWithLargestNcloc.stream() .flatMap(measure -> Arrays.stream(measure.locDistribution().split(";")) .map(languageAndLoc -> languageAndLoc.split("=")) |