0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-21 12:00:40 +00:00

feat: support non version tags on changelog and release notes

This commit is contained in:
Beatriz Vieira 2021-12-29 18:02:50 -03:00
parent dad378482e
commit ff30229bf9
8 changed files with 71 additions and 68 deletions

View File

@ -148,7 +148,7 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF
date, _ = time.Parse("2006-01-02", commits[0].Date) date, _ = time.Parse("2006-01-02", commits[0].Date)
} }
output, err := outputFormatter.FormatReleaseNote(rnProcessor.Create(nil, date, commits)) output, err := outputFormatter.FormatReleaseNote(rnProcessor.Create(nil, "", date, commits))
if err != nil { if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err) return fmt.Errorf("could not format release notes, message: %v", err)
} }
@ -160,11 +160,12 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF
func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error { func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error {
return func(c *cli.Context) error { return func(c *cli.Context) error {
var commits []sv.GitCommitLog var commits []sv.GitCommitLog
var rnVersion semver.Version var rnVersion *semver.Version
var tag string
var date time.Time var date time.Time
var err error var err error
if tag := c.String("t"); tag != "" { if tag = c.String("t"); tag != "" {
rnVersion, date, commits, err = getTagVersionInfo(git, tag) rnVersion, date, commits, err = getTagVersionInfo(git, tag)
} else { } else {
// TODO: should generate release notes if version was not updated? // TODO: should generate release notes if version was not updated?
@ -175,7 +176,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
return err return err
} }
releasenote := rnProcessor.Create(&rnVersion, date, commits) releasenote := rnProcessor.Create(rnVersion, tag, date, commits)
output, err := outputFormatter.FormatReleaseNote(releasenote) output, err := outputFormatter.FormatReleaseNote(releasenote)
if err != nil { if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err) return fmt.Errorf("could not format release notes, message: %v", err)
@ -185,20 +186,17 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
} }
} }
func getTagVersionInfo(git sv.Git, tag string) (semver.Version, time.Time, []sv.GitCommitLog, error) { func getTagVersionInfo(git sv.Git, tag string) (*semver.Version, time.Time, []sv.GitCommitLog, error) {
tagVersion, err := sv.ToVersion(tag) tagVersion, _ := sv.ToVersion(tag)
if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from tag, message: %v", tag, err)
}
previousTag, currentTag, err := getTags(git, tag) previousTag, currentTag, err := getTags(git, tag)
if err != nil { if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error listing tags, message: %v", err) return nil, time.Time{}, nil, fmt.Errorf("error listing tags, message: %v", err)
} }
commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag)) commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag))
if err != nil { if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error getting git log from tag: %s, message: %v", tag, err) return nil, time.Time{}, nil, fmt.Errorf("error getting git log from tag: %s, message: %v", tag, err)
} }
return tagVersion, currentTag.Date, commits, nil return tagVersion, currentTag.Date, commits, nil
@ -231,20 +229,17 @@ func find(tag string, tags []sv.GitTag) int {
return -1 return -1
} }
func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) (semver.Version, bool, time.Time, []sv.GitCommitLog, error) { func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) (*semver.Version, bool, time.Time, []sv.GitCommitLog, error) {
lastTag := git.LastTag() lastTag := git.LastTag()
currentVer, err := sv.ToVersion(lastTag)
if err != nil {
return semver.Version{}, false, time.Time{}, nil, fmt.Errorf("error parsing version: %s from git tag, message: %v", lastTag, err)
}
commits, err := git.Log(sv.NewLogRange(sv.TagRange, lastTag, "")) commits, err := git.Log(sv.NewLogRange(sv.TagRange, lastTag, ""))
if err != nil { if err != nil {
return semver.Version{}, false, time.Time{}, nil, fmt.Errorf("error getting git log, message: %v", err) return nil, false, time.Time{}, nil, fmt.Errorf("error getting git log, message: %v", err)
} }
currentVer, _ := sv.ToVersion(lastTag)
version, updated := semverProcessor.NextVersion(currentVer, commits) version, updated := semverProcessor.NextVersion(currentVer, commits)
return version, updated, time.Now(), commits, nil return version, updated, time.Now(), commits, nil
} }
@ -263,7 +258,7 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *c
} }
nextVer, _ := semverProcessor.NextVersion(currentVer, commits) nextVer, _ := semverProcessor.NextVersion(currentVer, commits)
tagname, err := git.Tag(nextVer) tagname, err := git.Tag(*nextVer)
fmt.Println(tagname) fmt.Println(tagname)
if err != nil { if err != nil {
return fmt.Errorf("error generating tag version: %s, message: %v", nextVer.String(), err) return fmt.Errorf("error generating tag version: %s, message: %v", nextVer.String(), err)
@ -424,7 +419,7 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
return uerr return uerr
} }
if updated { if updated {
releaseNotes = append(releaseNotes, rnProcessor.Create(&rnVersion, date, commits)) releaseNotes = append(releaseNotes, rnProcessor.Create(rnVersion, "", date, commits))
} }
} }
for i, tag := range tags { for i, tag := range tags {
@ -442,11 +437,8 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
return fmt.Errorf("error getting git log from tag: %s, message: %v", tag.Name, err) return fmt.Errorf("error getting git log from tag: %s, message: %v", tag.Name, err)
} }
currentVer, err := sv.ToVersion(tag.Name) currentVer, _ := sv.ToVersion(tag.Name)
if err != nil { releaseNotes = append(releaseNotes, rnProcessor.Create(currentVer, tag.Name, tag.Date, commits))
return fmt.Errorf("error parsing version: %s from git tag, message: %v", tag.Name, err)
}
releaseNotes = append(releaseNotes, rnProcessor.Create(&currentVer, tag.Date, commits))
} }
output, err := formatter.FormatChangelog(releaseNotes) output, err := formatter.FormatChangelog(releaseNotes)

View File

@ -6,7 +6,7 @@ import (
) )
type releaseNoteTemplateVariables struct { type releaseNoteTemplateVariables struct {
Version string Release string
Date string Date string
Sections map[string]ReleaseNoteSection Sections map[string]ReleaseNoteSection
Order []string Order []string
@ -40,7 +40,7 @@ const (
{{- end}} {{- end}}
{{- end}}` {{- end}}`
rnTemplate = `## {{if .Version}}v{{.Version}}{{end}}{{if and .Date .Version}} ({{end}}{{.Date}}{{if and .Version .Date}}){{end}} rnTemplate = `## {{if .Release}}{{.Release}}{{end}}{{if and .Date .Release}} ({{end}}{{.Date}}{{if and .Date .Release}}){{end}}
{{- $sections := .Sections }} {{- $sections := .Sections }}
{{- range $key := .Order }} {{- range $key := .Order }}
{{- template "rnSection" (index $sections $key) }} {{- template "rnSection" (index $sections $key) }}
@ -100,12 +100,14 @@ func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables
date = releasenote.Date.Format("2006-01-02") date = releasenote.Date.Format("2006-01-02")
} }
version := "" release := ""
if releasenote.Version != nil { if releasenote.Version != nil {
version = releasenote.Version.String() release = "v" + releasenote.Version.String()
} else if releasenote.Tag != "" {
release = releasenote.Tag
} }
return releaseNoteTemplateVariables{ return releaseNoteTemplateVariables{
Version: version, Release: release,
Date: date, Date: date,
Sections: releasenote.Sections, Sections: releasenote.Sections,
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"}, Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},

View File

@ -10,6 +10,9 @@ import (
var dateChangelog = `## v1.0.0 (2020-05-01) var dateChangelog = `## v1.0.0 (2020-05-01)
` `
var nonVersioningChangelog = `## abc (2020-05-01)
`
var emptyDateChangelog = `## v1.0.0 var emptyDateChangelog = `## v1.0.0
` `
@ -47,6 +50,7 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog, false}, {"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog, false},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, false}, {"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, false},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog, false}, {"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog, false},
{"non versioning tag", emptyReleaseNote("abc", date.Truncate(time.Minute)), nonVersioningChangelog, false},
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false}, {"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false},
} }
for _, tt := range tests { for _, tt := range tests {
@ -63,27 +67,21 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
} }
} }
func emptyReleaseNote(version string, date time.Time) ReleaseNote { func emptyReleaseNote(tag string, date time.Time) ReleaseNote {
var v *semver.Version v, _ := semver.NewVersion(tag)
if version != "" {
v = semver.MustParse(version)
}
return ReleaseNote{ return ReleaseNote{
Version: v, Version: v,
Tag: tag,
Date: date, Date: date,
} }
} }
func fullReleaseNote(version string, date time.Time) ReleaseNote { func fullReleaseNote(tag string, date time.Time) ReleaseNote {
var v *semver.Version v, _ := semver.NewVersion(tag)
if version != "" {
v = semver.MustParse(version)
}
sections := map[string]ReleaseNoteSection{ sections := map[string]ReleaseNoteSection{
"build": newReleaseNoteSection("Build", []GitCommitLog{commitlog("build", map[string]string{})}), "build": newReleaseNoteSection("Build", []GitCommitLog{commitlog("build", map[string]string{})}),
"feat": newReleaseNoteSection("Features", []GitCommitLog{commitlog("feat", map[string]string{})}), "feat": newReleaseNoteSection("Features", []GitCommitLog{commitlog("feat", map[string]string{})}),
"fix": newReleaseNoteSection("Bug Fixes", []GitCommitLog{commitlog("fix", map[string]string{})}), "fix": newReleaseNoteSection("Bug Fixes", []GitCommitLog{commitlog("fix", map[string]string{})}),
} }
return releaseNote(v, date, sections, []string{"break change message"}) return releaseNote(v, tag, date, sections, []string{"break change message"})
} }

View File

@ -6,9 +6,9 @@ import (
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
) )
func version(v string) semver.Version { func version(v string) *semver.Version {
r, _ := semver.NewVersion(v) r, _ := semver.NewVersion(v)
return *r return r
} }
func commitlog(ctype string, metadata map[string]string) GitCommitLog { func commitlog(ctype string, metadata map[string]string) GitCommitLog {
@ -26,13 +26,14 @@ func commitlog(ctype string, metadata map[string]string) GitCommitLog {
} }
} }
func releaseNote(version *semver.Version, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote { func releaseNote(version *semver.Version, tag string, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote {
var bchanges BreakingChangeSection var bchanges BreakingChangeSection
if len(breakingChanges) > 0 { if len(breakingChanges) > 0 {
bchanges = BreakingChangeSection{Name: "Breaking Changes", Messages: breakingChanges} bchanges = BreakingChangeSection{Name: "Breaking Changes", Messages: breakingChanges}
} }
return ReleaseNote{ return ReleaseNote{
Version: version, Version: version,
Tag: tag,
Date: date.Truncate(time.Minute), Date: date.Truncate(time.Minute),
Sections: sections, Sections: sections,
BreakingChanges: bchanges, BreakingChanges: bchanges,

View File

@ -8,7 +8,7 @@ import (
// ReleaseNoteProcessor release note processor interface. // ReleaseNoteProcessor release note processor interface.
type ReleaseNoteProcessor interface { type ReleaseNoteProcessor interface {
Create(version *semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote Create(version *semver.Version, tag string, date time.Time, commits []GitCommitLog) ReleaseNote
} }
// ReleaseNoteProcessorImpl release note based on commit log. // ReleaseNoteProcessorImpl release note based on commit log.
@ -22,7 +22,7 @@ func NewReleaseNoteProcessor(cfg ReleaseNotesConfig) *ReleaseNoteProcessorImpl {
} }
// Create create a release note based on commits. // Create create a release note based on commits.
func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote { func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, tag string, date time.Time, commits []GitCommitLog) ReleaseNote {
sections := make(map[string]ReleaseNoteSection) sections := make(map[string]ReleaseNoteSection)
var breakingChanges []string var breakingChanges []string
for _, commit := range commits { for _, commit := range commits {
@ -44,12 +44,13 @@ func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, date time.Time
if name, exists := p.cfg.Headers[breakingChangeMetadataKey]; exists && len(breakingChanges) > 0 { if name, exists := p.cfg.Headers[breakingChangeMetadataKey]; exists && len(breakingChanges) > 0 {
breakingChangeSection = BreakingChangeSection{Name: name, Messages: breakingChanges} breakingChangeSection = BreakingChangeSection{Name: name, Messages: breakingChanges}
} }
return ReleaseNote{Version: version, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChangeSection} return ReleaseNote{Version: version, Tag: tag, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChangeSection}
} }
// ReleaseNote release note. // ReleaseNote release note.
type ReleaseNote struct { type ReleaseNote struct {
Version *semver.Version Version *semver.Version
Tag string
Date time.Time Date time.Time
Sections map[string]ReleaseNoteSection Sections map[string]ReleaseNoteSection
BreakingChanges BreakingChangeSection BreakingChanges BreakingChangeSection

View File

@ -14,6 +14,7 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
version *semver.Version version *semver.Version
tag string
date time.Time date time.Time
commits []GitCommitLog commits []GitCommitLog
want ReleaseNote want ReleaseNote
@ -21,29 +22,32 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
{ {
name: "mapped tag", name: "mapped tag",
version: semver.MustParse("1.0.0"), version: semver.MustParse("1.0.0"),
tag: "v1.0.0",
date: date, date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{})}, commits: []GitCommitLog{commitlog("t1", map[string]string{})},
want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), want: releaseNote(semver.MustParse("1.0.0"), "v1.0.0", date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil),
}, },
{ {
name: "unmapped tag", name: "unmapped tag",
version: semver.MustParse("1.0.0"), version: semver.MustParse("1.0.0"),
tag: "v1.0.0",
date: date, date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{})}, commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{})},
want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), want: releaseNote(semver.MustParse("1.0.0"), "v1.0.0", date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil),
}, },
{ {
name: "breaking changes tag", name: "breaking changes tag",
version: semver.MustParse("1.0.0"), version: semver.MustParse("1.0.0"),
tag: "v1.0.0",
date: date, date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breaking-change": "breaks"})}, commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breaking-change": "breaks"})},
want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}), want: releaseNote(semver.MustParse("1.0.0"), "v1.0.0", date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := NewReleaseNoteProcessor(ReleaseNotesConfig{Headers: map[string]string{"t1": "Tag 1", "t2": "Tag 2", "breaking-change": "Breaking Changes"}}) p := NewReleaseNoteProcessor(ReleaseNotesConfig{Headers: map[string]string{"t1": "Tag 1", "t2": "Tag 2", "breaking-change": "Breaking Changes"}})
if got := p.Create(tt.version, tt.date, tt.commits); !reflect.DeepEqual(got, tt.want) { if got := p.Create(tt.version, tt.tag, tt.date, tt.commits); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReleaseNoteProcessorImpl.Create() = %v, want %v", got, tt.want) t.Errorf("ReleaseNoteProcessorImpl.Create() = %v, want %v", got, tt.want)
} }
}) })

View File

@ -12,21 +12,17 @@ const (
) )
// ToVersion parse string to semver.Version. // ToVersion parse string to semver.Version.
func ToVersion(value string) (semver.Version, error) { func ToVersion(value string) (*semver.Version, error) {
version := value version := value
if version == "" { if version == "" {
version = "0.0.0" version = "0.0.0"
} }
v, err := semver.NewVersion(version) return semver.NewVersion(version)
if err != nil {
return semver.Version{}, err
}
return *v, nil
} }
// SemVerCommitsProcessor interface. // SemVerCommitsProcessor interface.
type SemVerCommitsProcessor interface { type SemVerCommitsProcessor interface {
NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool) NextVersion(version *semver.Version, commits []GitCommitLog) (*semver.Version, bool)
} }
// SemVerCommitsProcessorImpl process versions using commit log. // SemVerCommitsProcessorImpl process versions using commit log.
@ -50,7 +46,7 @@ func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig)
} }
// NextVersion calculates next version based on commit log. // NextVersion calculates next version based on commit log.
func (p SemVerCommitsProcessorImpl) NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool) { func (p SemVerCommitsProcessorImpl) NextVersion(version *semver.Version, commits []GitCommitLog) (*semver.Version, bool) {
versionToUpdate := none versionToUpdate := none
for _, commit := range commits { for _, commit := range commits {
if v := p.versionTypeToUpdate(commit); v > versionToUpdate { if v := p.versionTypeToUpdate(commit); v > versionToUpdate {
@ -58,15 +54,24 @@ func (p SemVerCommitsProcessorImpl) NextVersion(version semver.Version, commits
} }
} }
updated := versionToUpdate != none
if version == nil {
return nil, updated
}
newVersion := updateVersion(*version, versionToUpdate)
return &newVersion, updated
}
func updateVersion(version semver.Version, versionToUpdate versionType) semver.Version {
switch versionToUpdate { switch versionToUpdate {
case major: case major:
return version.IncMajor(), true return version.IncMajor()
case minor: case minor:
return version.IncMinor(), true return version.IncMinor()
case patch: case patch:
return version.IncPatch(), true return version.IncPatch()
default: default:
return version, false return version
} }
} }

View File

@ -11,9 +11,9 @@ func TestSemVerCommitsProcessorImpl_NextVersion(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ignoreUnknown bool ignoreUnknown bool
version semver.Version version *semver.Version
commits []GitCommitLog commits []GitCommitLog
want semver.Version want *semver.Version
wantUpdated bool wantUpdated bool
}{ }{
{"no update", true, version("0.0.0"), []GitCommitLog{}, version("0.0.0"), false}, {"no update", true, version("0.0.0"), []GitCommitLog{}, version("0.0.0"), false},
@ -43,11 +43,11 @@ func TestToVersion(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input string input string
want semver.Version want *semver.Version
wantErr bool wantErr bool
}{ }{
{"empty version", "", version("0.0.0"), false}, {"empty version", "", version("0.0.0"), false},
{"invalid version", "abc", semver.Version{}, true}, {"invalid version", "abc", nil, true},
{"valid version", "1.2.3", version("1.2.3"), false}, {"valid version", "1.2.3", version("1.2.3"), false},
} }
for _, tt := range tests { for _, tt := range tests {