diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStep.java index 098e93732d74..31f4a9869852 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStep.java @@ -37,10 +37,13 @@ import org.sonar.ce.task.step.ComputationStep; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; +import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; +import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; import static org.sonar.api.utils.DateUtils.formatDateTime; @@ -91,7 +94,11 @@ private void validateTargetBranch(DbSession session) { String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get(); int moduleCount = dbClient.componentDao().countEnabledModulesByProjectUuid(session, mergeBranchUuid); if (moduleCount > 0) { - throw MessageException.of("Due to an upgrade, you need to re-analyze the target branch before analyzing this branch."); + Optional opt = dbClient.branchDao().selectByUuid(session, mergeBranchUuid); + checkState(opt.isPresent(), "Merge branch '%s' does not exist", mergeBranchUuid); + String type = analysisMetadataHolder.getBranch().getType() == BranchType.PULL_REQUEST ? "pull request" : "short-lived branch"; + throw MessageException.of(String.format( + "Due to an upgrade, you need first to re-analyze the target branch '%s' before analyzing this %s.", opt.get().getKey(), type)); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStepTest.java index acf221596ca3..ccfc54e67087 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ValidateProjectStepTest.java @@ -70,44 +70,66 @@ public class ValidateProjectStepTest { ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder); @Test - public void fail_if_pr_is_targeting_branch_with_modules() { - ComponentDto masterProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD") - .setDbKey(PROJECT_KEY); - ComponentDto branchProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "DEFG") - .setDbKey(PROJECT_KEY + ":BRANCH:branch"); - dbClient.componentDao().insert(dbTester.getSession(), masterProject); - dbClient.componentDao().insert(dbTester.getSession(), branchProject); + public void fail_if_slb_is_targeting_master_with_modules() { + ComponentDto masterProject = dbTester.components().insertMainBranch(); dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject)); setBranch(BranchType.SHORT, masterProject.uuid()); dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") - .setKey(branchProject.getDbKey()) + .setKey("branch") + .build()); + + thrown.expect(MessageException.class); + thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'master' before analyzing this short-lived branch."); + underTest.execute(new TestComputationStepContext()); + } + + @Test + public void fail_if_pr_is_targeting_branch_with_modules() { + ComponentDto masterProject = dbTester.components().insertMainBranch(); + ComponentDto mergeBranch = dbTester.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch")); + dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(mergeBranch)); + setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid()); + dbTester.getSession().commit(); + + treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") + .setKey("branch") .build()); thrown.expect(MessageException.class); - thrown.expectMessage("Due to an upgrade, you need to re-analyze the target branch before analyzing this branch."); + thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request."); underTest.execute(new TestComputationStepContext()); } @Test - public void dont_fail_if_pr_is_targeting_branch_without_modules() { - ComponentDto masterProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD") - .setDbKey(PROJECT_KEY); - ComponentDto branchProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "DEFG") - .setDbKey(PROJECT_KEY + ":BRANCH:branch"); - dbClient.componentDao().insert(dbTester.getSession(), masterProject); - dbClient.componentDao().insert(dbTester.getSession(), branchProject); + public void dont_fail_if_slb_is_targeting_branch_without_modules() { + ComponentDto masterProject = dbTester.components().insertMainBranch(); setBranch(BranchType.SHORT, masterProject.uuid()); dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") - .setKey(branchProject.getDbKey()) + .setKey("branch") .build()); underTest.execute(new TestComputationStepContext()); } + @Test + public void dont_fail_for_long_forked_from_master_with_modules() { + ComponentDto masterProject = dbTester.components().insertMainBranch(); + dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject)); + setBranch(BranchType.LONG, masterProject.uuid()); + dbTester.getSession().commit(); + + treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") + .setKey("branch") + .build()); + + underTest.execute(new TestComputationStepContext()); + } + + private void setBranch(BranchType type, @Nullable String mergeBranchUuid) { Branch branch = mock(Branch.class); when(branch.getType()).thenReturn(type);