Skip to content

Commit

Permalink
chloggen: add component validation (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored Sep 21, 2023
1 parent 4e238e9 commit 2382304
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .chloggen/add-component-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
component: chloggen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: add component validation

# One or more tracking issues related to the change
issues: [401]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 1 addition & 1 deletion chloggen/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func validateCmd() *cobra.Command {
for changeLogKey := range globalCfg.ChangeLogs {
validChangeLogs = append(validChangeLogs, changeLogKey)
}
if err = entry.Validate(changelogRequired, validChangeLogs...); err != nil {
if err = entry.Validate(changelogRequired, globalCfg.Components, validChangeLogs...); err != nil {
return err
}
}
Expand Down
21 changes: 20 additions & 1 deletion chloggen/cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestValidateErr(t *testing.T) {
func TestValidate(t *testing.T) {
tests := []struct {
name string
cfgFn func(*config.Config)
entries []*chlog.Entry
wantErr string
}{
Expand Down Expand Up @@ -138,10 +139,28 @@ func TestValidate(t *testing.T) {
}(),
wantErr: "'fake' is not a valid 'change_type'",
},
{
name: "gomodule_validation",
cfgFn: func(cfg *config.Config) {
cfg.Components = []string{"github.com/foo/bar/receiver", "github.com/foo/bar/exporter"}
},
entries: func() []*chlog.Entry {
sampleEntries := getSampleEntries()
for _, e := range sampleEntries {
e.Component = "foo"
}
return sampleEntries
}(),
wantErr: "Error: foo is not a valid 'component'. It must be one of \\[github.com/foo/bar/receiver github.com/foo/bar/exporter\\]",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
globalCfg = config.New(t.TempDir())
cfg := config.New(t.TempDir())
if tc.cfgFn != nil {
tc.cfgFn(cfg)
}
globalCfg = cfg
setupTestDir(t, tc.entries)

out, err := runCobra(t, "validate")
Expand Down
14 changes: 13 additions & 1 deletion chloggen/internal/chlog/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var changeTypes = []string{
BugFix,
}

func (e Entry) Validate(requireChangelog bool, validChangeLogs ...string) error {
func (e Entry) Validate(requireChangelog bool, components []string, validChangeLogs ...string) error {
if requireChangelog && len(e.ChangeLogs) == 0 {
return fmt.Errorf("specify one or more 'change_logs'")
}
Expand Down Expand Up @@ -81,6 +81,18 @@ func (e Entry) Validate(requireChangelog bool, validChangeLogs ...string) error
return fmt.Errorf("specify a 'component'")
}

found := false
for _, validComp := range components {
if e.Component == validComp {
found = true
break
}
}
// only apply component validation if one or more values are present.
if len(components) > 0 && !found {
return fmt.Errorf("%s is not a valid 'component'. It must be one of %v", e.Component, components)
}

if strings.TrimSpace(e.Note) == "" {
return fmt.Errorf("specify a 'note'")
}
Expand Down
18 changes: 17 additions & 1 deletion chloggen/internal/chlog/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestEntry(t *testing.T) {
entry Entry
requireChangeLog bool
validChangeLogs []string
components []string
expectErr string
toString string
}{
Expand Down Expand Up @@ -196,11 +197,26 @@ func TestEntry(t *testing.T) {
validChangeLogs: []string{"foo", "bar"},
toString: "- `foo`: broke foo (#123)\n more details",
},
{
name: "with_components",
entry: Entry{
ChangeLogs: []string{"foo", "bar"},
ChangeType: "enhancement",
Component: "foo",
Note: "changed foo",
Issues: []int{123},
SubText: "more details",
},
components: []string{"bar"},
validChangeLogs: []string{"foo", "bar"},
toString: "- `foo`: changed foo (#123)\n more details",
expectErr: "foo is not a valid 'component'. It must be one of [bar]",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.entry.Validate(tc.requireChangeLog, tc.validChangeLogs...)
err := tc.entry.Validate(tc.requireChangeLog, tc.components, tc.validChangeLogs...)
if tc.expectErr != "" {
assert.Error(t, err)
assert.Equal(t, tc.expectErr, err.Error())
Expand Down
1 change: 1 addition & 0 deletions chloggen/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
DefaultChangeLogs []string `yaml:"default_change_logs"`
EntriesDir string `yaml:"entries_dir"`
TemplateYAML string `yaml:"template_yaml"`
Components []string `yaml:"components"`
ConfigYAML string
}

Expand Down
3 changes: 3 additions & 0 deletions chloggen/internal/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
# If 'change_logs' is specified in this file, and no value is specified for 'default_change_logs',
# then 'change_logs' MUST be specified in every entry file.
# default_change_logs: []

# The component values accepted. If empty, any component value is accepted.
# components: []

0 comments on commit 2382304

Please sign in to comment.