diff --git a/cmd/joj3/conf/conf.go b/cmd/joj3/conf/conf.go index 7c3af10..04f2561 100644 --- a/cmd/joj3/conf/conf.go +++ b/cmd/joj3/conf/conf.go @@ -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) } } diff --git a/cmd/joj3/conf/conf_test.go b/cmd/joj3/conf/conf_test.go index ccab160..653c7e1 100644 --- a/cmd/joj3/conf/conf_test.go +++ b/cmd/joj3/conf/conf_test.go @@ -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.",