Skip to content

Commit 85d4bef

Browse files
committed
Create pull request for base repo after editing file, if not enabled for fork
Currently if pull requests are disabled on a fork but enabled on a base repo, creating/editing/deleting files does not offer the option to create a pull request. This change enables creating a pull request in that case.
1 parent 5992840 commit 85d4bef

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

routers/web/repo/editor.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,39 @@ const (
4141
frmCommitChoiceNewBranch string = "commit-to-new-branch"
4242
)
4343

44+
func canCreateBasePullRequest(ctx *context.Context) bool {
45+
baseRepo := ctx.Repo.Repository.BaseRepo
46+
return baseRepo != nil && baseRepo.UnitEnabled(ctx, unit.TypePullRequests)
47+
}
48+
4449
func renderCommitRights(ctx *context.Context) bool {
4550
canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx, ctx.Doer)
4651
if err != nil {
4752
log.Error("CanCommitToBranch: %v", err)
4853
}
4954
ctx.Data["CanCommitToBranch"] = canCommitToBranch
55+
ctx.Data["CanCreatePullRequest"] = ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) || canCreateBasePullRequest(ctx)
5056

5157
return canCommitToBranch.CanCommitToBranch
5258
}
5359

60+
// redirect to pull request after commit to branch
61+
func redirectToPullRequest(ctx *context.Context, newBranchName string) bool {
62+
repo := ctx.Repo.Repository
63+
baseBranch := util.PathEscapeSegments(ctx.Repo.BranchName)
64+
headBranch := util.PathEscapeSegments(newBranchName)
65+
if !repo.UnitEnabled(ctx, unit.TypePullRequests) {
66+
if !canCreateBasePullRequest(ctx) {
67+
return false
68+
}
69+
baseBranch = util.PathEscapeSegments(repo.BaseRepo.DefaultBranch)
70+
headBranch = util.PathEscapeSegments(repo.Owner.Name) + "/" + util.PathEscapeSegments(repo.Name) + ":" + headBranch
71+
repo = repo.BaseRepo
72+
}
73+
ctx.Redirect(repo.Link() + "/compare/" + baseBranch + "..." + headBranch)
74+
return true
75+
}
76+
5477
// getParentTreeFields returns list of parent tree names and corresponding tree paths
5578
// based on given tree path.
5679
func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
@@ -331,9 +354,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
331354
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
332355
}
333356

334-
if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
335-
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
336-
} else {
357+
if !(form.CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest(ctx, branchName)) {
337358
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath))
338359
}
339360
}
@@ -517,9 +538,7 @@ func DeleteFilePost(ctx *context.Context) {
517538
}
518539

519540
ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath))
520-
if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
521-
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
522-
} else {
541+
if !(form.CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest(ctx, branchName)) {
523542
treePath := path.Dir(ctx.Repo.TreePath)
524543
if treePath == "." {
525544
treePath = "" // the file deleted was in the root, so we return the user to the root directory
@@ -722,9 +741,7 @@ func UploadFilePost(ctx *context.Context) {
722741
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
723742
}
724743

725-
if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
726-
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
727-
} else {
744+
if !(form.CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest(ctx, branchName)) {
728745
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath))
729746
}
730747
}

templates/repo/editor/commit_form.tmpl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@
4141
</div>
4242
{{if not .Repository.IsEmpty}}
4343
<div class="field">
44-
{{$pullRequestEnabled := .Repository.UnitEnabled $.Context $.UnitTypePullRequests}}
45-
{{$prUnit := .Repository.MustGetUnit $.Context $.UnitTypePullRequests}}
4644
<div class="ui radio checkbox">
47-
{{if $pullRequestEnabled}}
45+
{{if .CanCreatePullRequest}}
4846
<input type="radio" class="js-quick-pull-choice-option" name="commit_choice" value="commit-to-new-branch" button_text="{{.locale.Tr "repo.editor.propose_file_change"}}" {{if eq .commit_choice "commit-to-new-branch"}}checked{{end}}>
4947
{{else}}
5048
<input type="radio" class="js-quick-pull-choice-option" name="commit_choice" value="commit-to-new-branch" button_text="{{.locale.Tr "repo.editor.commit_changes"}}" {{if eq .commit_choice "commit-to-new-branch"}}checked{{end}}>
5149
{{end}}
5250
<label>
5351
{{svg "octicon-git-pull-request"}}
54-
{{if $pullRequestEnabled}}
52+
{{if .CanCreatePullRequest}}
5553
{{.locale.Tr "repo.editor.create_new_branch" | Safe}}
5654
{{else}}
5755
{{.locale.Tr "repo.editor.create_new_branch_np" | Safe}}

0 commit comments

Comments
 (0)