private BranchDto createMainBranch(DbSession session, String componentUuid) { BranchDto branch = new BranchDto() .setBranchType(BranchType.LONG) .setUuid(componentUuid) .setKey(BranchDto.DEFAULT_MAIN_BRANCH_NAME) .setMergeBranchUuid(null) .setProjectUuid(componentUuid); dbClient.branchDao().upsert(session, branch); return branch; }
public static BranchDto newBranchDto(@Nullable String projectUuid, BranchType branchType) { String key = projectUuid == null ? null : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(Uuids.createFast()) // MainBranchProjectUuid will be null if it's a main branch .setProjectUuid(projectUuid) .setBranchType(branchType); }
@Test @UseDataProvider("branchBranchTypes") public void selectByKeyAndBranch(BranchType branchType) { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch").setBranchType(branchType)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(project.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "my_branch").get().uuid()).isEqualTo(file.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, "unknown", "my_branch")).isNotPresent(); assertThat(underTest.selectByKeyAndBranch(dbSession, file.getKey(), "unknown")).isNotPresent(); }
@Test public void fail_when_pull_request_branch_provided() { ComponentDto project = db.components().insertMainBranch(); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Either branch or pull request can be provided, not both"); assertThat(underTest.getByKeyAndOptionalBranchOrPullRequest(dbSession, project.getKey(), "pr-123", "pr-123").uuid()).isEqualTo(pullRequest.uuid()); }
@Test public void loadQualityGate_returns_hardcoded_gate_for_pull_requests() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPublicProject(organization); BranchDto pullRequest = newBranchDto(project).setBranchType(BranchType.PULL_REQUEST); db.components().insertProjectBranch(project, pullRequest); QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, pullRequest); assertThat(result).isSameAs(ShortLivingBranchQualityGate.GATE); }
@Test public void get_by_key_and_pull_request() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST).setMergeBranchUuid(project.uuid())); ComponentDto module = db.components().insertComponent(newModuleDto(branch)); ComponentDto directory = db.components().insertComponent(newDirectory(module, "scr")); ComponentDto file = db.components().insertComponent(newFileDto(module)); assertThat(underTest.getByKeyAndOptionalBranchOrPullRequest(dbSession, project.getKey(), null, "pr-123").uuid()).isEqualTo(branch.uuid()); assertThat(underTest.getByKeyAndOptionalBranchOrPullRequest(dbSession, module.getKey(), null, "pr-123").uuid()).isEqualTo(module.uuid()); assertThat(underTest.getByKeyAndOptionalBranchOrPullRequest(dbSession, file.getKey(), null, "pr-123").uuid()).isEqualTo(file.uuid()); assertThat(underTest.getByKeyAndOptionalBranchOrPullRequest(dbSession, directory.getKey(), null, "pr-123").uuid()).isEqualTo(directory.uuid()); }
@Test public void loadQualityGate_returns_hardcoded_gate_for_short_living_branches() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPublicProject(organization); BranchDto branch = newBranchDto(project).setBranchType(BranchType.SHORT); db.components().insertProjectBranch(project, branch); QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, branch); assertThat(result).isSameAs(ShortLivingBranchQualityGate.GATE); }
public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) { boolean isMain = branchComponent.getMainBranchProjectUuid() == null; String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid(); String key = isMain ? "master" : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(branchComponent.uuid()) .setProjectUuid(projectUuid) .setBranchType(branchType); }
@Test public void return_error_on_not_existing_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); TestResponse response = ws.newRequest() .setParam("project", branch.getKey()) .setParam("branch", "unknown") .execute(); checkError(response, "Project has not been found"); }
@Test public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid(project.uuid())); ProjectBranches.ShowWsResponse response = ws.newRequest() .setParam("project", shortLivingBranch.getKey()) .executeProtobuf(ProjectBranches.ShowWsResponse.class); assertThat(response.getBranch()) .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), BranchType.SHORT, "master"); }
@Test public void short_living_branch_on_removed_branch() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid("unknown")); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) .extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch, Branch::getIsOrphan) .containsExactlyInAnyOrder( tuple("master", BranchType.LONG, false, false), tuple(shortLivingBranch.getBranch(), BranchType.SHORT, false, true)); }
@Test public void return_error_on_short_living_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); ComponentDto shortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT)); TestResponse response = ws.newRequest() .setParam("project", shortBranch.getKey()) .setParam("branch", shortBranch.getBranch()) .execute(); checkError(response, "Project is invalid"); }
@Test public void delete_pull_request() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("1984").setBranchType(PULL_REQUEST)); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); ws.newRequest() .setParam("project", project.getKey()) .setParam("pullRequest", "1984") .execute(); verifyDeletedKey(branch.getDbKey()); }
@Test public void fail_if_both_componentId_and_pull_request_parameters_provided() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Parameter 'componentId' cannot be used at the same time as 'branch' or 'pullRequest'"); ws.newRequest() .setParam("uuid", file.uuid()) .setParam("pullRequest", file.getPullRequest()) .execute(); }
@Test public void should_find_components_with_issues_to_merge_on_derived_pull_request() { ComponentDto project = db.components().insertMainBranch(); setRootUuid(project.uuid()); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(project.uuid())); RuleDefinitionDto rule = db.rules().insert(); ComponentDto fileWithResolvedIssue = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null)); db.issues().insertIssue(IssueTesting.newIssue(rule, pullRequest, fileWithResolvedIssue).setStatus("RESOLVED")); underTest = new ShortBranchComponentsWithIssues(treeRootHolder, db.getDbClient()); assertThat(underTest.getUuids(fileWithResolvedIssue.getKey())).hasSize(1); }
@Test public void status_on_long_living_branch() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(org.sonar.db.component.BranchType.LONG)); db.measures().insertLiveMeasure(branch, qualityGateStatus, m -> m.setData("OK")); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) .extracting(b -> b.getStatus().hasQualityGateStatus(), b -> b.getStatus().getQualityGateStatus()) .containsExactlyInAnyOrder(tuple(false, ""), tuple(true, "OK")); }
@Test public void should_find_components_with_issues_to_merge_on_derived_short() { ComponentDto project = db.components().insertMainBranch(); setRootUuid(project.uuid()); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.SHORT), b -> b.setMergeBranchUuid(project.uuid())); RuleDefinitionDto rule = db.rules().insert(); ComponentDto fileWithResolvedIssue = db.components().insertComponent(ComponentTesting.newFileDto(branch, null)); db.issues().insertIssue(IssueTesting.newIssue(rule, branch, fileWithResolvedIssue).setStatus("RESOLVED")); underTest = new ShortBranchComponentsWithIssues(treeRootHolder, db.getDbClient()); assertThat(underTest.getUuids(fileWithResolvedIssue.getKey())).hasSize(1); }
@Test public void should_not_find_components_with_issues_to_merge_on_derived_long() { ComponentDto project = db.components().insertMainBranch(); setRootUuid(project.uuid()); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG), b -> b.setMergeBranchUuid(project.uuid())); RuleDefinitionDto rule = db.rules().insert(); ComponentDto fileWithResolvedIssue = db.components().insertComponent(ComponentTesting.newFileDto(branch, null)); db.issues().insertIssue(IssueTesting.newIssue(rule, branch, fileWithResolvedIssue).setStatus("RESOLVED")); underTest = new ShortBranchComponentsWithIssues(treeRootHolder, db.getDbClient()); assertThat(underTest.getUuids(fileWithResolvedIssue.getKey())).isEmpty(); }
@Test public void return_error_if_branch_does_not_exist() throws ParseException { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG)); userSession.addProjectPermission(USER, project); MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY)); TestResponse response = ws.newRequest() .setParam("project", branch.getKey()) .setParam("branch", "unknown") .setParam("metric", metric.getKey()) .execute(); checkError(response, "Project has not been found"); }
@Test public void fail_when_not_having_right_permission() { ComponentDto project = db.components().insertMainBranch(); db.components().insertProjectBranch(project, b -> b.setKey("123") .setBranchType(PULL_REQUEST) .setMergeBranchUuid(project.uuid()) .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); userSession.logIn().addProjectPermission(CODEVIEWER, project); expectedException.expect(ForbiddenException.class); ws.newRequest() .setParam("project", project.getDbKey()) .execute(); }