Skip to content

Commit

Permalink
Validate kinds match config in fragment when batching release notes
Browse files Browse the repository at this point in the history
* Add changie fragment checks in CI
  • Loading branch information
miniscruff committed Sep 8, 2024
1 parent 49bf31d commit 9b76e32
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20240908-004631.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed-typo-checking-ci
body: Validate kinds match config in fragment when batching release notes
time: 2024-09-08T00:46:31.38990871-07:00
custom:
Issue: "688"
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: Test
run: go test -coverprofile=c.out ./...

- name: Validate Changie fragments
run: go run main.go batch major --dry-run

- name: Coverage
uses: codacy/codacy-coverage-reporter-action@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ func (b *Batch) WriteChanges(changes []core.Change) error {

if b.config.KindFormat != "" && lastKind != change.Kind {
lastKind = change.Kind

newKind := b.config.KindFromKeyOrLabel(change.Kind)
if newKind == nil {
return fmt.Errorf("kind not found by key or label: '%s'", change.Kind)
}

kindHeader := b.config.KindHeader(change.Kind)

err := b.WriteTemplate(
Expand Down
21 changes: 21 additions & 0 deletions cmd/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,27 @@ func TestBatchWriteChanges(t *testing.T) {
then.Equals(t, expected, builder.String())
}

func TestBatchWriteChangesWithInvalidKind(t *testing.T) {
cfg := batchTestConfig()
then.WithTempDirConfig(t, cfg)

var builder strings.Builder

batch := NewBatch(time.Now, core.NewTemplateCache())
batch.config = cfg
batch.writer = &builder

changes := []core.Change{
{Kind: "added", Body: "w"},
{Kind: "added", Body: "x"},
{Kind: "removed", Body: "y"},
{Kind: "removed not found", Body: "z"},
}

err := batch.WriteChanges(changes)
then.NotNil(t, err)
}

func TestBatchCreateVersionsWithoutKindHeaders(t *testing.T) {
cfg := batchTestConfig()
cfg.KindFormat = ""
Expand Down
2 changes: 1 addition & 1 deletion core/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Change struct {
// Kind key of our change, if one was provided.
KindKey string `yaml:"kindKey,omitempty" default:""`
// Kind label of our change, if one was provided.
KindLabel string `yaml:"kindLabel,omitempty" default:""`
KindLabel string `yaml:"kindLabel,omitempty" default:"nil"`
}

// WriteTo will write a change to the writer as YAML
Expand Down
3 changes: 3 additions & 0 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
ErrBadVersionOrPart = errors.New("part string is not a supported version or version increment")
ErrMissingAutoLevel = errors.New("kind config missing auto level value for auto bumping")
ErrNoChangesFoundForAuto = errors.New("no unreleased changes found for automatic bumping")
ErrKindNotFound = errors.New("kind not found but configuration expects one")
)

var (
Expand Down Expand Up @@ -310,6 +311,8 @@ func GetChanges(
kc := cfg.KindFromKeyOrLabel(c.KindKey)
if kc != nil {
c.KindLabel = kc.Label
} else if len(cfg.Kinds) > 0 {
return nil, fmt.Errorf("%w: '%s'", ErrKindNotFound, c.KindKey)
}

changes = append(changes, c)
Expand Down
21 changes: 21 additions & 0 deletions core/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,27 @@ func TestGetAllChanges(t *testing.T) {
then.Equals(t, "third", changes[2].Body)
}

func TestGetAllChangesErrorIfNoKindWhenConfigured(t *testing.T) {
then.WithTempDir(t)

cfg := utilsTestConfig()
orderedTimes := []time.Time{
time.Date(2019, 5, 25, 20, 45, 0, 0, time.UTC),
}

changes := []Change{
{Kind: "added not found", Body: "ignored", Time: orderedTimes[0]},
}
for i, c := range changes {
then.WriteFileTo(t, c, cfg.ChangesDir, cfg.UnreleasedDir, fmt.Sprintf("%d.yaml", i))
}

then.CreateFile(t, cfg.ChangesDir, cfg.UnreleasedDir, "ignored.txt")

_, err := GetChanges(cfg, nil, "")
then.Err(t, ErrKindNotFound, err)
}

func TestGetAllChangesWithProject(t *testing.T) {
then.WithTempDir(t)

Expand Down
22 changes: 22 additions & 0 deletions docs/integrations/ci.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
When creating changie fragments as part of your team workflow, you may have
suggestions or comments about the kind, body or custom fields that are
changed outside of the changie tool.
As changie fragments are plain yaml files there is nothing wrong with editing
these after creating the fragment.
However, it is possible to create invalid fragments when doing so,
one such example is if you typo a kind or invalid custom prompt answer.

One way to prevent this issue from causing later problems is to run
changie as part of your CI tests.
Below is an example if you are using the github action.

```yaml
- name: Validate changie fragment is valid
uses: miniscruff/changie-action@VERSION # view action repo for latest version
with:
version: latest # use the latest changie version
# dry run may not be required as you likely aren't
# committing the changes anyway, but it will print
# to stdout this way
args: batch major --dry-run
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ nav:
- Goreleaser: integrations/goreleaser.md
- Release Trigger: integrations/release_trigger.md
- yq: integrations/yq.md
- ci: integrations/ci.md
- Config:
- config/index.md
- CLI:
Expand Down

0 comments on commit 9b76e32

Please sign in to comment.