merge travis to origin

This commit is contained in:
Felix Wiedmann
2019-06-13 18:21:47 +02:00
8 changed files with 101 additions and 40 deletions

1
.gitignore vendored
View File

@@ -13,3 +13,4 @@ go-semantic-release
*.out *.out
.version .version
.vscode/settings.json .vscode/settings.json
CHANGELOG.md

View File

@@ -1,5 +1,15 @@
# go-semantic-release # go-semantic-release
## Release Types
| Type | Git tag | Changelog | Release | Write access git | Api token |
|--- |:---: |:---: |:---: |:---: |:---: |
| `git` | :white_check_mark: | | | :white_check_mark:| |
| `github` | :white_check_mark: | :white_check_mark: | :white_check_mark:| | :white_check_mark: |
| `gitlab` | :white_check_mark: | :white_check_mark: | :white_check_mark:| | :white_check_mark: |
## Build ## Build
`go build ./cmd/go-semantic-release/` `go build ./cmd/go-semantic-release/`

3
go.sum
View File

@@ -16,6 +16,7 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjr
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw= github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@@ -64,6 +65,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJV
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas= golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -72,6 +74,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

View File

@@ -2,6 +2,8 @@
package analyzer package analyzer
import ( import (
"fmt"
"github.com/Nightapes/go-semantic-release/internal/gitutil" "github.com/Nightapes/go-semantic-release/internal/gitutil"
"github.com/Nightapes/go-semantic-release/pkg/config" "github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -9,8 +11,8 @@ import (
//Analyzer struct //Analyzer struct
type Analyzer struct { type Analyzer struct {
CommitFormat string analyzeCommit analyzeCommit
Config config.ChangelogConfig Config config.ChangelogConfig
} }
//Rule for commits //Rule for commits
@@ -38,24 +40,30 @@ type AnalyzedCommit struct {
} }
//New Analyzer struct for given commit format //New Analyzer struct for given commit format
func New(format string, config config.ChangelogConfig) *Analyzer { func New(format string, config config.ChangelogConfig) (*Analyzer, error) {
return &Analyzer{ analyzer := &Analyzer{
CommitFormat: format, Config: config,
Config: config,
} }
switch format {
case "angular":
log.Debugf("Commit format set to angular")
analyzer.analyzeCommit = newAngular()
default:
return nil, fmt.Errorf("Invalid commit format: %s", format)
}
return analyzer, nil
}
// GetRules from current mode
func (a *Analyzer) GetRules() []Rule {
return a.analyzeCommit.getRules()
} }
// Analyze commits and return commits splitted by major,minor,patch // Analyze commits and return commits splitted by major,minor,patch
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit { func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
var commitAnalayzer analyzeCommit
switch a.CommitFormat {
case "angular":
log.Debugf("Commit format set to angular")
commitAnalayzer = newAngular()
}
analyzedCommits := make(map[string][]AnalyzedCommit) analyzedCommits := make(map[string][]AnalyzedCommit)
analyzedCommits["major"] = make([]AnalyzedCommit, 0) analyzedCommits["major"] = make([]AnalyzedCommit, 0)
analyzedCommits["minor"] = make([]AnalyzedCommit, 0) analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
@@ -63,8 +71,8 @@ func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit
analyzedCommits["none"] = make([]AnalyzedCommit, 0) analyzedCommits["none"] = make([]AnalyzedCommit, 0)
for _, commit := range commits { for _, commit := range commits {
for _, rule := range commitAnalayzer.getRules() { for _, rule := range a.analyzeCommit.getRules() {
analyzedCommit, hasBreakingChange, err := commitAnalayzer.analyze(commit, rule) analyzedCommit, hasBreakingChange, err := a.analyzeCommit.analyze(commit, rule)
if err == nil { if err == nil {
if a.Config.PrintAll { if a.Config.PrintAll {
analyzedCommit.Print = true analyzedCommit.Print = true

View File

@@ -64,7 +64,7 @@ func newAngular() *angular {
Changelog: false, Changelog: false,
}, { }, {
Tag: "build", Tag: "build",
TagString: "Changes to ci config", TagString: "Changes to CI/CD",
Release: "none", Release: "none",
Changelog: false, Changelog: false,
}, },

View File

@@ -2,48 +2,65 @@ package changelog
import ( import (
"bytes" "bytes"
"strings"
"text/template" "text/template"
"time" "time"
"github.com/Nightapes/go-semantic-release/internal/analyzer" "github.com/Nightapes/go-semantic-release/internal/analyzer"
"github.com/Nightapes/go-semantic-release/pkg/config" "github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
) )
const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})` const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
const defaultChangelog string = `{{ $version := .Version -}} const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
{{ $backtick := .Backtick -}} {{ range $key := .Order }}
# v{{.Version}} ({{.Now.Format "2006-01-02"}}) {{ $commits := index $.Commits $key}} {{if $commits -}}
{{ range $key, $commits := .Commits }}
### {{ $key }} ### {{ $key }}
{{ range $index,$commit := $commits -}}
{{range $index,$commit := $commits}}* **{{$backtick}}{{$commit.Scope}}:{{$backtick}}** {{$commit.ParsedMessage}} * **{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})) {{end}}
{{ end -}}
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}
` `
type changelogContent struct { type changelogContent struct {
Commits map[string][]analyzer.AnalyzedCommit Commits map[string][]analyzer.AnalyzedCommit
Order []string
Version string Version string
Now time.Time Now time.Time
Backtick string Backtick string
HasURL bool
URL string
} }
//CommitFormat struct //Changelog struct
type Changelog struct { type Changelog struct {
config *config.ReleaseConfig config *config.ReleaseConfig
rules []analyzer.Rule
} }
//New Changelog struct for generating changelog from commits //New Changelog struct for generating changelog from commits
func New(config *config.ReleaseConfig) *Changelog { func New(config *config.ReleaseConfig, rules []analyzer.Rule) *Changelog {
return &Changelog{ return &Changelog{
config: config, config: config,
rules: rules,
} }
} }
// GenerateChanglog from given commits // GenerateChanglog from given commits
func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string][]analyzer.AnalyzedCommit) (string, string, error) { func (c *Changelog) GenerateChanglog(version, url string, analyzedCommits map[string][]analyzer.AnalyzedCommit) (string, string, error) {
commitsPerScope := map[string][]analyzer.AnalyzedCommit{} commitsPerScope := map[string][]analyzer.AnalyzedCommit{}
order := make([]string, 0)
for _, rule := range c.rules {
log.Debugf("Add %s to list", rule.TagString)
if rule.Changelog || c.config.Changelog.PrintAll {
order = append(order, rule.TagString)
}
}
for _, commits := range analyzedCommits { for _, commits := range analyzedCommits {
for _, commit := range commits { for _, commit := range commits {
if commit.Print { if commit.Print {
@@ -60,23 +77,38 @@ func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string]
Commits: commitsPerScope, Commits: commitsPerScope,
Now: time.Now(), Now: time.Now(),
Backtick: "`", Backtick: "`",
Order: order,
HasURL: url != "",
URL: url,
} }
title, err := generateTemplate(defaultChangelogTitle, changelogContent) title, err := generateTemplate(defaultChangelogTitle, changelogContent)
if err != nil {
return "", "", err
}
content, err := generateTemplate(defaultChangelog, changelogContent) content, err := generateTemplate(defaultChangelog, changelogContent)
return title, content, err return title, content, err
} }
func generateTemplate(text string, values changelogContent) (string, error) { func generateTemplate(text string, values changelogContent) (string, error) {
funcMap := template.FuncMap{
"replace": replace,
}
var tpl bytes.Buffer var tpl bytes.Buffer
tmpl, err := template.New("template").Parse(text) tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
if err != nil { if err != nil {
return "", nil return "", err
} }
err = tmpl.Execute(&tpl, values) err = tmpl.Execute(&tpl, values)
if err != nil { if err != nil {
return "", nil return "", err
} }
return tpl.String(), nil return tpl.String(), nil
} }
func replace(input, from, to string) string {
return strings.Replace(input, from, to, -1)
}

View File

@@ -40,8 +40,8 @@ type ReleaseConfig struct {
Branch map[string]string `yaml:"branch"` Branch map[string]string `yaml:"branch"`
Changelog ChangelogConfig `yaml:"changelog,omitempty"` Changelog ChangelogConfig `yaml:"changelog,omitempty"`
Release string `yaml:"release,omitempty"` Release string `yaml:"release,omitempty"`
Github GitlabConfig `yaml:"github, omitempty"` Github GitlabConfig `yaml:"github,omitempty"`
Gitlab GitlabConfig `yaml:"gitlab, omitempty"` Gitlab GitlabConfig `yaml:"gitlab,omitempty"`
Assets []Asset `yaml:"assets"` Assets []Asset `yaml:"assets"`
IsPreRelease, IsDraft bool IsPreRelease, IsDraft bool
} }
@@ -62,7 +62,5 @@ func Read(configPath string) (*ReleaseConfig, error) {
log.Debugf("Found config %+v", releaseConfig) log.Debugf("Found config %+v", releaseConfig)
return &ReleaseConfig{ return &releaseConfig, nil
IsPreRelease: false,
IsDraft: false}, nil
} }

View File

@@ -59,6 +59,7 @@ func (s *SemanticRelease) GetNextVersion(repro string, force bool) (string, erro
if lastVersion == nil { if lastVersion == nil {
defaultVersion, _ := semver.NewVersion("1.0.0") defaultVersion, _ := semver.NewVersion("1.0.0")
newVersion = *defaultVersion newVersion = *defaultVersion
lastVersion = defaultVersion
} else { } else {
newVersion = *lastVersion newVersion = *lastVersion
} }
@@ -70,7 +71,10 @@ func (s *SemanticRelease) GetNextVersion(repro string, force bool) (string, erro
log.Debugf("Found %d commits till last release", len(commits)) log.Debugf("Found %d commits till last release", len(commits))
a := analyzer.New(s.config.CommitFormat, s.config.Changelog) a, err := analyzer.New(s.config.CommitFormat, s.config.Changelog)
if err != nil {
return "", err
}
result := a.Analyze(commits) result := a.Analyze(commits)
currentBranch, err := util.GetBranch() currentBranch, err := util.GetBranch()
@@ -101,8 +105,8 @@ func (s *SemanticRelease) GetNextVersion(repro string, force bool) (string, erro
if err != nil { if err != nil {
return "", err return "", err
} }
c := changelog.New(s.config) c := changelog.New(s.config, a.GetRules())
c.GenerateChanglog(newVersion.String(), result) c.GenerateChanglog(newVersion.String(), "https://github.com/Nightapes/go-semantic-release/commit/{{hash}}", result)
return newVersion.String(), err return newVersion.String(), err
} }
@@ -202,12 +206,17 @@ func (s *SemanticRelease) GetChangelog(repro, file string) error {
log.Debugf("Found %d commits till last release", len(commits)) log.Debugf("Found %d commits till last release", len(commits))
a := analyzer.New(s.config.CommitFormat, s.config.Changelog) a, err := analyzer.New(s.config.CommitFormat, s.config.Changelog)
if err != nil {
return err
}
result := a.Analyze(commits) result := a.Analyze(commits)
c := changelog.New(s.config) c := changelog.New(s.config, a.GetRules())
_, content, err := c.GenerateChanglog(nextVersion, result) _, content, err := c.GenerateChanglog(nextVersion, "https://github.com/Nightapes/go-semantic-release/commit/{{hash}}", result)
if err != nil {
return err
}
return ioutil.WriteFile(file, []byte(content), 0644) return ioutil.WriteFile(file, []byte(content), 0644)
} }