Browse Source

Provide button to delete merged pull request (#441)

* provide button to delete merged pull request

* golint fix
Lunny Xiao 3 years ago
parent
commit
4b7594d9fa

+ 1 - 0
cmd/web.go

@@ -518,6 +518,7 @@ func runWeb(ctx *cli.Context) error {
518 518
 		}, context.RepoRef())
519 519
 
520 520
 		// m.Get("/branches", repo.Branches)
521
+		m.Post("/branches/:name/delete", reqSignIn, reqRepoWriter, repo.DeleteBranchPost)
521 522
 
522 523
 		m.Group("/wiki", func() {
523 524
 			m.Get("/?:page", repo.Wiki)

+ 1 - 0
options/locale/locale_en-US.ini

@@ -588,6 +588,7 @@ pulls.cannot_auto_merge_desc = This pull request can't be merged automatically b
588 588
 pulls.cannot_auto_merge_helper = Please merge manually in order to resolve the conflicts.
589 589
 pulls.merge_pull_request = Merge Pull Request
590 590
 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.`
591
+pulls.delete_branch = Delete Branch
591 592
 
592 593
 milestones.new = New Milestone
593 594
 milestones.open_tab = %d Open

+ 19 - 0
routers/repo/branch.go

@@ -5,6 +5,7 @@
5 5
 package repo
6 6
 
7 7
 import (
8
+	"code.gitea.io/git"
8 9
 	"code.gitea.io/gitea/modules/base"
9 10
 	"code.gitea.io/gitea/modules/context"
10 11
 )
@@ -30,3 +31,21 @@ func Branches(ctx *context.Context) {
30 31
 	ctx.Data["Branches"] = brs
31 32
 	ctx.HTML(200, tplBranch)
32 33
 }
34
+
35
+// DeleteBranchPost responses for delete merged branch
36
+func DeleteBranchPost(ctx *context.Context) {
37
+	branchName := ctx.Params(":name")
38
+
39
+	if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
40
+		Force: false,
41
+	}); err != nil {
42
+		ctx.Handle(500, "DeleteBranch", err)
43
+		return
44
+	}
45
+
46
+	redirectTo := ctx.Query("redirect_to")
47
+	if len(redirectTo) == 0 {
48
+		redirectTo = ctx.Repo.RepoLink
49
+	}
50
+	ctx.Redirect(redirectTo)
51
+}

+ 9 - 0
routers/repo/issue.go

@@ -661,6 +661,15 @@ func ViewIssue(ctx *context.Context) {
661 661
 		}
662 662
 	}
663 663
 
664
+	if issue.IsPull {
665
+		pull := issue.PullRequest
666
+		ctx.Data["IsPullBranchDeletable"] = ctx.Repo.IsWriter() && ctx.Repo.GitRepo.IsBranchExist(pull.HeadBranch)
667
+
668
+		deleteBranchURL := ctx.Repo.RepoLink + "/branches/" + pull.HeadBranch + "/delete"
669
+		queryParams := "?redirect_to=" + ctx.Data["Link"].(string)
670
+		ctx.Data["DeleteBranchLink"] = deleteBranchURL + queryParams
671
+	}
672
+
664 673
 	ctx.Data["Participants"] = participants
665 674
 	ctx.Data["NumParticipants"] = len(participants)
666 675
 	ctx.Data["Issue"] = issue

+ 10 - 1
templates/repo/issue/view_content.tmpl

@@ -163,6 +163,15 @@
163 163
 								<div class="item text purple">
164 164
 									{{$.i18n.Tr "repo.pulls.has_merged"}}
165 165
 								</div>
166
+								{{if .IsPullBranchDeletable}}
167
+									<div class="ui divider"></div>
168
+									<div>
169
+										<form class="ui form" action="{{.DeleteBranchLink}}" method="post">
170
+											{{.CsrfTokenHtml}}
171
+											<button class="ui red button">{{$.i18n.Tr "repo.pulls.delete_branch"}}</button>
172
+										</form>
173
+									</div>
174
+								{{end}}
166 175
 							{{else if .Issue.IsClosed}}
167 176
 								<div class="item text grey">
168 177
 									{{$.i18n.Tr "repo.pulls.reopen_to_merge"}}
@@ -265,7 +274,7 @@
265 274
 					<div class="item">
266 275
 						<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>
267 276
 					</div>
268
-					
277
+
269 278
 				{{end}}
270 279
 			</div>
271 280