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

refactor: add version to releasenote struct

This commit is contained in:
Beatriz Vieira 2020-02-01 21:00:53 -03:00
parent 32231552a5
commit 2cd32e3ed3
4 changed files with 24 additions and 16 deletions

View File

@ -93,8 +93,8 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
return err return err
} }
releasenote := rnProcessor.Get(date, commits) releasenote := rnProcessor.Create(rnVersion, date, commits)
fmt.Println(rnProcessor.Format(releasenote, rnVersion)) fmt.Println(rnProcessor.Format(releasenote))
return nil return nil
} }
} }

View File

@ -19,8 +19,9 @@ func commitlog(t string, metadata map[string]string) GitCommitLog {
} }
} }
func releaseNote(date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote { func releaseNote(version semver.Version, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote {
return ReleaseNote{ return ReleaseNote{
Version: version,
Date: date.Truncate(time.Minute), Date: date.Truncate(time.Minute),
Sections: sections, Sections: sections,
BreakingChanges: breakingChanges, BreakingChanges: breakingChanges,

View File

@ -33,8 +33,8 @@ const markdownTemplate = `## v{{.Version}} ({{.Date}})
// ReleaseNoteProcessor release note processor interface. // ReleaseNoteProcessor release note processor interface.
type ReleaseNoteProcessor interface { type ReleaseNoteProcessor interface {
Get(date time.Time, commits []GitCommitLog) ReleaseNote Create(version semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote
Format(releasenote ReleaseNote, version semver.Version) string Format(releasenote ReleaseNote) string
} }
// ReleaseNoteProcessorImpl release note based on commit log. // ReleaseNoteProcessorImpl release note based on commit log.
@ -49,8 +49,8 @@ func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl {
return &ReleaseNoteProcessorImpl{tags: tags, template: template} return &ReleaseNoteProcessorImpl{tags: tags, template: template}
} }
// Get generate a release note based on commits. // Create create a release note based on commits.
func (p ReleaseNoteProcessorImpl) Get(date time.Time, commits []GitCommitLog) ReleaseNote { func (p ReleaseNoteProcessorImpl) Create(version semver.Version, 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 {
@ -67,13 +67,13 @@ func (p ReleaseNoteProcessorImpl) Get(date time.Time, commits []GitCommitLog) Re
} }
} }
return ReleaseNote{Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChanges} return ReleaseNote{Version: version, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChanges}
} }
// Format format a release note. // Format format a release note.
func (p ReleaseNoteProcessorImpl) Format(releasenote ReleaseNote, version semver.Version) string { func (p ReleaseNoteProcessorImpl) Format(releasenote ReleaseNote) string {
templateVars := releaseNoteTemplate{ templateVars := releaseNoteTemplate{
Version: fmt.Sprintf("%d.%d.%d", version.Major(), version.Minor(), version.Patch()), Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
Date: releasenote.Date.Format("2006-01-02"), Date: releasenote.Date.Format("2006-01-02"),
Sections: releasenote.Sections, Sections: releasenote.Sections,
BreakingChanges: releasenote.BreakingChanges, BreakingChanges: releasenote.BreakingChanges,
@ -86,6 +86,7 @@ func (p ReleaseNoteProcessorImpl) Format(releasenote ReleaseNote, version semver
// ReleaseNote release note. // ReleaseNote release note.
type ReleaseNote struct { type ReleaseNote struct {
Version semver.Version
Date time.Time Date time.Time
Sections map[string]ReleaseNoteSection Sections map[string]ReleaseNoteSection
BreakingChanges []string BreakingChanges []string

View File

@ -4,41 +4,47 @@ import (
"reflect" "reflect"
"testing" "testing"
"time" "time"
"github.com/Masterminds/semver"
) )
func TestReleaseNoteProcessorImpl_Get(t *testing.T) { func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
date := time.Now() date := time.Now()
tests := []struct { tests := []struct {
name string name string
version semver.Version
date time.Time date time.Time
commits []GitCommitLog commits []GitCommitLog
want ReleaseNote want ReleaseNote
}{ }{
{ {
name: "mapped tag", name: "mapped tag",
version: *semver.MustParse("1.0.0"),
date: date, date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{})}, commits: []GitCommitLog{commitlog("t1", map[string]string{})},
want: releaseNote(date, map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil),
}, },
{ {
name: "unmapped tag", name: "unmapped tag",
version: *semver.MustParse("1.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(date, map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil),
}, },
{ {
name: "breaking changes tag", name: "breaking changes tag",
version: *semver.MustParse("1.0.0"),
date: date, date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breakingchange": "breaks"})}, commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breakingchange": "breaks"})},
want: releaseNote(date, map[string]ReleaseNoteSection{"t1": rnSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}), want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": rnSection("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(map[string]string{"t1": "Tag 1", "t2": "Tag 2"}) p := NewReleaseNoteProcessor(map[string]string{"t1": "Tag 1", "t2": "Tag 2"})
if got := p.Get(tt.date, tt.commits); !reflect.DeepEqual(got, tt.want) { if got := p.Create(tt.version, tt.date, tt.commits); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReleaseNoteProcessorImpl.Get() = %v, want %v", got, tt.want) t.Errorf("ReleaseNoteProcessorImpl.Create() = %v, want %v", got, tt.want)
} }
}) })
} }