Skip to content

Commit

Permalink
Add validation for changelog label (#504)
Browse files Browse the repository at this point in the history
* Add validation for changelog label

* Fix test name
  • Loading branch information
Kevin Dorosh authored Oct 19, 2022
1 parent 08d4573 commit 1725f92
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog/v0.22.4/require-label.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: NEW_FEATURE
description: Add require label to changelog validation to ensure we have valid version
issueLink: https://github.com/solo-io/go-utils/issues/503
12 changes: 12 additions & 0 deletions changelogutils/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var (
InvalidLabelError = func(label string, allowed []string) error {
return eris.Errorf("Changelog version has label %s, which isn't in the list of allowed labels: %v", label, allowed)
}
ExpectedVersionLabelError = func(actual string) error {
return eris.Errorf("Expected version %s to to have a semver label suffix", actual)
}
)

type ChangelogValidator interface {
Expand Down Expand Up @@ -92,6 +95,11 @@ type ValidationSettings struct {
// versioning for new features or breaking changes
RelaxSemverValidation bool `json:"relaxSemverValidation"`

// If true, then the validator will require a changelog version with a label.
// This is useful to enforce version schemes like we use for envoy-gloo / envoy-gloo-ee, which always have the form:
// $ENVOY_VERSION-$PATCH_NUM
RequireLabel bool `json:"requireLabel"`

// If non-empty, then the validator will reject a changelog if the version's label is not contained in this slice
AllowedLabels []string `json:"allowedLabels"`

Expand Down Expand Up @@ -222,6 +230,10 @@ func (c *changelogValidator) validateVersionBump(ctx context.Context, latestTag
return err
}

if settings.RequireLabel && len(changelog.Version.Label) == 0 {
return ExpectedVersionLabelError(changelog.Version.String())
}

// If the settings contain specific allowed labels, ensure the label used here, if any, is in the list
if changelog.Version.Label != "" && len(settings.AllowedLabels) > 0 {
if !stringutils.ContainsString(changelog.Version.Label, settings.AllowedLabels) {
Expand Down
37 changes: 37 additions & 0 deletions changelogutils/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var _ = Describe("changelog validator utils", func() {
code.EXPECT().GetFileContents(ctx, changelogutils.GetValidationSettingsPath()).Return([]byte(validationYaml), nil).AnyTimes()
}

requireLabelSettingsExists := func() {
repoClient.EXPECT().FileExists(ctx, sha, changelogutils.GetValidationSettingsPath()).Return(true, nil).AnyTimes()
code.EXPECT().GetFileContents(ctx, changelogutils.GetValidationSettingsPath()).Return([]byte(requireLabelsYaml), nil).AnyTimes()
}

Context("should check changelog", func() {
BeforeEach(func() {
code.EXPECT().GetSha().Return(sha)
Expand Down Expand Up @@ -772,12 +777,44 @@ var _ = Describe("changelog validator utils", func() {
Expect(err).To(BeNil())
})
})

Context("require label settings", func() {

It("properly errors without a label on erroneous new changelog files", func() {
requireLabelSettingsExists()
validationPath := changelogutils.GetValidationSettingsPath()
file1 := github.CommitFile{Filename: &path1, Status: &added}
file2 := github.CommitFile{Filename: &validationPath, Status: &added}
cc := github.CommitsComparison{Files: []*github.CommitFile{&file1, &file2}}
repoClient.EXPECT().
CompareCommits(ctx, base, sha).
Return(&cc, nil)
code.EXPECT().
GetFileContents(ctx, path1).
Return([]byte(validChangelog1), nil).Times(2)
repoClient.EXPECT().
FindLatestTagIncludingPrereleaseBeforeSha(ctx, base).
Return("v0.5.0", nil)
code.EXPECT().
ListFiles(ctx, changelogutils.ChangelogDirectory).
Return([]os.FileInfo{getChangelogDir(tag)}, nil)
code.EXPECT().
ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, tag)).
Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil)
file, err := validator.ValidateChangelog(ctx)
Expect(file).To(BeNil())
Expect(err).To(MatchError(changelogutils.ExpectedVersionLabelError(tag)))
})
})
})
})

const (
validationYaml = `
relaxSemverValidation: true
`
requireLabelsYaml = `
requireLabel: true
`
allowedLabelsYaml = `
allowedLabels:
Expand Down

0 comments on commit 1725f92

Please sign in to comment.