0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-25 07:30:38 +00:00

Merge pull request #3 from bvieira/changelog

Feature: changelog command
This commit is contained in:
Beatriz Vieira 2020-02-02 00:57:32 -03:00 committed by GitHub
commit 67da4c987b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 202 additions and 82 deletions

View File

@ -55,14 +55,15 @@ git-sv rn -h
##### Available commands ##### Available commands
| Variable | description | | Variable | description | has options |
| --------- | ---------- | | --------- | ---------- | :----------: |
| current-version, cv | get last released version from git | | current-version, cv | get last released version from git | :x: |
| next-version, nv | generate the next version based on git commit messages | | next-version, nv | generate the next version based on git commit messages | :x: |
| commit-log, cl | list all commit logs since last version as jsons | | commit-log, cl | list all commit logs since last version as jsons | :heavy_check_mark: |
| release-notes, rn | generate release notes | | release-notes, rn | generate release notes | :heavy_check_mark: |
| tag, tg | generate tag with version based on git commit messages | | changelog, cgl | generate changelog | :heavy_check_mark: |
| help, h | Shows a list of commands or help for one command | | tag, tg | generate tag with version based on git commit messages | :x: |
| help, h | Shows a list of commands or help for one command | :x: |
## Development ## Development

View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort"
"sv4git/sv" "sv4git/sv"
"time" "time"
@ -76,7 +77,7 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) {
return git.Log(prev, tag) return git.Log(prev, tag)
} }
func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor) 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
@ -93,8 +94,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(outputFormatter.FormatReleaseNote(releasenote))
return nil return nil
} }
} }
@ -161,7 +162,7 @@ func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) (
return semverProcessor.NextVersion(currentVer, commits), time.Now(), commits, nil return semverProcessor.NextVersion(currentVer, commits), time.Now(), commits, nil
} }
func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor) func(c *cli.Context) error { func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *cli.Context) error {
return func(c *cli.Context) error { return func(c *cli.Context) error {
describe := git.Describe() describe := git.Describe()
@ -184,3 +185,46 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcess
return nil return nil
} }
} }
func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, formatter sv.OutputFormatter) func(c *cli.Context) error {
return func(c *cli.Context) error {
tags, err := git.Tags()
if err != nil {
return err
}
sort.Slice(tags, func(i, j int) bool {
return tags[i].Date.After(tags[j].Date)
})
var releaseNotes []sv.ReleaseNote
size := c.Int("size")
all := c.Bool("all")
for i, tag := range tags {
if !all && i >= size {
break
}
previousTag := ""
if i+1 < len(tags) {
previousTag = tags[i+1].Name
}
commits, err := git.Log(previousTag, tag.Name)
if err != nil {
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 describe, message: %v", tag.Name, err)
}
releaseNotes = append(releaseNotes, rnProcessor.Create(currentVer, tag.Date, commits))
}
fmt.Println(formatter.FormatChangelog(releaseNotes))
return nil
}
}

View File

@ -17,6 +17,7 @@ func main() {
git := sv.NewGit(cfg.BreakingChangePrefixes, cfg.IssueIDPrefixes, cfg.TagPattern) git := sv.NewGit(cfg.BreakingChangePrefixes, cfg.IssueIDPrefixes, cfg.TagPattern)
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes) semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags) releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
outputFormatter := sv.NewOutputFormatter()
app := cli.NewApp() app := cli.NewApp()
app.Name = "sv" app.Name = "sv"
@ -40,20 +41,30 @@ func main() {
Aliases: []string{"cl"}, Aliases: []string{"cl"},
Usage: "list all commit logs since last version as jsons", Usage: "list all commit logs since last version as jsons",
Action: commitLogHandler(git, semverProcessor), Action: commitLogHandler(git, semverProcessor),
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Usage: "get commit log from tag"}}, Flags: []cli.Flag{&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from tag"}},
}, },
{ {
Name: "release-notes", Name: "release-notes",
Aliases: []string{"rn"}, Aliases: []string{"rn"},
Usage: "generate release notes", Usage: "generate release notes",
Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor), Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor, outputFormatter),
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Usage: "get release note from tag"}}, Flags: []cli.Flag{&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get release note from tag"}},
},
{
Name: "changelog",
Aliases: []string{"cgl"},
Usage: "generate changelog",
Action: changelogHandler(git, semverProcessor, releasenotesProcessor, outputFormatter),
Flags: []cli.Flag{
&cli.IntFlag{Name: "size", Value: 10, Aliases: []string{"n"}, Usage: "get changelog from last 'n' tags"},
&cli.BoolFlag{Name: "all", Usage: "ignore size parameter, get changelog for every tag"},
},
}, },
{ {
Name: "tag", Name: "tag",
Aliases: []string{"tg"}, Aliases: []string{"tg"},
Usage: "generate tag with version based on git commit messages", Usage: "generate tag with version based on git commit messages",
Action: tagHandler(git, semverProcessor, releasenotesProcessor), Action: tagHandler(git, semverProcessor),
}, },
} }

98
sv/formatter.go Normal file
View File

@ -0,0 +1,98 @@
package sv
import (
"bytes"
"fmt"
"text/template"
)
type releaseNoteTemplateVariables struct {
Version string
Date string
Sections map[string]ReleaseNoteSection
BreakingChanges []string
}
const (
cglTemplate = `# Changelog
{{- range .}}
{{template "rnTemplate" .}}
---
{{- end}}
`
rnSectionItem = "- {{if .Scope}}**{{.Scope}}:** {{end}}{{.Subject}} ({{.Hash}}){{if .Metadata.issueid}} ({{.Metadata.issueid}}){{end}}"
rnSection = `{{- if .}}
### {{.Name}}
{{range $k,$v := .Items}}
{{template "rnSectionItem" $v}}
{{- end}}
{{- end}}`
rnSectionBreakingChanges = `{{- if .}}
### Breaking Changes
{{range $k,$v := .}}
- {{$v}}
{{- end}}
{{- end}}`
rnTemplate = `## v{{.Version}} ({{.Date}})
{{- template "rnSection" .Sections.feat}}
{{- template "rnSection" .Sections.fix}}
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
`
)
// OutputFormatter output formatter interface.
type OutputFormatter interface {
FormatReleaseNote(releasenote ReleaseNote) string
FormatChangelog(releasenotes []ReleaseNote) string
}
// OutputFormatterImpl formater for release note and changelog.
type OutputFormatterImpl struct {
releasenoteTemplate *template.Template
changelogTemplate *template.Template
}
// NewOutputFormatter TemplateProcessor constructor.
func NewOutputFormatter() *OutputFormatterImpl {
cgl := template.Must(template.New("cglTemplate").Parse(cglTemplate))
rn := template.Must(cgl.New("rnTemplate").Parse(rnTemplate))
template.Must(rn.New("rnSectionItem").Parse(rnSectionItem))
template.Must(rn.New("rnSection").Parse(rnSection))
template.Must(rn.New("rnSectionBreakingChanges").Parse(rnSectionBreakingChanges))
return &OutputFormatterImpl{releasenoteTemplate: rn, changelogTemplate: cgl}
}
// FormatReleaseNote format a release note.
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
var b bytes.Buffer
p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote))
return b.String()
}
// FormatChangelog format a changelog
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string {
var templateVars []releaseNoteTemplateVariables
for _, v := range releasenotes {
templateVars = append(templateVars, releaseNoteVariables(v))
}
var b bytes.Buffer
p.changelogTemplate.Execute(&b, templateVars)
return b.String()
}
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {
return releaseNoteTemplateVariables{
Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
Date: releasenote.Date.Format("2006-01-02"),
Sections: releasenote.Sections,
BreakingChanges: releasenote.BreakingChanges,
}
}

View File

@ -15,8 +15,11 @@ import (
const ( const (
logSeparator = "##" logSeparator = "##"
endLine = "~~" endLine = "~~"
breakingChangesKey = "breakingchange"
issueIDKey = "issueid" // BreakingChangesKey key to breaking change metadata
BreakingChangesKey = "breakingchange"
// IssueIDKey key to issue id metadata
IssueIDKey = "issueid"
) )
// Git commands // Git commands
@ -52,7 +55,7 @@ type GitImpl struct {
// NewGit constructor // NewGit constructor
func NewGit(breakinChangePrefixes, issueIDPrefixes []string, tagPattern string) *GitImpl { func NewGit(breakinChangePrefixes, issueIDPrefixes []string, tagPattern string) *GitImpl {
return &GitImpl{ return &GitImpl{
messageMetadata: map[string][]string{breakingChangesKey: breakinChangePrefixes, issueIDKey: issueIDPrefixes}, messageMetadata: map[string][]string{BreakingChangesKey: breakinChangePrefixes, IssueIDKey: issueIDPrefixes},
tagPattern: tagPattern, tagPattern: tagPattern,
} }
} }

View File

@ -19,15 +19,16 @@ 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,
} }
} }
func rnSection(name string, items []GitCommitLog) ReleaseNoteSection { func newReleaseNoteSection(name string, items []GitCommitLog) ReleaseNoteSection {
return ReleaseNoteSection{ return ReleaseNoteSection{
Name: name, Name: name,
Items: items, Items: items,

View File

@ -1,56 +1,28 @@
package sv package sv
import ( import (
"bytes"
"fmt"
"text/template"
"time" "time"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
) )
type releaseNoteTemplate struct {
Version string
Date string
Sections map[string]ReleaseNoteSection
BreakingChanges []string
}
const markdownTemplate = `## v{{.Version}} ({{.Date}})
{{if .Sections.feat}}### {{.Sections.feat.Name}}
{{range $k,$v := .Sections.feat.Items}}
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
{{if .Sections.fix}}### {{.Sections.fix.Name}}
{{range $k,$v := .Sections.fix.Items}}
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
{{if .BreakingChanges}}### Breaking Changes
{{range $k,$v := .BreakingChanges}}
- {{$v}}{{end}}
{{end}}`
// 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
} }
// ReleaseNoteProcessorImpl release note based on commit log. // ReleaseNoteProcessorImpl release note based on commit log.
type ReleaseNoteProcessorImpl struct { type ReleaseNoteProcessorImpl struct {
tags map[string]string tags map[string]string
template *template.Template
} }
// NewReleaseNoteProcessor ReleaseNoteProcessor constructor. // NewReleaseNoteProcessor ReleaseNoteProcessor constructor.
func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl { func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl {
template := template.Must(template.New("markdown").Parse(markdownTemplate)) return &ReleaseNoteProcessorImpl{tags: tags}
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 {
@ -62,30 +34,17 @@ func (p ReleaseNoteProcessorImpl) Get(date time.Time, commits []GitCommitLog) Re
section.Items = append(section.Items, commit) section.Items = append(section.Items, commit)
sections[commit.Type] = section sections[commit.Type] = section
} }
if value, exists := commit.Metadata[BreakingChangeTag]; exists { if value, exists := commit.Metadata[BreakingChangesKey]; exists {
breakingChanges = append(breakingChanges, value) breakingChanges = append(breakingChanges, value)
} }
} }
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.
func (p ReleaseNoteProcessorImpl) Format(releasenote ReleaseNote, version semver.Version) string {
templateVars := releaseNoteTemplate{
Version: fmt.Sprintf("%d.%d.%d", version.Major(), version.Minor(), version.Patch()),
Date: releasenote.Date.Format("2006-01-02"),
Sections: releasenote.Sections,
BreakingChanges: releasenote.BreakingChanges,
}
var b bytes.Buffer
p.template.Execute(&b, templateVars)
return b.String()
} }
// 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": newReleaseNoteSection("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": newReleaseNoteSection("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": 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(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)
} }
}) })
} }

View File

@ -24,9 +24,6 @@ func ToVersion(value string) (semver.Version, error) {
return *v, nil return *v, nil
} }
// BreakingChangeTag breaking change tag from commit metadata
const BreakingChangeTag string = "breakingchange"
// SemVerCommitsProcessor interface // SemVerCommitsProcessor interface
type SemVerCommitsProcessor interface { type SemVerCommitsProcessor interface {
NextVersion(version semver.Version, commits []GitCommitLog) semver.Version NextVersion(version semver.Version, commits []GitCommitLog) semver.Version
@ -72,7 +69,7 @@ func (p SemVerCommitsProcessorImpl) NextVersion(version semver.Version, commits
} }
func (p SemVerCommitsProcessorImpl) versionTypeToUpdate(commit GitCommitLog) versionType { func (p SemVerCommitsProcessorImpl) versionTypeToUpdate(commit GitCommitLog) versionType {
if _, exists := commit.Metadata[BreakingChangeTag]; exists { if _, exists := commit.Metadata[BreakingChangesKey]; exists {
return major return major
} }
if _, exists := p.MajorVersionTypes[commit.Type]; exists { if _, exists := p.MajorVersionTypes[commit.Type]; exists {