@SafeVarargs public final ComponentDto insertMainBranch(OrganizationDto organization, String mainBranchName, Consumer<ComponentDto>... dtoPopulators) { ComponentDto project = newPrivateProjectDto(organization); BranchDto branchDto = newBranchDto(project, LONG).setKey(mainBranchName); Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(project)); insertComponent(project); dbClient.branchDao().insert(dbSession, branchDto); db.commit(); return project; }
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 public void non_main_branches_are_not_indexed_during_analysis() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); underTest.indexOnAnalysis(branch.uuid()); assertThat(es.countDocuments(INDEX_TYPE_PROJECT_MEASURES)).isEqualTo(0); }
@Test public void indexOnStartup_does_not_index_non_main_branches() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project); }
@Test public void indexOnStartup_ignores_non_main_branches() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project); }
@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 bulk_updateKey_on_branch_containing_slash() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch/with/slash")); String newKey = "newKey"; underTest.bulkUpdateKey(dbSession, project.uuid(), project.getKey(), newKey, t -> true); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, newKey)).hasSize(1); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, ComponentDto.generateBranchKey(newKey, branch.getBranch()))).hasSize(1); }
@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 get_by_key_and_branch() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); ComponentDto module = db.components().insertComponent(newModuleDto(branch)); ComponentDto directory = db.components().insertComponent(newDirectory(module, "scr")); ComponentDto file = db.components().insertComponent(newFileDto(module)); assertThat(underTest.getByKeyAndBranch(dbSession, project.getKey(), "my_branch").uuid()).isEqualTo(branch.uuid()); assertThat(underTest.getByKeyAndBranch(dbSession, module.getKey(), "my_branch").uuid()).isEqualTo(module.uuid()); assertThat(underTest.getByKeyAndBranch(dbSession, file.getKey(), "my_branch").uuid()).isEqualTo(file.uuid()); assertThat(underTest.getByKeyAndBranch(dbSession, directory.getKey(), "my_branch").uuid()).isEqualTo(directory.uuid()); }
@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()); }
@Before public void setUp() { underTest = new MergeBranchComponentUuids(analysisMetadataHolder, db.getDbClient()); analysisMetadataHolder.setBranch(branch); ComponentDto project = db.components().insertMainBranch(); mergeBranch = db.components().insertProjectBranch(project, b -> b.setKey("mergeBranch")); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1")); mergeBranchFile = ComponentTesting.newFileDto(mergeBranch, null, "file").setUuid("mergeFile"); branchFile = ComponentTesting.newFileDto(branch, null, "file").setUuid("file1"); db.components().insertComponents(mergeBranchFile, branchFile); }
@Test public void delete_branch() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1")); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); tester.newRequest() .setParam("project", project.getKey()) .setParam("branch", "branch1") .execute(); verifyDeletedKey(branch.getDbKey()); verify(projectLifeCycleListeners).onProjectBranchesDeleted(singleton(Project.from(project))); }
public ComponentAndBranch insertMainBranch(OrganizationDto organization) { ComponentDto project = newPrivateProjectDto(organization); BranchDto branch = newBranchDto(project, LONG).setKey("master"); dbTester.components().insertComponent(project); dbClient.branchDao().insert(dbTester.getSession(), branch); dbTester.commit(); return new ComponentAndBranch(project, branch); }
@Test public void fail_if_branch_does_not_exist() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); expectedException.expect(NotFoundException.class); expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", project.getKey(), "another_branch")); ws.newRequest() .setParam(PARAM_COMPONENT, project.getKey()) .setParam(PARAM_BRANCH, "another_branch") .execute(); }
@Test public void fail_if_branch_does_not_exist() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); expectedException.expect(NotFoundException.class); expectedException.expectMessage(format("Component '%s' on branch '%s' not found", project.getKey(), "another_branch")); call(SearchRequest.builder() .setProject(project.getKey()) .setBranch("another_branch") .build()); }
@Test public void branch_issues_are_ignored_when_no_branch_param() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); IssueDoc projectIssue = newDoc(project); IssueDoc branchIssue = newDoc(branch); indexIssues(projectIssue, branchIssue); assertThatSearchReturnsOnly(IssueQuery.builder(), projectIssue.key()); }
@Test public void fail_when_componentId_and_branch_params_are_used_together() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Parameter 'componentId' cannot be used at the same time as 'branch' or 'pullRequest'"); ws.newRequest() .setParam(PARAM_COMPONENT_ID, branch.uuid()) .setParam(PARAM_BRANCH, "my_branch") .execute(); }
@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()); }
private ComponentDto setUpProjectWithBranch() { ComponentDto project = newPrivateProjectDto(newOrganizationDto()); ComponentDto branch = newProjectBranch(project, newBranchDto(project).setKey(BRANCH_NAME)); ComponentDto file = newFileDto(branch); treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren( builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build()); return branch; }