From 28c16f206c5f170a0edb02a471cd071f8ad4fa78 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 17 Oct 2017 23:45:19 -0200 Subject: [PATCH] feat: also support regexp Support for filtering commits with regexp. Refs #284 --- docs/115-release.md | 5 +++-- pipeline/changelog/changelog.go | 17 ++++++++++++++--- pipeline/changelog/changelog_test.go | 7 +++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/115-release.md b/docs/115-release.md index cb893eff8..b90650f8a 100644 --- a/docs/115-release.md +++ b/docs/115-release.md @@ -43,11 +43,12 @@ You can customize how the changelog is generated using the # .goreleaser.yml changelog: filters: - # commit messages containing the words listed here will be removed from + # commit messages matching the regexp listed here will be removed from # the changelog exclude: - - docs + - '^docs:' - typo + - (?i)foo ``` ## Custom release notes diff --git a/pipeline/changelog/changelog.go b/pipeline/changelog/changelog.go index 6e721b075..2b439f8e2 100644 --- a/pipeline/changelog/changelog.go +++ b/pipeline/changelog/changelog.go @@ -3,11 +3,13 @@ package changelog import ( "fmt" + "regexp" "strings" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/pipeline" + "github.com/pkg/errors" ) // Pipe for checksums @@ -32,21 +34,30 @@ func (Pipe) Run(ctx *context.Context) error { } var entries = strings.Split(log, "\n") for _, filter := range ctx.Config.Changelog.Filters.Exclude { - entries = filterLog(filter, entries) + r, err := regexp.Compile(filter) + if err != nil { + return errors.Wrapf(err, "couldn't compile regexp %s", filter) + } + entries = remove(r, entries) } ctx.ReleaseNotes = fmt.Sprintf("## Changelog\n\n%v", strings.Join(entries, "\n")) return nil } -func filterLog(filter string, entries []string) (result []string) { +func remove(filter *regexp.Regexp, entries []string) (result []string) { for _, entry := range entries { - if !strings.Contains(entry, filter) { + if !match(filter, entry) { result = append(result, entry) } } return result } +func match(filter *regexp.Regexp, line string) bool { + s := strings.Join(strings.SplitAfter(line, " ")[1:], "") + return filter.MatchString(s) +} + func getChangelog(tag string) (string, error) { prev, err := previous(tag) if err != nil { diff --git a/pipeline/changelog/changelog_test.go b/pipeline/changelog/changelog_test.go index 78c729d73..c7a9c4efd 100644 --- a/pipeline/changelog/changelog_test.go +++ b/pipeline/changelog/changelog_test.go @@ -36,7 +36,10 @@ func TestChangelog(t *testing.T) { testlib.GitCommit(t, "fixed bug 2") testlib.GitCommit(t, "ignored: whatever") testlib.GitCommit(t, "docs: whatever") + testlib.GitCommit(t, "something about cArs we dont need") testlib.GitCommit(t, "feat: added that thing") + testlib.GitCommit(t, "Merge pull request #999 from goreleaser/some-branch") + testlib.GitCommit(t, "this is not a Merge pull request") testlib.GitTag(t, "v0.0.2") var ctx = context.New(config.Project{ Changelog: config.Changelog{ @@ -44,6 +47,8 @@ func TestChangelog(t *testing.T) { Exclude: []string{ "docs:", "ignored:", + "(?i)cars", + "^Merge pull request", }, }, }, @@ -57,6 +62,8 @@ func TestChangelog(t *testing.T) { assert.Contains(t, ctx.ReleaseNotes, "fixed bug 2") assert.NotContains(t, ctx.ReleaseNotes, "docs") assert.NotContains(t, ctx.ReleaseNotes, "ignored") + assert.NotContains(t, ctx.ReleaseNotes, "cArs") + assert.NotContains(t, ctx.ReleaseNotes, "from goreleaser/some-branch") } func TestChangelogOfFirstRelease(t *testing.T) {