diff --git a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala index 2ab0e96..2fe7805 100644 --- a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala +++ b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala @@ -97,7 +97,9 @@ diffs, isEditable(repository), isManageable(repository), + hasDeveloperRole(pullreq.requestUserName, pullreq.requestRepositoryName, context.loginAccount), repository, + getRepository(pullreq.requestUserName, pullreq.requestRepositoryName), flash.toMap.map(f => f._1 -> f._2.toString)) } } @@ -138,19 +140,33 @@ } getOrElse NotFound() }) - get("/:owner/:repository/pull/:id/delete/*")(writableUsersOnly { repository => - params("id").toIntOpt.map { issueId => - val branchName = multiParams("splat").head - val userName = context.loginAccount.get.userName - if(repository.repository.defaultBranch != branchName){ - using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => - git.branchDelete().setForce(true).setBranchNames(branchName).call() - recordDeleteBranchActivity(repository.owner, repository.name, userName, branchName) + get("/:owner/:repository/pull/:id/delete_branch")(readableUsersOnly { baseRepository => + (for { + issueId <- params("id").toIntOpt + loginAccount <- context.loginAccount + (issue, pullreq) <- getPullRequest(baseRepository.owner, baseRepository.name, issueId) + owner = pullreq.requestUserName + name = pullreq.requestRepositoryName + if hasDeveloperRole(owner, name, context.loginAccount) + } yield { + val repository = getRepository(owner, name).get + val branchProtection = getProtectedBranchInfo(owner, name, pullreq.requestBranch) + if(branchProtection.enabled){ + flash += "error" -> s"branch ${pullreq.requestBranch} is protected." + } else { + if(repository.repository.defaultBranch != pullreq.requestBranch){ + val userName = context.loginAccount.get.userName + using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + git.branchDelete().setForce(true).setBranchNames(pullreq.requestBranch).call() + recordDeleteBranchActivity(repository.owner, repository.name, userName, pullreq.requestBranch) + } + createComment(baseRepository.owner, baseRepository.name, userName, issueId, pullreq.requestBranch, "delete_branch") + } else { + flash += "error" -> s"""Can't delete the default branch "${pullreq.requestBranch}".""" } } - createComment(repository.owner, repository.name, userName, issueId, branchName, "delete_branch") - redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}") - } getOrElse NotFound() + redirect(s"/${baseRepository.owner}/${baseRepository.name}/pull/${issueId}") + }) getOrElse NotFound() }) post("/:owner/:repository/pull/:id/update_branch")(readableUsersOnly { baseRepository => diff --git a/src/main/twirl/gitbucket/core/pulls/conversation.scala.html b/src/main/twirl/gitbucket/core/pulls/conversation.scala.html index 1785c89..f29d9de 100644 --- a/src/main/twirl/gitbucket/core/pulls/conversation.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/conversation.scala.html @@ -7,7 +7,9 @@ labels: List[gitbucket.core.model.Label], isEditable: Boolean, isManageable: Boolean, - repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context) + isManageableForkedRepository: Boolean, + repository: gitbucket.core.service.RepositoryService.RepositoryInfo, + forkedRepository: Option[gitbucket.core.service.RepositoryService.RepositoryInfo])(implicit context: gitbucket.core.controller.Context) @import gitbucket.core.view.helpers
@@ -26,11 +28,11 @@
} - @if(isManageable && issue.closed && pullreq.userName == pullreq.requestUserName && merged && - pullreq.repositoryName == pullreq.requestRepositoryName && repository.branchList.contains(pullreq.requestBranch)){ + @if(isManageableForkedRepository && issue.closed && merged && + forkedRepository.map(r => (r.branchList.contains(pullreq.requestBranch) && r.repository.defaultBranch != pullreq.requestBranch)).getOrElse(false)){
- Delete branch + Delete branch
Pull request successfully merged and closed
@@ -56,11 +58,9 @@ $.get('@helpers.url(repository)/pull/@issue.issueId/mergeguide', function(data){ $('.check-conflict').html(data); }); } - @if(isManageable){ - $('.delete-branch').click(function(e){ - var branchName = $(e.target).data('name'); - return confirm('Are you sure you want to remove the ' + branchName + ' branch?'); - }); - } + $('.delete-branch').click(function(e){ + var branchName = $(e.target).data('name'); + return confirm('Are you sure you want to remove the ' + branchName + ' branch?'); + }); }); diff --git a/src/main/twirl/gitbucket/core/pulls/pullreq.scala.html b/src/main/twirl/gitbucket/core/pulls/pullreq.scala.html index e605677..0606d3e 100644 --- a/src/main/twirl/gitbucket/core/pulls/pullreq.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/pullreq.scala.html @@ -9,7 +9,9 @@ diffs: Seq[gitbucket.core.util.JGitUtil.DiffInfo], isEditable: Boolean, isManageable: Boolean, + isManageableForkedRepository: Boolean, repository: gitbucket.core.service.RepositoryService.RepositoryInfo, + forkedRepository: Option[gitbucket.core.service.RepositoryService.RepositoryInfo], flash: Map[String, String])(implicit context: gitbucket.core.controller.Context) @import gitbucket.core.view.helpers @import gitbucket.core.model.IssueComment @@ -83,7 +85,7 @@ @flash.get("info").map{ info =>
@info
} - @gitbucket.core.pulls.html.conversation(issue, pullreq, comments, issueLabels, collaborators, milestones, labels, isEditable, isManageable, repository) + @gitbucket.core.pulls.html.conversation(issue, pullreq, comments, issueLabels, collaborators, milestones, labels, isEditable, isManageable, isManageableForkedRepository, repository, forkedRepository)
@gitbucket.core.pulls.html.commits(dayByDayCommits, Some(comments), repository)