diff --git a/cmd/go-semantic-release/commands/changelog.go b/cmd/go-semantic-release/commands/changelog.go index 625b020..87adf63 100644 --- a/cmd/go-semantic-release/commands/changelog.go +++ b/cmd/go-semantic-release/commands/changelog.go @@ -45,13 +45,13 @@ var changelogCmd = &cobra.Command{ return err } - releaseVersion, commits, err := s.GetNextVersion(provider, force) + releaseVersion, err := s.GetNextVersion(provider, force) if err != nil { return err } - log.Debugf("Found %d commits till last release", len(commits)) + log.Debugf("Found %d commits till last release", len(releaseVersion.Commits)) - generatedChangelog, err := s.GetChangelog(commits, releaseVersion) + generatedChangelog, err := s.GetChangelog(releaseVersion) if err != nil { return err } diff --git a/cmd/go-semantic-release/commands/next.go b/cmd/go-semantic-release/commands/next.go index 65ed0bc..fc6499c 100644 --- a/cmd/go-semantic-release/commands/next.go +++ b/cmd/go-semantic-release/commands/next.go @@ -43,7 +43,7 @@ var nextCmd = &cobra.Command{ return nil } - releaseVersion, _, err := s.GetNextVersion(provider, force) + releaseVersion, err := s.GetNextVersion(provider, force) if err != nil { return err } diff --git a/cmd/go-semantic-release/commands/root.go b/cmd/go-semantic-release/commands/root.go index 0dcfbe8..87f4421 100644 --- a/cmd/go-semantic-release/commands/root.go +++ b/cmd/go-semantic-release/commands/root.go @@ -31,11 +31,7 @@ func Execute(version string) { } func init() { - currentDir, err := os.Getwd() - if err != nil { - panic(err) - } - rootCmd.PersistentFlags().StringP("repository", "r", currentDir, "Path to repository") + rootCmd.PersistentFlags().StringP("repository", "r", "./", "Path to repository") rootCmd.PersistentFlags().StringP("loglevel", "l", "error", "Set loglevel") rootCmd.PersistentFlags().StringP("config", "c", ".release.yml", "Path to config file") rootCmd.PersistentFlags().Bool("no-cache", false, "Ignore cache, don't use in ci build") diff --git a/internal/analyzer/analyzer.go b/internal/analyzer/analyzer.go index 8e6d1be..14cde1c 100644 --- a/internal/analyzer/analyzer.go +++ b/internal/analyzer/analyzer.go @@ -4,47 +4,30 @@ package analyzer import ( "fmt" - "github.com/Nightapes/go-semantic-release/internal/gitutil" + "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" log "github.com/sirupsen/logrus" ) //Analyzer struct type Analyzer struct { - analyzeCommit analyzeCommit - Config config.ChangelogConfig + analyzeCommits analyzeCommits + Config config.ChangelogConfig } -//Release types, like major -type Release string - -//Scope of the commit, like feat, fix,.. -type Scope string - //Rule for commits type Rule struct { Tag string TagString string - Release Release + Release shared.Release Changelog bool } -type analyzeCommit interface { - analyze(commit gitutil.Commit, tag Rule) (AnalyzedCommit, bool, error) +type analyzeCommits interface { + analyze(commit shared.Commit, tag Rule) (shared.AnalyzedCommit, bool, error) getRules() []Rule } -//AnalyzedCommit struct -type AnalyzedCommit struct { - Commit gitutil.Commit - ParsedMessage string - Scope Scope - ParsedBreakingChangeMessage string - Tag string - TagString string - Print bool -} - //New Analyzer struct for given commit format func New(format string, config config.ChangelogConfig) (*Analyzer, error) { analyzer := &Analyzer{ @@ -52,9 +35,9 @@ func New(format string, config config.ChangelogConfig) (*Analyzer, error) { } switch format { - case "angular": - log.Debugf("Commit format set to angular") - analyzer.analyzeCommit = newAngular() + case ANGULAR: + analyzer.analyzeCommits = newAngular() + log.Debugf("Commit format set to %s", ANGULAR) default: return nil, fmt.Errorf("invalid commit format: %s", format) } @@ -64,21 +47,21 @@ func New(format string, config config.ChangelogConfig) (*Analyzer, error) { // GetRules from current mode func (a *Analyzer) GetRules() []Rule { - return a.analyzeCommit.getRules() + return a.analyzeCommits.getRules() } // Analyze commits and return commits splitted by major,minor,patch -func (a *Analyzer) Analyze(commits []gitutil.Commit) map[Release][]AnalyzedCommit { +func (a *Analyzer) Analyze(commits []shared.Commit) map[shared.Release][]shared.AnalyzedCommit { - analyzedCommits := make(map[Release][]AnalyzedCommit) - analyzedCommits["major"] = make([]AnalyzedCommit, 0) - analyzedCommits["minor"] = make([]AnalyzedCommit, 0) - analyzedCommits["patch"] = make([]AnalyzedCommit, 0) - analyzedCommits["none"] = make([]AnalyzedCommit, 0) + analyzedCommits := make(map[shared.Release][]shared.AnalyzedCommit) + analyzedCommits["major"] = make([]shared.AnalyzedCommit, 0) + analyzedCommits["minor"] = make([]shared.AnalyzedCommit, 0) + analyzedCommits["patch"] = make([]shared.AnalyzedCommit, 0) + analyzedCommits["none"] = make([]shared.AnalyzedCommit, 0) for _, commit := range commits { - for _, rule := range a.analyzeCommit.getRules() { - analyzedCommit, hasBreakingChange, err := a.analyzeCommit.analyze(commit, rule) + for _, rule := range a.analyzeCommits.getRules() { + analyzedCommit, hasBreakingChange, err := a.analyzeCommits.analyze(commit, rule) if err == nil { if a.Config.PrintAll { analyzedCommit.Print = true diff --git a/internal/analyzer/angular.go b/internal/analyzer/angular.go index 47fe6d2..3f0f898 100644 --- a/internal/analyzer/angular.go +++ b/internal/analyzer/angular.go @@ -8,17 +8,22 @@ import ( log "github.com/sirupsen/logrus" - "github.com/Nightapes/go-semantic-release/internal/gitutil" + "github.com/Nightapes/go-semantic-release/internal/shared" ) type angular struct { rules []Rule regex string + log *log.Entry } +// ANGULAR identifer +const ANGULAR = "angular" + func newAngular() *angular { return &angular{ regex: `(TAG)(?:\((.*)\))?: (.*)`, + log: log.WithField("analyzer", ANGULAR), rules: []Rule{ { Tag: "feat", @@ -76,9 +81,9 @@ func (a *angular) getRules() []Rule { return a.rules } -func (a *angular) analyze(commit gitutil.Commit, rule Rule) (AnalyzedCommit, bool, error) { +func (a *angular) analyze(commit shared.Commit, rule Rule) (shared.AnalyzedCommit, bool, error) { - analyzed := AnalyzedCommit{ + analyzed := shared.AnalyzedCommit{ Commit: commit, Tag: rule.Tag, TagString: rule.TagString, @@ -89,13 +94,13 @@ func (a *angular) analyze(commit gitutil.Commit, rule Rule) (AnalyzedCommit, boo if len(matches) >= 1 { if len(matches[0]) >= 3 { - analyzed.Scope = Scope(matches[0][2]) + analyzed.Scope = shared.Scope(matches[0][2]) message := strings.Join(matches[0][3:], "") if !strings.Contains(message, "BREAKING CHANGE:") { analyzed.ParsedMessage = strings.Trim(message, " ") - log.Tracef("%s: found %s", commit.Message, rule.Tag) + a.log.Tracef("%s: found %s", commit.Message, rule.Tag) return analyzed, false, nil } breakingChange := strings.SplitN(message, "BREAKING CHANGE:", 2) @@ -103,11 +108,11 @@ func (a *angular) analyze(commit gitutil.Commit, rule Rule) (AnalyzedCommit, boo analyzed.ParsedMessage = strings.Trim(breakingChange[0], " ") analyzed.ParsedBreakingChangeMessage = strings.Trim(breakingChange[1], " ") - log.Tracef(" %s, BREAKING CHANGE found", commit.Message) + a.log.Tracef(" %s, BREAKING CHANGE found", commit.Message) return analyzed, true, nil } } - log.Tracef("%s does not match %s, skip", commit.Message, rule.Tag) + a.log.Tracef("%s does not match %s, skip", commit.Message, rule.Tag) return analyzed, false, fmt.Errorf("not found") } diff --git a/internal/analyzer/angular_test.go b/internal/analyzer/angular_test.go index c49b046..4aedc59 100644 --- a/internal/analyzer/angular_test.go +++ b/internal/analyzer/angular_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/Nightapes/go-semantic-release/internal/analyzer" - "github.com/Nightapes/go-semantic-release/internal/gitutil" + "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" "github.com/stretchr/testify/assert" ) @@ -13,15 +13,15 @@ func TestAngular(t *testing.T) { testConfigs := []struct { testCase string - commits []gitutil.Commit - analyzedCommits map[analyzer.Release][]analyzer.AnalyzedCommit + commits []shared.Commit + analyzedCommits map[shared.Release][]shared.AnalyzedCommit }{ { testCase: "feat", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", @@ -33,12 +33,12 @@ func TestAngular(t *testing.T) { Print: true, }, }, - "major": []analyzer.AnalyzedCommit{}, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + "major": []shared.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, - commits: []gitutil.Commit{ - gitutil.Commit{ + commits: []shared.Commit{ + shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", @@ -47,10 +47,10 @@ func TestAngular(t *testing.T) { }, { testCase: "feat breaking change", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", @@ -62,9 +62,9 @@ func TestAngular(t *testing.T) { Print: true, }, }, - "major": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + "major": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(internal/changelog): my first break BREAKING CHANGE: change api to v2", Author: "me", Hash: "12345668", @@ -77,16 +77,16 @@ func TestAngular(t *testing.T) { ParsedBreakingChangeMessage: "change api to v2", }, }, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, - commits: []gitutil.Commit{ - gitutil.Commit{ + commits: []shared.Commit{ + shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", }, - gitutil.Commit{ + shared.Commit{ Message: "feat(internal/changelog): my first break BREAKING CHANGE: change api to v2", Author: "me", Hash: "12345668", @@ -95,14 +95,14 @@ func TestAngular(t *testing.T) { }, { testCase: "invalid", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{}, - "major": []analyzer.AnalyzedCommit{}, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{}, + "major": []shared.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, - commits: []gitutil.Commit{ - gitutil.Commit{ + commits: []shared.Commit{ + shared.Commit{ Message: "internal/changelog: my first commit", Author: "me", Hash: "12345667", @@ -111,10 +111,10 @@ func TestAngular(t *testing.T) { }, { testCase: "feat and build", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", @@ -126,9 +126,9 @@ func TestAngular(t *testing.T) { Print: true, }, }, - "none": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + "none": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "build(internal/changelog): my first build", Author: "me", Hash: "12345668", @@ -141,16 +141,16 @@ func TestAngular(t *testing.T) { ParsedBreakingChangeMessage: "", }, }, - "patch": []analyzer.AnalyzedCommit{}, - "major": []analyzer.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "major": []shared.AnalyzedCommit{}, }, - commits: []gitutil.Commit{ - gitutil.Commit{ + commits: []shared.Commit{ + shared.Commit{ Message: "feat(internal/changelog): my first commit", Author: "me", Hash: "12345667", }, - gitutil.Commit{ + shared.Commit{ Message: "build(internal/changelog): my first build", Author: "me", Hash: "12345668", diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 0c33d48..8118127 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -2,52 +2,35 @@ package cache import ( - log "github.com/sirupsen/logrus" "io/ioutil" "path" + log "github.com/sirupsen/logrus" + "github.com/Masterminds/semver" "github.com/Nightapes/go-semantic-release/internal/shared" "gopkg.in/yaml.v2" ) -// ReleaseVersion struct -type ReleaseVersion struct { - Last ReleaseVersionEntry `yaml:"last"` - Next ReleaseVersionEntry `yaml:"next"` - Branch string `yaml:"branch"` - Draft bool `yaml:"draft"` -} - -//ReleaseVersionEntry struct -type ReleaseVersionEntry struct { - Commit string `yaml:"commit"` - Version string `yaml:"version"` -} - // Write version into .version func Write(repository string, releaseVersion shared.ReleaseVersion) error { completePath := path.Join(path.Dir(repository), ".version") - toCache := &ReleaseVersion{ - Next: ReleaseVersionEntry{ - Commit: releaseVersion.Next.Commit, - Version: releaseVersion.Next.Version.String(), - }, - Last: ReleaseVersionEntry{ - Commit: releaseVersion.Last.Commit, - Version: releaseVersion.Last.Version.String(), - }, - Branch: releaseVersion.Branch, - Draft: releaseVersion.Draft, + if releaseVersion.Last.Version != nil { + releaseVersion.Last.VersionString = releaseVersion.Last.Version.String() } - data, err := yaml.Marshal(toCache) + if releaseVersion.Next.Version != nil { + releaseVersion.Next.VersionString = releaseVersion.Next.Version.String() + } + + //toCache := &ReleaseVersion(releaseVersion) + data, err := yaml.Marshal(releaseVersion) if err != nil { return err } - log.Infof("Save %s with hash %s to cache", releaseVersion.Next.Version.String(), releaseVersion.Next.Commit) + log.Infof("Save %s with hash %s to cache %s", releaseVersion.Next.Version.String(), releaseVersion.Next.Commit, completePath) return ioutil.WriteFile(completePath, data, 0644) } @@ -57,37 +40,26 @@ func Read(repository string) (*shared.ReleaseVersion, error) { content, err := ioutil.ReadFile(completePath) if err != nil { - return &shared.ReleaseVersion{}, err + log.Warnf("Could not read cache %s, will ignore cache", completePath) + return &shared.ReleaseVersion{}, nil } - var parsedContent ReleaseVersion + var parsedContent shared.ReleaseVersion err = yaml.Unmarshal(content, &parsedContent) if err != nil { return &shared.ReleaseVersion{}, err } - nextVersion, err := semver.NewVersion(parsedContent.Next.Version) + parsedContent.Next.Version, err = semver.NewVersion(parsedContent.Next.VersionString) if err != nil { return nil, err } - lastVersion, err := semver.NewVersion(parsedContent.Last.Version) + parsedContent.Last.Version, err = semver.NewVersion(parsedContent.Last.VersionString) if err != nil { return nil, err } - releaseVersion := &shared.ReleaseVersion{ - Next: shared.ReleaseVersionEntry{ - Commit: parsedContent.Next.Commit, - Version: nextVersion, - }, - Last: shared.ReleaseVersionEntry{ - Commit: parsedContent.Last.Commit, - Version: lastVersion, - }, - Branch: parsedContent.Branch, - Draft: parsedContent.Draft, - } log.Infof("Found cache, will return cached version %s", parsedContent.Next.Version) - return releaseVersion, nil + return &parsedContent, nil } diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 07d619e..d128c80 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -14,8 +14,9 @@ import ( func TestReadCacheNotFound(t *testing.T) { - _, err := cache.Read("notfound/dir") - assert.Errorf(t, err, "Read non exsiting file") + resp, err := cache.Read("notfound/dir") + assert.NoErrorf(t, err, "Read non exsiting file") + assert.NotNil(t, resp) } @@ -43,15 +44,34 @@ func TestWriteAndReadCache(t *testing.T) { content := shared.ReleaseVersion{ Last: shared.ReleaseVersionEntry{ - Commit: "12345", - Version: createVersion("1.0.0"), + Commit: "12345", + Version: createVersion("1.0.0"), + VersionString: "1.0.0", }, Next: shared.ReleaseVersionEntry{ - Commit: "12346", - Version: createVersion("1.1.0"), + Commit: "12346", + Version: createVersion("1.1.0"), + VersionString: "1.1.0", }, Branch: "master", Draft: true, + Commits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ + Message: "Message", + Author: "Author", + Hash: "Hash", + }, + ParsedMessage: "add gitlab as relase option", + Scope: "releaser", + ParsedBreakingChangeMessage: "", + Tag: "feat", + TagString: "Features", + Print: true, + }, + }, + }, } defer os.RemoveAll(dir) diff --git a/internal/calculator/calculator.go b/internal/calculator/calculator.go index d2541fb..69c6eca 100644 --- a/internal/calculator/calculator.go +++ b/internal/calculator/calculator.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/Masterminds/semver" - "github.com/Nightapes/go-semantic-release/internal/analyzer" + "github.com/Nightapes/go-semantic-release/internal/shared" log "github.com/sirupsen/logrus" ) @@ -40,7 +40,7 @@ func (c *Calculator) IncPrerelease(preReleaseType string, version *semver.Versio } //CalculateNewVersion from given commits and lastversion -func (c *Calculator) CalculateNewVersion(commits map[analyzer.Release][]analyzer.AnalyzedCommit, lastVersion *semver.Version, releaseType string, firstRelease bool) (semver.Version, bool) { +func (c *Calculator) CalculateNewVersion(commits map[shared.Release][]shared.AnalyzedCommit, lastVersion *semver.Version, releaseType string, firstRelease bool) (semver.Version, bool) { switch releaseType { case "beta", "alpha": if len(commits["major"]) > 0 || len(commits["minor"]) > 0 || len(commits["patch"]) > 0 { diff --git a/internal/calculator/calculator_test.go b/internal/calculator/calculator_test.go index 5c8f2b4..50a1577 100644 --- a/internal/calculator/calculator_test.go +++ b/internal/calculator/calculator_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/Masterminds/semver" - "github.com/Nightapes/go-semantic-release/internal/analyzer" "github.com/Nightapes/go-semantic-release/internal/calculator" + "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/stretchr/testify/assert" ) @@ -74,20 +74,20 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { nextVersion string isDraft bool isFirst bool - analyzedCommits map[analyzer.Release][]analyzer.AnalyzedCommit + analyzedCommits map[shared.Release][]shared.AnalyzedCommit }{ { testCase: "version with preRelease alpha", releaseType: "alpha", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.0-alpha.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{}, }, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: true, @@ -97,13 +97,13 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "beta", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.0-beta.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{}, }, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: true, @@ -113,11 +113,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "alpha", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{}, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, @@ -127,11 +127,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "release", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "minor": []analyzer.AnalyzedCommit{}, - "patch": []analyzer.AnalyzedCommit{}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "minor": []shared.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{}, + "none": []shared.AnalyzedCommit{}, }, isFirst: true, isDraft: false, @@ -141,11 +141,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "rc", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.0-rc.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "minor": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "patch": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "minor": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "patch": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, @@ -155,11 +155,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "rc", lastVersion: createVersion("1.0.0-rc.0"), nextVersion: "1.0.0-rc.1", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "patch": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "patch": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, @@ -169,11 +169,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "release", lastVersion: createVersion("1.0.0"), nextVersion: "2.0.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "minor": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "patch": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "minor": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "patch": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, @@ -183,11 +183,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "release", lastVersion: createVersion("1.0.0"), nextVersion: "1.1.0", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "patch": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "patch": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, @@ -197,11 +197,11 @@ func TestCalculator_CalculateNewVersion(t *testing.T) { releaseType: "release", lastVersion: createVersion("1.0.0"), nextVersion: "1.0.1", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "major": []analyzer.AnalyzedCommit{}, - "minor": []analyzer.AnalyzedCommit{}, - "patch": []analyzer.AnalyzedCommit{analyzer.AnalyzedCommit{}}, - "none": []analyzer.AnalyzedCommit{}, + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "major": []shared.AnalyzedCommit{}, + "minor": []shared.AnalyzedCommit{}, + "patch": []shared.AnalyzedCommit{shared.AnalyzedCommit{}}, + "none": []shared.AnalyzedCommit{}, }, isFirst: false, isDraft: false, diff --git a/internal/changelog/changelog.go b/internal/changelog/changelog.go index e00a29f..cdbe754 100644 --- a/internal/changelog/changelog.go +++ b/internal/changelog/changelog.go @@ -34,8 +34,8 @@ introduced by commit: ` type changelogContent struct { - Commits map[string][]analyzer.AnalyzedCommit - BreakingChanges []analyzer.AnalyzedCommit + Commits map[string][]shared.AnalyzedCommit + BreakingChanges []shared.AnalyzedCommit Order []string Version string Now time.Time @@ -49,6 +49,7 @@ type Changelog struct { config *config.ReleaseConfig rules []analyzer.Rule releaseTime time.Time + log *log.Entry } //New Changelog struct for generating changelog from commits @@ -57,18 +58,19 @@ func New(config *config.ReleaseConfig, rules []analyzer.Rule, releaseTime time.T config: config, rules: rules, releaseTime: releaseTime, + log: log.WithField("changelog", config.CommitFormat), } } // GenerateChanglog from given commits -func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConfig, analyzedCommits map[analyzer.Release][]analyzer.AnalyzedCommit) (*shared.GeneratedChangelog, error) { +func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConfig, analyzedCommits map[shared.Release][]shared.AnalyzedCommit) (*shared.GeneratedChangelog, error) { - commitsPerScope := map[string][]analyzer.AnalyzedCommit{} - commitsBreakingChange := []analyzer.AnalyzedCommit{} + commitsPerScope := map[string][]shared.AnalyzedCommit{} + commitsBreakingChange := []shared.AnalyzedCommit{} order := make([]string, 0) for _, rule := range c.rules { - log.Debugf("Add %s to list", rule.TagString) + c.log.Tracef("Add %s to list", rule.TagString) if rule.Changelog || c.config.Changelog.PrintAll { order = append(order, rule.TagString) } @@ -82,7 +84,7 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf continue } if _, ok := commitsPerScope[commit.TagString]; !ok { - commitsPerScope[commit.TagString] = make([]analyzer.AnalyzedCommit, 0) + commitsPerScope[commit.TagString] = make([]shared.AnalyzedCommit, 0) } commitsPerScope[commit.TagString] = append(commitsPerScope[commit.TagString], commit) } diff --git a/internal/changelog/changelog_test.go b/internal/changelog/changelog_test.go index b4a473a..31f3616 100644 --- a/internal/changelog/changelog_test.go +++ b/internal/changelog/changelog_test.go @@ -5,7 +5,6 @@ import ( "github.com/Nightapes/go-semantic-release/internal/analyzer" "github.com/Nightapes/go-semantic-release/internal/changelog" - "github.com/Nightapes/go-semantic-release/internal/gitutil" "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" "github.com/stretchr/testify/assert" @@ -23,16 +22,16 @@ func TestChangelog(t *testing.T) { testConfigs := []struct { testCase string - analyzedCommits map[analyzer.Release][]analyzer.AnalyzedCommit + analyzedCommits map[shared.Release][]shared.AnalyzedCommit result *shared.GeneratedChangelog hasError bool }{ { testCase: "feat", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(test): my first commit", Author: "me", Hash: "12345667", @@ -53,10 +52,10 @@ func TestChangelog(t *testing.T) { }, { testCase: "feat breaking change", - analyzedCommits: map[analyzer.Release][]analyzer.AnalyzedCommit{ - "minor": []analyzer.AnalyzedCommit{ - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{ + "minor": []shared.AnalyzedCommit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(test): my first commit", Author: "me", Hash: "12345667", @@ -67,8 +66,8 @@ func TestChangelog(t *testing.T) { TagString: "Features", Print: true, }, - analyzer.AnalyzedCommit{ - Commit: gitutil.Commit{ + shared.AnalyzedCommit{ + Commit: shared.Commit{ Message: "feat(test): my first break: BREAKING CHANGE: change api to v2", Author: "me", Hash: "12345668", diff --git a/internal/gitutil/gitutil.go b/internal/gitutil/gitutil.go index 2588e5c..b161864 100644 --- a/internal/gitutil/gitutil.go +++ b/internal/gitutil/gitutil.go @@ -10,15 +10,9 @@ import ( "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" + "github.com/Nightapes/go-semantic-release/internal/shared" ) -// Commit struct -type Commit struct { - Message string - Author string - Hash string -} - // GitUtil struct type GitUtil struct { Repository *git.Repository @@ -126,7 +120,7 @@ func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) { } // GetCommits from git hash to HEAD -func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) { +func (g *GitUtil) GetCommits(lastTagHash string) ([]shared.Commit, error) { ref, err := g.Repository.Head() if err != nil { @@ -138,7 +132,7 @@ func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) { return nil, err } - var commits []Commit + var commits []shared.Commit var foundEnd bool err = cIter.ForEach(func(c *object.Commit) error { @@ -149,7 +143,7 @@ func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) { } if !foundEnd { log.Tracef("Found commit with hash %s", c.Hash.String()) - commit := Commit{ + commit := shared.Commit{ Message: c.Message, Author: c.Committer.Name, Hash: c.Hash.String(), diff --git a/internal/releaser/github/github.go b/internal/releaser/github/github.go index 0f5af06..a7c631e 100644 --- a/internal/releaser/github/github.go +++ b/internal/releaser/github/github.go @@ -69,7 +69,7 @@ func (g *Client) GetCompareURL(oldVersion, newVersion string) string { //ValidateConfig for github func (g *Client) ValidateConfig() error { - log.Debugf("validate GitHub provider config") + g.log.Debugf("validate GitHub provider config") if g.config.Repo == "" { return fmt.Errorf("github Repro is not set") @@ -91,8 +91,6 @@ func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedC prerelease := releaseVersion.Next.Version.Prerelease() != "" - log.Debugf("Send %+v", generatedChangelog) - release, _, err := g.client.Repositories.CreateRelease(g.context, g.config.User, g.config.Repo, &github.RepositoryRelease{ TagName: &tag, TargetCommitish: &releaseVersion.Branch, @@ -103,14 +101,14 @@ func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedC }) if err != nil { if strings.Contains(err.Error(), "already_exists") { - log.Infof("A release with tag %s already exits, will not perform a release or update", tag) + g.log.Infof("A release with tag %s already exits, will not perform a release or update", tag) return nil } return fmt.Errorf("could not create release: %s", err.Error()) } g.release = release - log.Debugf("Release repsone: %+v", *release) - log.Infof("Crated release") + g.log.Debugf("Release repsone: %+v", *release) + g.log.Infof("Crated release") return nil } diff --git a/internal/releaser/gitlab/gitlab.go b/internal/releaser/gitlab/gitlab.go index 1f2b465..4e08f9f 100644 --- a/internal/releaser/gitlab/gitlab.go +++ b/internal/releaser/gitlab/gitlab.go @@ -123,7 +123,7 @@ func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedC return err } - log.Infof("Crated release") + g.log.Infof("Crated release") return nil } @@ -151,7 +151,7 @@ func (g *Client) UploadAssets(repoDir string, assets []config.Asset) error { downloadURL := fmt.Sprintf("%s/%s%s", g.baseURL, g.config.Repo, result.URL) - log.Infof("Uploaded file %s to gitlab can be downloaded under %s", file.Name(), downloadURL) + g.log.Infof("Uploaded file %s to gitlab can be downloaded under %s", file.Name(), downloadURL) path := fmt.Sprintf("%s/projects/%s/releases/%s/assets/links?name=%s&url=%s", g.apiURL, util.PathEscape(g.config.Repo), g.Release, util.PathEscape(fileInfo.Name()), downloadURL) @@ -160,7 +160,7 @@ func (g *Client) UploadAssets(repoDir string, assets []config.Asset) error { return err } - log.Infof("Link file %s with release %s", file.Name(), g.Release) + g.log.Infof("Link file %s with release %s", file.Name(), g.Release) resp, err := util.Do(g.client, req, nil) if err != nil { @@ -171,7 +171,7 @@ func (g *Client) UploadAssets(repoDir string, assets []config.Asset) error { return err } - log.Infof("Link file with release %s is done", g.Release) + g.log.Infof("Link file with release %s is done", g.Release) } return nil } diff --git a/internal/shared/shared.go b/internal/shared/shared.go index 2630aab..da30e3a 100644 --- a/internal/shared/shared.go +++ b/internal/shared/shared.go @@ -6,16 +6,18 @@ import ( //ReleaseVersion struct type ReleaseVersion struct { - Last ReleaseVersionEntry - Next ReleaseVersionEntry - Branch string - Draft bool + Last ReleaseVersionEntry `yaml:"last"` + Next ReleaseVersionEntry `yaml:"next"` + Branch string `yaml:"branch"` + Draft bool `yaml:"draft"` + Commits map[Release][]AnalyzedCommit `yaml:"commits"` } //ReleaseVersionEntry struct type ReleaseVersionEntry struct { - Commit string - Version *semver.Version + Commit string `yaml:"commit"` + VersionString string `yaml:"version"` + Version *semver.Version `yaml:"-"` } //GeneratedChangelog struct @@ -31,3 +33,27 @@ type ChangelogTemplateConfig struct { Hash string Version string } + +//AnalyzedCommit struct +type AnalyzedCommit struct { + Commit Commit `yaml:"commit"` + ParsedMessage string `yaml:"parsedMessage"` + Scope Scope `yaml:"scope"` + ParsedBreakingChangeMessage string `yaml:"parsedBreakingChangeMessage"` + Tag string `yaml:"tag"` + TagString string `yaml:"tagString"` + Print bool `yaml:"print"` +} + +//Scope of the commit, like feat, fix,.. +type Scope string + +//Release types, like major +type Release string + +// Commit struct +type Commit struct { + Message string `yaml:"message"` + Author string `yaml:"author"` + Hash string `yaml:"hash"` +} diff --git a/pkg/semanticrelease/semantic-release.go b/pkg/semanticrelease/semantic-release.go index cd3e2b0..69e0ae3 100644 --- a/pkg/semanticrelease/semantic-release.go +++ b/pkg/semanticrelease/semantic-release.go @@ -62,22 +62,22 @@ func (s *SemanticRelease) GetCIProvider() (*ci.ProviderConfig, error) { } // GetNextVersion from .version or calculate new from commits -func (s *SemanticRelease) GetNextVersion(provider *ci.ProviderConfig, force bool) (*shared.ReleaseVersion, map[analyzer.Release][]analyzer.AnalyzedCommit, error) { +func (s *SemanticRelease) GetNextVersion(provider *ci.ProviderConfig, force bool) (*shared.ReleaseVersion, error) { log.Debugf("Ignore .version file if exits, %t", force) if !force { releaseVersion, err := cache.Read(s.repository) if err != nil { - return nil, nil, err + return nil, err } if releaseVersion.Next.Commit == provider.Commit && releaseVersion != nil { - return releaseVersion, nil, nil + return releaseVersion, nil } } lastVersion, lastVersionHash, err := s.gitutil.GetLastVersion() if err != nil { - return nil, nil, err + return nil, err } firstRelease := false @@ -90,7 +90,7 @@ func (s *SemanticRelease) GetNextVersion(provider *ci.ProviderConfig, force bool commits, err := s.gitutil.GetCommits(lastVersionHash) if err != nil { - return nil, nil, err + return nil, err } log.Debugf("Found %d commits till last release", len(commits)) @@ -116,16 +116,17 @@ func (s *SemanticRelease) GetNextVersion(provider *ci.ProviderConfig, force bool Commit: lastVersionHash, Version: lastVersion, }, - Branch: provider.Branch, - Draft: isDraft, + Branch: provider.Branch, + Draft: isDraft, + Commits: analyzedCommits, } log.Infof("New version %s -> %s", lastVersion.String(), newVersion.String()) err = cache.Write(s.repository, releaseVersion) if err != nil { - return nil, nil, err + return nil, err } - return &releaseVersion, analyzedCommits, err + return &releaseVersion, err } //SetVersion for git repository @@ -158,14 +159,14 @@ func (s *SemanticRelease) SetVersion(provider *ci.ProviderConfig, version string } // GetChangelog from last version till now -func (s *SemanticRelease) GetChangelog(analyzedCommits map[analyzer.Release][]analyzer.AnalyzedCommit, releaseVersion *shared.ReleaseVersion) (*shared.GeneratedChangelog, error) { +func (s *SemanticRelease) GetChangelog(releaseVersion *shared.ReleaseVersion) (*shared.GeneratedChangelog, error) { c := changelog.New(s.config, s.analyzer.GetRules(), time.Now()) return c.GenerateChanglog(shared.ChangelogTemplateConfig{ Version: releaseVersion.Next.Version.String(), Hash: releaseVersion.Last.Commit, CommitURL: s.releaser.GetCommitURL(), CompareURL: s.releaser.GetCompareURL(releaseVersion.Last.Version.String(), releaseVersion.Next.Version.String()), - }, analyzedCommits) + }, releaseVersion.Commits) } @@ -187,7 +188,7 @@ func (s *SemanticRelease) Release(provider *ci.ProviderConfig, force bool) error return nil } - releaseVersion, analyzedCommits, err := s.GetNextVersion(provider, force) + releaseVersion, err := s.GetNextVersion(provider, force) if err != nil { log.Debugf("Could not get next version") return err @@ -198,7 +199,7 @@ func (s *SemanticRelease) Release(provider *ci.ProviderConfig, force bool) error return nil } - generatedChanglog, err := s.GetChangelog(analyzedCommits, releaseVersion) + generatedChanglog, err := s.GetChangelog(releaseVersion) if err != nil { log.Debugf("Could not get changelog") return err