private BranchDto loadBranch(DbSession dbSession, ComponentDto project) { return dbClient.branchDao().selectByUuid(dbSession, project.uuid()) .orElseThrow(() -> new IllegalStateException("Branch not found: " + project.uuid())); }
private void lazyInit() { if (uuidsByKey == null) { String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get(); uuidsByKey = new HashMap<>(); try (DbSession dbSession = dbClient.openSession(false)) { List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(mergeBranchUuid, dbSession); for (ComponentDto dto : components) { uuidsByKey.put(dto.getKey(), dto.uuid()); } Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, mergeBranchUuid); checkState(opt.isPresent(), "Merge branch '%s' does not exist", mergeBranchUuid); mergeBranchName = opt.get().getKey(); } } }
private void validateTargetBranch(DbSession session) { if (!analysisMetadataHolder.isShortLivingBranch() && !analysisMetadataHolder.isPullRequest()) { return; } String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get(); int moduleCount = dbClient.componentDao().countEnabledModulesByProjectUuid(session, mergeBranchUuid); if (moduleCount > 0) { 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)); } }
private void checkComponentType(DbSession dbSession, ComponentDto project) { Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbSession, project.uuid()); if (project.isPrivate()) { throw generateInvalidProjectExcpetion(); } if (branch.isPresent() && !branch.get().getBranchType().equals(LONG)) { throw generateInvalidProjectExcpetion(); } if (!project.qualifier().equals(PROJECT) && !project.qualifier().equals(APP)) { throw generateInvalidProjectExcpetion(); } }
private ComponentDto createComponent(DbSession dbSession, OrganizationDto organization, BranchSupport.ComponentKey componentKey, @Nullable String projectName) { if (componentKey.isMainBranch() || componentKey.isDeprecatedBranch()) { ComponentDto project = createProject(dbSession, organization, componentKey, projectName); componentUpdater.commitAndIndex(dbSession, project); return project; } Optional<ComponentDto> existingMainComponent = dbClient.componentDao().selectByKey(dbSession, componentKey.getKey()); ComponentDto mainComponentDto = existingMainComponent .orElseGet(() -> createProject(dbSession, organization, componentKey.getMainBranchComponentKey(), projectName)); BranchDto mainComponentBranchDto = dbClient.branchDao().selectByUuid(dbSession, mainComponentDto.uuid()) .orElseThrow(() -> new IllegalStateException("Branch of main component does not exist")); ComponentDto branchComponent = branchSupport.createBranchComponent(dbSession, componentKey, organization, mainComponentDto, mainComponentBranchDto); if (existingMainComponent.isPresent()) { dbSession.commit(); } else { componentUpdater.commitAndIndex(dbSession, mainComponentDto); } return branchComponent; }
@Test public void selectByUuid() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch1 = db.components().insertProjectBranch(project); ComponentDto branch2 = db.components().insertProjectBranch(project); assertThat(underTest.selectByUuid(db.getSession(), branch1.uuid()).get()) .extracting(BranchDto::getUuid) .containsExactlyInAnyOrder(branch1.uuid()); assertThat(underTest.selectByUuid(db.getSession(), project.uuid())).isNotPresent(); assertThat(underTest.selectByUuid(db.getSession(), "unknown")).isNotPresent(); }
BranchDto exitingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), nonExistingProject.uuid()).get(); verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(organization), eq(nonExistingProject), eq(exitingProjectMainBranch)); verify(branchSupportDelegate).createComponentKey(nonExistingProject.getDbKey(), randomCharacteristics);
@Test public void insert_pull_request_branch_with_only_non_null_fields() { String projectUuid = "U1"; String uuid = "U2"; BranchType branchType = BranchType.PULL_REQUEST; String kee = "123"; BranchDto dto = new BranchDto(); dto.setProjectUuid(projectUuid); dto.setUuid(uuid); dto.setBranchType(branchType); dto.setKey(kee); underTest.insert(dbSession, dto); BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get(); assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid); assertThat(loaded.getUuid()).isEqualTo(uuid); assertThat(loaded.getBranchType()).isEqualTo(branchType); assertThat(loaded.getKey()).isEqualTo(kee); assertThat(loaded.getMergeBranchUuid()).isNull(); assertThat(loaded.getPullRequestData()).isNull(); }
@Test public void submit_a_report_on_missing_branch_but_existing_project() { OrganizationDto organization = db.organizations().insert(); ComponentDto existingProject = db.components().insertMainBranch(organization); BranchDto exitingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), existingProject.uuid()).get(); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(SCAN_EXECUTION, existingProject); Map<String, String> randomCharacteristics = randomNonEmptyMap(); ComponentDto createdBranch = createButDoNotInsertBranch(existingProject); BranchSupport.ComponentKey componentKey = createComponentKeyOfBranch(createdBranch); when(branchSupportDelegate.createComponentKey(existingProject.getDbKey(), randomCharacteristics)) .thenReturn(componentKey); when(branchSupportDelegate.createBranchComponent(any(DbSession.class), same(componentKey), eq(organization), eq(existingProject), eq(exitingProjectMainBranch))) .thenReturn(createdBranch); InputStream reportInput = IOUtils.toInputStream("{binary}", StandardCharsets.UTF_8); String taskUuid = mockSuccessfulPrepareSubmitCall(); underTest.submit(organization.getKey(), existingProject.getDbKey(), null, existingProject.name(), randomCharacteristics, reportInput); verifyZeroInteractions(permissionTemplateService); verifyZeroInteractions(favoriteUpdater); verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(organization), eq(existingProject), eq(exitingProjectMainBranch)); verify(branchSupportDelegate).createComponentKey(existingProject.getDbKey(), randomCharacteristics); verify(branchSupportDelegate).createBranchComponent(any(DbSession.class), same(componentKey), eq(organization), eq(existingProject), eq(exitingProjectMainBranch)); verifyNoMoreInteractions(branchSupportDelegate); verify(componentUpdater, times(0)).commitAndIndex(any(), any()); verifyQueueSubmit(existingProject, createdBranch, user, randomCharacteristics, taskUuid); }
@Test public void create_application() { NewComponent application = NewComponent.newComponentBuilder() .setKey("app-key") .setName("app-name") .setQualifier(APP) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); ComponentDto returned = underTest.create(db.getSession(), application, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.getDbKey()).isEqualTo("app-key"); assertThat(loaded.name()).isEqualTo("app-name"); assertThat(loaded.qualifier()).isEqualTo("APP"); assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue(); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isPresent(); assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); assertThat(branch.get().getMergeBranchUuid()).isNull(); assertThat(branch.get().getBranchType()).isEqualTo(BranchType.LONG); assertThat(branch.get().getUuid()).isEqualTo(returned.uuid()); assertThat(branch.get().getProjectUuid()).isEqualTo(returned.uuid()); }
BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
@Test public void rename() { userSession.logIn(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch")); userSession.addProjectPermission(UserRole.ADMIN, project); tester.newRequest() .setParam("project", project.getKey()) .setParam("name", "master") .execute(); assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2); Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid()); assertThat(mainBranch.get().getKey()).isEqualTo("master"); Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid()); assertThat(unchangedBranch.get().getKey()).isEqualTo("branch"); }
Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isPresent(); assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
@Test public void create_view() { NewComponent view = NewComponent.newComponentBuilder() .setKey("view-key") .setName("view-name") .setQualifier(VIEW) .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); ComponentDto returned = underTest.create(db.getSession(), view, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.getDbKey()).isEqualTo("view-key"); assertThat(loaded.name()).isEqualTo("view-name"); assertThat(loaded.qualifier()).isEqualTo("VW"); assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue(); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isNotPresent(); }
@Test public void rename_with_same_name() { userSession.logIn(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch")); userSession.addProjectPermission(UserRole.ADMIN, project); tester.newRequest() .setParam("project", project.getKey()) .setParam("name", "master") .execute(); tester.newRequest() .setParam("project", project.getKey()) .setParam("name", "master") .execute(); assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2); Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid()); assertThat(mainBranch.get().getKey()).isEqualTo("master"); Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid()); assertThat(unchangedBranch.get().getKey()).isEqualTo("branch"); }
@Test @UseDataProvider("nullOrNotNullString") public void persist_creates_row_in_PROJECTS_BRANCHES_for_long_branch(@Nullable String mergeBranchUuid) { String branchName = "branch"; // add project and branch in table PROJECTS ComponentDto mainComponent = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), MAIN.getUuid()).setDbKey(MAIN.getKey()); ComponentDto component = ComponentTesting.newProjectBranch(mainComponent, new BranchDto().setUuid(BRANCH.getUuid()).setKey(BRANCH.getKey()).setBranchType(LONG)); dbTester.getDbClient().componentDao().insert(dbTester.getSession(), mainComponent, component); dbTester.commit(); // set project in metadata treeRootHolder.setRoot(BRANCH); analysisMetadataHolder.setBranch(createBranch(LONG, false, branchName, mergeBranchUuid)); analysisMetadataHolder.setProject(Project.from(mainComponent)); underTest.persist(dbTester.getSession()); dbTester.getSession().commit(); assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2); Optional<BranchDto> branchDto = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), BRANCH.getUuid()); assertThat(branchDto).isPresent(); assertThat(branchDto.get().getBranchType()).isEqualTo(LONG); assertThat(branchDto.get().getKey()).isEqualTo(branchName); assertThat(branchDto.get().getMergeBranchUuid()).isEqualTo(mergeBranchUuid); assertThat(branchDto.get().getProjectUuid()).isEqualTo(MAIN.getUuid()); assertThat(branchDto.get().getPullRequestData()).isNull(); }
@Test @UseDataProvider("nullOrNotNullString") public void persist_creates_row_in_PROJECTS_BRANCHES_for_pull_request(@Nullable String mergeBranchUuid) { String pullRequestId = "pr-123"; // add project and branch in table PROJECTS ComponentDto mainComponent = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), MAIN.getUuid()).setDbKey(MAIN.getKey()); ComponentDto component = ComponentTesting.newProjectBranch(mainComponent, new BranchDto().setUuid(BRANCH.getUuid()).setKey(BRANCH.getKey()).setBranchType(PULL_REQUEST)); dbTester.getDbClient().componentDao().insert(dbTester.getSession(), mainComponent, component); dbTester.commit(); // set project in metadata treeRootHolder.setRoot(BRANCH); analysisMetadataHolder.setBranch(createBranch(PULL_REQUEST, false, pullRequestId, mergeBranchUuid)); analysisMetadataHolder.setProject(Project.from(mainComponent)); analysisMetadataHolder.setPullRequestKey(pullRequestId); underTest.persist(dbTester.getSession()); dbTester.getSession().commit(); assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2); Optional<BranchDto> branchDto = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), BRANCH.getUuid()); assertThat(branchDto).isPresent(); assertThat(branchDto.get().getBranchType()).isEqualTo(PULL_REQUEST); assertThat(branchDto.get().getKey()).isEqualTo(pullRequestId); assertThat(branchDto.get().getMergeBranchUuid()).isEqualTo(mergeBranchUuid); assertThat(branchDto.get().getProjectUuid()).isEqualTo(MAIN.getUuid()); assertThat(branchDto.get().getPullRequestData()).isEqualTo(DbProjectBranches.PullRequestData.newBuilder() .setBranch(pullRequestId) .setTitle(pullRequestId) .build()); }
private Optional<BranchDto> loadMainBranchByProjectUuid(String projectUuid) { try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.branchDao().selectByUuid(dbSession, projectUuid); } }
private void lazyInit() { if (uuidsByKey == null) { String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get(); uuidsByKey = new HashMap<>(); try (DbSession dbSession = dbClient.openSession(false)) { List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(mergeBranchUuid, dbSession); for (ComponentDto dto : components) { uuidsByKey.put(dto.getKey(), dto.uuid()); } Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, mergeBranchUuid); checkState(opt.isPresent(), "Merge branch '%s' does not exist", mergeBranchUuid); mergeBranchName = opt.get().getKey(); } } }
private void checkComponentType(DbSession dbSession, ComponentDto project) { Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbSession, project.uuid()); if (project.isPrivate()) { throw generateInvalidProjectExcpetion(); } if (branch.isPresent() && !branch.get().getBranchType().equals(LONG)) { throw generateInvalidProjectExcpetion(); } if (!project.qualifier().equals(PROJECT) && !project.qualifier().equals(APP)) { throw generateInvalidProjectExcpetion(); } }