@CheckForNull private ComponentDto loadComponent(DbSession session, String componentKey, @Nullable String branch, @Nullable String pullRequest) { if (branch != null) { return componentDao.selectByKeyAndBranch(session, componentKey, branch).orElse(null); } else if (pullRequest != null) { return componentDao.selectByKeyAndPullRequest(session, componentKey, pullRequest).orElse(null); } else { return componentDao.selectByKey(session, componentKey).orElse(null); } }
/** * Components of the main branch won't be found */ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) { Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch)); }
@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 selectByKeyAndPullRequest() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setKey("my_PR").setBranchType(PULL_REQUEST)); ComponentDto pullRequestNamedAsMainBranch = db.components().insertProjectBranch(project, b -> b.setKey("master").setBranchType(PULL_REQUEST)); ComponentDto pullRequestNamedAsBranch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch").setBranchType(PULL_REQUEST)); ComponentDto file = db.components().insertComponent(newFileDto(pullRequest)); assertThat(underTest.selectByKeyAndPullRequest(dbSession, project.getKey(), "my_PR").get().uuid()).isEqualTo(pullRequest.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(project.uuid()); assertThat(underTest.selectByKeyAndPullRequest(dbSession, project.getKey(), "master").get().uuid()).isEqualTo(pullRequestNamedAsMainBranch.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid()); assertThat(underTest.selectByKeyAndPullRequest(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(pullRequestNamedAsBranch.uuid()); assertThat(underTest.selectByKeyAndPullRequest(dbSession, file.getKey(), "my_PR").get().uuid()).isEqualTo(file.uuid()); assertThat(underTest.selectByKeyAndPullRequest(dbSession, "unknown", "my_branch")).isNotPresent(); assertThat(underTest.selectByKeyAndPullRequest(dbSession, file.getKey(), "unknown")).isNotPresent(); }
@Test public void select_projects_by_name_ignore_branches() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project1 = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setName("project1")); ComponentDto module1 = db.components().insertComponent(newModuleDto(project1).setName("project1")); ComponentDto subModule1 = db.components().insertComponent(newModuleDto(module1).setName("project1")); db.components().insertComponent(newFileDto(subModule1).setName("project1")); db.components().insertProjectBranch(project1, b -> b.setKey("branch1")); // check that branch is present with same name as main branch assertThat(underTest.selectByKeyAndBranch(dbSession, project1.getKey(), "branch1").get().name()).isEqualTo("project1"); // branch is not returned assertThat(underTest.selectProjectsByNameQuery(dbSession, null, false)).extracting(ComponentDto::uuid) .containsOnly(project1.uuid()); assertThat(underTest.selectProjectsByNameQuery(dbSession, "project", false)).extracting(ComponentDto::uuid) .containsOnly(project1.uuid()); }
/** * Components of the main branch won't be found */ public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) { java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch); if (componentDto.isPresent() && componentDto.get().isEnabled()) { return componentDto.get(); } throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch)); }
private Duplication createDuplication(Map<String, ComponentDto> componentsByKey, @Nullable String branch, @Nullable String pullRequest, String from, String size, String componentDbKey, DbSession session) { String componentKey = convertToKey(componentDbKey); ComponentDto component = componentsByKey.get(componentKey); if (component == null) { Optional<ComponentDto> componentDtoOptional; if (branch != null) { componentDtoOptional = Optional.fromNullable(componentDao.selectByKeyAndBranch(session, componentKey, branch).orElseGet(null)); } else if (pullRequest != null) { componentDtoOptional = Optional.fromNullable(componentDao.selectByKeyAndPullRequest(session, componentKey, pullRequest).orElseGet(null)); } else { componentDtoOptional = componentDao.selectByKey(session, componentKey); } component = componentDtoOptional.isPresent() ? componentDtoOptional.get() : null; componentsByKey.put(componentKey, component); } return new Duplication(component, Integer.valueOf(from), Integer.valueOf(size)); }