mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-22 08:20:39 +00:00
feat: add changelog command
This commit is contained in:
parent
11e40b68ee
commit
cc57ec4c76
17
README.md
17
README.md
@ -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
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"sv4git/sv"
|
"sv4git/sv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -184,3 +185,46 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *c
|
|||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -50,6 +50,16 @@ func main() {
|
|||||||
Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor, outputFormatter),
|
Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor, outputFormatter),
|
||||||
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, 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"},
|
||||||
|
@ -13,56 +13,86 @@ type releaseNoteTemplateVariables struct {
|
|||||||
BreakingChanges []string
|
BreakingChanges []string
|
||||||
}
|
}
|
||||||
|
|
||||||
const rnSectionItem = "- {{if .Scope}}**{{.Scope}}:** {{end}}{{.Subject}} ({{.Hash}}){{if .Metadata.issueid}} ({{.Metadata.issueid}}){{end}}"
|
const (
|
||||||
const rnSection = `{{- if .}}
|
cglTemplate = `# Changelog
|
||||||
|
{{- range .}}
|
||||||
|
|
||||||
|
{{template "rnTemplate" .}}
|
||||||
|
---
|
||||||
|
{{- end}}
|
||||||
|
`
|
||||||
|
|
||||||
|
rnSectionItem = "- {{if .Scope}}**{{.Scope}}:** {{end}}{{.Subject}} ({{.Hash}}){{if .Metadata.issueid}} ({{.Metadata.issueid}}){{end}}"
|
||||||
|
|
||||||
|
rnSection = `{{- if .}}
|
||||||
|
|
||||||
### {{.Name}}
|
### {{.Name}}
|
||||||
{{range $k,$v := .Items}}
|
{{range $k,$v := .Items}}
|
||||||
{{template "rnSectionItem" $v}}
|
{{template "rnSectionItem" $v}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end}}`
|
{{- end}}`
|
||||||
const rnSectionBreakingChanges = `{{- if .}}
|
|
||||||
|
rnSectionBreakingChanges = `{{- if .}}
|
||||||
|
|
||||||
### Breaking Changes
|
### Breaking Changes
|
||||||
{{range $k,$v := .}}
|
{{range $k,$v := .}}
|
||||||
- {{$v}}
|
- {{$v}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end}}`
|
{{- end}}`
|
||||||
const rnTemplate = `## v{{.Version}} ({{.Date}})
|
|
||||||
|
rnTemplate = `## v{{.Version}} ({{.Date}})
|
||||||
{{- template "rnSection" .Sections.feat}}
|
{{- template "rnSection" .Sections.feat}}
|
||||||
{{- template "rnSection" .Sections.fix}}
|
{{- template "rnSection" .Sections.fix}}
|
||||||
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
|
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
|
||||||
`
|
`
|
||||||
|
)
|
||||||
|
|
||||||
// OutputFormatter output formatter interface.
|
// OutputFormatter output formatter interface.
|
||||||
type OutputFormatter interface {
|
type OutputFormatter interface {
|
||||||
FormatReleaseNote(releasenote ReleaseNote) string
|
FormatReleaseNote(releasenote ReleaseNote) string
|
||||||
|
FormatChangelog(releasenotes []ReleaseNote) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// OutputFormatterImpl formater for release note and changelog.
|
// OutputFormatterImpl formater for release note and changelog.
|
||||||
type OutputFormatterImpl struct {
|
type OutputFormatterImpl struct {
|
||||||
releasenoteTemplate *template.Template
|
releasenoteTemplate *template.Template
|
||||||
|
changelogTemplate *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOutputFormatter TemplateProcessor constructor.
|
// NewOutputFormatter TemplateProcessor constructor.
|
||||||
func NewOutputFormatter() *OutputFormatterImpl {
|
func NewOutputFormatter() *OutputFormatterImpl {
|
||||||
t := template.Must(template.New("releasenotes").Parse(rnTemplate))
|
cgl := template.Must(template.New("cglTemplate").Parse(cglTemplate))
|
||||||
template.Must(t.New("rnSectionItem").Parse(rnSectionItem))
|
rn := template.Must(cgl.New("rnTemplate").Parse(rnTemplate))
|
||||||
template.Must(t.New("rnSection").Parse(rnSection))
|
template.Must(rn.New("rnSectionItem").Parse(rnSectionItem))
|
||||||
template.Must(t.New("rnSectionBreakingChanges").Parse(rnSectionBreakingChanges))
|
template.Must(rn.New("rnSection").Parse(rnSection))
|
||||||
return &OutputFormatterImpl{releasenoteTemplate: t}
|
template.Must(rn.New("rnSectionBreakingChanges").Parse(rnSectionBreakingChanges))
|
||||||
|
return &OutputFormatterImpl{releasenoteTemplate: rn, changelogTemplate: cgl}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatReleaseNote format a release note.
|
// FormatReleaseNote format a release note.
|
||||||
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
|
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
|
||||||
templateVars := releaseNoteTemplateVariables{
|
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()),
|
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
|
||||||
p.releasenoteTemplate.Execute(&b, templateVars)
|
|
||||||
return b.String()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user