From 57995c3458f51afe9549bc6b8a39924a2de30a7e Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 15 Feb 2021 03:23:02 -0300 Subject: [PATCH] feat: support # as footer metadata separator --- sv/message.go | 17 +++++++++++++---- sv/message_test.go | 24 +++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/sv/message.go b/sv/message.go index 1ba1ed9..2208b06 100644 --- a/sv/message.go +++ b/sv/message.go @@ -97,7 +97,7 @@ func (p MessageProcessorImpl) Validate(message string) error { // Enhance add metadata on commit message. func (p MessageProcessorImpl) Enhance(branch string, message string) (string, error) { - if p.branchesCfg.DisableIssue || p.messageCfg.IssueFooterConfig().Key == "" || hasIssueID(message, p.messageCfg.IssueFooterConfig().Key) { + if p.branchesCfg.DisableIssue || p.messageCfg.IssueFooterConfig().Key == "" || hasIssueID(message, p.messageCfg.IssueFooterConfig()) { return "", nil //enhance disabled } @@ -151,7 +151,11 @@ func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string) if footer.Len() > 0 { footer.WriteString("\n") } - footer.WriteString(fmt.Sprintf("%s: %s", p.messageCfg.IssueFooterConfig().Key, issue)) + if p.messageCfg.IssueFooterConfig().UseHash { + footer.WriteString(fmt.Sprintf("%s #%s", p.messageCfg.IssueFooterConfig().Key, strings.TrimPrefix(issue, "#"))) + } else { + footer.WriteString(fmt.Sprintf("%s: %s", p.messageCfg.IssueFooterConfig().Key, issue)) + } } return header.String(), msg.Body, footer.String() @@ -225,8 +229,13 @@ func hasFooter(message string) bool { return false } -func hasIssueID(message, issueKeyName string) bool { - r := regexp.MustCompile(fmt.Sprintf("(?m)^%s: .+$", issueKeyName)) +func hasIssueID(message string, issueConfig CommitMessageFooterConfig) bool { + var r *regexp.Regexp + if issueConfig.UseHash { + r = regexp.MustCompile(fmt.Sprintf("(?m)^%s #.+$", issueConfig.Key)) + } else { + r = regexp.MustCompile(fmt.Sprintf("(?m)^%s: .+$", issueConfig.Key)) + } return r.MatchString(message) } diff --git a/sv/message_test.go b/sv/message_test.go index 68b3c7f..2bdeb2a 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -174,26 +174,32 @@ jira: JIRA-123` ) func Test_hasIssueID(t *testing.T) { + cfgColon := CommitMessageFooterConfig{Key: "jira"} + cfgHash := CommitMessageFooterConfig{Key: "jira", UseHash: true} + tests := []struct { - name string - message string - issueKeyName string - want bool + name string + message string + issueCfg CommitMessageFooterConfig + want bool }{ - {"single line without issue", "feat: something", "jira", false}, + {"single line without issue", "feat: something", cfgColon, false}, {"multi line without issue", `feat: something -yay`, "jira", false}, +yay`, cfgColon, false}, {"multi line without jira issue", `feat: something -jira1: JIRA-123`, "jira", false}, +jira1: JIRA-123`, cfgColon, false}, {"multi line with issue", `feat: something -jira: JIRA-123`, "jira", true}, +jira: JIRA-123`, cfgColon, true}, + {"multi line with issue and hash", `feat: something + +jira #JIRA-123`, cfgHash, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := hasIssueID(tt.message, tt.issueKeyName); got != tt.want { + if got := hasIssueID(tt.message, tt.issueCfg); got != tt.want { t.Errorf("hasIssueID() = %v, want %v", got, tt.want) } })