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 {
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) {
}
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);
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()) {
return Optional.empty();
}
+ private boolean isReferenceBranch() {
+ return periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name());
+ }
+
}
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;
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;
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() {
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);