]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9820 Telemetry returns usingBranches
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 3 Oct 2017 09:26:49 +0000 (11:26 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Tue, 3 Oct 2017 12:45:02 +0000 (14:45 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryData.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java
server/sonar-server/src/test/resources/org/sonar/server/telemetry/telemetry-example.json
tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryUploadTest.java

index ec66130acdda1e1e434382a403375648924e3fa3..ae6558869cf20aedb3fc803a664fd8efabda9490 100644 (file)
@@ -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);
   }
index ae4fc73dc0355e6c8f8ea60eb327ef5b8371517d..d1c08ed6bc0b0514e1e3007bbd1f9651b257ab71 100644 (file)
@@ -39,4 +39,6 @@ public interface BranchMapper {
   Collection<BranchDto> selectByProjectUuid(@Param("projectUuid") String projectUuid);
 
   List<BranchDto> selectByUuids(@Param("uuids") Collection<String> uuids);
+
+  long countNonMainBranches();
 }
index a3a24d7913fa78b352820af73343bb2b0d696052..dd5206678334c6c56dbb89671933fe9476e22edc 100644 (file)
     pb.uuid = #{uuid, jdbcType=VARCHAR}
   </select>
 
+  <select id="countNonMainBranches" resultType="long">
+    select count(pb.uuid)
+    from project_branches pb
+    where
+    pb.uuid &lt;&gt; pb.project_uuid
+  </select>
+
 </mapper>
index ab5049dca3f91ffcd23e0dd777ea2a865f7f11d4..f7f20dda309fce7d6ce5881e63ba3b702fcfade3 100644 (file)
@@ -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();
+  }
 }
index 92d31b248abd7f22d80a7f655348c2d8a96f5a22..116db257635fb2b62c88a0768782b4c42eeb0876 100644 (file)
@@ -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<String, Long> projectCountByLanguage;
   private final Map<String, Long> 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<String, String> 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);
     }
index 723747f20818bdfce2a2e4e6cbd2a5abd986264c..7e9b49656028c8e5be9d9678b0f79e3d0cf59ed0 100644 (file)
@@ -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");
index d16bfd1efbbb273d395856e8a0fdfe183539c712..bf4834963fd8441d486939868f3c455d99bbe16f 100644 (file)
@@ -59,7 +59,6 @@ public class TelemetryDataLoader {
 
     data.setServerId(server.getId());
     data.setVersion(server.getVersion());
-    data.setDatabase(loadDatabaseMetadata());
     Function<PluginInfo, String> getVersion = plugin -> plugin.getVersion() == null ? "undefined" : plugin.getVersion().getName();
     Map<String, String> 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) {
index 711e36759b50be26975acd8364861668d1b71280..389a013d44a3af8291e737a77ca01b9d416df2a0 100644 (file)
@@ -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() {
index 4d68cd9aac8cd42818dcac51f03c66783f75248f..84a20d03de66c9898249cc35b1abe6e18e633d78 100644 (file)
@@ -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));
index 58cb80408054892e0c8230eb0e2eea1d81f19bdd..5ec6050cba7cc50894eeb4412095e8bc4d0cb69f 100644 (file)
@@ -21,6 +21,7 @@
   ],
   "userCount": 3,
   "projectCount": 2,
+  "usingBranches": true,
   "lines": 500,
   "ncloc": 300,
   "projectCountByLanguage": [
index 82016455508c954209dca09e8f57abedc1b3b7f2..30f085e3706ff407ce4a7de39dadb8ec2a2bff8a 100644 (file)
@@ -85,6 +85,7 @@ public class TelemetryUploadTest {
     Map<String, String> database = (Map<String, String>) 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<String> plugins = ((List<Map<String, String>>) json.get("plugins")).stream().map(p -> p.get("name")).collect(Collectors.toList());
     assertThat(plugins).contains("xoo");