You've already forked go-semantic-release
feat(changelog): add order and hash url
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -13,3 +13,4 @@ go-semantic-release
|
||||
*.out
|
||||
.version
|
||||
.vscode/settings.json
|
||||
CHANGELOG.md
|
||||
|
||||
10
README.md
10
README.md
@@ -1,5 +1,15 @@
|
||||
# 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
|
||||
|
||||
`go build ./cmd/go-semantic-release/`
|
||||
|
||||
12
go.sum
12
go.sum
@@ -16,8 +16,14 @@ 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/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
|
||||
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/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-github/v25 v25.1.1 h1:6eW++i/CXcR5GKfYaaJT7oJJtHNU+/iiw55noEPNVao=
|
||||
github.com/google/go-github/v25 v25.1.1/go.mod h1:6z5pC69qHtrPJ0sXPsj4BLnd82b+r6sLB7qcBoRZqpw=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
@@ -57,6 +63,10 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
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/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/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-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
@@ -64,6 +74,8 @@ 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/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
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=
|
||||
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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -9,8 +11,8 @@ import (
|
||||
|
||||
//Analyzer struct
|
||||
type Analyzer struct {
|
||||
CommitFormat string
|
||||
Config config.ChangelogConfig
|
||||
analyzeCommit analyzeCommit
|
||||
Config config.ChangelogConfig
|
||||
}
|
||||
|
||||
//Rule for commits
|
||||
@@ -38,24 +40,30 @@ type AnalyzedCommit struct {
|
||||
}
|
||||
|
||||
//New Analyzer struct for given commit format
|
||||
func New(format string, config config.ChangelogConfig) *Analyzer {
|
||||
return &Analyzer{
|
||||
CommitFormat: format,
|
||||
Config: config,
|
||||
func New(format string, config config.ChangelogConfig) (*Analyzer, error) {
|
||||
analyzer := &Analyzer{
|
||||
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
|
||||
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["major"] = 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)
|
||||
|
||||
for _, commit := range commits {
|
||||
for _, rule := range commitAnalayzer.getRules() {
|
||||
analyzedCommit, hasBreakingChange, err := commitAnalayzer.analyze(commit, rule)
|
||||
for _, rule := range a.analyzeCommit.getRules() {
|
||||
analyzedCommit, hasBreakingChange, err := a.analyzeCommit.analyze(commit, rule)
|
||||
if err == nil {
|
||||
if a.Config.PrintAll {
|
||||
analyzedCommit.Print = true
|
||||
|
||||
@@ -64,7 +64,7 @@ func newAngular() *angular {
|
||||
Changelog: false,
|
||||
}, {
|
||||
Tag: "build",
|
||||
TagString: "Changes to ci config",
|
||||
TagString: "Changes to CI/CD",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
},
|
||||
|
||||
@@ -2,48 +2,65 @@ package changelog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/internal/analyzer"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
|
||||
const defaultChangelog string = `{{ $version := .Version -}}
|
||||
{{ $backtick := .Backtick -}}
|
||||
# v{{.Version}} ({{.Now.Format "2006-01-02"}})
|
||||
{{ range $key, $commits := .Commits }}
|
||||
const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
|
||||
{{ range $key := .Order }}
|
||||
{{ $commits := index $.Commits $key}} {{if $commits -}}
|
||||
### {{ $key }}
|
||||
|
||||
{{range $index,$commit := $commits}}* **{{$backtick}}{{$commit.Scope}}:{{$backtick}}** {{$commit.ParsedMessage}}
|
||||
{{ range $index,$commit := $commits -}}
|
||||
* **{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})) {{end}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
`
|
||||
|
||||
type changelogContent struct {
|
||||
Commits map[string][]analyzer.AnalyzedCommit
|
||||
Order []string
|
||||
Version string
|
||||
Now time.Time
|
||||
Backtick string
|
||||
HasURL bool
|
||||
URL string
|
||||
}
|
||||
|
||||
//CommitFormat struct
|
||||
//Changelog struct
|
||||
type Changelog struct {
|
||||
config *config.ReleaseConfig
|
||||
rules []analyzer.Rule
|
||||
}
|
||||
|
||||
//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{
|
||||
config: config,
|
||||
rules: rules,
|
||||
}
|
||||
}
|
||||
|
||||
// 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{}
|
||||
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 _, commit := range commits {
|
||||
if commit.Print {
|
||||
@@ -60,23 +77,38 @@ func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string]
|
||||
Commits: commitsPerScope,
|
||||
Now: time.Now(),
|
||||
Backtick: "`",
|
||||
Order: order,
|
||||
HasURL: url != "",
|
||||
URL: url,
|
||||
}
|
||||
|
||||
title, err := generateTemplate(defaultChangelogTitle, changelogContent)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
content, err := generateTemplate(defaultChangelog, changelogContent)
|
||||
|
||||
return title, content, err
|
||||
}
|
||||
|
||||
func generateTemplate(text string, values changelogContent) (string, error) {
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"replace": replace,
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
tmpl, err := template.New("template").Parse(text)
|
||||
tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return "", err
|
||||
}
|
||||
err = tmpl.Execute(&tpl, values)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return "", err
|
||||
}
|
||||
return tpl.String(), nil
|
||||
}
|
||||
|
||||
func replace(input, from, to string) string {
|
||||
return strings.Replace(input, from, to, -1)
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ type ReleaseConfig struct {
|
||||
Branch map[string]string `yaml:"branch"`
|
||||
Changelog ChangelogConfig `yaml:"changelog,omitempty"`
|
||||
Release string `yaml:"release,omitempty"`
|
||||
Github GitlabConfig `yaml:"github, omitempty"`
|
||||
Gitlab GitlabConfig `yaml:"gitlab, omitempty"`
|
||||
Github GitlabConfig `yaml:"github,omitempty"`
|
||||
Gitlab GitlabConfig `yaml:"gitlab,omitempty"`
|
||||
Assets []Asset `yaml:"assets"`
|
||||
IsPreRelease, IsDraft bool
|
||||
}
|
||||
@@ -62,7 +62,5 @@ func Read(configPath string) (*ReleaseConfig, error) {
|
||||
|
||||
log.Debugf("Found config %+v", releaseConfig)
|
||||
|
||||
return &ReleaseConfig{
|
||||
IsPreRelease: false,
|
||||
IsDraft: false}, nil
|
||||
return &releaseConfig, nil
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ func (s *SemanticRelease) GetNextVersion(repro string, force bool) (string, erro
|
||||
if lastVersion == nil {
|
||||
defaultVersion, _ := semver.NewVersion("1.0.0")
|
||||
newVersion = *defaultVersion
|
||||
lastVersion = defaultVersion
|
||||
} else {
|
||||
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))
|
||||
|
||||
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)
|
||||
|
||||
currentBranch, err := util.GetBranch()
|
||||
@@ -101,8 +105,8 @@ func (s *SemanticRelease) GetNextVersion(repro string, force bool) (string, erro
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c := changelog.New(s.config)
|
||||
c.GenerateChanglog(newVersion.String(), result)
|
||||
c := changelog.New(s.config, a.GetRules())
|
||||
c.GenerateChanglog(newVersion.String(), "https://github.com/Nightapes/go-semantic-release/commit/{{hash}}", result)
|
||||
|
||||
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))
|
||||
|
||||
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)
|
||||
|
||||
c := changelog.New(s.config)
|
||||
_, content, err := c.GenerateChanglog(nextVersion, result)
|
||||
|
||||
c := changelog.New(s.config, a.GetRules())
|
||||
_, 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)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user