Skip to content
Permalink
Browse files
fix(changelog): group regexps (#3527)
Fix the regular expressions used in changelog group processing to valid
golang (RE2) regexps. These previously used PCRE character class `\w`
which is not supported in RE2 which is what golang regexp uses.

Also document that format matches not just title as some may thing but
the format `<abbrev-commit> <title-commit>`.
  • Loading branch information
stevenh committed Nov 7, 2022
1 parent 53fa477 commit 0ea4f1d5b7d8b25d4a5deaa302327a1f76a10d71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
@@ -53,16 +53,16 @@ changelog:
- go mod tidy
groups:
- title: Dependency updates
regexp: "^.*(feat|fix)\\(deps\\)*:+.*$"
regexp: '^.*?(feat|fix)\(deps\)!?:.+$'
order: 300
- title: 'New Features'
regexp: "^.*feat[(\\w)]*:+.*$"
regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$'
order: 100
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$'
order: 200
- title: 'Documentation updates'
regexp: "^.*docs[(\\w)]*:+.*$"
regexp: ^.*?doc(\([[:word:]]+\))??!?:.+$
order: 400
- title: Other work
order: 9999
@@ -155,16 +155,27 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to group into %q: %w", group.Title, err)
}
for i, entry := range entries {

log.Debugf("group: %#v", group)
i := 0
for _, entry := range entries {
match := regex.MatchString(entry)
log.Debugf("entry: %s match: %b\n", entry, match)
if match {
item.entries = append(item.entries, li+entry)
// Striking out the matched entry
entries[i] = ""
} else {
// Keep unmatched entry.
entries[i] = entry
i++
}
}
entries = entries[:i]
}
groups = append(groups, item)

if len(entries) == 0 {
break // No more entries to process.
}
}

sort.Slice(groups, func(i, j int) bool { return groups[i].order < groups[j].order })
@@ -638,12 +638,12 @@ func TestGroup(t *testing.T) {
},
{
Title: "Features",
Regexp: "^.*feat[(\\w)]*:+.*$",
Regexp: `^.*?feat(\([[:word:]]+\))??!?:.+$`,
Order: 0,
},
{
Title: "Bug Fixes",
Regexp: "^.*bug[(\\w)]*:+.*$",
Regexp: `^.*?bug(\([[:word:]]+\))??!?:.+$`,
Order: 1,
},
{
@@ -680,13 +680,13 @@ func TestGroupBadRegex(t *testing.T) {
Groups: []config.ChangeLogGroup{
{
Title: "Something",
Regexp: "^.*feat[(\\w", // unterminated regex
Regexp: "^.*feat[a-z", // 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`+"`")
require.EqualError(t, Pipe{}.Run(ctx), "failed to group into \"Something\": error parsing regexp: missing closing ]: `[a-z`")
}

func TestChangelogFormat(t *testing.T) {
@@ -51,14 +51,16 @@ changelog:
# Order value defines the order of the groups.
# Proving no regex means all commits will be grouped under the default group.
# Groups are disabled when using github-native, as it already groups things by itself.
# Matches are performed against strings of the form: "<abbrev-commit>[:] <title-commit>".
# Regex use RE2 syntax as defined here: https://github.com/google/re2/wiki/Syntax.
#
# Default is no groups.
groups:
- title: Features
regexp: "^.*feat[(\\w)]*:+.*$"
regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$'
order: 0
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
regexp: '^.*?bug(\([[:word:]]+\))??!?:.+$'
order: 1
- title: Others
order: 999

0 comments on commit 0ea4f1d

Please sign in to comment.