From 4b7594d9fa0da67cbc8df74ee1711043168ebbbd Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Sun, 25 Dec 2016 23:27:25 +0800
Subject: [PATCH] Provide button to delete merged pull request (#441)

* provide button to delete merged pull request

* golint fix
---
 cmd/web.go                             |  1 +
 options/locale/locale_en-US.ini        |  1 +
 routers/repo/branch.go                 | 19 +++++++++++++++++++
 routers/repo/issue.go                  |  9 +++++++++
 templates/repo/issue/view_content.tmpl | 11 ++++++++++-
 5 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/cmd/web.go b/cmd/web.go
index 9776fdc11a..f6baf6ad4d 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -518,6 +518,7 @@ func runWeb(ctx *cli.Context) error {
 		}, context.RepoRef())
 
 		// m.Get("/branches", repo.Branches)
+		m.Post("/branches/:name/delete", reqSignIn, reqRepoWriter, repo.DeleteBranchPost)
 
 		m.Group("/wiki", func() {
 			m.Get("/?:page", repo.Wiki)
diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini
index 97725a950a..579ab85157 100644
--- a/options/locale/locale_en-US.ini
+++ b/options/locale/locale_en-US.ini
@@ -588,6 +588,7 @@ pulls.cannot_auto_merge_desc = This pull request can't be merged automatically b
 pulls.cannot_auto_merge_helper = Please merge manually in order to resolve the conflicts.
 pulls.merge_pull_request = Merge Pull Request
 pulls.open_unmerged_pull_exists = `You can't perform reopen operation because there is already an open pull request (#%d) from same repository with same merge information and is waiting for merging.`
+pulls.delete_branch = Delete Branch
 
 milestones.new = New Milestone
 milestones.open_tab = %d Open
diff --git a/routers/repo/branch.go b/routers/repo/branch.go
index fcb6efd010..c3336e598c 100644
--- a/routers/repo/branch.go
+++ b/routers/repo/branch.go
@@ -5,6 +5,7 @@
 package repo
 
 import (
+	"code.gitea.io/git"
 	"code.gitea.io/gitea/modules/base"
 	"code.gitea.io/gitea/modules/context"
 )
@@ -30,3 +31,21 @@ func Branches(ctx *context.Context) {
 	ctx.Data["Branches"] = brs
 	ctx.HTML(200, tplBranch)
 }
+
+// DeleteBranchPost responses for delete merged branch
+func DeleteBranchPost(ctx *context.Context) {
+	branchName := ctx.Params(":name")
+
+	if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
+		Force: false,
+	}); err != nil {
+		ctx.Handle(500, "DeleteBranch", err)
+		return
+	}
+
+	redirectTo := ctx.Query("redirect_to")
+	if len(redirectTo) == 0 {
+		redirectTo = ctx.Repo.RepoLink
+	}
+	ctx.Redirect(redirectTo)
+}
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 1cf5722ed1..104c893136 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -661,6 +661,15 @@ func ViewIssue(ctx *context.Context) {
 		}
 	}
 
+	if issue.IsPull {
+		pull := issue.PullRequest
+		ctx.Data["IsPullBranchDeletable"] = ctx.Repo.IsWriter() && ctx.Repo.GitRepo.IsBranchExist(pull.HeadBranch)
+
+		deleteBranchURL := ctx.Repo.RepoLink + "/branches/" + pull.HeadBranch + "/delete"
+		queryParams := "?redirect_to=" + ctx.Data["Link"].(string)
+		ctx.Data["DeleteBranchLink"] = deleteBranchURL + queryParams
+	}
+
 	ctx.Data["Participants"] = participants
 	ctx.Data["NumParticipants"] = len(participants)
 	ctx.Data["Issue"] = issue
diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl
index bbb8f4f984..1cb69fe110 100644
--- a/templates/repo/issue/view_content.tmpl
+++ b/templates/repo/issue/view_content.tmpl
@@ -163,6 +163,15 @@
 								<div class="item text purple">
 									{{$.i18n.Tr "repo.pulls.has_merged"}}
 								</div>
+								{{if .IsPullBranchDeletable}}
+									<div class="ui divider"></div>
+									<div>
+										<form class="ui form" action="{{.DeleteBranchLink}}" method="post">
+											{{.CsrfTokenHtml}}
+											<button class="ui red button">{{$.i18n.Tr "repo.pulls.delete_branch"}}</button>
+										</form>
+									</div>
+								{{end}}
 							{{else if .Issue.IsClosed}}
 								<div class="item text grey">
 									{{$.i18n.Tr "repo.pulls.reopen_to_merge"}}
@@ -265,7 +274,7 @@
 					<div class="item">
 						<a class="ui label {{if not .IsChecked}}hide{{end}}" id="label_{{.ID}}" href="{{$.RepoLink}}/issues?labels={{.ID}}" style="color: {{.ForegroundColor}}; background-color: {{.Color}}">{{.Name}}</a>
 					</div>
-					
+
 				{{end}}
 			</div>