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:
parent
dad378482e
commit
ff30229bf9
@ -148,7 +148,7 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF
|
||||
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 {
|
||||
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 {
|
||||
return func(c *cli.Context) error {
|
||||
var commits []sv.GitCommitLog
|
||||
var rnVersion semver.Version
|
||||
var rnVersion *semver.Version
|
||||
var tag string
|
||||
var date time.Time
|
||||
var err error
|
||||
|
||||
if tag := c.String("t"); tag != "" {
|
||||
if tag = c.String("t"); tag != "" {
|
||||
rnVersion, date, commits, err = getTagVersionInfo(git, tag)
|
||||
} else {
|
||||
// TODO: should generate release notes if version was not updated?
|
||||
@ -175,7 +176,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
|
||||
return err
|
||||
}
|
||||
|
||||
releasenote := rnProcessor.Create(&rnVersion, date, commits)
|
||||
releasenote := rnProcessor.Create(rnVersion, tag, date, commits)
|
||||
output, err := outputFormatter.FormatReleaseNote(releasenote)
|
||||
if err != nil {
|
||||
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) {
|
||||
tagVersion, err := sv.ToVersion(tag)
|
||||
if err != nil {
|
||||
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from tag, message: %v", tag, err)
|
||||
}
|
||||
func getTagVersionInfo(git sv.Git, tag string) (*semver.Version, time.Time, []sv.GitCommitLog, error) {
|
||||
tagVersion, _ := sv.ToVersion(tag)
|
||||
|
||||
previousTag, currentTag, err := getTags(git, tag)
|
||||
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))
|
||||
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
|
||||
@ -231,20 +229,17 @@ func find(tag string, tags []sv.GitTag) int {
|
||||
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()
|
||||
|
||||
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, ""))
|
||||
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)
|
||||
|
||||
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)
|
||||
tagname, err := git.Tag(nextVer)
|
||||
tagname, err := git.Tag(*nextVer)
|
||||
fmt.Println(tagname)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
if updated {
|
||||
releaseNotes = append(releaseNotes, rnProcessor.Create(&rnVersion, date, commits))
|
||||
releaseNotes = append(releaseNotes, rnProcessor.Create(rnVersion, "", date, commits))
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
currentVer, err := sv.ToVersion(tag.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing version: %s from git tag, message: %v", tag.Name, err)
|
||||
}
|
||||
releaseNotes = append(releaseNotes, rnProcessor.Create(¤tVer, tag.Date, commits))
|
||||
currentVer, _ := sv.ToVersion(tag.Name)
|
||||
releaseNotes = append(releaseNotes, rnProcessor.Create(currentVer, tag.Name, tag.Date, commits))
|
||||
}
|
||||
|
||||
output, err := formatter.FormatChangelog(releaseNotes)
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type releaseNoteTemplateVariables struct {
|
||||
Version string
|
||||
Release string
|
||||
Date string
|
||||
Sections map[string]ReleaseNoteSection
|
||||
Order []string
|
||||
@ -40,7 +40,7 @@ const (
|
||||
{{- 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 }}
|
||||
{{- range $key := .Order }}
|
||||
{{- template "rnSection" (index $sections $key) }}
|
||||
@ -100,12 +100,14 @@ func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables
|
||||
date = releasenote.Date.Format("2006-01-02")
|
||||
}
|
||||
|
||||
version := ""
|
||||
release := ""
|
||||
if releasenote.Version != nil {
|
||||
version = releasenote.Version.String()
|
||||
release = "v" + releasenote.Version.String()
|
||||
} else if releasenote.Tag != "" {
|
||||
release = releasenote.Tag
|
||||
}
|
||||
return releaseNoteTemplateVariables{
|
||||
Version: version,
|
||||
Release: release,
|
||||
Date: date,
|
||||
Sections: releasenote.Sections,
|
||||
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},
|
||||
|
@ -10,6 +10,9 @@ import (
|
||||
var dateChangelog = `## v1.0.0 (2020-05-01)
|
||||
`
|
||||
|
||||
var nonVersioningChangelog = `## abc (2020-05-01)
|
||||
`
|
||||
|
||||
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},
|
||||
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, 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},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@ -63,27 +67,21 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func emptyReleaseNote(version string, date time.Time) ReleaseNote {
|
||||
var v *semver.Version
|
||||
if version != "" {
|
||||
v = semver.MustParse(version)
|
||||
}
|
||||
func emptyReleaseNote(tag string, date time.Time) ReleaseNote {
|
||||
v, _ := semver.NewVersion(tag)
|
||||
return ReleaseNote{
|
||||
Version: v,
|
||||
Tag: tag,
|
||||
Date: date,
|
||||
}
|
||||
}
|
||||
|
||||
func fullReleaseNote(version string, date time.Time) ReleaseNote {
|
||||
var v *semver.Version
|
||||
if version != "" {
|
||||
v = semver.MustParse(version)
|
||||
}
|
||||
|
||||
func fullReleaseNote(tag string, date time.Time) ReleaseNote {
|
||||
v, _ := semver.NewVersion(tag)
|
||||
sections := map[string]ReleaseNoteSection{
|
||||
"build": newReleaseNoteSection("Build", []GitCommitLog{commitlog("build", map[string]string{})}),
|
||||
"feat": newReleaseNoteSection("Features", []GitCommitLog{commitlog("feat", 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"})
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ import (
|
||||
"github.com/Masterminds/semver/v3"
|
||||
)
|
||||
|
||||
func version(v string) semver.Version {
|
||||
func version(v string) *semver.Version {
|
||||
r, _ := semver.NewVersion(v)
|
||||
return *r
|
||||
return r
|
||||
}
|
||||
|
||||
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
|
||||
if len(breakingChanges) > 0 {
|
||||
bchanges = BreakingChangeSection{Name: "Breaking Changes", Messages: breakingChanges}
|
||||
}
|
||||
return ReleaseNote{
|
||||
Version: version,
|
||||
Tag: tag,
|
||||
Date: date.Truncate(time.Minute),
|
||||
Sections: sections,
|
||||
BreakingChanges: bchanges,
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
// ReleaseNoteProcessor release note processor 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.
|
||||
@ -22,7 +22,7 @@ func NewReleaseNoteProcessor(cfg ReleaseNotesConfig) *ReleaseNoteProcessorImpl {
|
||||
}
|
||||
|
||||
// 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)
|
||||
var breakingChanges []string
|
||||
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 {
|
||||
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.
|
||||
type ReleaseNote struct {
|
||||
Version *semver.Version
|
||||
Tag string
|
||||
Date time.Time
|
||||
Sections map[string]ReleaseNoteSection
|
||||
BreakingChanges BreakingChangeSection
|
||||
|
@ -14,6 +14,7 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
version *semver.Version
|
||||
tag string
|
||||
date time.Time
|
||||
commits []GitCommitLog
|
||||
want ReleaseNote
|
||||
@ -21,29 +22,32 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
|
||||
{
|
||||
name: "mapped tag",
|
||||
version: semver.MustParse("1.0.0"),
|
||||
tag: "v1.0.0",
|
||||
date: date,
|
||||
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",
|
||||
version: semver.MustParse("1.0.0"),
|
||||
tag: "v1.0.0",
|
||||
date: date,
|
||||
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",
|
||||
version: semver.MustParse("1.0.0"),
|
||||
tag: "v1.0.0",
|
||||
date: date,
|
||||
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 {
|
||||
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"}})
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
29
sv/semver.go
29
sv/semver.go
@ -12,21 +12,17 @@ const (
|
||||
)
|
||||
|
||||
// ToVersion parse string to semver.Version.
|
||||
func ToVersion(value string) (semver.Version, error) {
|
||||
func ToVersion(value string) (*semver.Version, error) {
|
||||
version := value
|
||||
if version == "" {
|
||||
version = "0.0.0"
|
||||
}
|
||||
v, err := semver.NewVersion(version)
|
||||
if err != nil {
|
||||
return semver.Version{}, err
|
||||
}
|
||||
return *v, nil
|
||||
return semver.NewVersion(version)
|
||||
}
|
||||
|
||||
// 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.
|
||||
@ -50,7 +46,7 @@ func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig)
|
||||
}
|
||||
|
||||
// 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
|
||||
for _, commit := range commits {
|
||||
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 {
|
||||
case major:
|
||||
return version.IncMajor(), true
|
||||
return version.IncMajor()
|
||||
case minor:
|
||||
return version.IncMinor(), true
|
||||
return version.IncMinor()
|
||||
case patch:
|
||||
return version.IncPatch(), true
|
||||
return version.IncPatch()
|
||||
default:
|
||||
return version, false
|
||||
return version
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,9 +11,9 @@ func TestSemVerCommitsProcessorImpl_NextVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ignoreUnknown bool
|
||||
version semver.Version
|
||||
version *semver.Version
|
||||
commits []GitCommitLog
|
||||
want semver.Version
|
||||
want *semver.Version
|
||||
wantUpdated bool
|
||||
}{
|
||||
{"no update", true, version("0.0.0"), []GitCommitLog{}, version("0.0.0"), false},
|
||||
@ -43,11 +43,11 @@ func TestToVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want semver.Version
|
||||
want *semver.Version
|
||||
wantErr bool
|
||||
}{
|
||||
{"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},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
Loading…
Reference in New Issue
Block a user