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

feat: add commit-notes action

issue: #7
This commit is contained in:
Beatriz Vieira 2021-01-25 17:16:56 -03:00
parent 8b57bee97b
commit a6259a910c
7 changed files with 75 additions and 26 deletions

View File

@ -55,14 +55,13 @@ func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) fun
rangeFlag := c.String("r")
startFlag := c.String("s")
endFlag := c.String("e")
if tagFlag != "" && (rangeFlag != "" || startFlag != "" || endFlag != "") {
if tagFlag != "" && (rangeFlag != string(sv.TagRange) || startFlag != "" || endFlag != "") {
return fmt.Errorf("cannot define tag flag with range, start or end flags")
}
if tagFlag != "" {
commits, err = getTagCommits(git, tagFlag)
} else {
// commits, err = git.Log(sv.NewLogRange(sv.TagRange, git.Describe(), ""))
r, rerr := logRange(git, rangeFlag, startFlag, endFlag)
if rerr != nil {
return rerr
@ -94,7 +93,7 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) {
func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, error) {
switch rangeFlag {
case "", string(sv.TagRange):
case string(sv.TagRange):
return sv.NewLogRange(sv.TagRange, str(startFlag, git.Describe()), endFlag), nil
case string(sv.DateRange):
return sv.NewLogRange(sv.DateRange, startFlag, endFlag), nil
@ -105,6 +104,31 @@ func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, er
}
}
func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error {
return func(c *cli.Context) error {
var date time.Time
rangeFlag := c.String("r")
lr, err := logRange(git, rangeFlag, c.String("s"), c.String("e"))
if err != nil {
return err
}
commits, err := git.Log(lr)
if err != nil {
return fmt.Errorf("error getting git log from range: %s, message: %v", rangeFlag, err)
}
if len(commits) > 0 {
date, _ = time.Parse("2006-01-02", commits[0].Date)
}
releasenote := rnProcessor.Create(nil, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
return nil
}
}
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
@ -122,7 +146,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
return err
}
releasenote := rnProcessor.Create(rnVersion, date, commits)
releasenote := rnProcessor.Create(&rnVersion, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
return nil
}
@ -310,7 +334,7 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
if err != nil {
return fmt.Errorf("error parsing version: %s from describe, message: %v", tag.Name, err)
}
releaseNotes = append(releaseNotes, rnProcessor.Create(currentVer, tag.Date, commits))
releaseNotes = append(releaseNotes, rnProcessor.Create(&currentVer, tag.Date, commits))
}
fmt.Println(formatter.FormatChangelog(releaseNotes))

View File

@ -40,13 +40,26 @@ func main() {
Action: nextVersionHandler(git, semverProcessor),
},
{
Name: "commit-log",
Aliases: []string{"cl"},
Usage: "list all commit logs since last version or according with range as jsons",
Action: commitLogHandler(git, semverProcessor),
Name: "commit-log",
Aliases: []string{"cl"},
Usage: "list all commit logs according to range as jsons",
Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead",
Action: commitLogHandler(git, semverProcessor),
Flags: []cli.Flag{
&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"},
&cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash"},
&cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Value: string(sv.TagRange)},
&cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"},
&cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"},
},
},
{
Name: "commit-notes",
Aliases: []string{"cn"},
Usage: "create a release notes according to range",
Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead",
Action: commitNotesHandler(git, releasenotesProcessor, outputFormatter),
Flags: []cli.Flag{
&cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Required: true},
&cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"},
&cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"},
},

View File

@ -40,7 +40,7 @@ const (
{{- end}}
{{- end}}`
rnTemplate = `## v{{.Version}}{{if .Date}} ({{.Date}}){{end}}
rnTemplate = `## {{if .Version}}v{{.Version}}{{end}}{{if and .Date .Version}} ({{end}}{{.Date}}{{if and .Version .Date}}){{end}}
{{- template "rnSection" .Sections.feat}}
{{- template "rnSection" .Sections.fix}}
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
@ -93,8 +93,13 @@ func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables
if !releasenote.Date.IsZero() {
date = releasenote.Date.Format("2006-01-02")
}
var version = ""
if releasenote.Version != nil {
version = fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch())
}
return releaseNoteTemplateVariables{
Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
Version: version,
Date: date,
Sections: releasenote.Sections,
BreakingChanges: releasenote.BreakingChanges,

View File

@ -11,6 +11,8 @@ var dateChangelog = `## v1.0.0 (2020-05-01)
`
var emptyDateChangelog = `## v1.0.0
`
var emptyVersionChangelog = `## 2020-05-01
`
func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2020-05-01")
@ -20,8 +22,9 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
input ReleaseNote
want string
}{
{"", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog},
{"", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog},
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -33,8 +36,12 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
}
func emptyReleaseNote(version string, date time.Time) ReleaseNote {
var v *semver.Version
if version != "" {
v = semver.MustParse(version)
}
return ReleaseNote{
Version: *semver.MustParse(version),
Version: v,
Date: date,
}
}

View File

@ -19,7 +19,7 @@ func commitlog(t 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, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote {
return ReleaseNote{
Version: version,
Date: date.Truncate(time.Minute),

View File

@ -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, date time.Time, commits []GitCommitLog) ReleaseNote
}
// ReleaseNoteProcessorImpl release note based on commit log.
@ -22,7 +22,7 @@ func NewReleaseNoteProcessor(tags map[string]string) *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, date time.Time, commits []GitCommitLog) ReleaseNote {
sections := make(map[string]ReleaseNoteSection)
var breakingChanges []string
for _, commit := range commits {
@ -44,7 +44,7 @@ func (p ReleaseNoteProcessorImpl) Create(version semver.Version, date time.Time,
// ReleaseNote release note.
type ReleaseNote struct {
Version semver.Version
Version *semver.Version
Date time.Time
Sections map[string]ReleaseNoteSection
BreakingChanges []string

View File

@ -13,31 +13,31 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) {
tests := []struct {
name string
version semver.Version
version *semver.Version
date time.Time
commits []GitCommitLog
want ReleaseNote
}{
{
name: "mapped tag",
version: *semver.MustParse("1.0.0"),
version: semver.MustParse("1.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"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil),
},
{
name: "unmapped tag",
version: *semver.MustParse("1.0.0"),
version: semver.MustParse("1.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"), 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"),
version: semver.MustParse("1.0.0"),
date: date,
commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breakingchange": "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"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}),
},
}
for _, tt := range tests {