aboutsummaryrefslogtreecommitdiffstats
path: root/routers/repo/pull.go
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-06-21 04:00:03 +0300
committerLunny Xiao <xiaolunwen@gmail.com>2017-06-21 09:00:03 +0800
commit0a5dc640a12d1c0475052b73a721056b53460275 (patch)
tree0296d1ac30ae82269eed06daed3eb1d9bc468c2d /routers/repo/pull.go
parent6db387a21e245dc4aa7f009ca60574e42d934ffb (diff)
downloadgitea-0a5dc640a12d1c0475052b73a721056b53460275.tar.gz
gitea-0a5dc640a12d1c0475052b73a721056b53460275.zip
Make branch deletion URL more like GitHub's, fixes #1397 (#1994)
* Make branch deletion URL more like GitHub's, fixes #1397 * Add PR branch deletion integration test * Do not allow deleting protected branch * Change http error code to 403 if user has no write rights to repository * Add check to not panic if forked repository has alrady been deleted
Diffstat (limited to 'routers/repo/pull.go')
-rw-r--r--routers/repo/pull.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index b710e94207..10e7449aa4 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -757,3 +757,130 @@ func TriggerTask(ctx *context.Context) {
go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
ctx.Status(202)
}
+
+// CleanUpPullRequest responses for delete merged branch when PR has been merged
+func CleanUpPullRequest(ctx *context.Context) {
+ issue := checkPullInfo(ctx)
+ if ctx.Written() {
+ return
+ }
+
+ pr, err := models.GetPullRequestByIssueID(issue.ID)
+ if err != nil {
+ if models.IsErrPullRequestNotExist(err) {
+ ctx.Handle(404, "GetPullRequestByIssueID", nil)
+ } else {
+ ctx.Handle(500, "GetPullRequestByIssueID", err)
+ }
+ return
+ }
+
+ // Allow cleanup only for merged PR
+ if !pr.HasMerged {
+ ctx.Handle(404, "CleanUpPullRequest", nil)
+ return
+ }
+
+ if err = pr.GetHeadRepo(); err != nil {
+ ctx.Handle(500, "GetHeadRepo", err)
+ return
+ } else if pr.HeadRepo == nil {
+ // Forked repository has already been deleted
+ ctx.Handle(404, "CleanUpPullRequest", nil)
+ return
+ } else if pr.GetBaseRepo(); err != nil {
+ ctx.Handle(500, "GetBaseRepo", err)
+ return
+ } else if pr.HeadRepo.GetOwner(); err != nil {
+ ctx.Handle(500, "HeadRepo.GetOwner", err)
+ return
+ }
+
+ if !ctx.User.IsWriterOfRepo(pr.HeadRepo) {
+ ctx.Handle(403, "CleanUpPullRequest", nil)
+ return
+ }
+
+ fullBranchName := pr.HeadRepo.Owner.Name + "/" + pr.HeadBranch
+
+ gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
+ if err != nil {
+ ctx.Handle(500, fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
+ return
+ }
+
+ gitBaseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ if err != nil {
+ ctx.Handle(500, fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
+ return
+ }
+
+ defer func() {
+ ctx.JSON(200, map[string]interface{}{
+ "redirect": pr.BaseRepo.Link() + "/pulls/" + com.ToStr(issue.Index),
+ })
+ }()
+
+ if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ }
+
+ // Check if branch is not protected
+ if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch); err != nil || protected {
+ if err != nil {
+ log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
+ }
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ }
+
+ // Check if branch has no new commits
+ if len(pr.MergedCommitID) > 0 {
+ branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
+ if err != nil {
+ log.Error(4, "GetBranchCommitID: %v", err)
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ }
+
+ commit, err := gitBaseRepo.GetCommit(pr.MergedCommitID)
+ if err != nil {
+ log.Error(4, "GetCommit: %v", err)
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ }
+
+ isParent := false
+ for i := 0; i < commit.ParentCount(); i++ {
+ if parent, err := commit.Parent(i); err != nil {
+ log.Error(4, "Parent: %v", err)
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ } else if parent.ID.String() == branchCommitID {
+ isParent = true
+ break
+ }
+ }
+
+ if !isParent {
+ ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
+ return
+ }
+ }
+
+ if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
+ Force: true,
+ }); err != nil {
+ log.Error(4, "DeleteBranch: %v", err)
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ return
+ }
+
+ if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
+ // Do not fail here as branch has already been deleted
+ log.Error(4, "DeleteBranch: %v", err)
+ }
+
+ ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
+}