]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16100 When reference branch strategy used and file is not under SCM, use compon...
authorJacek <jacek.poreda@sonarsource.com>
Mon, 7 Mar 2022 11:54:18 +0000 (12:54 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 11 Mar 2022 10:30:55 +0000 (10:30 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoader.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoRepositoryImpl.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java

index 9ab36aa83eab443b41bd93162f6c9b8c0e9ed46b..254cffdcc979ad3064648ed20f5c884a4a18e662 100644 (file)
@@ -27,8 +27,11 @@ 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.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.db.source.FileSourceDto;
 
 public class ScmInfoDbLoader {
@@ -38,13 +41,20 @@ public class ScmInfoDbLoader {
   private final MovedFilesRepository movedFilesRepository;
   private final DbClient dbClient;
   private final ReferenceBranchComponentUuids referenceBranchComponentUuid;
+  private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids;
+  private final PeriodHolder periodHolder;
 
-  public ScmInfoDbLoader(AnalysisMetadataHolder analysisMetadataHolder, MovedFilesRepository movedFilesRepository, DbClient dbClient,
-    ReferenceBranchComponentUuids referenceBranchComponentUuid) {
+  public ScmInfoDbLoader(AnalysisMetadataHolder analysisMetadataHolder, MovedFilesRepository movedFilesRepository,
+      DbClient dbClient,
+      ReferenceBranchComponentUuids referenceBranchComponentUuid,
+      NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids,
+      PeriodHolder periodHolder) {
     this.analysisMetadataHolder = analysisMetadataHolder;
     this.movedFilesRepository = movedFilesRepository;
     this.dbClient = dbClient;
     this.referenceBranchComponentUuid = referenceBranchComponentUuid;
+    this.newCodeReferenceBranchComponentUuids = newCodeReferenceBranchComponentUuids;
+    this.periodHolder = periodHolder;
   }
 
   public Optional<DbScmInfo> getScmInfo(Component file) {
@@ -64,7 +74,7 @@ public class ScmInfoDbLoader {
   }
 
   private Optional<String> getFileUUid(Component file) {
-    if (!analysisMetadataHolder.isFirstAnalysis() && !analysisMetadataHolder.isPullRequest()) {
+    if (!analysisMetadataHolder.isFirstAnalysis() && !analysisMetadataHolder.isPullRequest() && !isReferenceBranch()) {
       Optional<MovedFilesRepository.OriginalFile> originalFile = movedFilesRepository.getOriginalFile(file);
       if (originalFile.isPresent()) {
         return originalFile.map(MovedFilesRepository.OriginalFile::getUuid);
@@ -72,6 +82,10 @@ public class ScmInfoDbLoader {
       return Optional.of(file.getUuid());
     }
 
+    if (isReferenceBranch()) {
+      return Optional.ofNullable(newCodeReferenceBranchComponentUuids.getComponentUuid(file.getDbKey()));
+    }
+
     // 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()) {
@@ -81,4 +95,8 @@ public class ScmInfoDbLoader {
     return Optional.empty();
   }
 
+  private boolean isReferenceBranch() {
+    return periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name());
+  }
+
 }
index 19b0111940b883e342f54b0935585df960d70efb..679fea498d424babda1250183561c10eab458621 100644 (file)
@@ -113,7 +113,7 @@ public class ScmInfoRepositoryImpl implements ScmInfoRepository {
    */
   private Optional<ScmInfo> generateAndMergeDb(Component file, boolean keepAuthorAndRevision) {
     Optional<DbScmInfo> dbInfoOpt = scmInfoDbLoader.getScmInfo(file);
-    if (!dbInfoOpt.isPresent()) {
+    if (dbInfoOpt.isEmpty()) {
       return generateScmInfoForAllFile(file);
     }
 
index 04437354c97dcae8e7eab8ef2a2386466ed7e072..fdd502b82fb5a2c7398bcaa7b3840b0c13327ad4 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
 import javax.annotation.Nullable;
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
@@ -36,10 +37,14 @@ import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
 import org.sonar.ce.task.projectanalysis.component.Component;
 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
 import org.sonar.ce.task.projectanalysis.filemove.MutableMovedFilesRepositoryRule;
+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.core.hash.SourceHashComputer;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchType;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.db.protobuf.DbFileSources;
 import org.sonar.db.source.FileSourceDto;
 
@@ -69,11 +74,20 @@ public class ScmInfoDbLoaderTest {
   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
   @Rule
   public MutableMovedFilesRepositoryRule movedFiles = new MutableMovedFilesRepositoryRule();
+  @Rule
+  public PeriodHolderRule periodHolder = new PeriodHolderRule();
+
+  private final Branch branch = mock(Branch.class);
+  private final ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
+  private final NewCodeReferenceBranchComponentUuids newCodeReferenceBranchComponentUuids = mock(NewCodeReferenceBranchComponentUuids.class);
 
-  private Branch branch = mock(Branch.class);
-  private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
+  private final ScmInfoDbLoader underTest = new ScmInfoDbLoader(analysisMetadataHolder, movedFiles, dbTester.getDbClient(), referenceBranchComponentUuids,
+      newCodeReferenceBranchComponentUuids, periodHolder);
 
-  private ScmInfoDbLoader underTest = new ScmInfoDbLoader(analysisMetadataHolder, movedFiles, dbTester.getDbClient(), referenceBranchComponentUuids);
+  @Before
+  public void before() {
+    periodHolder.setPeriod(new Period(NewCodePeriodType.PREVIOUS_VERSION.name(), null, null));
+  }
 
   @Test
   public void returns_ScmInfo_from_DB() {
@@ -126,6 +140,27 @@ public class ScmInfoDbLoaderTest {
     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'targetBranchFileUuid'");
   }
 
+  @Test
+  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);
+
+    String targetBranchFileUuid = "targetBranchFileUuid";
+    String hash = computeSourceHash(1);
+
+    when(newCodeReferenceBranchComponentUuids.getComponentUuid(FILE.getDbKey())).thenReturn(targetBranchFileUuid);
+    addFileSourceInDb("henry", DATE_1, "rev-1", hash, targetBranchFileUuid);
+
+    DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
+    assertThat(scmInfo.getAllChangesets()).hasSize(1);
+    assertThat(scmInfo.fileHash()).isEqualTo(hash);
+    assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'targetBranchFileUuid'");
+  }
+
   @Test
   public void return_empty_if_no_dto_available() {
     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);