diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2023-03-28 10:16:41 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-04-27 20:03:01 +0000 |
commit | 4114d4103e050e6213c2b514773e22d4e4a00bb2 (patch) | |
tree | 5fd582004ad0394e86a1032608a4a276144b911f /server/sonar-ce-task-projectanalysis | |
parent | 3758946e528a5234c5ab9aede8a9206fa161da0a (diff) | |
download | sonarqube-4114d4103e050e6213c2b514773e22d4e4a00bb2.tar.gz sonarqube-4114d4103e050e6213c2b514773e22d4e4a00bb2.zip |
SONAR-18872 improve total ncloc computation9.9.1.69595
Diffstat (limited to 'server/sonar-ce-task-projectanalysis')
3 files changed, 117 insertions, 0 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java new file mode 100644 index 00000000000..c2cc101df84 --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.ce.task.projectanalysis.step; + +import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; +import org.sonar.ce.task.step.ComputationStep; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; + +public class ProjectNclocComputationStep implements ComputationStep { + + private final AnalysisMetadataHolder analysisMetadataHolder; + private final DbClient dbClient; + + public ProjectNclocComputationStep(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) { + this.analysisMetadataHolder = analysisMetadataHolder; + this.dbClient = dbClient; + } + + @Override + public void execute(Context context) { + try (DbSession dbSession = dbClient.openSession(false)) { + String projectUuid = analysisMetadataHolder.getProject().getUuid(); + long maxncloc = dbClient.liveMeasureDao().sumNclocOfBiggestBranchForProject(dbSession, projectUuid); + dbClient.projectDao().updateNcloc(dbSession, projectUuid, maxncloc); + dbSession.commit(); + } + } + + @Override + public String getDescription() { + return "Compute total Project ncloc"; + } +} diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java index 8267f5a216a..e93279055b4 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ReportComputationSteps.java @@ -110,6 +110,7 @@ public class ReportComputationSteps extends AbstractComputationSteps { PurgeDatastoresStep.class, IndexAnalysisStep.class, UpdateNeedIssueSyncStep.class, + ProjectNclocComputationStep.class, PersistPushEventsStep.class, // notifications are sent at the end, so that webapp displays up-to-date information diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStepIT.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStepIT.java new file mode 100644 index 00000000000..38c925fb7ff --- /dev/null +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStepIT.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.ce.task.projectanalysis.step; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; +import org.sonar.ce.task.step.TestComputationStepContext; +import org.sonar.db.DbClient; +import org.sonar.db.DbTester; +import org.sonar.db.component.BranchType; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.metric.MetricDto; +import org.sonar.server.project.Project; + +import static java.util.Collections.emptyList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.measures.Metric.ValueType.INT; + +public class ProjectNclocComputationStepIT { + @Rule + public DbTester db = DbTester.create(); + private final DbClient dbClient = db.getDbClient(); + + @Rule + public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); + + private final ProjectNclocComputationStep step = new ProjectNclocComputationStep(analysisMetadataHolder, dbClient); + + @Test + public void test_computing_branch_ncloc() { + MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); + ComponentDto project = db.components().insertPublicProject(); + ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); + db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(200d)); + ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); + db.measures().insertLiveMeasure(branch2, ncloc, m -> m.setValue(10d)); + analysisMetadataHolder.setProject(new Project(project.uuid(), project.getKey(), project.name(), project.description(), emptyList())); + step.execute(TestComputationStepContext.TestStatistics::new); + + assertThat(dbClient.projectDao().getNclocSum(db.getSession())).isEqualTo(200L); + } + + @Test + public void description_is_not_missing() { + assertThat(step.getDescription()).isNotBlank(); + } +}
\ No newline at end of file |