Skip to content

Commit

Permalink
fix: changelog include incorrectly compresses log while filtering (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-paul-t authored Jan 17, 2023
1 parent b0023f5 commit 35cbbca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
60 changes: 33 additions & 27 deletions internal/task/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ type release struct {
Changes []git.LogEntry
}

// Define functions for filtering commits
type commitFilter func([]git.LogEntry, *regexp.Regexp) []git.LogEntry

// Task that generates a changelog for the current repository
type Task struct{}

Expand Down Expand Up @@ -175,15 +172,15 @@ func changelogRelease(ctx *context.Context) ([]release, error) {

if len(ctx.Changelog.Include) > 0 {
log.Info("cherry-picking commits based on include list")
ents, err = filterCommits(ents, ctx.Changelog.Include, includeCommits)
ents, err = includeCommits(ents, ctx.Changelog.Include)
if err != nil {
return []release{}, err
}
}

if len(ctx.Changelog.Exclude) > 0 {
log.Info("removing commits based on exclude list")
ents, err = filterCommits(ents, ctx.Changelog.Exclude, excludeCommits)
ents, err = excludeCommits(ents, ctx.Changelog.Exclude)
if err != nil {
return []release{}, err
}
Expand Down Expand Up @@ -247,15 +244,15 @@ func changelogReleases(ctx *context.Context) ([]release, error) {

if len(ctx.Changelog.Include) > 0 {
log.Info("cherry-picking commits based on include list")
ents, err = filterCommits(ents, ctx.Changelog.Include, includeCommits)
ents, err = includeCommits(ents, ctx.Changelog.Include)
if err != nil {
return []release{}, err
}
}

if len(ctx.Changelog.Exclude) > 0 {
log.Info("removing commits based on exclude list")
ents, err = filterCommits(ents, ctx.Changelog.Exclude, excludeCommits)
ents, err = excludeCommits(ents, ctx.Changelog.Exclude)
if err != nil {
return []release{}, err
}
Expand Down Expand Up @@ -364,35 +361,44 @@ func reverse(ents []git.LogEntry) {
}
}

func filterCommits(commits []git.LogEntry, regexes []string, filter commitFilter) ([]git.LogEntry, error) {
filteredCommits := commits
func includeCommits(commits []git.LogEntry, regexes []string) ([]git.LogEntry, error) {
filtered := []git.LogEntry{}
for _, regex := range regexes {
rgx, err := regexp.Compile(regex)
includeRgx, err := regexp.Compile(regex)
if err != nil {
return filteredCommits, err
return filtered, err
}

// Iterate over the entire list of log entries for each regex and
// append any match to the filtered list
for _, commit := range commits {
if includeRgx.MatchString(commit.Message) {
filtered = append(filtered, commit)
}
}
filteredCommits = filter(filteredCommits, rgx)
}

return filteredCommits, nil
return filtered, nil
}

func includeCommits(commits []git.LogEntry, rgx *regexp.Regexp) []git.LogEntry {
filteredCommits := []git.LogEntry{}
for _, commit := range commits {
if rgx.MatchString(commit.Message) {
filteredCommits = append(filteredCommits, commit)
func excludeCommits(commits []git.LogEntry, regexes []string) ([]git.LogEntry, error) {
filtered := commits
for _, regex := range regexes {
excludeRgx, err := regexp.Compile(regex)
if err != nil {
return filtered, err
}
}
return filteredCommits
}

func excludeCommits(commits []git.LogEntry, rgx *regexp.Regexp) []git.LogEntry {
filteredCommits := []git.LogEntry{}
for _, commit := range commits {
if !rgx.MatchString(commit.Message) {
filteredCommits = append(filteredCommits, commit)
// Repeat over the filtered list for every exclude, compressing the list
// of log entries on each iteration
filterPass := []git.LogEntry{}
for _, commit := range filtered {
if !excludeRgx.MatchString(commit.Message) {
filterPass = append(filterPass, commit)
}
}
filtered = filterPass
}
return filteredCommits

return filtered, nil
}
14 changes: 7 additions & 7 deletions internal/task/changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,17 +590,17 @@ func TestRun_IdentifiedSCM(t *testing.T) {
assert.Contains(t, buf.String(), expected)
}

func TestRun_WithIncludes(t *testing.T) {
func TestRun_WithMultipleIncludes(t *testing.T) {
git.InitRepo(t)
git.Tag("1.0.0")
git.EmptyCommitsAndTag(t, "1.1.0", "ci: tweak", "fix(scope1): a fix", "feat(scope1): a feature", "fix(scope2): another fix")
git.EmptyCommitsAndTag(t, "1.1.0", "ci: tweak", "fix(scope1): a fix", "feat(scope1): a feature", "fix(common): another fix")

var buf bytes.Buffer
ctx := &context.Context{
Out: &buf,
Changelog: context.Changelog{
DiffOnly: true,
Include: []string{`^.*\(scope1\)`},
Include: []string{`^.*\(scope1\)`, `^.*\(common\)`},
},
CurrentVersion: semver.Version{
Raw: "1.0.0",
Expand All @@ -619,8 +619,8 @@ func TestRun_WithIncludes(t *testing.T) {
actual := buf.String()
assert.Contains(t, actual, "fix(scope1): a fix")
assert.Contains(t, actual, "feat(scope1): a feature")
assert.Contains(t, actual, "fix(common): another fix")
assert.NotContains(t, actual, "ci: tweak")
assert.NotContains(t, actual, "fix(scope2): another fix")
}

func TestRun_AllWithIncludes(t *testing.T) {
Expand Down Expand Up @@ -655,14 +655,14 @@ func TestRun_AllWithIncludes(t *testing.T) {
func TestRun_CombinedIncludeAndExclude(t *testing.T) {
git.InitRepo(t)
git.Tag("1.0.0")
git.EmptyCommitsAndTag(t, "1.1.0", "ci: tweak", "fix(scope1): a fix", "feat(scope1): a feature", "fix(scope2): another fix")
git.EmptyCommitsAndTag(t, "1.1.0", "ci: tweak", "fix(scope1): a fix", "feat(scope1): a feature", "feat(scope2): another feature")

var buf bytes.Buffer
ctx := &context.Context{
Out: &buf,
Changelog: context.Changelog{
DiffOnly: true,
Include: []string{`^.*\(scope1\)`},
Include: []string{`^.*\(scope1\)`, `^.*\(scope2\)`},
Exclude: []string{`^fix`},
},
CurrentVersion: semver.Version{
Expand All @@ -681,7 +681,7 @@ func TestRun_CombinedIncludeAndExclude(t *testing.T) {

actual := buf.String()
assert.Contains(t, actual, "feat(scope1): a feature")
assert.Contains(t, actual, "feat(scope2): another feature")
assert.NotContains(t, actual, "ci: tweak")
assert.NotContains(t, actual, "fix(scope1): a fix")
assert.NotContains(t, actual, "fix(scope2): another fix")
}

0 comments on commit 35cbbca

Please sign in to comment.