Skip to content

Commit

Permalink
option to include keys for kind configs (#657)
Browse files Browse the repository at this point in the history
* change sort checks both kind keys and kind values
  • Loading branch information
miniscruff committed May 13, 2024
1 parent fa5835d commit 4cafb25
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kind: Added
kind: added
body: Search upwards for a changie config file
time: 2024-05-10T01:20:51.00611009-07:00
custom:
Expand Down
5 changes: 5 additions & 0 deletions .changes/unreleased/added-20240511-231647.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: added
body: Key option to kind configs to separate visual labels from file names and yaml values
time: 2024-05-11T23:16:47.587612868-07:00
custom:
Issue: "612"
20 changes: 13 additions & 7 deletions .changie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ versionFormat: '## {{.Version}} on {{.Time.Format "2006-01-02"}}'
kindFormat: '### {{.Kind}}'
changeFormat: '* [#{{.Custom.Issue}}](https://github.com/miniscruff/changie/issues/{{.Custom.Issue}}) {{.Body}}'
kinds:
- label: Added
- label: ✨ Added
key: added
auto: minor
- label: Changed
- label: 🔥 Changed
key: changed
auto: major
- label: Deprecated
- label: ⚰️ Deprecated
key: deprecated
auto: minor
- label: Removed
auto: minor # temporarily consider removed a minor change
- label: Fixed
- label: 🗑️ Removed
key: removed
auto: minor
- label: 🪲 Fixed
key: fixed
auto: patch
- label: Security
- label: 🦺 Security
key: security
auto: patch
newlines:
afterChangelogHeader: 1
Expand Down
3 changes: 2 additions & 1 deletion cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,15 @@ func (b *Batch) WriteChanges(changes []core.Change) error {

if b.config.KindFormat != "" && lastKind != change.Kind {
lastKind = change.Kind
newKind := b.config.KindFromKeyOrLabel(change.Kind)
kindHeader := b.config.KindHeader(change.Kind)

err := b.WriteTemplate(
kindHeader,
b.config.Newlines.BeforeKind+1,
b.config.Newlines.AfterKind,
core.KindData{
Kind: lastKind,
Kind: newKind.Label,
Env: b.config.EnvVars(),
},
)
Expand Down
29 changes: 29 additions & 0 deletions cmd/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,35 @@ func TestBatchDryRun(t *testing.T) {
then.DirectoryFileCount(t, 3, cfg.ChangesDir, cfg.UnreleasedDir)
}

func TestBatchDryRunWithKeys(t *testing.T) {
cfg := batchTestConfig()
cfg.Kinds[0].Label = ":fire: Added"
cfg.Kinds[0].Key = "added"
then.WithTempDirConfig(t, cfg)

batch := NewBatch(time.Now, core.NewTemplateCache())
batch.DryRun = true

var builder strings.Builder

batch.Command.SetOut(&builder)
writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "D"})
writeChangeFile(t, cfg, &core.Change{Kind: "added", Body: "E"})
writeChangeFile(t, cfg, &core.Change{Kind: "removed", Body: "F"})

err := batch.Run(batch.Command, []string{"v0.2.0"})
then.Nil(t, err)

verContents := `## v0.2.0
### :fire: Added
* D
* E
### removed
* F`
then.Equals(t, verContents, builder.String())
then.DirectoryFileCount(t, 3, cfg.ChangesDir, cfg.UnreleasedDir)
}

func TestBatchRemovePrereleases(t *testing.T) {
cfg := batchTestConfig()
then.WithTempDirConfig(t, cfg)
Expand Down
6 changes: 3 additions & 3 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func mergeTestConfig() *core.Config {
KindFormat: "### {{.Kind}}",
ChangeFormat: "* {{.Body}}",
Kinds: []core.KindConfig{
{Label: "added"},
{Label: "removed"},
{Label: "other"},
{Label: "Added"},
{Label: "Removed"},
{Label: "Other"},
},
Newlines: core.NewlinesConfig{
// BeforeVersion: 1,
Expand Down
4 changes: 2 additions & 2 deletions core/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func ChangeLess(cfg *Config, changes []Change) func(i, j int) bool {
// Then sort by kind index
if len(cfg.Kinds) > 0 && a.Kind != b.Kind {
for _, k := range cfg.Kinds {
if a.Kind == k.Label {
if a.Kind == k.Key || a.Kind == k.Label {
return true
} else if b.Kind == k.Label {
} else if b.Kind == k.Key || b.Kind == k.Label {
return false
}
}
Expand Down
40 changes: 32 additions & 8 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type GetVersions func(Config) ([]*semver.Version, error)

// Kind config allows you to customize the options depending on what kind was selected.
type KindConfig struct {
// Key is the value used for lookups and file names for kinds.
// By default it will use label if no key is provided.
// example: yaml
// key: feature
Key string `yaml:",omitempty"`
// Label is the value used in the prompt when selecting a kind.
// example: yaml
// label: Feature
Expand Down Expand Up @@ -69,6 +74,15 @@ type KindConfig struct {
AutoLevel string `yaml:"auto,omitempty"`
}

// KeyOrLabel returns the kind config key if set, otherwise the label
func (kc *KindConfig) KeyOrLabel() string {
if kc.Key != "" {
return kc.Key
}

return kc.Label
}

// Body config allows you to customize the default body prompt
type BodyConfig struct {
// Min length specifies the minimum body length
Expand Down Expand Up @@ -374,20 +388,30 @@ type Config struct {
cachedEnvVars map[string]string
}

func (c *Config) KindHeader(label string) string {
for _, kindConfig := range c.Kinds {
if kindConfig.Format != "" && kindConfig.Label == label {
return kindConfig.Format
func (c *Config) KindFromKeyOrLabel(keyOrLabel string) *KindConfig {
for _, kc := range c.Kinds {
if kc.KeyOrLabel() == keyOrLabel {
return &kc
}
}

return nil
}

func (c *Config) KindHeader(keyOrLabel string) string {
for _, kc := range c.Kinds {
if kc.Format != "" && (kc.Key == keyOrLabel || kc.Label == keyOrLabel) {
return kc.Format
}
}

return c.KindFormat
}

func (c *Config) ChangeFormatForKind(label string) string {
for _, kindConfig := range c.Kinds {
if kindConfig.ChangeFormat != "" && kindConfig.Label == label {
return kindConfig.ChangeFormat
func (c *Config) ChangeFormatForKind(keyOrLabel string) string {
for _, kc := range c.Kinds {
if kc.ChangeFormat != "" && (kc.Key == keyOrLabel || kc.Label == keyOrLabel) {
return kc.ChangeFormat
}
}

Expand Down
24 changes: 24 additions & 0 deletions core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ func TestGetHeaderFromKindLabel(t *testing.T) {
then.Equals(t, "KF", format)
}

func TestGetHeaderFromKindKey(t *testing.T) {
config := Config{
Kinds: []KindConfig{
{Label: "A", Format: "unused"},
{Label: "unused", Format: ""},
{Key: "C", Format: "KF"},
},
}
format := config.KindHeader("C")
then.Equals(t, "KF", format)
}

func TestGetDefaultHeaderForKind(t *testing.T) {
config := Config{
Kinds: []KindConfig{
Expand All @@ -300,6 +312,18 @@ func TestGetChangeFormatFromKindLabel(t *testing.T) {
then.Equals(t, "CF", format)
}

func TestGetChangeFormatFromKindKey(t *testing.T) {
config := Config{
Kinds: []KindConfig{
{Label: "A", ChangeFormat: "unused"},
{Label: "unused", ChangeFormat: ""},
{Key: "C", ChangeFormat: "CF"},
},
}
format := config.ChangeFormatForKind("C")
then.Equals(t, "CF", format)
}

func TestGetDefaultChangeFormatIfNoCustomOnesExist(t *testing.T) {
config := Config{
Kinds: []KindConfig{
Expand Down
4 changes: 3 additions & 1 deletion core/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ func (p *Prompts) kind() error {
for i := range p.Config.Kinds {
kindConfig := &p.Config.Kinds[i]

if kindConfig.Label == p.Kind {
if kindConfig.Label == p.Kind || kindConfig.Key == p.Kind {
p.KindConfig = kindConfig
p.Kind = kindConfig.KeyOrLabel()

return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func HighestAutoLevel(config *Config, allChanges []Change) (string, error) {

for _, change := range allChanges {
for _, kc := range config.Kinds {
if kc.Label != change.Kind {
if kc.KeyOrLabel() != change.Kind {
continue
}

Expand Down

0 comments on commit 4cafb25

Please sign in to comment.