feat(cmd/joj3): optional group in commit msg
Some checks failed
submodules sync / sync (push) Successful in 46s
build / build (push) Failing after 1m30s
build / trigger-build-image (push) Has been skipped

This commit is contained in:
张泊明518370910136 2024-11-27 03:10:04 -05:00
parent d2c53205ac
commit 7d9cd723f0
GPG Key ID: D47306D7062CDA9D
2 changed files with 35 additions and 6 deletions

View File

@ -126,6 +126,7 @@ type ConventionalCommit struct {
Type string
Scope string
Description string
Group string
Body string
Footer string
}
@ -148,7 +149,7 @@ func GetCommitMsg() (msg string, err error) {
}
func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?)(\n\n(.+?))?(\n\n(.+))?$`)
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?(\[([^\]]+)\])?)(\n\n(.+?))?(\n\n(.+))?$`)
matches := re.FindStringSubmatch(strings.TrimSpace(commit))
if matches == nil {
return nil, fmt.Errorf("invalid conventional commit format")
@ -157,8 +158,9 @@ func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
Type: matches[1],
Scope: matches[3],
Description: strings.TrimSpace(matches[4]),
Body: strings.TrimSpace(matches[6]),
Footer: strings.TrimSpace(matches[8]),
Group: matches[6],
Body: strings.TrimSpace(matches[8]),
Footer: strings.TrimSpace(matches[10]),
}
return cc, nil
}
@ -329,7 +331,12 @@ func CheckExpire(conf *Conf) error {
func MatchGroups(conf *Conf, conventionalCommit *ConventionalCommit) []string {
seen := make(map[string]bool)
keywords := []string{}
for _, stage := range conf.Stage.Stages {
loweredCommitGroup := strings.ToLower(conventionalCommit.Group)
loweredCommitDescription := strings.ToLower(conventionalCommit.Description)
for i, stage := range conf.Stage.Stages {
if loweredCommitGroup == "all" {
conf.Stage.Stages[i].Group = ""
}
if stage.Group == "" {
continue
}
@ -341,9 +348,9 @@ func MatchGroups(conf *Conf, conventionalCommit *ConventionalCommit) []string {
}
slog.Info("group keywords from stages", "keywords", keywords)
groups := []string{}
loweredDescription := strings.ToLower(conventionalCommit.Description)
for _, keyword := range keywords {
if strings.Contains(loweredDescription, keyword) {
if strings.Contains(loweredCommitGroup, keyword) ||
strings.Contains(loweredCommitDescription, keyword) { // TODO: remove me
groups = append(groups, keyword)
}
}

View File

@ -51,6 +51,28 @@ func TestParseConventionalCommit(t *testing.T) {
},
wantErr: false,
},
{
name: "Commit with body and group",
commit: "docs: update README [group]\n\nAdd installation instructions and improve examples",
want: &ConventionalCommit{
Type: "docs",
Description: "update README [group]",
Group: "group",
Body: "Add installation instructions and improve examples",
},
wantErr: false,
},
{
name: "Commit with body and empty group",
commit: "docs: update README []\n\nAdd installation instructions and improve examples",
want: &ConventionalCommit{
Type: "docs",
Description: "update README []",
Group: "",
Body: "Add installation instructions and improve examples",
},
wantErr: false,
},
{
name: "Full commit with body and footer",
commit: "feat(auth)!: implement OAuth2\n\nThis commit adds OAuth2 support to the authentication system.\n\nBREAKING CHANGE: Previous authentication tokens are no longer valid.",