Backport #37465 Make DEFAULT_TITLE_SOURCE default to "auto" like GitHub --------- Co-authored-by: 0xGREG <28388707+0xGREG@users.noreply.github.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Nicolas <bircni@icloud.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
@@ -429,13 +430,46 @@ func ParseCompareInfo(ctx *context.Context) *git_service.CompareInfo {
|
||||
return compareInfo
|
||||
}
|
||||
|
||||
func prepareNewPullRequestTitleContent(ci *git_service.CompareInfo, commits []*git_model.SignCommitWithStatuses) (title, content string) {
|
||||
title = ci.HeadRef.ShortName()
|
||||
// autoTitleFromBranchName humanizes a branch name into a PR title.
|
||||
func autoTitleFromBranchName(name string) string {
|
||||
var buf strings.Builder
|
||||
var prevIsSpace bool
|
||||
runes := []rune(name)
|
||||
for i, r := range runes {
|
||||
isSpace := unicode.IsSpace(r)
|
||||
if r == '-' || r == '_' || isSpace {
|
||||
if !prevIsSpace {
|
||||
buf.WriteRune(' ')
|
||||
}
|
||||
prevIsSpace = true
|
||||
continue
|
||||
}
|
||||
if !prevIsSpace && unicode.IsUpper(r) {
|
||||
needSpace := i > 0 && unicode.IsLower(runes[i-1]) || i < len(runes)-1 && unicode.IsLower(runes[i+1])
|
||||
if needSpace {
|
||||
buf.WriteRune(' ')
|
||||
}
|
||||
}
|
||||
buf.WriteRune(unicode.ToLower(r))
|
||||
prevIsSpace = isSpace
|
||||
}
|
||||
out := strings.TrimSpace(buf.String())
|
||||
if out == "" {
|
||||
return out
|
||||
}
|
||||
outRunes := []rune(out)
|
||||
outRunes[0] = unicode.ToUpper(outRunes[0])
|
||||
return string(outRunes)
|
||||
}
|
||||
|
||||
if len(commits) > 0 {
|
||||
func prepareNewPullRequestTitleContent(ci *git_service.CompareInfo, commits []*git_model.SignCommitWithStatuses, defaultTitleSource string) (title, content string) {
|
||||
useFirstCommitAsTitle := len(commits) == 1 || (defaultTitleSource == setting.RepoPRTitleSourceFirstCommit && len(commits) > 0)
|
||||
if useFirstCommitAsTitle {
|
||||
// the "commits" are from "ShowPrettyFormatLogToList", which is ordered from newest to oldest, here take the oldest one
|
||||
c := commits[len(commits)-1]
|
||||
title = strings.TrimSpace(c.UserCommit.Summary())
|
||||
} else {
|
||||
title = autoTitleFromBranchName(ci.HeadRef.ShortName())
|
||||
}
|
||||
|
||||
if len(commits) == 1 {
|
||||
@@ -571,7 +605,7 @@ func PrepareCompareDiff(
|
||||
ctx.Data["Commits"] = commits
|
||||
ctx.Data["CommitCount"] = len(commits)
|
||||
|
||||
ctx.Data["title"], ctx.Data["content"] = prepareNewPullRequestTitleContent(ci, commits)
|
||||
ctx.Data["title"], ctx.Data["content"] = prepareNewPullRequestTitleContent(ci, commits, setting.Repository.PullRequest.DefaultTitleSource)
|
||||
ctx.Data["Username"] = ci.HeadRepo.OwnerName
|
||||
ctx.Data["Reponame"] = ci.HeadRepo.Name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user