From 21b98bd56f012c0fba882c0950c41af5ce5e4506 Mon Sep 17 00:00:00 2001 From: "Thierno IB. BARRY" Date: Fri, 9 Jul 2021 09:26:33 +0200 Subject: [PATCH] feat: Automatic link for gitlab merge requests (#160) --- processor.go | 16 +++++++++++----- processor_test.go | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/processor.go b/processor.go index 505fec6c..06580408 100644 --- a/processor.go +++ b/processor.go @@ -70,12 +70,14 @@ func (p *GitHubProcessor) addLinks(input string) string { // // The following processing is performed // - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://gitlab.com/tsuyoshiwada)) -// - Automatic link to references (#123 -> [#123](https://gitlab.com/owner/repo/issues/123)) +// - Automatic link to references issues (#123 -> [#123](https://gitlab.com/owner/repo/issues/123)) +// - Automatic link to references merge request (!123 -> [#123](https://gitlab.com/owner/repo/merge_requests/123)) type GitLabProcessor struct { - Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://gitlab.com") - config *Config - reMention *regexp.Regexp - reIssue *regexp.Regexp + Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://gitlab.com") + config *Config + reMention *regexp.Regexp + reIssue *regexp.Regexp + reMergeRequest *regexp.Regexp } // Bootstrap ... @@ -90,6 +92,7 @@ func (p *GitLabProcessor) Bootstrap(config *Config) { p.reMention = regexp.MustCompile(`@(\w+)`) p.reIssue = regexp.MustCompile(`(?i)#(\d+)`) + p.reMergeRequest = regexp.MustCompile(`(?i)!(\d+)`) } // ProcessCommit ... @@ -118,6 +121,9 @@ func (p *GitLabProcessor) addLinks(input string) string { // issues input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1)") + // merge requests + input = p.reMergeRequest.ReplaceAllString(input, "[!$1]("+repoURL+"/merge_requests/$1)") + return input } diff --git a/processor_test.go b/processor_test.go index 88e8502e..1b15bd3f 100644 --- a/processor_test.go +++ b/processor_test.go @@ -83,30 +83,34 @@ func TestGitLabProcessor(t *testing.T) { assert.Equal( &Commit{ - Header: "message [@foo](https://gitlab.com/foo) [#123](https://example.com/issues/123)", - Subject: "message [@foo](https://gitlab.com/foo) [#123](https://example.com/issues/123)", + Header: "message [@foo](https://gitlab.com/foo) [#123](https://example.com/issues/123) [!345](https://example.com/merge_requests/345)", + Subject: "message [@foo](https://gitlab.com/foo) [#123](https://example.com/issues/123) [!345](https://example.com/merge_requests/345)", Body: `issue [#456](https://example.com/issues/456) multiline [#789](https://example.com/issues/789) +merge request [!345](https://example.com/merge_requests/345) [@foo](https://gitlab.com/foo), [@bar](https://gitlab.com/bar)`, Notes: []*Note{ { - Body: `issue1 [#11](https://example.com/issues/11) + Body: `issue1 [#11](https://example.com/issues/11) [!33](https://example.com/merge_requests/33) issue2 [#22](https://example.com/issues/22) +merge request [!33](https://example.com/merge_requests/33) gh-56 hoge fuga`, }, }, }, processor.ProcessCommit( &Commit{ - Header: "message @foo #123", - Subject: "message @foo #123", + Header: "message @foo #123 !345", + Subject: "message @foo #123 !345", Body: `issue #456 multiline #789 +merge request !345 @foo, @bar`, Notes: []*Note{ { - Body: `issue1 #11 + Body: `issue1 #11 !33 issue2 #22 +merge request !33 gh-56 hoge fuga`, }, }, @@ -117,13 +121,13 @@ gh-56 hoge fuga`, assert.Equal( &Commit{ Revert: &Revert{ - Header: "revert header [@mention](https://gitlab.com/mention) [#123](https://example.com/issues/123)", + Header: "revert header [@mention](https://gitlab.com/mention) [#123](https://example.com/issues/123) [!345](https://example.com/merge_requests/345)", }, }, processor.ProcessCommit( &Commit{ Revert: &Revert{ - Header: "revert header @mention #123", + Header: "revert header @mention #123 !345", }, }, ),