@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String pullRequestId = request.mandatoryParam(PARAM_PULL_REQUEST); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto pullRequest = dbClient.branchDao().selectByPullRequestKey(dbSession, project.uuid(), pullRequestId) .filter(branch -> branch.getBranchType() == PULL_REQUEST) .orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' is not found for project '%s'", pullRequestId, projectKey))); ComponentDto branchComponent = componentFinder.getByKeyAndPullRequest(dbSession, projectKey, pullRequest.getKey()); componentCleanerService.deleteBranch(dbSession, branchComponent); response.noContent(); } }
@Test public void selectByPullRequestKey() { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); mainBranch.setBranchType(BranchType.LONG); mainBranch.setKey("master"); underTest.insert(dbSession, mainBranch); String pullRequestId = "123"; BranchDto pullRequest = new BranchDto(); pullRequest.setProjectUuid("U1"); pullRequest.setUuid("U2"); pullRequest.setBranchType(BranchType.PULL_REQUEST); pullRequest.setKey(pullRequestId); pullRequest.setMergeBranchUuid("U3"); underTest.insert(dbSession, pullRequest); // select the feature branch BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", pullRequestId).get(); assertThat(loaded.getUuid()).isEqualTo(pullRequest.getUuid()); assertThat(loaded.getKey()).isEqualTo(pullRequest.getKey()); assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid()); assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType()); assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid()); // select a branch on another project with same branch name assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty(); }
underTest.upsert(dbSession, dto); BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get(); assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3"); assertThat(loaded.getProjectUuid()).isEqualTo("U1");
BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", "foo").get(); assertThat(loaded.getMergeBranchUuid()).isEqualTo(mergeBranchUuid); assertThat(loaded.getProjectUuid()).isEqualTo("U1");
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String projectKey = request.mandatoryParam(PARAM_PROJECT); String pullRequestId = request.mandatoryParam(PARAM_PULL_REQUEST); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey); checkPermission(project); BranchDto pullRequest = dbClient.branchDao().selectByPullRequestKey(dbSession, project.uuid(), pullRequestId) .filter(branch -> branch.getBranchType() == PULL_REQUEST) .orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' is not found for project '%s'", pullRequestId, projectKey))); ComponentDto branchComponent = componentFinder.getByKeyAndPullRequest(dbSession, projectKey, pullRequest.getKey()); componentCleanerService.deleteBranch(dbSession, branchComponent); response.noContent(); } }