@Test public void insert_branch_with_all_fields_and_max_length_values() { BranchDto dto = new BranchDto(); dto.setProjectUuid(repeat("a", 50)); dto.setUuid(repeat("b", 50)); dto.setBranchType(BranchType.SHORT); dto.setKey(repeat("c", 255)); dto.setMergeBranchUuid(repeat("d", 50)); underTest.insert(dbSession, dto); Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'"); assertThat((String) map.get("projectUuid")).contains("a").isEqualTo(dto.getProjectUuid()); assertThat((String) map.get("uuid")).contains("b").isEqualTo(dto.getUuid()); assertThat((String) map.get("kee")).contains("c").isEqualTo(dto.getKey()); assertThat((String) map.get("mergeBranchUuid")).contains("d").isEqualTo(dto.getMergeBranchUuid()); }
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; }
@Test public void upsert_branch() { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); dto.setBranchType(BranchType.LONG); dto.setKey("foo"); underTest.insert(dbSession, dto); // the fields that can be updated dto.setMergeBranchUuid("U3"); // the fields that can't be updated. New values are ignored. dto.setProjectUuid("ignored"); dto.setBranchType(BranchType.SHORT); underTest.upsert(dbSession, dto); BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "foo").get(); assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3"); assertThat(loaded.getProjectUuid()).isEqualTo("U1"); assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG); }
@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 status_on_short_living_branch_with_no_issue() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(org.sonar.db.component.BranchType.LONG)); db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid())); issueIndexer.indexOnStartup(emptySet()); permissionIndexerTester.allowOnlyAnyone(project); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList().stream().filter(b -> b.getType().equals(BranchType.SHORT)).map(ProjectBranches.Branch::getStatus)) .extracting(Status::getBugs, Status::getVulnerabilities, Status::getCodeSmells) .containsExactlyInAnyOrder(tuple(0L, 0L, 0L)); }
protected BranchDto toBranchDto(ComponentDto componentDto, Branch branch) { BranchDto dto = new BranchDto(); dto.setUuid(componentDto.uuid()); // MainBranchProjectUuid will be null if it's a main branch dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); dto.setBranchType(branch.getType()); // merge branch is only present if it's a short living branch dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); if (branch.getType() == BranchType.PULL_REQUEST) { dto.setKey(analysisMetadataHolder.getPullRequestKey()); DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder() .setBranch(branch.getName()) .setTitle(branch.getName()) .build(); dto.setPullRequestData(pullRequestData); } else { dto.setKey(branch.getName()); } return dto; }
@Test public void status_on_pull_request_with_no_issue() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(UserRole.USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG)); db.components().insertProjectBranch(project, b -> b.setKey("pr-123") .setBranchType(PULL_REQUEST) .setMergeBranchUuid(longLivingBranch.uuid()) .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); issueIndexer.indexOnStartup(emptySet()); permissionIndexerTester.allowOnlyAnyone(project); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList().stream().map(PullRequest::getStatus)) .extracting(Status::getBugs, Status::getVulnerabilities, Status::getCodeSmells) .containsExactlyInAnyOrder(tuple(0L, 0L, 0L)); }
@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 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 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 does_not_fail_when_only_scan_permission_on_project() { 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(SCAN_EXECUTION, project); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList()) .extracting(PullRequest::getKey) .containsExactlyInAnyOrder("123"); }
@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 does_not_fail_when_only_browse_permission_on_project() { 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(UserRole.USER, project); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList()) .extracting(PullRequest::getKey) .containsExactlyInAnyOrder("123"); }
@Test public void base_branch_is_using_default_main_name_when_main_branch_has_no_name() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(UserRole.USER, project); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setKey("pr-123") .setBranchType(PULL_REQUEST) .setMergeBranchUuid(project.uuid()) .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder() .setBranch("feature123").build())); ListWsResponse response = ws.newRequest() .setParam("project", pullRequest.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequests(0)) .extracting(PullRequest::getKey, PullRequest::getBase) .containsExactlyInAnyOrder(pullRequest.getPullRequest(), "master"); }
@Test public void pull_request() { 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(UserRole.USER, project); ListWsResponse response = ws.newRequest() .setParam("project", project.getDbKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList()) .extracting(PullRequest::getKey, PullRequest::getBranch, PullRequest::getIsOrphan, PullRequest::hasUrl, PullRequest::hasTitle) .containsExactlyInAnyOrder(tuple("123", "feature/bar", false, false, false)); }
@Test public void short_living_branches() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("long").setBranchType(org.sonar.db.component.BranchType.LONG)); ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid())); ComponentDto shortLivingBranchOnMaster = db.components().insertProjectBranch(project, b -> b.setKey("short_on_master").setBranchType(SHORT).setMergeBranchUuid(project.uuid())); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) .containsExactlyInAnyOrder( tuple("master", BranchType.LONG, ""), tuple(longLivingBranch.getBranch(), BranchType.LONG, ""), tuple(shortLivingBranch.getBranch(), BranchType.SHORT, longLivingBranch.getBranch()), tuple(shortLivingBranchOnMaster.getBranch(), BranchType.SHORT, "master")); }
@Test public void pull_request_on_removed_branch() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(UserRole.USER, project); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setKey("pr-123") .setBranchType(PULL_REQUEST) .setMergeBranchUuid("unknown") .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList()) .extracting(PullRequest::getKey, PullRequest::hasBase, PullRequest::getIsOrphan) .containsExactlyInAnyOrder( tuple(pullRequest.getPullRequest(), false, true)); }
@Test public void does_not_fail_when_only_scan_permission_on_organization() { OrganizationDto organization = db.organizations().insert(); userSession.logIn().addPermission(OrganizationPermission.SCAN, organization); ComponentDto project = db.components().insertMainBranch(organization); db.components().insertProjectBranch(project, b -> b.setKey("123") .setBranchType(PULL_REQUEST) .setMergeBranchUuid(project.uuid()) .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getPullRequestsList()) .extracting(PullRequest::getKey) .containsExactlyInAnyOrder("123"); }
@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(); }