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

feat: add support to append a prefix to issue value on footer metadata

This commit is contained in:
Beatriz Vieira 2021-07-18 17:18:00 -03:00
parent 8c266a5a20
commit 50ba69318c
5 changed files with 24 additions and 4 deletions

View File

@ -17,5 +17,6 @@ commit-message:
footer: footer:
issue: issue:
key: issue key: issue
add-value-prefix: '#'
issue: issue:
regex: '#[0-9]+' regex: '#?[0-9]+'

View File

@ -127,6 +127,7 @@ commit-message:
- Jira - Jira
- JIRA - JIRA
use-hash: false # If false, use :<space> separator. If true, use <space># separator. use-hash: false # If false, use :<space> separator. If true, use <space># separator.
add-value-prefix: '' # Add a prefix to issue value.
issue: issue:
regex: '[A-Z]+-[0-9]+' # Regex for issue id. regex: '[A-Z]+-[0-9]+' # Regex for issue id.
``` ```

View File

@ -25,9 +25,10 @@ type CommitMessageScopeConfig struct {
// CommitMessageFooterConfig config footer metadata. // CommitMessageFooterConfig config footer metadata.
type CommitMessageFooterConfig struct { type CommitMessageFooterConfig struct {
Key string `yaml:"key"` Key string `yaml:"key"`
KeySynonyms []string `yaml:"key-synonyms"` KeySynonyms []string `yaml:"key-synonyms"`
UseHash bool `yaml:"use-hash"` UseHash bool `yaml:"use-hash"`
AddValuePrefix string `yaml:"add-value-prefix"`
} }
// CommitMessageIssueConfig issue preferences. // CommitMessageIssueConfig issue preferences.

View File

@ -117,6 +117,9 @@ func (p MessageProcessorImpl) Enhance(branch string, message string) (string, er
} }
func formatIssueFooter(cfg CommitMessageFooterConfig, issue string) string { func formatIssueFooter(cfg CommitMessageFooterConfig, issue string) string {
if !strings.HasPrefix(issue, cfg.AddValuePrefix) {
issue = cfg.AddValuePrefix + issue
}
if cfg.UseHash { if cfg.UseHash {
return fmt.Sprintf("%s #%s", cfg.Key, strings.TrimPrefix(issue, "#")) return fmt.Sprintf("%s #%s", cfg.Key, strings.TrimPrefix(issue, "#"))
} }

View File

@ -25,6 +25,15 @@ var ccfgHash = CommitMessageConfig{
Issue: CommitMessageIssueConfig{Regex: "[A-Z]+-[0-9]+"}, Issue: CommitMessageIssueConfig{Regex: "[A-Z]+-[0-9]+"},
} }
var ccfgGitIssue = CommitMessageConfig{
Types: []string{"feat", "fix"},
Scope: CommitMessageScopeConfig{},
Footer: map[string]CommitMessageFooterConfig{
"issue": {Key: "issue", KeySynonyms: []string{"Issue"}, UseHash: false, AddValuePrefix: "#"},
},
Issue: CommitMessageIssueConfig{Regex: "#?[0-9]+"},
}
var ccfgEmptyIssue = CommitMessageConfig{ var ccfgEmptyIssue = CommitMessageConfig{
Types: []string{"feat", "fix"}, Types: []string{"feat", "fix"},
Scope: CommitMessageScopeConfig{}, Scope: CommitMessageScopeConfig{},
@ -166,6 +175,9 @@ func TestMessageProcessorImpl_Enhance(t *testing.T) {
{"no issue on branch name", ccfg, "branch", "fix: fix something", "", true}, {"no issue on branch name", ccfg, "branch", "fix: fix something", "", true},
{"unexpected branch name", ccfg, "feature /JIRA-123", "fix: fix something", "", true}, {"unexpected branch name", ccfg, "feature /JIRA-123", "fix: fix something", "", true},
{"issue on branch name using hash", ccfgHash, "JIRA-123-some-description", "fix: fix something", "\njira #JIRA-123", false}, {"issue on branch name using hash", ccfgHash, "JIRA-123-some-description", "fix: fix something", "\njira #JIRA-123", false},
{"numeric issue on branch name", ccfgGitIssue, "#13", "fix: fix something", "\nissue: #13", false},
{"numeric issue on branch name without hash", ccfgGitIssue, "13", "fix: fix something", "\nissue: #13", false},
{"numeric issue on branch name with description without hash", ccfgGitIssue, "13-some-fix", "fix: fix something", "\nissue: #13", false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -343,6 +355,8 @@ func TestMessageProcessorImpl_Format(t *testing.T) {
{"with multiline body", ccfg, NewCommitMessage("feat", "", "something", multilineBody, "", ""), "feat: something", multilineBody, ""}, {"with multiline body", ccfg, NewCommitMessage("feat", "", "something", multilineBody, "", ""), "feat: something", multilineBody, ""},
{"full message", ccfg, NewCommitMessage("feat", "scope", "something", multilineBody, "JIRA-123", "breaks"), "feat(scope): something", multilineBody, fullFooter}, {"full message", ccfg, NewCommitMessage("feat", "scope", "something", multilineBody, "JIRA-123", "breaks"), "feat(scope): something", multilineBody, fullFooter},
{"config without issue key", ccfgEmptyIssue, NewCommitMessage("feat", "", "something", "", "JIRA-123", ""), "feat: something", "", ""}, {"config without issue key", ccfgEmptyIssue, NewCommitMessage("feat", "", "something", "", "JIRA-123", ""), "feat: something", "", ""},
{"with issue and issue prefix", ccfgGitIssue, NewCommitMessage("feat", "", "something", "", "123", ""), "feat: something", "", "issue: #123"},
{"with #issue and issue prefix", ccfgGitIssue, NewCommitMessage("feat", "", "something", "", "#123", ""), "feat: something", "", "issue: #123"},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {