@PreAuthorize("hasPermission(#branchId, 'branch', 'admin')")
@RequestMapping(value = "/branches/{branchId}", method = RequestMethod.DELETE)
@ResponseBody
public DeleteBranchResponse deleteBranch(@PathVariable String branchId) throws IOException, GitAPIException, CannotDeleteCurrentBranchException, NotFound {
MetaGraphTx tx = metaGraphService.newTransaction();
try {
BranchMetadata branchMetadata = tx.getBranch(branchId);
if (branchMetadata == null) {
throw new NotFound(BranchMetadata.class, branchId);
}
ProjectMetadata projectMetadata = branchMetadata.getProject();
if (projectMetadata == null) {
throw new NotFound(ProjectMetadata.class);
}
Git git = historyService.projectGitRepository(projectMetadata);
String branchName = branchMetadata.getName();
tx.deleteBranch(branchMetadata);
try {
git.branchDelete()
.setBranchNames(branchName)
.call();
} finally {
git.close();
}
} catch (Throwable e) {
tx.rollback();
throw e;
}
tx.commit();
return new DeleteBranchResponse();
}