0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-13 21:30:40 +00:00

feat: support # as footer metadata separator

This commit is contained in:
Beatriz Vieira 2021-02-15 03:23:02 -03:00
parent abeae14b96
commit 57995c3458
2 changed files with 28 additions and 13 deletions

View File

@ -97,7 +97,7 @@ func (p MessageProcessorImpl) Validate(message string) error {
// Enhance add metadata on commit message. // Enhance add metadata on commit message.
func (p MessageProcessorImpl) Enhance(branch string, message string) (string, error) { 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 return "", nil //enhance disabled
} }
@ -151,8 +151,12 @@ func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string)
if footer.Len() > 0 { if footer.Len() > 0 {
footer.WriteString("\n") footer.WriteString("\n")
} }
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)) footer.WriteString(fmt.Sprintf("%s: %s", p.messageCfg.IssueFooterConfig().Key, issue))
} }
}
return header.String(), msg.Body, footer.String() return header.String(), msg.Body, footer.String()
} }
@ -225,8 +229,13 @@ func hasFooter(message string) bool {
return false return false
} }
func hasIssueID(message, issueKeyName string) bool { func hasIssueID(message string, issueConfig CommitMessageFooterConfig) bool {
r := regexp.MustCompile(fmt.Sprintf("(?m)^%s: .+$", issueKeyName)) 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) return r.MatchString(message)
} }

View File

@ -174,26 +174,32 @@ jira: JIRA-123`
) )
func Test_hasIssueID(t *testing.T) { func Test_hasIssueID(t *testing.T) {
cfgColon := CommitMessageFooterConfig{Key: "jira"}
cfgHash := CommitMessageFooterConfig{Key: "jira", UseHash: true}
tests := []struct { tests := []struct {
name string name string
message string message string
issueKeyName string issueCfg CommitMessageFooterConfig
want bool want bool
}{ }{
{"single line without issue", "feat: something", "jira", false}, {"single line without issue", "feat: something", cfgColon, false},
{"multi line without issue", `feat: something {"multi line without issue", `feat: something
yay`, "jira", false}, yay`, cfgColon, false},
{"multi line without jira issue", `feat: something {"multi line without jira issue", `feat: something
jira1: JIRA-123`, "jira", false}, jira1: JIRA-123`, cfgColon, false},
{"multi line with issue", `feat: something {"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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("hasIssueID() = %v, want %v", got, tt.want)
} }
}) })