mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-07 13:31:37 +02:00
refactor: improve changelog code
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
2257b63414
commit
a1477f9370
@ -101,48 +101,66 @@ type changelogGroup struct {
|
||||
order int
|
||||
}
|
||||
|
||||
func formatChangelog(ctx *context.Context, entries []string) (string, error) {
|
||||
newLine := "\n"
|
||||
func title(s string, level int) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%s %s", strings.Repeat("#", level), s)
|
||||
}
|
||||
|
||||
func newLineFor(ctx *context.Context) string {
|
||||
if ctx.TokenType == context.TokenTypeGitLab || ctx.TokenType == context.TokenTypeGitea {
|
||||
// We need two or more whitespace to let markdown interpret
|
||||
// it as newline. See https://docs.gitlab.com/ee/user/markdown.html#newlines for details
|
||||
log.Debug("is gitlab or gitea changelog")
|
||||
newLine = " \n"
|
||||
return " \n"
|
||||
}
|
||||
|
||||
if !useChangelog(ctx.Config.Changelog.Use).formatable() {
|
||||
return strings.Join(entries, newLine), nil
|
||||
}
|
||||
return "\n"
|
||||
}
|
||||
|
||||
for i := range entries {
|
||||
entry := entries[i]
|
||||
abbr := ctx.Config.Changelog.Abbrev
|
||||
switch abbr {
|
||||
case 0:
|
||||
continue
|
||||
case -1:
|
||||
_, rest, _ := strings.Cut(entry, " ")
|
||||
entries[i] = rest
|
||||
default:
|
||||
commit, rest, _ := strings.Cut(entry, " ")
|
||||
if abbr > len(commit) {
|
||||
continue
|
||||
}
|
||||
entries[i] = fmt.Sprintf("%s %s", commit[:abbr], rest)
|
||||
func abbrevEntry(s string, abbr int) string {
|
||||
switch abbr {
|
||||
case 0:
|
||||
return s
|
||||
case -1:
|
||||
_, rest, _ := strings.Cut(s, " ")
|
||||
return rest
|
||||
default:
|
||||
commit, rest, _ := strings.Cut(s, " ")
|
||||
if abbr > len(commit) {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("%s %s", commit[:abbr], rest)
|
||||
}
|
||||
}
|
||||
|
||||
func abbrev(entries []string, abbr int) []string {
|
||||
result := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
result = append(result, abbrevEntry(entry, abbr))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func formatChangelog(ctx *context.Context, entries []string) (string, error) {
|
||||
if !useChangelog(ctx.Config.Changelog.Use).formatable() {
|
||||
return strings.Join(entries, newLineFor(ctx)), nil
|
||||
}
|
||||
|
||||
result := []string{"## Changelog"}
|
||||
entries = abbrev(entries, ctx.Config.Changelog.Abbrev)
|
||||
|
||||
result := []string{title("Changelog", 2)}
|
||||
if len(ctx.Config.Changelog.Groups) == 0 {
|
||||
log.Debug("not grouping entries")
|
||||
return strings.Join(append(result, filterAndPrefixItems(entries)...), newLine), nil
|
||||
return strings.Join(append(result, filterAndPrefixItems(entries)...), newLineFor(ctx)), nil
|
||||
}
|
||||
|
||||
log.Debug("grouping entries")
|
||||
var groups []changelogGroup
|
||||
for _, group := range ctx.Config.Changelog.Groups {
|
||||
item := changelogGroup{
|
||||
title: group.Title,
|
||||
title: title(group.Title, 3),
|
||||
order: group.Order,
|
||||
}
|
||||
if group.Regexp == "" {
|
||||
@ -151,7 +169,7 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) {
|
||||
// clear array
|
||||
entries = nil
|
||||
} else {
|
||||
regex, err := regexp.Compile(group.Regexp)
|
||||
re, err := regexp.Compile(group.Regexp)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to group into %q: %w", group.Title, err)
|
||||
}
|
||||
@ -159,7 +177,7 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) {
|
||||
log.Debugf("group: %#v", group)
|
||||
i := 0
|
||||
for _, entry := range entries {
|
||||
match := regex.MatchString(entry)
|
||||
match := re.MatchString(entry)
|
||||
log.Debugf("entry: %s match: %b\n", entry, match)
|
||||
if match {
|
||||
item.entries = append(item.entries, li+entry)
|
||||
@ -178,14 +196,20 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(groups, func(i, j int) bool { return groups[i].order < groups[j].order })
|
||||
sort.Slice(groups, groupSort(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.entries) > 0 {
|
||||
result = append(result, fmt.Sprintf("\n### %s", group.title))
|
||||
result = append(result, group.title)
|
||||
result = append(result, group.entries...)
|
||||
}
|
||||
}
|
||||
return strings.Join(result, newLine), nil
|
||||
return strings.Join(result, newLineFor(ctx)), nil
|
||||
}
|
||||
|
||||
func groupSort(groups []changelogGroup) func(i, j int) bool {
|
||||
return func(i, j int) bool {
|
||||
return groups[i].order < groups[j].order
|
||||
}
|
||||
}
|
||||
|
||||
func filterAndPrefixItems(ss []string) []string {
|
||||
|
@ -817,7 +817,6 @@ func TestChangelogFormat(t *testing.T) {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `## Changelog
|
||||
|
||||
### catch-all
|
||||
* aea123 foo
|
||||
* aef653 bar`, out)
|
||||
|
Loading…
x
Reference in New Issue
Block a user