From 50ba69318cbb70c7efcac7284ab78f64142852bb Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sun, 18 Jul 2021 17:18:00 -0300 Subject: [PATCH] feat: add support to append a prefix to issue value on footer metadata --- .sv4git.yml | 3 ++- README.md | 1 + sv/config.go | 7 ++++--- sv/message.go | 3 +++ sv/message_test.go | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.sv4git.yml b/.sv4git.yml index 3f38617..192ca92 100644 --- a/.sv4git.yml +++ b/.sv4git.yml @@ -17,5 +17,6 @@ commit-message: footer: issue: key: issue + add-value-prefix: '#' issue: - regex: '#[0-9]+' + regex: '#?[0-9]+' diff --git a/README.md b/README.md index 4bab651..8c6390c 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ commit-message: - Jira - JIRA use-hash: false # If false, use : separator. If true, use # separator. + add-value-prefix: '' # Add a prefix to issue value. issue: regex: '[A-Z]+-[0-9]+' # Regex for issue id. ``` diff --git a/sv/config.go b/sv/config.go index 4f86b10..d45aad0 100644 --- a/sv/config.go +++ b/sv/config.go @@ -25,9 +25,10 @@ type CommitMessageScopeConfig struct { // CommitMessageFooterConfig config footer metadata. type CommitMessageFooterConfig struct { - Key string `yaml:"key"` - KeySynonyms []string `yaml:"key-synonyms"` - UseHash bool `yaml:"use-hash"` + Key string `yaml:"key"` + KeySynonyms []string `yaml:"key-synonyms"` + UseHash bool `yaml:"use-hash"` + AddValuePrefix string `yaml:"add-value-prefix"` } // CommitMessageIssueConfig issue preferences. diff --git a/sv/message.go b/sv/message.go index 7ae43a5..99f99bd 100644 --- a/sv/message.go +++ b/sv/message.go @@ -117,6 +117,9 @@ func (p MessageProcessorImpl) Enhance(branch string, message string) (string, er } func formatIssueFooter(cfg CommitMessageFooterConfig, issue string) string { + if !strings.HasPrefix(issue, cfg.AddValuePrefix) { + issue = cfg.AddValuePrefix + issue + } if cfg.UseHash { return fmt.Sprintf("%s #%s", cfg.Key, strings.TrimPrefix(issue, "#")) } diff --git a/sv/message_test.go b/sv/message_test.go index fe84a99..6e0f3c1 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -25,6 +25,15 @@ var ccfgHash = CommitMessageConfig{ 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{ Types: []string{"feat", "fix"}, Scope: CommitMessageScopeConfig{}, @@ -166,6 +175,9 @@ func TestMessageProcessorImpl_Enhance(t *testing.T) { {"no issue on branch name", ccfg, "branch", "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}, + {"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 { 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, ""}, {"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", "", ""}, + {"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 { t.Run(tt.name, func(t *testing.T) {