mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-21 12:00:40 +00:00
feat: support # as footer metadata separator
This commit is contained in:
parent
abeae14b96
commit
57995c3458
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user