@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();
}
}