aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task-projectanalysis/src
diff options
context:
space:
mode:
authorantoine.vinot <antoine.vinot@sonarsource.com>2024-12-11 18:17:23 +0100
committerSteve Marion <steve.marion@sonarsource.com>2024-12-18 11:13:23 +0100
commitdb70e0ef0445d40c5449f8747f261dddbb24eaba (patch)
tree09f2de19aa9c286ea96f7e3a1cc6aa2ff9b20d7a /server/sonar-ce-task-projectanalysis/src
parentd6cb128e490ec8eeee51acfbbf45ebca1187d907 (diff)
downloadsonarqube-db70e0ef0445d40c5449f8747f261dddbb24eaba.tar.gz
sonarqube-db70e0ef0445d40c5449f8747f261dddbb24eaba.zip
SONAR-22265 Get the scm info from the previous branch analysis instead of the reference branch
Diffstat (limited to 'server/sonar-ce-task-projectanalysis/src')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderIT.java21
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java2
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java59
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/OriginalFileResolver.java93
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImpl.java34
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImplTest.java6
6 files changed, 119 insertions, 96 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderIT.java
index 82537868ff1..ac6f2a95a35 100644
--- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderIT.java
+++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderIT.java
@@ -40,6 +40,7 @@ import org.sonar.ce.task.projectanalysis.filemove.MutableMovedFilesRepositoryRul
import org.sonar.ce.task.projectanalysis.period.NewCodeReferenceBranchComponentUuids;
import org.sonar.ce.task.projectanalysis.period.Period;
import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
+import org.sonar.ce.task.projectanalysis.source.OriginalFileResolver;
import org.sonar.core.hash.SourceHashComputer;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbTester;
@@ -49,6 +50,7 @@ import org.sonar.db.protobuf.DbFileSources;
import org.sonar.db.source.FileSourceDto;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.slf4j.event.Level.TRACE;
@@ -80,9 +82,10 @@ public class ScmInfoDbLoaderIT {
private final Branch branch = mock(Branch.class);
private final ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids = mock(NewCodeReferenceBranchComponentUuids.class);
+ private final OriginalFileResolver originalFileResolver = new OriginalFileResolver(analysisMetadataHolder, movedFiles, referenceBranchComponentUuids,
+ newCodeReferenceBranchComponentUuids, periodHolder);
- private final ScmInfoDbLoader underTest = new ScmInfoDbLoader(analysisMetadataHolder, movedFiles, dbTester.getDbClient(), referenceBranchComponentUuids,
- newCodeReferenceBranchComponentUuids, periodHolder);
+ private final ScmInfoDbLoader underTest = new ScmInfoDbLoader(dbTester.getDbClient(), originalFileResolver);
@Before
public void before() {
@@ -98,7 +101,7 @@ public class ScmInfoDbLoaderIT {
String hash = computeSourceHash(1);
addFileSourceInDb("henry", DATE_1, "rev-1", hash);
- DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+ DbScmInfo scmInfo = underTest.getScmInfo(FILE).orElseGet(() -> fail("Expected SCM info"));
assertThat(scmInfo.getAllChangesets()).hasSize(1);
assertThat(scmInfo.fileHash()).isEqualTo(hash);
@@ -116,7 +119,7 @@ public class ScmInfoDbLoaderIT {
when(referenceBranchComponentUuids.getComponentUuid(FILE.getKey())).thenReturn(referenceFileUuid);
addFileSourceInDb("henry", DATE_1, "rev-1", hash, referenceFileUuid);
- DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+ DbScmInfo scmInfo = underTest.getScmInfo(FILE).orElseGet(() -> fail("Expected SCM info"));
assertThat(scmInfo.getAllChangesets()).hasSize(1);
assertThat(scmInfo.fileHash()).isEqualTo(hash);
assertThat(logTester.logs(TRACE)).contains("Reading SCM info from DB for file 'referenceFileUuid'");
@@ -124,7 +127,6 @@ public class ScmInfoDbLoaderIT {
@Test
public void read_from_target_if_pullrequest() {
- Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
@@ -135,7 +137,7 @@ public class ScmInfoDbLoaderIT {
when(referenceBranchComponentUuids.getComponentUuid(FILE.getKey())).thenReturn(targetBranchFileUuid);
addFileSourceInDb("henry", DATE_1, "rev-1", hash, targetBranchFileUuid);
- DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+ DbScmInfo scmInfo = underTest.getScmInfo(FILE).orElseGet(() -> fail("Expected SCM info"));
assertThat(scmInfo.getAllChangesets()).hasSize(1);
assertThat(scmInfo.fileHash()).isEqualTo(hash);
assertThat(logTester.logs(TRACE)).contains("Reading SCM info from DB for file 'targetBranchFileUuid'");
@@ -145,7 +147,6 @@ public class ScmInfoDbLoaderIT {
public void read_from_target_if_reference_branch() {
periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), null, null));
- Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.BRANCH);
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
@@ -156,7 +157,7 @@ public class ScmInfoDbLoaderIT {
when(newCodeReferenceBranchComponentUuids.getComponentUuid(FILE.getKey())).thenReturn(targetBranchFileUuid);
addFileSourceInDb("henry", DATE_1, "rev-1", hash, targetBranchFileUuid);
- DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+ DbScmInfo scmInfo = underTest.getScmInfo(FILE).orElseGet(() -> fail("Expected SCM info"));
assertThat(scmInfo.getAllChangesets()).hasSize(1);
assertThat(scmInfo.fileHash()).isEqualTo(hash);
assertThat(logTester.logs(TRACE)).contains("Reading SCM info from DB for file 'targetBranchFileUuid'");
@@ -166,7 +167,6 @@ public class ScmInfoDbLoaderIT {
public void read_from_db_if_not_exist_in_reference_branch() {
periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), null, null));
- Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.BRANCH);
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
@@ -175,7 +175,7 @@ public class ScmInfoDbLoaderIT {
addFileSourceInDb("henry", DATE_1, "rev-1", hash, FILE.getUuid());
- DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+ DbScmInfo scmInfo = underTest.getScmInfo(FILE).orElseGet(() -> fail("Expected SCM info"));
assertThat(scmInfo.getAllChangesets()).hasSize(1);
assertThat(scmInfo.fileHash()).isEqualTo(hash);
assertThat(logTester.logs(TRACE)).contains("Reading SCM info from DB for file 'FILE_UUID'");
@@ -194,7 +194,6 @@ public class ScmInfoDbLoaderIT {
@Test
public void do_not_read_from_db_on_first_analysis_if_there_is_no_reference_branch() {
- Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
index 97ee442ea37..f219b4b3103 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java
@@ -137,6 +137,7 @@ import org.sonar.ce.task.projectanalysis.source.FileSourceDataComputer;
import org.sonar.ce.task.projectanalysis.source.FileSourceDataWarnings;
import org.sonar.ce.task.projectanalysis.source.LastCommitVisitor;
import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
+import org.sonar.ce.task.projectanalysis.source.OriginalFileResolver;
import org.sonar.ce.task.projectanalysis.source.SignificantCodeRepository;
import org.sonar.ce.task.projectanalysis.source.SourceHashRepositoryImpl;
import org.sonar.ce.task.projectanalysis.source.SourceLineReadersFactory;
@@ -201,6 +202,7 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop
FileStatusesImpl.class,
IndexDiffResolverImpl.class,
+ OriginalFileResolver.class,
new MetricModule(),
// holders
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java
index 33b77bc7d55..6b2eef6709b 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java
@@ -22,43 +22,25 @@ package org.sonar.ce.task.projectanalysis.scm;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
-import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.component.Component;
-import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
-import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
-import org.sonar.ce.task.projectanalysis.period.NewCodeReferenceBranchComponentUuids;
-import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
+import org.sonar.ce.task.projectanalysis.source.OriginalFileResolver;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.source.FileSourceDto;
public class ScmInfoDbLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ScmInfoDbLoader.class);
- private final AnalysisMetadataHolder analysisMetadataHolder;
- private final MovedFilesRepository movedFilesRepository;
private final DbClient dbClient;
- private final ReferenceBranchComponentUuids referenceBranchComponentUuid;
- private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids;
- private final PeriodHolder periodHolder;
+ private final OriginalFileResolver originalFileResolver;
- public ScmInfoDbLoader(AnalysisMetadataHolder analysisMetadataHolder, MovedFilesRepository movedFilesRepository,
- DbClient dbClient,
- ReferenceBranchComponentUuids referenceBranchComponentUuid,
- NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids,
- PeriodHolder periodHolder) {
- this.analysisMetadataHolder = analysisMetadataHolder;
- this.movedFilesRepository = movedFilesRepository;
+ public ScmInfoDbLoader(DbClient dbClient, OriginalFileResolver originalFileResolver) {
this.dbClient = dbClient;
- this.referenceBranchComponentUuid = referenceBranchComponentUuid;
- this.newCodeReferenceBranchComponentUuids = newCodeReferenceBranchComponentUuids;
- this.periodHolder = periodHolder;
+ this.originalFileResolver = originalFileResolver;
}
public Optional<DbScmInfo> getScmInfo(Component file) {
- Optional<String> uuid = getFileUUid(file);
+ Optional<String> uuid = originalFileResolver.getFileUuid(file);
if (uuid.isEmpty()) {
return Optional.empty();
}
@@ -73,35 +55,4 @@ public class ScmInfoDbLoader {
}
}
- private Optional<String> getFileUUid(Component file) {
- if (!analysisMetadataHolder.isFirstAnalysis() && !analysisMetadataHolder.isPullRequest() && !isReferenceBranch()) {
- Optional<MovedFilesRepository.OriginalFile> originalFile = movedFilesRepository.getOriginalFile(file);
- if (originalFile.isPresent()) {
- return originalFile.map(MovedFilesRepository.OriginalFile::uuid);
- }
- return Optional.of(file.getUuid());
- }
-
- if (isReferenceBranch()) {
- var referencedBranchComponentUuid = newCodeReferenceBranchComponentUuids.getComponentUuid(file.getKey());
- if (referencedBranchComponentUuid != null) {
- return Optional.of(referencedBranchComponentUuid);
- }
- // no file to diff was found or missing reference branch changeset - use existing file
- return Optional.of(file.getUuid());
- }
-
- // at this point, it's the first analysis of a branch with copyFromPrevious flag true or any analysis of a PR
- Branch branch = analysisMetadataHolder.getBranch();
- if (!branch.isMain()) {
- return Optional.ofNullable(referenceBranchComponentUuid.getComponentUuid(file.getKey()));
- }
-
- return Optional.empty();
- }
-
- private boolean isReferenceBranch() {
- return periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name());
- }
-
}
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/OriginalFileResolver.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/OriginalFileResolver.java
new file mode 100644
index 00000000000..8cda1cfaf52
--- /dev/null
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/OriginalFileResolver.java
@@ -0,0 +1,93 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.source;
+
+import java.util.Optional;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
+import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
+import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
+import org.sonar.ce.task.projectanalysis.period.NewCodeReferenceBranchComponentUuids;
+import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
+
+import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
+
+/**
+ * Business logic to retrieve the uuid of a file in other branches/analysis.
+ * It supports different scenarios depending on if the file exists in the reference branch or in the new code period, or if the file was moved since the last analysis.
+ */
+public class OriginalFileResolver {
+ private final AnalysisMetadataHolder analysisMetadataHolder;
+ private final MovedFilesRepository movedFilesRepository;
+ private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
+ private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids;
+ private final PeriodHolder periodHolder;
+
+ public OriginalFileResolver(AnalysisMetadataHolder analysisMetadataHolder,
+ MovedFilesRepository movedFilesRepository,
+ ReferenceBranchComponentUuids referenceBranchComponentUuids,
+ NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids,
+ PeriodHolder periodHolder) {
+
+ this.analysisMetadataHolder = analysisMetadataHolder;
+ this.movedFilesRepository = movedFilesRepository;
+ this.referenceBranchComponentUuids = referenceBranchComponentUuids;
+ this.newCodeReferenceBranchComponentUuids = newCodeReferenceBranchComponentUuids;
+ this.periodHolder = periodHolder;
+ }
+
+ public Optional<String> getFileUuid(Component file) {
+
+ if (analysisMetadataHolder.isPullRequest()) {
+ return Optional.ofNullable(referenceBranchComponentUuids.getComponentUuid(file.getKey()));
+ }
+
+ if (analysisMetadataHolder.isFirstAnalysis()) {
+
+ if (isNewCodePeriodReferenceBranch()) {
+
+ String componentUuidFromNewCodeReference = newCodeReferenceBranchComponentUuids.getComponentUuid(file.getKey());
+ if (componentUuidFromNewCodeReference != null) {
+ return Optional.of(componentUuidFromNewCodeReference);
+ }
+
+ }
+
+ String componentUuidFromReferenceBranch = referenceBranchComponentUuids.getComponentUuid(file.getKey());
+ if (componentUuidFromReferenceBranch != null) {
+ return Optional.of(componentUuidFromReferenceBranch);
+ }
+
+ }
+
+ return getOrignalFileIfMoved(file);
+
+ }
+
+ private boolean isNewCodePeriodReferenceBranch() {
+ return periodHolder.hasPeriod() && REFERENCE_BRANCH.name().equals(periodHolder.getPeriod().getMode());
+ }
+
+ private Optional<String> getOrignalFileIfMoved(Component file) {
+ return movedFilesRepository.getOriginalFile(file)
+ .map(MovedFilesRepository.OriginalFile::uuid)
+ .or(() -> Optional.ofNullable(file.getUuid()));
+ }
+}
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImpl.java
index 2d1d6c54e63..1e48cf78b66 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImpl.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImpl.java
@@ -21,16 +21,9 @@ package org.sonar.ce.task.projectanalysis.source;
import java.util.Collections;
import java.util.List;
-import java.util.Optional;
-import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.component.Component;
-import org.sonar.ce.task.projectanalysis.period.NewCodeReferenceBranchComponentUuids;
-import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
-import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
-import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.source.FileSourceDao;
public class SourceLinesDiffImpl implements SourceLinesDiff {
@@ -38,23 +31,13 @@ public class SourceLinesDiffImpl implements SourceLinesDiff {
private final DbClient dbClient;
private final FileSourceDao fileSourceDao;
private final SourceLinesHashRepository sourceLinesHash;
- private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
- private final MovedFilesRepository movedFilesRepository;
- private final AnalysisMetadataHolder analysisMetadataHolder;
- private final PeriodHolder periodHolder;
- private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids;
+ private final OriginalFileResolver originalFileResolver;
- public SourceLinesDiffImpl(DbClient dbClient, FileSourceDao fileSourceDao, SourceLinesHashRepository sourceLinesHash, ReferenceBranchComponentUuids referenceBranchComponentUuids,
- MovedFilesRepository movedFilesRepository, AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder,
- NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids) {
+ public SourceLinesDiffImpl(DbClient dbClient, FileSourceDao fileSourceDao, SourceLinesHashRepository sourceLinesHash, OriginalFileResolver originalFileResolver) {
this.dbClient = dbClient;
this.fileSourceDao = fileSourceDao;
this.sourceLinesHash = sourceLinesHash;
- this.referenceBranchComponentUuids = referenceBranchComponentUuids;
- this.movedFilesRepository = movedFilesRepository;
- this.analysisMetadataHolder = analysisMetadataHolder;
- this.periodHolder = periodHolder;
- this.newCodeReferenceBranchComponentUuids = newCodeReferenceBranchComponentUuids;
+ this.originalFileResolver = originalFileResolver;
}
@Override
@@ -67,15 +50,8 @@ public class SourceLinesDiffImpl implements SourceLinesDiff {
private List<String> getDBLines(Component component) {
try (DbSession dbSession = dbClient.openSession(false)) {
- String uuid;
- if (analysisMetadataHolder.isPullRequest()) {
- uuid = referenceBranchComponentUuids.getComponentUuid(component.getKey());
- } else if (periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name())) {
- uuid = newCodeReferenceBranchComponentUuids.getComponentUuid(component.getKey());
- } else {
- Optional<MovedFilesRepository.OriginalFile> originalFile = movedFilesRepository.getOriginalFile(component);
- uuid = originalFile.map(MovedFilesRepository.OriginalFile::uuid).orElse(component.getUuid());
- }
+
+ String uuid = originalFileResolver.getFileUuid(component).orElse(null);
if (uuid == null) {
return Collections.emptyList();
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImplTest.java
index 087e578a494..b3b175199c9 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImplTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/SourceLinesDiffImplTest.java
@@ -57,8 +57,10 @@ class SourceLinesDiffImplTest {
@RegisterExtension
private final MutableMovedFilesRepositoryRule movedFiles = new MutableMovedFilesRepositoryRule();
- private final SourceLinesDiffImpl underTest = new SourceLinesDiffImpl(dbClient, fileSourceDao, sourceLinesHash,
- referenceBranchComponentUuids, movedFiles, analysisMetadataHolder, periodHolder, newCodeReferenceBranchComponentUuids);
+ private final OriginalFileResolver originalFileResolver = new OriginalFileResolver(analysisMetadataHolder, movedFiles, referenceBranchComponentUuids,
+ newCodeReferenceBranchComponentUuids, periodHolder);
+
+ private final SourceLinesDiffImpl underTest = new SourceLinesDiffImpl(dbClient, fileSourceDao, sourceLinesHash, originalFileResolver);
private static final int FILE_REF = 1;