From ea5cfd377b0b8d2d4c258841b02b1e12325d94ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Wed, 31 Jan 2024 15:13:39 +0100 Subject: [PATCH] SONAR-21259 'pull_request_fixed_issues' measure is only computed if a target branch analysis exists --- .../step/PullRequestFixedIssuesMeasureStep.java | 12 ++++++++---- .../PullRequestFixedIssuesMeasureStepTest.java | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStep.java index 01c6d815d32..09d973ccd02 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStep.java @@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.step; import org.sonar.api.measures.CoreMetrics; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.ce.task.projectanalysis.component.TreeRootHolder; +import org.sonar.ce.task.projectanalysis.issue.TrackerTargetBranchInputFactory; import org.sonar.ce.task.projectanalysis.issue.fixedissues.PullRequestFixedIssueRepository; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.measure.MeasureRepository; @@ -39,21 +40,24 @@ public class PullRequestFixedIssuesMeasureStep implements ComputationStep { private final MeasureRepository measureRepository; private final PullRequestFixedIssueRepository pullRequestFixedIssueRepository; private final AnalysisMetadataHolder analysisMetadataHolder; + private final TrackerTargetBranchInputFactory targetInputFactory; public PullRequestFixedIssuesMeasureStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, - MeasureRepository measureRepository, - PullRequestFixedIssueRepository pullRequestFixedIssueRepository, - AnalysisMetadataHolder analysisMetadataHolder) { + MeasureRepository measureRepository, + PullRequestFixedIssueRepository pullRequestFixedIssueRepository, + AnalysisMetadataHolder analysisMetadataHolder, + TrackerTargetBranchInputFactory targetInputFactory) { this.treeRootHolder = treeRootHolder; this.metricRepository = metricRepository; this.measureRepository = measureRepository; this.pullRequestFixedIssueRepository = pullRequestFixedIssueRepository; this.analysisMetadataHolder = analysisMetadataHolder; + this.targetInputFactory = targetInputFactory; } @Override public void execute(Context context) { - if (analysisMetadataHolder.isPullRequest()) { + if (analysisMetadataHolder.isPullRequest() && targetInputFactory.hasTargetBranchAnalysis()) { int fixedIssuesCount = pullRequestFixedIssueRepository.getFixedIssues().size(); measureRepository.add(treeRootHolder.getRoot(), metricRepository.getByKey(CoreMetrics.PULL_REQUEST_FIXED_ISSUES_KEY), Measure.newMeasureBuilder().create(fixedIssuesCount)); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStepTest.java index 85522807849..3c06cbbafae 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PullRequestFixedIssuesMeasureStepTest.java @@ -25,11 +25,13 @@ import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.mockito.Mockito; import org.sonar.api.measures.CoreMetrics; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.ReportComponent; import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule; +import org.sonar.ce.task.projectanalysis.issue.TrackerTargetBranchInputFactory; import org.sonar.ce.task.projectanalysis.issue.fixedissues.PullRequestFixedIssueRepository; import org.sonar.ce.task.projectanalysis.measure.Measure; import org.sonar.ce.task.projectanalysis.measure.MeasureAssert; @@ -55,13 +57,16 @@ public class PullRequestFixedIssuesMeasureStepTest { private final PullRequestFixedIssueRepository pullRequestFixedIssueRepository = mock(PullRequestFixedIssueRepository.class); private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); + private final TrackerTargetBranchInputFactory targetBranchInputFactory = mock(TrackerTargetBranchInputFactory.class); + private final PullRequestFixedIssuesMeasureStep underTest = new PullRequestFixedIssuesMeasureStep(treeRootHolder, metricRepository, - measureRepository, pullRequestFixedIssueRepository, analysisMetadataHolder); + measureRepository, pullRequestFixedIssueRepository, analysisMetadataHolder, targetBranchInputFactory); @Before public void setUp() throws Exception { treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, ROOT_REF).build()); metricRepository.add(CoreMetrics.PULL_REQUEST_FIXED_ISSUES); + Mockito.when(targetBranchInputFactory.hasTargetBranchAnalysis()).thenReturn(true); } @Test @@ -85,6 +90,16 @@ public class PullRequestFixedIssuesMeasureStepTest { assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty(); } + @Test + public void execute_whenPRHasNoTargetBranchAnalysis_shouldNotCreateMeasure() { + when(analysisMetadataHolder.isPullRequest()).thenReturn(true); + when(targetBranchInputFactory.hasTargetBranchAnalysis()).thenReturn(false); + + underTest.execute(new TestComputationStepContext()); + + assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty(); + } + @Test public void execute_whenNoFixedIssues_shouldCreateMeasureWithValueZero() { when(analysisMetadataHolder.isPullRequest()).thenReturn(true); -- 2.39.5