@Test public void isMain_is_true_if_branch_uuid_equals_project_uuid() { underTest.setProjectUuid("U1"); underTest.setUuid("U1"); assertThat(underTest.isMain()).isTrue(); }
private static ProjectBranches.Branch.Builder toBranchBuilder(BranchDto branch, Optional<BranchDto> mergeBranch) { ProjectBranches.Branch.Builder builder = ProjectBranches.Branch.newBuilder(); String branchKey = branch.getKey(); ofNullable(branchKey).ifPresent(builder::setName); builder.setIsMain(branch.isMain()); builder.setType(Common.BranchType.valueOf(branch.getBranchType().name())); if (branch.getBranchType() == SHORT) { if (mergeBranch.isPresent()) { String mergeBranchKey = mergeBranch.get().getKey(); builder.setMergeBranch(mergeBranchKey); } else { builder.setIsOrphan(true); } } return builder; }
@Test public void isMain_is_false_if_branch_uuid_does_not_equal_project_uuid() { underTest.setProjectUuid("U1"); underTest.setUuid("U2"); assertThat(underTest.isMain()).isFalse(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String newBranchName = request.mandatoryParam(PARAM_NAME); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), newBranchName); checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(), "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName); dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), newBranchName); dbSession.commit(); response.noContent(); } }
private WebhookPayload buildWebHookPayload(DbSession dbSession, QGChangeEvent event, @Nullable EvaluatedQualityGate evaluatedQualityGate) { ComponentDto project = event.getProject(); BranchDto branch = event.getBranch(); SnapshotDto analysis = event.getAnalysis(); Map<String, String> analysisProperties = dbClient.analysisPropertiesDao().selectBySnapshotUuid(dbSession, analysis.getUuid()) .stream() .collect(Collectors.toMap(AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)); String projectUuid = StringUtils.defaultString(project.getMainBranchProjectUuid(), project.projectUuid()); ProjectAnalysis projectAnalysis = new ProjectAnalysis( new Project(projectUuid, project.getKey(), project.name()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt()), new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())), evaluatedQualityGate, null, analysisProperties); return webhookPayloadFactory.create(projectAnalysis); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String branchKey = request.mandatoryParam(PARAM_BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto branch = checkFoundWithOptional( dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), branchKey), "Branch '%s' not found for project '%s'", branchKey, projectKey); if (branch.isMain()) { throw new IllegalArgumentException("Only non-main branches can be deleted"); } ComponentDto branchComponent = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey); componentCleanerService.deleteBranch(dbSession, branchComponent); projectLifeCycleListeners.onProjectBranchesDeleted(singleton(from(project))); response.noContent(); } }
private Branch getMergeBranch(String projectUuid, String branchName, BranchType branchType, @Nullable String mergeBranchName) { String mergeBranchUuid = getMergeBranchUuid(projectUuid, mergeBranchName); Optional<BranchDto> branchOpt = loadBranchByKey(projectUuid, branchName); boolean main = branchOpt.isPresent() && branchOpt.get().isMain(); return new BranchImpl(branchType, main, branchName, mergeBranchUuid); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String newBranchName = request.mandatoryParam(PARAM_NAME); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), newBranchName); checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(), "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName); dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), newBranchName); dbSession.commit(); response.noContent(); } }
private static ProjectBranches.Branch.Builder toBranchBuilder(BranchDto branch, Optional<BranchDto> mergeBranch) { ProjectBranches.Branch.Builder builder = ProjectBranches.Branch.newBuilder(); String branchKey = branch.getKey(); setNullable(branchKey, builder::setName); builder.setIsMain(branch.isMain()); builder.setType(Common.BranchType.valueOf(branch.getBranchType().name())); if (branch.getBranchType() == SHORT) { if (mergeBranch.isPresent()) { String mergeBranchKey = mergeBranch.get().getKey(); builder.setMergeBranch(mergeBranchKey); } else { builder.setIsOrphan(true); } } return builder; }
private WebhookPayload buildWebHookPayload(DbSession dbSession, QGChangeEvent event, @Nullable EvaluatedQualityGate evaluatedQualityGate) { ComponentDto project = event.getProject(); BranchDto branch = event.getBranch(); SnapshotDto analysis = event.getAnalysis(); Map<String, String> analysisProperties = dbClient.analysisPropertiesDao().selectBySnapshotUuid(dbSession, analysis.getUuid()) .stream() .collect(Collectors.toMap(AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)); String projectUuid = StringUtils.defaultString(project.getMainBranchProjectUuid(), project.projectUuid()); ProjectAnalysis projectAnalysis = new ProjectAnalysis( new Project(projectUuid, project.getKey(), project.name()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt()), new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())), evaluatedQualityGate, null, analysisProperties); return webhookPayloadFactory.create(projectAnalysis); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String branchKey = request.mandatoryParam(PARAM_BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto branch = checkFoundWithOptional( dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), branchKey), "Branch '%s' not found for project '%s'", branchKey, projectKey); if (branch.isMain()) { throw new IllegalArgumentException("Only non-main branches can be deleted"); } ComponentDto branchComponent = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey); componentCleanerService.deleteBranch(dbSession, branchComponent); response.noContent(); } }