Merge branch 'travis' of github.com:Nightapes/go-semantic-release into add_git_releases

This commit is contained in:
Felix Wiedmann
2019-05-15 21:31:23 +02:00
11 changed files with 139 additions and 128 deletions

11
.golangci.yml Normal file
View File

@@ -0,0 +1,11 @@
run:
tests: true
linters-settings:
golint:
min-confidence: 0
issues:
exclude-use-default: false
linters:
enable:
- golint

35
.travis.yml Normal file
View File

@@ -0,0 +1,35 @@
dist: xenial
language: go
go:
- 1.12.x
cache:
directories:
- $HOME/.cache/go-build
- $HOME/gopath/pkg/mod
services:
- docker
notifications:
email: false
git:
depth: 1
env:
- GO111MODULE=on
before_script:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0
script:
- golangci-lint run ./...
- go test -v ./...
- go build -o build/go-semantic-release ./cmd/go-semantic-release/
- GOOS=windows GOARCH=386 go build -o build/go-semantic-release.exe ./cmd/go-semantic-release/
branches:
except:
- /^v\d+\.\d+\.\d+$/

View File

@@ -1,3 +1,4 @@
// Package main as start point for go build
package main
import (
@@ -5,100 +6,39 @@ import (
"github.com/Nightapes/go-semantic-release/pkg/semanticrelease"
log "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1" // imports as package "cli"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("go-semantic-release", "A command-line for releasing software")
loglevel = app.Flag("loglevel", "Set loglevel.").Default("error").HintOptions("error", "warning", "info", "debug").Short('l').String()
nextCommand = app.Command("next", "Print next version")
nextRepository = nextCommand.Flag("repository", "Path to repository").String()
setCommand = app.Command("set", "Set version for current build.")
setRepository = setCommand.Flag("repository", "Path to repository").String()
setVersion = setCommand.Arg("version", "semver version").Required().String()
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "loglevel",
Value: "error",
Usage: "Set loglevel 'LEVEL",
},
}
app.Commands = []cli.Command{
{
Name: "version",
Aliases: []string{"v"},
Usage: "version commands",
Subcommands: []cli.Command{
{
Name: "set",
Usage: "set version `VERSION`",
Action: func(c *cli.Context) error {
setLoglevel(c.GlobalString("loglevel"))
path := c.String("path")
version := c.Args().First()
log.Infof("Version %s", version)
return semanticrelease.SetVersion(version, path)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "release-config.json",
Usage: "Load release configuration from `FILE`",
},
cli.StringFlag{
Name: "path, p",
Usage: "`PATH` to repro ",
},
},
},
{
Name: "next",
Usage: "get next `VERSION` or the set one ",
Action: func(c *cli.Context) error {
setLoglevel(c.GlobalString("loglevel"))
path := c.String("path")
return semanticrelease.GetNextVersion(path)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "release-config.json",
Usage: "Load release configuration from `FILE`",
},
cli.StringFlag{
Name: "path, p",
Usage: "`PATH` to repro ",
},
},
},
},
},
{
Name: "release",
Aliases: []string{},
Usage: "make release",
Action: func(c *cli.Context) error {
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "release-config.json",
Usage: "Load release configuration from `FILE`",
},
},
},
{
Name: "init",
Aliases: []string{},
Usage: "create config",
Action: func(c *cli.Context) error {
return nil
},
},
}
//gitutil.GetCommits(folder)
err := app.Run(os.Args)
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case nextCommand.FullCommand():
setLoglevel(*loglevel)
err := semanticrelease.GetNextVersion(*nextRepository)
if err != nil {
log.Fatal(err)
}
case setCommand.FullCommand():
setLoglevel(*loglevel)
log.Infof("Version %s", *setVersion)
err := semanticrelease.SetVersion(*setVersion, *setRepository)
if err != nil {
log.Fatal(err)
}
}
}
func setLoglevel(level string) {

7
go.mod
View File

@@ -4,10 +4,11 @@ go 1.12
require (
github.com/Masterminds/semver v1.4.2
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/sirupsen/logrus v1.4.1
github.com/urfave/cli v1.20.0 // indirect
golang.org/x/tools v0.0.0-20190508150211-cf84161cff3f // indirect
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/src-d/go-git.v4 v4.11.0
gopkg.in/urfave/cli.v1 v1.20.0
gopkg.in/yaml.v2 v2.2.2
)

19
go.sum
View File

@@ -2,13 +2,21 @@ github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITg
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
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/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
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=
@@ -26,6 +34,7 @@ github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnG
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -38,8 +47,6 @@ github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jW
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
@@ -50,8 +57,6 @@ 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/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/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=
@@ -59,8 +64,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=
golang.org/x/tools v0.0.0-20190508150211-cf84161cff3f h1:sjxqKRXfnzgJFg/igBXeLZoBVAKXuAAljgr+PcNr7u8=
golang.org/x/tools v0.0.0-20190508150211-cf84161cff3f/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
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=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -70,8 +75,6 @@ gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 h1:XWW/s5W18RaJpmo1l0IYGqXKuJITWRFuA45i
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
gopkg.in/src-d/go-git.v4 v4.11.0 h1:cJwWgJ0DXifrNrXM6RGN1Y2yR60Rr1zQ9Q5DX5S9qgU=
gopkg.in/src-d/go-git.v4 v4.11.0/go.mod h1:Vtut8izDyrM8BUVQnzJ+YvmNcem2J89EmfZYCkLokZk=
gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0=
gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

View File

@@ -1,3 +1,4 @@
// Package analyzer provides different commit analyzer
package analyzer
import (
@@ -5,20 +6,23 @@ import (
log "github.com/sirupsen/logrus"
)
//Analyzer struct
type Analyzer struct {
CommitFormat string
}
//Rules for commits
type Rules struct {
Tag string
Release string
}
type AnalyzeCommit interface {
Analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool)
GetRules() []Rules
type analyzeCommit interface {
analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool)
getRules() []Rules
}
//AnalyzedCommit struct
type AnalyzedCommit struct {
Commit gitutil.Commit
ParsedMessage string
@@ -26,6 +30,7 @@ type AnalyzedCommit struct {
ParsedBreakingChangeMessage string
}
//New Analyzer struct for given commit format
func New(format string) *Analyzer {
return &Analyzer{
CommitFormat: format,
@@ -33,13 +38,14 @@ func New(format string) *Analyzer {
}
// Analyze commits and return commits splitted by major,minor,patch
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
var commitAnalayzer AnalyzeCommit
var commitAnalayzer analyzeCommit
switch a.CommitFormat {
case "angular":
log.Infof("analyze angular format")
commitAnalayzer = NewAngular()
commitAnalayzer = newAngular()
}
analyzedCommits := make(map[string][]AnalyzedCommit)
@@ -48,8 +54,8 @@ func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit
analyzedCommits["patch"] = make([]AnalyzedCommit, 0)
for _, commit := range commits {
for _, rule := range commitAnalayzer.GetRules() {
analyzedCommit, hasBreakingChange := commitAnalayzer.Analyze(commit, rule.Tag)
for _, rule := range commitAnalayzer.getRules() {
analyzedCommit, hasBreakingChange := commitAnalayzer.analyze(commit, rule.Tag)
if hasBreakingChange {
analyzedCommits["major"] = append(analyzedCommits["major"], analyzedCommit)
} else {

View File

@@ -1,3 +1,4 @@
// Package analyzer provides different commit analyzer
package analyzer
import (
@@ -7,13 +8,13 @@ import (
"github.com/Nightapes/go-semantic-release/internal/gitutil"
)
type Angular struct {
type angular struct {
rules []Rules
regex string
}
func NewAngular() *Angular {
return &Angular{
func newAngular() *angular {
return &angular{
regex: `(TAG)(?:\((.*)\))?: (.*)`,
rules: []Rules{
{
@@ -31,11 +32,11 @@ func NewAngular() *Angular {
}
}
func (a *Angular) GetRules() []Rules {
func (a *angular) getRules() []Rules {
return a.rules
}
func (a *Angular) Analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool) {
func (a *angular) analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool) {
analyzed := AnalyzedCommit{
Commit: commit,

View File

@@ -1,3 +1,4 @@
// Package gitutil provides helper methods for git
package gitutil
import (
@@ -10,29 +11,33 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Commit struct
type Commit struct {
Message string
Author string
Hash string
}
type GitUtils struct {
// GitUtil struct
type GitUtil struct {
Repository *git.Repository
}
func New(folder string) (*GitUtils, error) {
// New GitUtil struct and open git repository
func New(folder string) (*GitUtil, error) {
r, err := git.PlainOpen(folder)
if err != nil {
return nil, err
}
utils := &GitUtils{
utils := &GitUtil{
Repository: r,
}
return utils, nil
}
func (g *GitUtils) GetHash() (string, error) {
// GetHash from git HEAD
func (g *GitUtil) GetHash() (string, error) {
ref, err := g.Repository.Head()
if err != nil {
return "", err
@@ -40,20 +45,22 @@ func (g *GitUtils) GetHash() (string, error) {
return ref.Hash().String(), nil
}
func (g *GitUtils) GetBranch() (string, error) {
// GetBranch from git HEAD
func (g *GitUtil) GetBranch() (string, error) {
ref, err := g.Repository.Head()
if err != nil {
return "", err
}
if !ref.Name().IsBranch() {
return "", fmt.Errorf("No branch found, found %s, please checkout a branch (git checkout <BRANCH>)", ref.Name().String())
return "", fmt.Errorf("no branch found, found %s, please checkout a branch (git checkout <BRANCH>)", ref.Name().String())
}
return ref.Name().Short(), nil
}
func (g *GitUtils) GetLastVersion() (*semver.Version, string, error) {
// GetLastVersion from git tags
func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) {
log.Debugf("GetLastVersion")
@@ -103,7 +110,8 @@ func (g *GitUtils) GetLastVersion() (*semver.Version, string, error) {
return tags[0], tagObject.Target.String(), nil
}
func (g *GitUtils) GetCommits(lastTagHash string) ([]Commit, error) {
// GetCommits from git hash to HEAD
func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) {
log.Printf("Read head")
ref, err := g.Repository.Head()
@@ -136,5 +144,5 @@ func (g *GitUtils) GetCommits(lastTagHash string) ([]Commit, error) {
return nil
})
return commits, nil
return commits, err
}

View File

View File

@@ -1,3 +1,4 @@
// Package storage helper for saving/reading version file
package storage
import (

View File

@@ -1,3 +1,4 @@
// Package semanticrelease provides public methods to include in own code
package semanticrelease
import (
@@ -10,7 +11,7 @@ import (
log "github.com/sirupsen/logrus"
)
// GetNextVersion from .version or calculate new
// GetNextVersion from .version or calculate new from commits
func GetNextVersion(repro string) error {
util, err := gitutil.New(repro)
if err != nil {
@@ -38,8 +39,11 @@ func GetNextVersion(repro string) error {
if lastVersion == nil {
defaultVersion, _ := semver.NewVersion("1.0.0")
SetVersion(defaultVersion.String(), repro)
fmt.Printf(defaultVersion.String())
err := SetVersion(defaultVersion.String(), repro)
if err != nil {
return err
}
fmt.Printf("%s", defaultVersion.String())
return nil
}
@@ -64,12 +68,16 @@ func GetNextVersion(repro string) error {
newVersion = lastVersion.IncPatch()
}
SetVersion(newVersion.String(), repro)
fmt.Printf(newVersion.String())
err = SetVersion(newVersion.String(), repro)
if err != nil {
return err
}
fmt.Printf("%s", newVersion.String())
return err
}
//SetVersion for git repository
func SetVersion(version string, repro string) error {
util, err := gitutil.New(repro)
@@ -109,6 +117,3 @@ func SetVersion(version string, repro string) error {
return storage.Write(newVersionContent)
}
func Release() {
}