Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic support for configurable changelog entry types #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cmd/changelog-entry/changelog-entry
Binary file not shown.
52 changes: 33 additions & 19 deletions cmd/changelog-entry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,26 @@ type Note struct {
URL string
}

func init() {
flag.Bool("add-url", false, "add GitHub issue URL (omitted by default due to formatting in changelog-build)")
flag.Int("pr", -1, "pull request number")
flag.String("subcategory", "", "the service or area of the codebase the pull request changes (optional)")
flag.String("type", "", "the type of change")
flag.String("description", "", "the changelog entry content")
flag.String("changelog-template", "", "the path of the file holding the template to use for the changelog entries")
flag.String("dir", "", "the relative path from the current directory of where the changelog entry file should be written")
flag.Parse()
}

func main() {
pwd, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var subcategory, changeType, description, changelogTmpl, dir, url string
var pr int
var Url bool
flag.BoolVar(&Url, "add-url", false, "add GitHub issue URL (omitted by default due to formatting in changelog-build)")
flag.IntVar(&pr, "pr", -1, "pull request number")
flag.StringVar(&subcategory, "subcategory", "", "the service or area of the codebase the pull request changes (optional)")
flag.StringVar(&changeType, "type", "", "the type of change")
flag.StringVar(&description, "description", "", "the changelog entry content")
flag.StringVar(&changelogTmpl, "changelog-template", "", "the path of the file holding the template to use for the changelog entries")
flag.StringVar(&dir, "dir", "", "the relative path from the current directory of where the changelog entry file should be written")
flag.Parse()

var url string
pr := flag.Lookup("pr").Value.(flag.Getter).Get().(int)
if pr == -1 {
pr, url, err = getPrNumberFromGithub(pwd)
if err != nil {
Expand All @@ -60,13 +62,14 @@ func main() {
flag.Usage()
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "Found matching pull request:", url)
}
fmt.Fprintln(os.Stderr, "Found matching pull request:", url)

changeType := flag.Lookup("type").Value.(flag.Getter).Get().(string)
if changeType == "" {
prompt := promptui.Select{
Label: "Select a change type",
Items: changelog.TypeValues,
Items: changelog.TypeValues(),
}

_, changeType, err = prompt.Run()
Expand All @@ -86,11 +89,13 @@ func main() {
}
}

subcategory := flag.Lookup("subcategory").Value.(flag.Getter).Get().(string)
if subcategory == "" {
prompt := promptui.Prompt{Label: "Subcategory (optional)"}
subcategory, err = prompt.Run()
}

description := flag.Lookup("description").Value.(flag.Getter).Get().(string)
if description == "" {
prompt := promptui.Prompt{Label: "Description"}
description, err = prompt.Run()
Expand All @@ -103,23 +108,28 @@ func main() {
}

var tmpl *template.Template
if changelogTmpl != "" {
file, err := os.ReadFile(changelogTmpl)
changelogTmpl := flag.Lookup("changelog-template").Value.(flag.Getter).Get().(string)
if changelogTmpl == "" {
tmpl, err = template.New("").Parse(changelogTmplDefault)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating changelog template from defaults:", err)
os.Exit(1)
}
tmpl, err = template.New("").Parse(string(file))
} else {
file, err := os.ReadFile(changelogTmpl)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading changelog template file:", err)
os.Exit(1)
}
} else {
tmpl, err = template.New("").Parse(changelogTmplDefault)
tmpl, err = template.New("").Parse(string(file))
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating changelog template:", err)
os.Exit(1)
}
}

if !Url {
addUrl := flag.Lookup("add-url").Value.(flag.Getter).Get().(bool)
if !addUrl {
url = ""
}
n := Note{Type: changeType, Description: description, Subcategory: subcategory, PR: pr, URL: url}
Expand All @@ -128,12 +138,16 @@ func main() {
err = tmpl.Execute(&buf, n)
fmt.Printf("\n%s\n", buf.String())
if err != nil {
fmt.Fprintln(os.Stderr, "Error rendering changelog entry:", err)
os.Exit(1)
}

filename := fmt.Sprintf("%d.txt", pr)
dir := flag.Lookup("dir").Value.(flag.Getter).Get().(string)
filepath := path.Join(pwd, dir, filename)
err = os.WriteFile(filepath, buf.Bytes(), 0644)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing changelog entry to file:", err)
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "Created changelog entry at", filepath)
Expand Down
45 changes: 34 additions & 11 deletions entry.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package changelog

import (
_ "embed"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"

Expand All @@ -16,15 +20,8 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
)

var TypeValues = []string{"enhancement",
"feature",
"bug",
"note",
"new-resource",
"new-datasource",
"deprecation",
"breaking-change",
}
//go:embed types.txt
var defaultTypes string

type Entry struct {
Issue string
Expand All @@ -39,6 +36,10 @@ type EntryList struct {
es []*Entry
}

func init() {
flag.String("types-file", "", "the path of a file holding a line-delimited list of valid changelog entry types")
}

// NewEntryList returns an EntryList with capacity c
func NewEntryList(c int) *EntryList {
return &EntryList{
Expand Down Expand Up @@ -139,7 +140,7 @@ func Diff(repo, ref1, ref2, dir string) (*EntryList, error) {
if err := wt.Checkout(&git.CheckoutOptions{
Hash: *rev2,
Force: true,
}); err != nil {
}); err != nil {
return nil, fmt.Errorf("could not checkout repository at %s: %w", ref2, err)
}
entriesAfterFI, err := wt.Filesystem.ReadDir(dir)
Expand Down Expand Up @@ -215,8 +216,30 @@ func Diff(repo, ref1, ref2, dir string) (*EntryList, error) {
return entries, nil
}

// TODO: memoize?
func TypeValues() []string {
var types string

changeTypesPath := flag.Lookup("types-file").Value.(flag.Getter).Get().(string)
fmt.Fprintln(os.Stderr, changeTypesPath)

if changeTypesPath != "" {
file, err := os.ReadFile(changeTypesPath)
if err != nil {
fmt.Fprintln(os.Stderr, changeTypesPath)
fmt.Fprintln(os.Stderr, "Error reading changelog entry types file:", err)
os.Exit(1)
}
types = strings.TrimSpace(string(file))
} else {
types = defaultTypes
}

return strings.Split(types, "\n")
}

func TypeValid(Type string) bool {
for _, a := range TypeValues {
for _, a := range TypeValues() {
if a == Type {
return true
}
Expand Down
8 changes: 8 additions & 0 deletions types.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enhancement
feature
bug
note
new-resource
new-datasource
deprecation
breaking-change