From 737454ef4285f3f28578cad422900f3448e6a512 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 3 Oct 2017 11:26:49 +0200 Subject: [PATCH] SONAR-9820 Telemetry returns usingBranches --- .../java/org/sonar/db/component/BranchDao.java | 4 ++++ .../java/org/sonar/db/component/BranchMapper.java | 2 ++ .../org/sonar/db/component/BranchMapper.xml | 7 +++++++ .../java/org/sonar/db/component/BranchDaoTest.java | 13 +++++++++++++ .../org/sonar/server/telemetry/TelemetryData.java | 14 ++++++++++++++ .../server/telemetry/TelemetryDataJsonWriter.java | 1 + .../server/telemetry/TelemetryDataLoader.java | 9 ++++++--- .../sonar/server/platform/ws/InfoActionTest.java | 4 ++-- .../server/telemetry/TelemetryDaemonTest.java | 1 + .../sonar/server/telemetry/telemetry-example.json | 1 + .../tests/telemetry/TelemetryUploadTest.java | 1 + 11 files changed, 52 insertions(+), 5 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index ec66130acdd..ae6558869cf 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -74,6 +74,10 @@ public class BranchDao implements Dao { return Optional.ofNullable(mapper(session).selectByUuid(uuid)); } + public boolean hasNonMainBranches(DbSession dbSession) { + return mapper(dbSession).countNonMainBranches() > 0L; + } + private static BranchMapper mapper(DbSession dbSession) { return dbSession.getMapper(BranchMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index ae4fc73dc03..d1c08ed6bc0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -39,4 +39,6 @@ public interface BranchMapper { Collection selectByProjectUuid(@Param("projectUuid") String projectUuid); List selectByUuids(@Param("uuids") Collection uuids); + + long countNonMainBranches(); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index a3a24d7913f..dd520667833 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -88,4 +88,11 @@ pb.uuid = #{uuid, jdbcType=VARCHAR} + + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java index ab5049dca3f..f7f20dda309 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java @@ -212,4 +212,17 @@ public class BranchDaoTest { assertThat(underTest.selectByUuid(db.getSession(), project.uuid())).isNotPresent(); assertThat(underTest.selectByUuid(db.getSession(), "unknown")).isNotPresent(); } + + @Test + public void existsNonMainBranch() { + assertThat(underTest.hasNonMainBranches(dbSession)).isFalse(); + ComponentDto project = db.components().insertPrivateProject(); + assertThat(underTest.hasNonMainBranches(dbSession)).isFalse(); + + ComponentDto branch1 = db.components().insertProjectBranch(project); + assertThat(underTest.hasNonMainBranches(dbSession)).isTrue(); + + ComponentDto branch2 = db.components().insertProjectBranch(project); + assertThat(underTest.hasNonMainBranches(dbSession)).isTrue(); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryData.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryData.java index 92d31b248ab..116db257635 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryData.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryData.java @@ -33,6 +33,7 @@ public class TelemetryData { private final long ncloc; private final long userCount; private final long projectCount; + private final boolean usingBranches; private final Database database; private final Map projectCountByLanguage; private final Map nclocByLanguage; @@ -45,6 +46,7 @@ public class TelemetryData { ncloc = builder.projectMeasuresStatistics.getNcloc(); userCount = builder.userCount; projectCount = builder.projectMeasuresStatistics.getProjectCount(); + usingBranches = builder.usingBranches; database = builder.database; projectCountByLanguage = builder.projectMeasuresStatistics.getProjectCountByLanguage(); nclocByLanguage = builder.projectMeasuresStatistics.getNclocByLanguage(); @@ -78,6 +80,10 @@ public class TelemetryData { return projectCount; } + public boolean isUsingBranches() { + return usingBranches; + } + public Database getDatabase() { return database; } @@ -101,6 +107,7 @@ public class TelemetryData { private Map plugins; private Database database; private ProjectMeasuresStatistics projectMeasuresStatistics; + private Boolean usingBranches; private Builder() { // enforce static factory method @@ -133,11 +140,18 @@ public class TelemetryData { return this; } + Builder setUsingBranches(boolean usingBranches) { + this.usingBranches = usingBranches; + return this; + } + TelemetryData build() { requireNonNull(serverId); requireNonNull(version); requireNonNull(plugins); requireNonNull(projectMeasuresStatistics); + requireNonNull(database); + requireNonNull(usingBranches); return new TelemetryData(this); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java index 723747f2081..7e9b4965602 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java @@ -50,6 +50,7 @@ public class TelemetryDataJsonWriter { json.endArray(); json.prop("userCount", statistics.getUserCount()); json.prop("projectCount", statistics.getProjectCount()); + json.prop("usingBranches", statistics.isUsingBranches()); json.prop(LINES_KEY, statistics.getLines()); json.prop(NCLOC_KEY, statistics.getNcloc()); json.name("projectCountByLanguage"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataLoader.java index d16bfd1efbb..bf4834963fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataLoader.java @@ -59,7 +59,6 @@ public class TelemetryDataLoader { data.setServerId(server.getId()); data.setVersion(server.getVersion()); - data.setDatabase(loadDatabaseMetadata()); Function getVersion = plugin -> plugin.getVersion() == null ? "undefined" : plugin.getVersion().getName(); Map plugins = pluginRepository.getPluginInfos().stream().collect(MoreCollectors.uniqueIndex(PluginInfo::getKey, getVersion)); data.setPlugins(plugins); @@ -67,6 +66,10 @@ public class TelemetryDataLoader { data.setUserCount(userCount); ProjectMeasuresStatistics projectMeasuresStatistics = projectMeasuresIndex.searchTelemetryStatistics(); data.setProjectMeasuresStatistics(projectMeasuresStatistics); + try (DbSession dbSession = dbClient.openSession(false)) { + data.setDatabase(loadDatabaseMetadata(dbSession)); + data.setUsingBranches(dbClient.branchDao().hasNonMainBranches(dbSession)); + } return data.build(); } @@ -75,8 +78,8 @@ public class TelemetryDataLoader { return server.getId(); } - private Database loadDatabaseMetadata() { - try (DbSession dbSession = dbClient.openSession(false)) { + private static Database loadDatabaseMetadata(DbSession dbSession) { + try { DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); return new Database(metadata.getDatabaseProductName(), metadata.getDatabaseProductVersion()); } catch (SQLException e) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java index 711e36759b5..389a013d44a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java @@ -100,8 +100,8 @@ public class InfoActionTest { TestResponse response = ws.newRequest().execute(); // response does not contain empty "Section Three" assertThat(response.getInput()).isEqualTo("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," + - "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"lines\":0,\"ncloc\":0," + - "\"projectCountByLanguage\":[],\"nclocByLanguage\":[]}}"); + "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"usingBranches\":false," + + "\"lines\":0,\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[]}}"); } private void logInAsSystemAdministrator() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java index 4d68cd9aac8..84a20d03de6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java @@ -125,6 +125,7 @@ public class TelemetryDaemonTest { MetricDto nclocDistrib = db.measures().insertMetric(m -> m.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY)); ComponentDto project1 = db.components().insertMainBranch(db.getDefaultOrganization()); + ComponentDto project1Branch = db.components().insertProjectBranch(project1); SnapshotDto analysis1 = db.components().insertSnapshot(project1); db.measures().insertMeasure(project1, analysis1, lines, m -> m.setValue(200d)); db.measures().insertMeasure(project1, analysis1, ncloc, m -> m.setValue(100d)); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json index 58cb8040805..5ec6050cba7 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json @@ -21,6 +21,7 @@ ], "userCount": 3, "projectCount": 2, + "usingBranches": true, "lines": 500, "ncloc": 300, "projectCountByLanguage": [ diff --git a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java index 82016455508..30f085e3706 100644 --- a/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java +++ b/tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java @@ -85,6 +85,7 @@ public class TelemetryUploadTest { Map database = (Map) json.get("database"); assertThat(database.get("name")).isNotEmpty(); assertThat(database.get("version")).isNotEmpty(); + assertThat((Boolean) json.get("usingBranches")).isFalse(); assertThat(getInteger(json.get("userCount"))).isEqualTo(1); List plugins = ((List>) json.get("plugins")).stream().map(p -> p.get("name")).collect(Collectors.toList()); assertThat(plugins).contains("xoo"); -- 2.39.5