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 Type string
Scope string Scope string
Description string Description string
Group string
Body string Body string
Footer string Footer string
} }
@ -148,7 +149,7 @@ func GetCommitMsg() (msg string, err error) {
} }
func parseConventionalCommit(commit string) (*ConventionalCommit, 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)) matches := re.FindStringSubmatch(strings.TrimSpace(commit))
if matches == nil { if matches == nil {
return nil, fmt.Errorf("invalid conventional commit format") return nil, fmt.Errorf("invalid conventional commit format")
@ -157,8 +158,9 @@ func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
Type: matches[1], Type: matches[1],
Scope: matches[3], Scope: matches[3],
Description: strings.TrimSpace(matches[4]), Description: strings.TrimSpace(matches[4]),
Body: strings.TrimSpace(matches[6]), Group: matches[6],
Footer: strings.TrimSpace(matches[8]), Body: strings.TrimSpace(matches[8]),
Footer: strings.TrimSpace(matches[10]),
} }
return cc, nil return cc, nil
} }
@ -329,7 +331,12 @@ func CheckExpire(conf *Conf) error {
func MatchGroups(conf *Conf, conventionalCommit *ConventionalCommit) []string { func MatchGroups(conf *Conf, conventionalCommit *ConventionalCommit) []string {
seen := make(map[string]bool) seen := make(map[string]bool)
keywords := []string{} 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 == "" { if stage.Group == "" {
continue continue
} }
@ -341,9 +348,9 @@ func MatchGroups(conf *Conf, conventionalCommit *ConventionalCommit) []string {
} }
slog.Info("group keywords from stages", "keywords", keywords) slog.Info("group keywords from stages", "keywords", keywords)
groups := []string{} groups := []string{}
loweredDescription := strings.ToLower(conventionalCommit.Description)
for _, keyword := range keywords { 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) groups = append(groups, keyword)
} }
} }

View File

@ -51,6 +51,28 @@ func TestParseConventionalCommit(t *testing.T) {
}, },
wantErr: false, 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", 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.", 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.",