diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-08 20:32:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 20:32:45 +0800 |
commit | 4344a6410788f30848e5153f6356dcdd0774bebc (patch) | |
tree | 6eba1230f1ac672f0eb0a36f148949470dd50048 /routers/web/repo | |
parent | 5ca224a789394d00cc1efb5afcae7b87aa7d1e49 (diff) | |
download | gitea-4344a6410788f30848e5153f6356dcdd0774bebc.tar.gz gitea-4344a6410788f30848e5153f6356dcdd0774bebc.zip |
Allow custom default merge message with .gitea/default_merge_message/<merge_style>_TEMPLATE.md (#18177)
* Allow custom default merge message with .gitea/MERGE_MESSAGE_<merge_style>_TEMPLATE.md
* Some improvements
* Follow some advices
* Fix bug
* Fix bug
* Fix lint
* Fix close comment
* Fix test
* Fix and docs
* Improve codes
* Update docs and remove unnecessary variables
* return error for GetDefaultMergeMessage
* Fix test
* improve code
* ignore unknow unit type
* return error for GetDefaultMergeMessage
* Update services/pull/merge.go
* Some improvements
* Follow some advices
* Fix bug
* Fix lint
* Improve codes
* Update docs and remove unnecessary variables
* return error for GetDefaultMergeMessage
* improve code
* Handle deleted HeadRepo in GetDefaultMergeMessage
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Fix test
* Fix test
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'routers/web/repo')
-rw-r--r-- | routers/web/repo/issue.go | 36 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 19 |
2 files changed, 39 insertions, 16 deletions
diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 620b76f46d..7ddeb05f71 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -712,8 +712,6 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull } func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) { - var bytes []byte - if ctx.Repo.Commit == nil { var err error ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch) @@ -734,7 +732,7 @@ func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (str return "", false } defer r.Close() - bytes, err = io.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { return "", false } @@ -1574,26 +1572,42 @@ func ViewIssue(ctx *context.Context) { } prConfig := prUnit.PullRequestsConfig() + var mergeStyle repo_model.MergeStyle // Check correct values and select default if ms, ok := ctx.Data["MergeStyle"].(repo_model.MergeStyle); !ok || !prConfig.IsMergeStyleAllowed(ms) { defaultMergeStyle := prConfig.GetDefaultMergeStyle() if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok { - ctx.Data["MergeStyle"] = defaultMergeStyle + mergeStyle = defaultMergeStyle } else if prConfig.AllowMerge { - ctx.Data["MergeStyle"] = repo_model.MergeStyleMerge + mergeStyle = repo_model.MergeStyleMerge } else if prConfig.AllowRebase { - ctx.Data["MergeStyle"] = repo_model.MergeStyleRebase + mergeStyle = repo_model.MergeStyleRebase } else if prConfig.AllowRebaseMerge { - ctx.Data["MergeStyle"] = repo_model.MergeStyleRebaseMerge + mergeStyle = repo_model.MergeStyleRebaseMerge } else if prConfig.AllowSquash { - ctx.Data["MergeStyle"] = repo_model.MergeStyleSquash + mergeStyle = repo_model.MergeStyleSquash } else if prConfig.AllowManualMerge { - ctx.Data["MergeStyle"] = repo_model.MergeStyleManuallyMerged - } else { - ctx.Data["MergeStyle"] = "" + mergeStyle = repo_model.MergeStyleManuallyMerged } } + + ctx.Data["MergeStyle"] = mergeStyle + + defaultMergeMessage, err := pull_service.GetDefaultMergeMessage(ctx.Repo.GitRepo, pull, mergeStyle) + if err != nil { + ctx.ServerError("GetDefaultMergeMessage", err) + return + } + ctx.Data["DefaultMergeMessage"] = defaultMergeMessage + + defaultSquashMergeMessage, err := pull_service.GetDefaultMergeMessage(ctx.Repo.GitRepo, pull, repo_model.MergeStyleSquash) + if err != nil { + ctx.ServerError("GetDefaultSquashMergeMessage", err) + return + } + ctx.Data["DefaultSquashMergeMessage"] = defaultSquashMergeMessage + if err = pull.LoadProtectedBranch(); err != nil { ctx.ServerError("LoadProtectedBranch", err) return diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index a0b3567738..27b61309a5 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -950,13 +950,22 @@ func MergePullRequest(ctx *context.Context) { return } - // set defaults to propagate needed fields - if err := form.SetDefaults(ctx, pr); err != nil { - ctx.ServerError("SetDefaults", fmt.Errorf("SetDefaults: %v", err)) - return + message := strings.TrimSpace(form.MergeTitleField) + if len(message) == 0 { + var err error + message, err = pull_service.GetDefaultMergeMessage(ctx.Repo.GitRepo, pr, repo_model.MergeStyle(form.Do)) + if err != nil { + ctx.ServerError("GetDefaultMergeMessage", err) + return + } + } + + form.MergeMessageField = strings.TrimSpace(form.MergeMessageField) + if len(form.MergeMessageField) > 0 { + message += "\n\n" + form.MergeMessageField } - if err := pull_service.Merge(pr, ctx.Doer, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, form.MergeTitleField); err != nil { + if err := pull_service.Merge(ctx, pr, ctx.Doer, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(issue.Link()) |