Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pull request for base after editing file, if not enabled on fork #24841

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,39 @@ const (
frmCommitChoiceNewBranch string = "commit-to-new-branch"
)

func canCreateBasePullRequest(ctx *context.Context) bool {
baseRepo := ctx.Repo.Repository.BaseRepo
return baseRepo != nil && baseRepo.UnitEnabled(ctx, unit.TypePullRequests)
}

func renderCommitRights(ctx *context.Context) bool {
canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx, ctx.Doer)
if err != nil {
log.Error("CanCommitToBranch: %v", err)
}
ctx.Data["CanCommitToBranch"] = canCommitToBranch
ctx.Data["CanCreatePullRequest"] = ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) || canCreateBasePullRequest(ctx)

return canCommitToBranch.CanCommitToBranch
}

// redirect to pull request after commit to branch
func redirectToPullRequest(ctx *context.Context, newBranchName string) bool {
repo := ctx.Repo.Repository
baseBranch := util.PathEscapeSegments(ctx.Repo.BranchName)
headBranch := util.PathEscapeSegments(newBranchName)
if !repo.UnitEnabled(ctx, unit.TypePullRequests) {
if !canCreateBasePullRequest(ctx) {
return false
}
baseBranch = util.PathEscapeSegments(repo.BaseRepo.DefaultBranch)
headBranch = util.PathEscapeSegments(repo.Owner.Name) + "/" + util.PathEscapeSegments(repo.Name) + ":" + headBranch
repo = repo.BaseRepo
}
ctx.Redirect(repo.Link() + "/compare/" + baseBranch + "..." + headBranch)
return true
}

// getParentTreeFields returns list of parent tree names and corresponding tree paths
// based on given tree path.
func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
Expand Down Expand Up @@ -331,9 +354,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty")
}

if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
} else {
if !(form.CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest(ctx, branchName)) {
brechtvl marked this conversation as resolved.
Show resolved Hide resolved
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath))
}
}
Expand Down Expand Up @@ -517,9 +538,7 @@ func DeleteFilePost(ctx *context.Context) {
}

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

if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) {
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName))
} else {
if !(form.CommitChoice == frmCommitChoiceNewBranch && redirectToPullRequest(ctx, branchName)) {
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(form.TreePath))
}
}
Expand Down
6 changes: 2 additions & 4 deletions templates/repo/editor/commit_form.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@
</div>
{{if not .Repository.IsEmpty}}
<div class="field">
{{$pullRequestEnabled := .Repository.UnitEnabled $.Context $.UnitTypePullRequests}}
{{$prUnit := .Repository.MustGetUnit $.Context $.UnitTypePullRequests}}
<div class="ui radio checkbox">
{{if $pullRequestEnabled}}
{{if .CanCreatePullRequest}}
<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}}>
{{else}}
<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}}>
{{end}}
<label>
{{svg "octicon-git-pull-request"}}
{{if $pullRequestEnabled}}
{{if .CanCreatePullRequest}}
{{.locale.Tr "repo.editor.create_new_branch" | Safe}}
{{else}}
{{.locale.Tr "repo.editor.create_new_branch_np" | Safe}}
Expand Down