diff options
author | Stas Vilchik <stas.vilchik@sonarsource.com> | 2018-10-03 11:01:08 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-10-12 20:20:59 +0200 |
commit | d8bcb75697909f1a6d7297f7356f0f7974c6826f (patch) | |
tree | 3a0bc91dc4913d26050e4e4d95173647a778c80d /server/sonar-db-dao | |
parent | f52d0c2586ee1fffb12e882cc86c41e036b46ce0 (diff) | |
download | sonarqube-d8bcb75697909f1a6d7297f7356f0f7974c6826f.tar.gz sonarqube-d8bcb75697909f1a6d7297f7356f0f7974c6826f.zip |
SONARCLOUD-140 Display breakdown by private LOC on the Billing page (#770)
Diffstat (limited to 'server/sonar-db-dao')
6 files changed, 134 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 411a98dd614..3d9cc3ad1be 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -45,6 +45,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static java.util.Collections.emptyList; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang.StringUtils.isBlank; +import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.DaoUtils.buildLikeValue; import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions; @@ -411,4 +412,8 @@ public class ComponentDao implements Dao { checkThatNotTooManyConditions(query.getComponentUuids(), "Too many component UUIDs in query"); } + public List<ProjectNclocDistributionDto> selectPrivateProjectsWithNcloc(DbSession dbSession, String organizationUuid) { + return mapper(dbSession).selectPrivateProjectsWithNcloc(NCLOC_KEY, organizationUuid, KeyType.BRANCH, BranchType.LONG); + } + } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 7d041d714ec..b810da61207 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -162,4 +162,10 @@ public interface ComponentMapper { void updateTags(ComponentDto component); List<KeyWithUuidDto> selectComponentKeysHavingIssuesToMerge(@Param("mergeBranchUuid") String mergeBranchUuid); + + List<ProjectNclocDistributionDto> selectPrivateProjectsWithNcloc( + @Param("ncloc") String ncloc, + @Param("organizationUuid") String organizationUuid, + @Param("branch") KeyType branchOrPullRequest, + @Param("branchType") BranchType branchType); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ProjectNclocDistributionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ProjectNclocDistributionDto.java new file mode 100644 index 00000000000..eb9246312b3 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ProjectNclocDistributionDto.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.component; + +public class ProjectNclocDistributionDto { + private String kee; + private String name; + private long ncloc; + + public String getKee() { + return kee; + } + + public ProjectNclocDistributionDto setKee(String kee) { + this.kee = kee; + return this; + } + + public String getName() { + return name; + } + + public ProjectNclocDistributionDto setName(String name) { + this.name = name; + return this; + } + + public long getNcloc() { + return ncloc; + } + + public ProjectNclocDistributionDto setNcloc(long ncloc) { + this.ncloc = ncloc; + return this; + } +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 28e539a7de9..d6f285b9590 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -825,4 +825,36 @@ AND i.status != 'CLOSED' </select> + <select id="selectPrivateProjectsWithNcloc" resultType="org.sonar.db.component.ProjectNclocDistributionDto"> + SELECT + p.kee AS kee, + p.name AS name, + t.ncloc AS ncloc + FROM + projects p + INNER JOIN ( + SELECT + b.project_uuid AS projectUuid, + max(lm.value) AS ncloc + FROM + live_measures lm + INNER JOIN metrics m ON m.id = lm.metric_id + INNER JOIN project_branches b ON b.uuid = lm.project_uuid + WHERE + m.name = #{ncloc, jdbcType=VARCHAR} + AND b.branch_type = #{branchType, jdbcType=VARCHAR} + AND b.key_type = #{branch, jdbcType=VARCHAR} + GROUP BY + projectUuid) t ON t.projectUuid = p.uuid + WHERE + p.enabled = ${_true} + AND p.private = ${_true} + AND p.scope = 'PRJ' + AND p.qualifier = 'TRK' + AND p.copy_component_uuid IS NULL + AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} + ORDER BY + ncloc DESC + </select> + </mapper> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java index dcc384e544d..f69460bec7b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -2021,6 +2021,33 @@ public class ComponentDaoTest { assertThat(privateFlagOfUuid(uuids[4])).isFalse(); } + @Test + public void selectPrivateProjectsWithNcloc() throws Exception { + MetricDto metric = db.measures().insertMetric(m -> m.setKey("ncloc")); + OrganizationDto organizationDto = db.organizations().insert(); + + // project1, not the biggest branch - not returned + final ComponentDto project1 = db.components().insertMainBranch(organizationDto, b -> b.setName("foo")); + insertMeasure(20d, project1, metric); + + // long branch of project1 - returned + insertMeasure(30d, db.components().insertProjectBranch(project1, b -> b.setBranchType(BranchType.LONG)), metric); + + // project2 - returned + insertMeasure(10d, db.components().insertMainBranch(organizationDto, b -> b.setName("bar")), metric); + + // public project - not returned + insertMeasure(10d, db.components().insertMainBranch(organizationDto, b -> b.setPrivate(false)), metric); + + // different org - not returned + insertMeasure(10d, db.components().insertMainBranch(db.organizations().insert()), metric); + + List<ProjectNclocDistributionDto> result = underTest.selectPrivateProjectsWithNcloc(db.getSession(), organizationDto.getUuid()); + + assertThat(result).extracting(ProjectNclocDistributionDto::getName).containsExactly("foo", "bar"); + assertThat(result).extracting(ProjectNclocDistributionDto::getNcloc).containsExactly(30L, 10L); + } + private boolean privateFlagOfUuid(String uuid) { return underTest.selectByUuid(db.getSession(), uuid).get().isPrivate(); } @@ -2032,4 +2059,8 @@ public class ComponentDaoTest { .collect(toSet()); } + private void insertMeasure(double value, ComponentDto componentDto, MetricDto metric) { + db.measures().insertLiveMeasure(componentDto, metric, m -> m.setValue(value)); + } + } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java index c9438b413e1..425a1b6360e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java @@ -286,6 +286,12 @@ public class ComponentDbTester { return branch; } + @SafeVarargs + public final ComponentDto insertProjectBranch(OrganizationDto organization, Consumer<BranchDto>... dtoPopulators) { + ComponentDto project = newPrivateProjectDto(organization); + return insertProjectBranch(project, dtoPopulators); + } + public final ComponentDto insertProjectBranch(ComponentDto project, BranchDto branchDto) { // MainBranchProjectUuid will be null if it's a main branch checkArgument(branchDto.getProjectUuid().equals(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid()))); |