Merge pull request #12 from git-chglog/feat/gitlab

Add support for GitLab
This commit is contained in:
tsuyoshi wada 2018-03-12 00:34:31 +09:00 committed by GitHub
commit 58cf56e664
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 219 additions and 11 deletions

View file

@ -30,3 +30,9 @@ coverage:
.PHONY: install
install:
go install ./cmd/git-chglog
.PHONY: changelog
changelog:
@git tag $(tag) > /dev/null 2>&1
@git-chglog $(tag)
@git tag -d $(tag) > /dev/null 2>&1

View file

@ -283,9 +283,9 @@ Git execution command.
CHANGELOG style. Automatic linking of issues and notices, initial value setting such as merges etc. are done automatically.
| Required | Type | Default | Description |
|:---------|:-------|:---------|:------------------------------|
| N | String | `"none"` | Should be `"github"` `"none"` |
| Required | Type | Default | Description |
|:---------|:-------|:---------|:-----------------------------------------|
| N | String | `"none"` | Should be `"github"` `"gitlab"` `"none"` |
### `template`
@ -432,7 +432,7 @@ See godoc [RenderData][doc-render-data] for available variables.
| Name | Status | Features |
|:-------------------------------------------|:----------------------|:-------------------------------------------------------|
| [GitHub](https://github.com/) | :white_check_mark: | Mentions automatic link. Automatic link to references. |
| [GitLab](https://about.gitlab.com/) | :white_medium_square: | - |
| [GitLab](https://about.gitlab.com/) | :white_check_mark: | Mentions automatic link. Automatic link to references. |
| [Bitbucket](https://bitbucket.org/product) | :white_medium_square: | - |
> :memo: Even with styles that are not yet supported, it is possible to make ordinary CHANGELOG.

View file

@ -147,6 +147,43 @@ func (config *Config) normalizeStyleOfGitHub() {
config.Options = opts
}
// For GitLab
func (config *Config) normalizeStyleOfGitLab() {
opts := config.Options
if len(opts.Issues.Prefix) == 0 {
opts.Issues.Prefix = []string{
"#",
}
}
if len(opts.Refs.Actions) == 0 {
opts.Refs.Actions = []string{
"close",
"closes",
"closed",
"closing",
"fix",
"fixes",
"fixed",
"fixing",
"resolve",
"resolves",
"resolved",
"resolving",
}
}
if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 {
opts.Merges.Pattern = "^Merge branch '.*' into '(.*)'$"
opts.Merges.PatternMaps = []string{
"Source",
}
}
config.Options = opts
}
// Convert ...
func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
info := config.Info

View file

@ -48,7 +48,7 @@ func (*customTemplateBuilderImpl) versionHeader(style, template string) string {
// parts
switch style {
case styleGitHub:
case styleGitHub, styleGitLab:
tpl = "<a name=\"{{.Tag.Name}}\"></a>\n"
tagName = "{{if .Tag.Previous}}[{{.Tag.Name}}]({{$.Info.RepositoryURL}}/compare/{{.Tag.Previous.Name}}...{{.Tag.Name}}){{else}}{{.Tag.Name}}{{end}}"
}
@ -115,6 +115,8 @@ func (t *customTemplateBuilderImpl) merges(style string) string {
switch style {
case styleGitHub:
title = "Pull Requests"
case styleGitLab:
title = "Merge Requests"
default:
title = "Merges"
}

View file

@ -47,7 +47,7 @@ func (t *kacTemplateBuilderImpl) versionHeader(style string) string {
)
switch style {
case styleGitHub:
case styleGitHub, styleGitLab:
tagName = "{{if .Tag.Previous}}[{{.Tag.Name}}]{{else}}{{.Tag.Name}}{{end}}"
}
@ -87,6 +87,8 @@ func (t *kacTemplateBuilderImpl) merges(style string) string {
switch style {
case styleGitHub:
title = "Pull Requests"
case styleGitLab:
title = "Merge Requests"
default:
title = "Merges"
}
@ -107,12 +109,13 @@ func (*kacTemplateBuilderImpl) notes() string {
}
func (*kacTemplateBuilderImpl) footer(style string) string {
if style != styleGitHub {
return ""
}
return `{{if .Versions}}
switch style {
case styleGitHub, styleGitLab:
return `{{if .Versions}}
[Unreleased]: {{.Info.RepositoryURL}}/compare/{{$latest := index .Versions 0}}{{$latest.Tag.Name}}...HEAD{{range .Versions}}{{if .Tag.Previous}}
[{{.Tag.Name}}]: {{$.Info.RepositoryURL}}/compare/{{.Tag.Previous.Name}}...{{.Tag.Name}}{{end}}{{end}}
{{end}}`
default:
return ""
}
}

View file

@ -17,6 +17,7 @@ func NewProcessorFactory() *ProcessorFactory {
return &ProcessorFactory{
hostRegistry: map[string]string{
"github": "github.com",
"gitlab": "gitlab.com",
},
}
}
@ -41,6 +42,10 @@ func (factory *ProcessorFactory) Create(config *Config) (chglog.Processor, error
return &chglog.GitHubProcessor{
Host: fmt.Sprintf("%s://%s", obj.Scheme, obj.Host),
}, nil
case "gitlab.com":
return &chglog.GitLabProcessor{
Host: fmt.Sprintf("%s://%s", obj.Scheme, obj.Host),
}, nil
default:
return nil, nil
}

View file

@ -56,3 +56,39 @@ func TestProcessorFactoryForGitHub(t *testing.T) {
processor,
)
}
func TestProcessorFactoryForGitLab(t *testing.T) {
assert := assert.New(t)
factory := NewProcessorFactory()
// gitlab.com
processor, err := factory.Create(&Config{
Info: Info{
RepositoryURL: "https://gitlab.com/owner/repo",
},
})
assert.Nil(err)
assert.Equal(
&chglog.GitLabProcessor{
Host: "https://gitlab.com",
},
processor,
)
// self-hosted
processor, err = factory.Create(&Config{
Style: "gitlab",
Info: Info{
RepositoryURL: "https://original-gitserver.com/owner/repo",
},
})
assert.Nil(err)
assert.Equal(
&chglog.GitLabProcessor{
Host: "https://original-gitserver.com",
},
processor,
)
}

View file

@ -15,9 +15,11 @@ var (
// Styles
var (
styleGitHub = "github"
styleGitLab = "gitlab"
styleNone = "none"
styles = []string{
styleGitHub,
styleGitLab,
styleNone,
}
)

View file

@ -65,3 +65,58 @@ func (p *GitHubProcessor) addLinks(input string) string {
return input
}
// GitLabProcessor is optimized for CHANGELOG used in GitLab
//
// 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))
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
}
// Bootstrap ...
func (p *GitLabProcessor) Bootstrap(config *Config) {
p.config = config
if p.Host == "" {
p.Host = "https://gitlab.com"
} else {
p.Host = strings.TrimRight(p.Host, "/")
}
p.reMention = regexp.MustCompile("@(\\w+)")
p.reIssue = regexp.MustCompile("(?i)#(\\d+)")
}
// ProcessCommit ...
func (p *GitLabProcessor) ProcessCommit(commit *Commit) *Commit {
commit.Header = p.addLinks(commit.Header)
commit.Subject = p.addLinks(commit.Subject)
commit.Body = p.addLinks(commit.Body)
for _, note := range commit.Notes {
note.Body = p.addLinks(note.Body)
}
if commit.Revert != nil {
commit.Revert.Header = p.addLinks(commit.Revert.Header)
}
return commit
}
func (p *GitLabProcessor) addLinks(input string) string {
repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/")
// mentions
input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1)")
// issues
input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1)")
return input
}

View file

@ -67,3 +67,65 @@ gh-56 hoge fuga`,
),
)
}
func TestGitLabProcessor(t *testing.T) {
assert := assert.New(t)
config := &Config{
Info: &Info{
RepositoryURL: "https://example.com",
},
}
processor := &GitLabProcessor{}
processor.Bootstrap(config)
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)",
Body: `issue [#456](https://example.com/issues/456)
multiline [#789](https://example.com/issues/789)
[@foo](https://gitlab.com/foo), [@bar](https://gitlab.com/bar)`,
Notes: []*Note{
&Note{
Body: `issue1 [#11](https://example.com/issues/11)
issue2 [#22](https://example.com/issues/22)
gh-56 hoge fuga`,
},
},
},
processor.ProcessCommit(
&Commit{
Header: "message @foo #123",
Subject: "message @foo #123",
Body: `issue #456
multiline #789
@foo, @bar`,
Notes: []*Note{
&Note{
Body: `issue1 #11
issue2 #22
gh-56 hoge fuga`,
},
},
},
),
)
assert.Equal(
&Commit{
Revert: &Revert{
Header: "revert header [@mention](https://gitlab.com/mention) [#123](https://example.com/issues/123)",
},
},
processor.ProcessCommit(
&Commit{
Revert: &Revert{
Header: "revert header @mention #123",
},
},
),
)
}