aboutsummaryrefslogtreecommitdiffstats
path: root/services/pull/merge.go
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-12-29 20:40:20 +0800
committerGitHub <noreply@github.com>2022-12-29 14:40:20 +0200
commit47efba78ec966631ccdea3e05a50f4cf59ca9fd3 (patch)
tree65a8ef34d5c03ab40bc70111d04fe309546f09e8 /services/pull/merge.go
parenta35749893b91db48310d91ae0a32fee3ad3bb901 (diff)
downloadgitea-47efba78ec966631ccdea3e05a50f4cf59ca9fd3.tar.gz
gitea-47efba78ec966631ccdea3e05a50f4cf59ca9fd3.zip
Support template for merge message description (#22248)
Fix #21435. Use the first line of the template as the git commit message title, and the rest as the description. ## Snapshots <img width="806" alt="image" src="https://user-images.githubusercontent.com/9418365/209644083-5d85179c-cf58-404f-bc98-c662398a2411.png"> <img width="860" alt="image" src="https://user-images.githubusercontent.com/9418365/209644392-22573090-e2c1-458b-ba44-855b79735632.png"> <img width="1154" alt="image" src="https://user-images.githubusercontent.com/9418365/209644457-a1b2711a-6787-45b4-b52c-a88d7fc132d7.png"> Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'services/pull/merge.go')
-rw-r--r--services/pull/merge.go38
1 files changed, 23 insertions, 15 deletions
diff --git a/services/pull/merge.go b/services/pull/merge.go
index 41ba45c177..7a936163f1 100644
--- a/services/pull/merge.go
+++ b/services/pull/merge.go
@@ -39,19 +39,19 @@ import (
)
// GetDefaultMergeMessage returns default message used when merging pull request
-func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr *issues_model.PullRequest, mergeStyle repo_model.MergeStyle) (string, error) {
+func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr *issues_model.PullRequest, mergeStyle repo_model.MergeStyle) (message, body string, err error) {
if err := pr.LoadHeadRepo(ctx); err != nil {
- return "", err
+ return "", "", err
}
if err := pr.LoadBaseRepo(ctx); err != nil {
- return "", err
+ return "", "", err
}
if pr.BaseRepo == nil {
- return "", repo_model.ErrRepoNotExist{ID: pr.BaseRepoID}
+ return "", "", repo_model.ErrRepoNotExist{ID: pr.BaseRepoID}
}
if err := pr.LoadIssue(ctx); err != nil {
- return "", err
+ return "", "", err
}
isExternalTracker := pr.BaseRepo.UnitEnabled(ctx, unit.TypeExternalTracker)
@@ -64,12 +64,12 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
templateFilepath := fmt.Sprintf(".gitea/default_merge_message/%s_TEMPLATE.md", strings.ToUpper(string(mergeStyle)))
commit, err := baseGitRepo.GetBranchCommit(pr.BaseRepo.DefaultBranch)
if err != nil {
- return "", err
+ return "", "", err
}
templateContent, err := commit.GetFileContent(templateFilepath, setting.Repository.PullRequest.DefaultMergeMessageSize)
if err != nil {
if !git.IsErrNotExist(err) {
- return "", err
+ return "", "", err
}
} else {
vars := map[string]string{
@@ -107,27 +107,35 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
vars["ClosingIssues"] = ""
}
}
-
- return os.Expand(templateContent, func(s string) string {
- return vars[s]
- }), nil
+ message, body = expandDefaultMergeMessage(templateContent, vars)
+ return message, body, nil
}
}
// Squash merge has a different from other styles.
if mergeStyle == repo_model.MergeStyleSquash {
- return fmt.Sprintf("%s (%s%d)", pr.Issue.Title, issueReference, pr.Issue.Index), nil
+ return fmt.Sprintf("%s (%s%d)", pr.Issue.Title, issueReference, pr.Issue.Index), "", nil
}
if pr.BaseRepoID == pr.HeadRepoID {
- return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadBranch, pr.BaseBranch), nil
+ return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadBranch, pr.BaseBranch), "", nil
}
if pr.HeadRepo == nil {
- return fmt.Sprintf("Merge pull request '%s' (%s%d) from <deleted>:%s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadBranch, pr.BaseBranch), nil
+ return fmt.Sprintf("Merge pull request '%s' (%s%d) from <deleted>:%s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadBranch, pr.BaseBranch), "", nil
}
- return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s:%s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseBranch), nil
+ return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s:%s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseBranch), "", nil
+}
+
+func expandDefaultMergeMessage(template string, vars map[string]string) (message, body string) {
+ message = strings.TrimSpace(template)
+ if splits := strings.SplitN(message, "\n", 2); len(splits) == 2 {
+ message = splits[0]
+ body = strings.TrimSpace(splits[1])
+ }
+ mapping := func(s string) string { return vars[s] }
+ return os.Expand(message, mapping), os.Expand(body, mapping)
}
// Merge merges pull request to base repository.