From 2f6c01779fcb9068e32e2659d5c8d5b11019fa57 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Fri, 26 Jan 2024 16:36:48 +0100 Subject: [PATCH] SONAR-21455 new_accepted_issues should only be computed in application when all the projects have been reanalyzed --- .../step/ViewsPersistComponentsStepIT.java | 2 +- .../component/ProjectViewAttributes.java | 17 ++++++++++++----- .../projectanalysis/formula/SumFormula.java | 15 +++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepIT.java index 5aa74cf95ad..2ef2270e983 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepIT.java @@ -470,7 +470,7 @@ public class ViewsPersistComponentsStepIT extends BaseStepTest { .setUuid(PROJECT_VIEW_1_UUID) .setName(PROJECT_VIEW_1_NAME) .setDescription("project view description is not persisted") - .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, null)); + .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, true, null)); } private void persistComponents(ComponentDto... componentDtos) { diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectViewAttributes.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectViewAttributes.java index 40c093fd6c4..1175514128b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectViewAttributes.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ProjectViewAttributes.java @@ -31,12 +31,14 @@ public class ProjectViewAttributes { private final String originalKey; @CheckForNull private final Long analysisDate; + private final boolean hasNewCodePeriod; private final String branchName; - public ProjectViewAttributes(String uuid, String originalKey, @Nullable Long analysisDate, @Nullable String branchName) { + public ProjectViewAttributes(String uuid, String originalKey, @Nullable Long analysisDate, boolean hasNewCodePeriod, @Nullable String branchName) { this.uuid = requireNonNull(uuid, "uuid can't be null"); this.originalKey = requireNonNull(originalKey, "originalKey can't be null"); this.analysisDate = analysisDate; + this.hasNewCodePeriod = hasNewCodePeriod; this.branchName = branchName; } @@ -57,12 +59,17 @@ public class ProjectViewAttributes { return originalKey; } + public boolean hasNewCodePeriod() { + return hasNewCodePeriod; + } + @Override public String toString() { return "ProjectViewAttributes{" + - ", uuid='" + uuid + '\'' + - ", branchName='" + branchName + '\'' + - ", analysisDate=" + analysisDate + - '}'; + ", uuid='" + uuid + '\'' + + ", branchName='" + branchName + '\'' + + ", hasNewCodePeriod=" + branchName + + ", analysisDate=" + analysisDate + + '}'; } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/SumFormula.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/SumFormula.java index 876ccc15db4..7e738c37cd2 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/SumFormula.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/SumFormula.java @@ -61,8 +61,9 @@ public abstract class SumFormula, U extends Number> i @Override public Optional createMeasure(IntSumCounter counter, CreateMeasureContext context) { Optional valueOptional = counter.getValue(); - if (shouldCreateMeasure(context, valueOptional)) { - return Optional.of(Measure.newMeasureBuilder().create(valueOptional.get())); + Integer value = valueOptional.orElse(null); + if (shouldCreateMeasure(context, value)) { + return Optional.of(Measure.newMeasureBuilder().create(value)); } return Optional.empty(); } @@ -89,15 +90,17 @@ public abstract class SumFormula, U extends Number> i @Override public Optional createMeasure(LongSumCounter counter, CreateMeasureContext context) { Optional valueOptional = counter.getValue(); - if (shouldCreateMeasure(context, valueOptional)) { - return Optional.of(Measure.newMeasureBuilder().create(valueOptional.get())); + Long value = valueOptional.orElse(null); + if (shouldCreateMeasure(context, value)) { + + return Optional.of(Measure.newMeasureBuilder().create(value)); } return Optional.empty(); } } - private static boolean shouldCreateMeasure(CreateMeasureContext context, Optional value) { - return value.isPresent() && CrawlerDepthLimit.LEAVES.isDeeperThan(context.getComponent().getType()); + protected static boolean shouldCreateMeasure(CreateMeasureContext context, @Nullable T value) { + return value != null && CrawlerDepthLimit.LEAVES.isDeeperThan(context.getComponent().getType()); } @Override -- 2.39.5