feat: Automatic link for gitlab merge requests (#160)

This commit is contained in:
Thierno IB. BARRY 2021-07-09 09:26:33 +02:00 committed by GitHub
parent 45e01f0f7f
commit 21b98bd56f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 13 deletions

View file

@ -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
}

View file

@ -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",
},
},
),