Skip to content

Commit

Permalink
SONAR-11548 Fail CE if SLB/PR targets a branch containing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and sonartech committed Jan 16, 2019
1 parent 51a1697 commit 83b2d3b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
Expand Up @@ -37,10 +37,13 @@
import org.sonar.ce.task.step.ComputationStep; import org.sonar.ce.task.step.ComputationStep;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; 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.ComponentDao;
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.SnapshotDto;


import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format; import static java.lang.String.format;
import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.utils.DateUtils.formatDateTime;


Expand Down Expand Up @@ -91,7 +94,11 @@ private void validateTargetBranch(DbSession session) {
String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get(); String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get();
int moduleCount = dbClient.componentDao().countEnabledModulesByProjectUuid(session, mergeBranchUuid); int moduleCount = dbClient.componentDao().countEnabledModulesByProjectUuid(session, mergeBranchUuid);
if (moduleCount > 0) { if (moduleCount > 0) {
throw MessageException.of("Due to an upgrade, you need to re-analyze the target branch before analyzing this branch."); Optional<BranchDto> 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));
} }
} }


Expand Down
Expand Up @@ -70,44 +70,66 @@ public class ValidateProjectStepTest {
ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder); ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);


@Test @Test
public void fail_if_pr_is_targeting_branch_with_modules() { public void fail_if_slb_is_targeting_master_with_modules() {
ComponentDto masterProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD") ComponentDto masterProject = dbTester.components().insertMainBranch();
.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);
dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject)); dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
setBranch(BranchType.SHORT, masterProject.uuid()); setBranch(BranchType.SHORT, masterProject.uuid());
dbTester.getSession().commit(); dbTester.getSession().commit();


treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") 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()); .build());


thrown.expect(MessageException.class); 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()); underTest.execute(new TestComputationStepContext());
} }


@Test @Test
public void dont_fail_if_pr_is_targeting_branch_without_modules() { public void dont_fail_if_slb_is_targeting_branch_without_modules() {
ComponentDto masterProject = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD") ComponentDto masterProject = dbTester.components().insertMainBranch();
.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);
setBranch(BranchType.SHORT, masterProject.uuid()); setBranch(BranchType.SHORT, masterProject.uuid());
dbTester.getSession().commit(); dbTester.getSession().commit();


treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG") treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
.setKey(branchProject.getDbKey()) .setKey("branch")
.build()); .build());


underTest.execute(new TestComputationStepContext()); 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) { private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
Branch branch = mock(Branch.class); Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(type); when(branch.getType()).thenReturn(type);
Expand Down

0 comments on commit 83b2d3b

Please sign in to comment.