You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-11-06 09:09:29 +02:00
feat: make each changelog commit a list item (#2695)
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
committed by
GitHub
parent
419b6fc7d2
commit
df0c31dea1
@@ -20,6 +20,8 @@ import (
|
||||
// ErrInvalidSortDirection happens when the sort order is invalid.
|
||||
var ErrInvalidSortDirection = errors.New("invalid sort direction")
|
||||
|
||||
const li = "* "
|
||||
|
||||
// Pipe for checksums.
|
||||
type Pipe struct{}
|
||||
|
||||
@@ -76,20 +78,24 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
sort.Slice(groups, func(i, j int) bool { return groups[i].Order < groups[j].Order })
|
||||
for _, group := range groups {
|
||||
items := make([]string, 0)
|
||||
if len(group.Regexp) > 0 {
|
||||
if group.Regexp == "" {
|
||||
// If no regexp is provided, we purge all strikethrough entries and add remaining entries to the list
|
||||
items = getAllNonEmpty(entries)
|
||||
// clear array
|
||||
entries = nil
|
||||
} else {
|
||||
regex, err := regexp.Compile(group.Regexp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to group into %q: %w", group.Title, err)
|
||||
}
|
||||
for i, entry := range entries {
|
||||
match := checkEntryType(group.Regexp, entry)
|
||||
match := regex.MatchString(entry)
|
||||
if match {
|
||||
items = append(items, entry)
|
||||
items = append(items, li+entry)
|
||||
// Striking out the matched entry
|
||||
entries[i] = ""
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If no regexp is provided, we purge all strikethrough entries and add remaining entries to the list
|
||||
items = deleteEmptyElement(entries)
|
||||
// clear array
|
||||
entries = nil
|
||||
}
|
||||
if len(items) > 0 {
|
||||
changelogElements = append(changelogElements, fmt.Sprintf("### %s", group.Title))
|
||||
@@ -98,7 +104,7 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
}
|
||||
} else {
|
||||
log.Debug("not grouping entries")
|
||||
changelogElements = append(changelogElements, strings.Join(entries, changelogStringJoiner))
|
||||
changelogElements = append(changelogElements, strings.Join(getAllNonEmpty(entries), changelogStringJoiner))
|
||||
}
|
||||
|
||||
if header != "" {
|
||||
@@ -118,22 +124,16 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
return os.WriteFile(path, []byte(ctx.ReleaseNotes), 0o644) //nolint: gosec
|
||||
}
|
||||
|
||||
func deleteEmptyElement(s []string) []string {
|
||||
func getAllNonEmpty(ss []string) []string {
|
||||
var r []string
|
||||
for _, str := range s {
|
||||
if str != "" {
|
||||
r = append(r, str)
|
||||
for _, s := range ss {
|
||||
if s != "" {
|
||||
r = append(r, li+s)
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func checkEntryType(expr, entry string) bool {
|
||||
regex, _ := regexp.Compile(expr)
|
||||
match := regex.Match([]byte(entry))
|
||||
return match
|
||||
}
|
||||
|
||||
func loadFromFile(file string) (string, error) {
|
||||
bts, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package changelog
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -89,6 +90,13 @@ func TestChangelog(t *testing.T) {
|
||||
require.NotContains(t, ctx.ReleaseNotes, "cArs")
|
||||
require.NotContains(t, ctx.ReleaseNotes, "from goreleaser/some-branch")
|
||||
|
||||
for _, line := range strings.Split(ctx.ReleaseNotes, "\n")[1:] {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
require.Truef(t, strings.HasPrefix(line, "* "), "%q: changelog commit must be a list item", line)
|
||||
}
|
||||
|
||||
bts, err := os.ReadFile(filepath.Join(folder, "CHANGELOG.md"))
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, string(bts))
|
||||
@@ -609,3 +617,24 @@ func TestGroup(t *testing.T) {
|
||||
require.NotContains(t, ctx.ReleaseNotes, "### Catch nothing")
|
||||
require.Contains(t, ctx.ReleaseNotes, "### Others")
|
||||
}
|
||||
|
||||
func TestGroupBadRegex(t *testing.T) {
|
||||
folder := testlib.Mktmp(t)
|
||||
testlib.GitInit(t)
|
||||
testlib.GitCommit(t, "first")
|
||||
testlib.GitTag(t, "v0.0.1")
|
||||
testlib.GitTag(t, "v0.0.2")
|
||||
ctx := context.New(config.Project{
|
||||
Dist: folder,
|
||||
Changelog: config.Changelog{
|
||||
Groups: []config.ChangeLogGroup{
|
||||
{
|
||||
Title: "Something",
|
||||
Regexp: "^.*feat[(\\w", // unterminated regex
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
ctx.Git.CurrentTag = "v0.0.2"
|
||||
require.EqualError(t, Pipe{}.Run(ctx), `failed to group into "Something": error parsing regexp: missing closing ]: `+"`"+`[(\w`+"`")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user