From 89db34478c467edcdaa01351fe0a54c8213bd19b Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 24 Sep 2021 13:16:54 -0500 Subject: [PATCH] SONAR-15450 Take pull requests into account when counting instance's NLOC --- .../org/sonar/db/measure/LiveMeasureDao.java | 4 +--- .../sonar/db/measure/LiveMeasureMapper.java | 2 -- .../sonar/db/measure/LiveMeasureMapper.xml | 2 +- .../TelemetryDataLoaderImplTest.java | 21 +++++++++++++++++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java index 64bbf11735b..a7c1813ba61 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java @@ -28,7 +28,6 @@ import org.sonar.api.utils.System2; import org.sonar.core.util.Uuids; import org.sonar.db.Dao; import org.sonar.db.DbSession; -import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; import org.sonar.db.dialect.Dialect; @@ -99,8 +98,7 @@ public class LiveMeasureDao implements Dao { * If Main Branch = 100 LOCs and the "largest branch" is 80 LOCs, I'm expecting to consider the value 100. */ public long sumNclocOfBiggestBranch(DbSession dbSession, SumNclocDbQuery dbQuery) { - Long ncloc = mapper(dbSession).sumNclocOfBiggestBranch( - NCLOC_KEY, BranchType.BRANCH, dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude()); + Long ncloc = mapper(dbSession).sumNclocOfBiggestBranch(NCLOC_KEY, dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude()); return ncloc == null ? 0L : ncloc; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java index 70b58ca84ef..4781db6c98d 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java @@ -24,7 +24,6 @@ import java.util.List; import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.ResultHandler; -import org.sonar.db.component.BranchType; public interface LiveMeasureMapper { @@ -57,7 +56,6 @@ public interface LiveMeasureMapper { Long sumNclocOfBiggestBranch( @Param("ncloc") String nclocKey, - @Param("branchType") BranchType branchType, @Param("private") Boolean privateProject, @Nullable @Param("projectUuidToExclude") String projectUuidToExclude); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml index 6350e358734..2141836ab7f 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml @@ -56,7 +56,7 @@ select b.project_uuid as projectUuid, max(lm.value) as maxncloc from live_measures lm inner join metrics m on m.uuid = lm.metric_uuid - inner join project_branches b on b.uuid = lm.component_uuid and b.branch_type = #{branchType, jdbcType=VARCHAR} + inner join project_branches b on b.uuid = lm.component_uuid inner join projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK' m.name = #{ncloc, jdbcType=VARCHAR} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index 4ec49748d9b..f46cbc179a5 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -171,7 +171,7 @@ public class TelemetryDataLoaderImplTest { } @Test - public void take_largest_branches() { + public void take_largest_branch() { server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY)); ComponentDto project = db.components().insertPublicProject(); @@ -179,7 +179,7 @@ public class TelemetryDataLoaderImplTest { ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(10d)); db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(20d)); - db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(30d)); + db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(15d)); projectMeasuresIndexer.indexAll(); TelemetryData data = communityUnderTest.load(); @@ -187,6 +187,23 @@ public class TelemetryDataLoaderImplTest { assertThat(data.getNcloc()).isEqualTo(20L); } + @Test + public void take_largest_branch_with_pr() { + server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4"); + MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY)); + ComponentDto project = db.components().insertPublicProject(); + ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(10d)); + db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(20d)); + db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(30d)); + projectMeasuresIndexer.indexAll(); + + TelemetryData data = communityUnderTest.load(); + + assertThat(data.getNcloc()).isEqualTo(30L); + } + @Test public void data_contains_no_license_type_on_community_edition() { TelemetryData data = communityUnderTest.load(); -- 2.39.5