diff --git a/.github/workflows/update-schemas.yml b/.github/workflows/update-schemas.yml index 93866e7d3..a2e343384 100644 --- a/.github/workflows/update-schemas.yml +++ b/.github/workflows/update-schemas.yml @@ -21,9 +21,7 @@ jobs: with: go-version-file: go.mod - run: go install golang.org/x/tools/cmd/goimports@latest - - uses: arduino/setup-task@v2 - - id: update - run: make schemas-update + - run: make update-schemas - id: date run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT - id: changes @@ -33,16 +31,16 @@ jobs: with: author: GitHub body-path: changes.out - branch: update/${{ steps.date.outputs.date }}-${{ github.run_id }} - commit-message: "chore(update): update schemas (${{ steps.date.outputs.date }})" - title: "chore(update): update schemas (${{ steps.date.outputs.date }})" + branch: update-schemas/${{ steps.date.outputs.date }}-${{ github.run_id }} + commit-message: "chore(update-schemas): ${{ steps.date.outputs.date }}" + title: "chore(update-schemas): ${{ steps.date.outputs.date }}" labels: | dependencies - - name: Close previous update PRs + - name: Close previous update schemas PRs if: steps.create_pr.outputs.pull-request-operation == 'created' run: | new_pr_number=${{ steps.create_pr.outputs.pull-request-number }} - prs=$(gh pr list --state open --json number,headRefName --jq '.[] | select(.headRefName | startswith("update/")) | .number') + prs=$(gh pr list --state open --json number,headRefName --jq '.[] | select(.headRefName | startswith("update-schemas/")) | .number') for pr in $prs; do if [ "$pr" != "$new_pr_number" ]; then gh pr close $pr --comment "Auto-closing pull request in favor of #$new_pr_number" --delete-branch diff --git a/Makefile b/Makefile index 3fa3d6215..cdcdd4e28 100644 --- a/Makefile +++ b/Makefile @@ -186,14 +186,19 @@ docs: $(TFPLUGINDOCS) OLD_SCHEMA ?= .oldSchema.json CHANGELOG := PROVIDER_AIVEN_ENABLE_BETA=1 go run ./changelog/... -schemas-update: +dump-schemas: $(CHANGELOG) -save -schema=$(OLD_SCHEMA) - go get github.com/aiven/go-client-codegen@latest github.com/aiven/go-api-schemas@latest - go mod tidy - $(MAKE) generate + +diff-schemas: $(CHANGELOG) -diff -schema=$(OLD_SCHEMA) -changelog=CHANGELOG.md rm $(OLD_SCHEMA) +load-schemas: + go get github.com/aiven/go-client-codegen@latest github.com/aiven/go-api-schemas@latest + go mod tidy + +update-schemas: dump-schemas load-schemas generate diff-schemas + ################################################# # CI ################################################# diff --git a/changelog/formatter.go b/changelog/formatter.go index cb91c8dc3..3a769a559 100644 --- a/changelog/formatter.go +++ b/changelog/formatter.go @@ -20,7 +20,7 @@ type changelogItem struct { var ( reVersion = regexp.MustCompile(`\w+\.\w+\.\w+`) reDate = regexp.MustCompile(`\w{4}-\w{2}-\w{2}`) - reSplitEntries = regexp.MustCompile(`(?m)^(\b[^a-z]| *- +)`) // A line that begins with "-" or a non-letter + reSplitEntries = regexp.MustCompile(`(?m)^(\b[^a-z]| *- +)`) // A line that begins with "-" or a non-lowercase letter reBulletLevel = regexp.MustCompile(`^ *- +`) reSpaces = regexp.MustCompile(`\s+`) reTrailingSpace = regexp.MustCompile(`\s+$`) @@ -30,7 +30,7 @@ var ( // Soft-wraps lines to the given lineLength // When reformat is true, reformats the whole given content func updateChangelog(content string, lineLength int, reformat bool, addLines ...string) (string, error) { - if addLines == nil && !reformat { + if len(addLines) == 0 && !reformat { return content, nil } @@ -41,13 +41,13 @@ func updateChangelog(content string, lineLength int, reformat bool, addLines ... if len(items) != 0 && items[0].Version == draftVersion { // Appends to the current draft items[0].Content = fmt.Sprintf("%s\n%s", items[0].Content, addText) - } else { + } else if addText != "" { // The First item is not the draft, so we need to add a new item - items = append(items, &changelogItem{ + items = append([]*changelogItem{{ Version: draftVersion, Date: draftDate, - Content: content, - }) + Content: addText, + }}, items...) } result := lines[:start] @@ -65,8 +65,7 @@ func updateChangelog(content string, lineLength int, reformat bool, addLines ... } func parseItems(lines []string) ([]*changelogItem, int, int) { - start := max(0, len(lines)-1) - end := start + var start, end int var item *changelogItem items := make([]*changelogItem, 0) for i, line := range lines { @@ -93,7 +92,6 @@ func parseItems(lines []string) ([]*changelogItem, int, int) { } func formatContent(content string, lineLength int) string { - // Golang doesn't support regexp "lookarounds", so we need to split the content, // and then join it to keep what we otherwise would be just ignored by negative lookbehind seps := reSplitEntries.FindAllStringSubmatchIndex(content, -1) @@ -127,6 +125,10 @@ func formatContent(content string, lineLength int) string { } } + if len(list) == 0 { + return content + } + return strings.Join(list, "\n") } diff --git a/changelog/formatter_test.go b/changelog/formatter_test.go index f5bdc1103..2bd1d49f8 100644 --- a/changelog/formatter_test.go +++ b/changelog/formatter_test.go @@ -91,12 +91,74 @@ const testSampleExpected = `# Changelog for all user config options. ` -func TestFormatChangelog(t *testing.T) { +func TestUpdateChangelog(t *testing.T) { result, err := updateChangelog(testSample, 60, true, "foo", "bar", "Use TypeSet for ip_filter_object") require.NoError(t, err) assert.Empty(t, cmp.Diff(testSampleExpected, result)) } +func TestUpdateChangelog_nothing_to_change(t *testing.T) { + sample := `--- +title: Changelog +parent: README +nav_order: 1 +--- + +# Changelog + + + + +## [4.29.0] - 2024-11-14 + +- Add support for autoscaler service integration +` + result, err := updateChangelog(sample, 60, false) + require.NoError(t, err) + assert.Empty(t, cmp.Diff(sample, result)) +} + +func TestUpdateChangelog_empty_changelog(t *testing.T) { + sample := `--- +title: Changelog +parent: README +nav_order: 1 +--- + +# Changelog + + + + +## [4.29.0] - 2024-11-14 + +- Add support for autoscaler service integration +` + expect := `--- +title: Changelog +parent: README +nav_order: 1 +--- + +# Changelog + + + + +## [MAJOR.MINOR.PATCH] - YYYY-MM-DD + +- Foo +- Bar + +## [4.29.0] - 2024-11-14 + +- Add support for autoscaler service integration +` + result, err := updateChangelog(sample, 60, false, "Foo", "Bar") + require.NoError(t, err) + assert.Empty(t, cmp.Diff(expect, result)) +} + func TestLineWrapping(t *testing.T) { input := `Add capability to map external service user with internal aiven user with external_identity data source` expectList := []string{