Skip to content

Commit

Permalink
feat: make each changelog commit a list item (#2695)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Nov 24, 2021
1 parent 419b6fc commit df0c31d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
38 changes: 19 additions & 19 deletions internal/pipe/changelog/changelog.go
Expand Up @@ -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{}

Expand Down Expand Up @@ -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))
Expand All @@ -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 != "" {
Expand All @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions internal/pipe/changelog/changelog_test.go
Expand Up @@ -3,6 +3,7 @@ package changelog
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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`+"`")
}

0 comments on commit df0c31d

Please sign in to comment.