Skip to content

Commit

Permalink
ci: add update schemas workflow (#1895)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Nov 13, 2024
1 parent 3174ef3 commit 85784bc
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 42 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ updates:
applies-to: security-updates
patterns:
- "*"
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 0
ignore:
# These libs are updated by a Makefile command
- dependency-name: github.com/aiven/go-client-codegen
- dependency-name: github.com/aiven/go-api-schemas
51 changes: 51 additions & 0 deletions .github/workflows/update-schemas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Update Schemas

on:
schedule:
- cron: 0 5 * * 1-5
workflow_dispatch: {}

permissions: read-all

jobs:
updater:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
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
- id: date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- id: changes
run: echo "changes=$(git diff CHANGELOG.md | awk '/^\+[ -]/ {print substr($0, 2)}')" >> $GITHUB_OUTPUT
- id: create_pr
uses: peter-evans/create-pull-request@v7
with:
author: GitHub <[email protected]>
body: ${{ steps.changes.outputs.changes }}
branch: update/${{ steps.date.outputs.date }}-${{ github.run_id }}
commit-message: "chore(update): update schemas (${{ steps.date.outputs.date }})"
labels: |
dependencies
- name: Close previous update 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')
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
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26 changes: 0 additions & 26 deletions .github/workflows/userconfig-generate-schema.yml

This file was deleted.

16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,26 @@ sweep-check:

generate: gen-go docs

OLD_SCHEMA ?= .oldSchema.json
CHANGELOG := PROVIDER_AIVEN_ENABLE_BETA=1 go run ./changelog/...
gen-go:
$(CHANGELOG) -save -schema=$(OLD_SCHEMA)
go generate ./...;
$(MAKE) fmt-imports
$(CHANGELOG) -diff -schema=$(OLD_SCHEMA) -changelog=CHANGELOG.md
rm $(OLD_SCHEMA)


docs: $(TFPLUGINDOCS)
rm -f docs/.DS_Store
PROVIDER_AIVEN_ENABLE_BETA=1 $(TFPLUGINDOCS) generate
rm -f docs/data-sources/influxdb*.md
rm -f docs/resources/influxdb*.md

OLD_SCHEMA ?= .oldSchema.json
CHANGELOG := PROVIDER_AIVEN_ENABLE_BETA=1 go run ./changelog/...
schemas-update:
$(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) gen-go
$(CHANGELOG) -diff -schema=$(OLD_SCHEMA) -changelog=CHANGELOG.md
rm $(OLD_SCHEMA)

#################################################
# CI
#################################################
Expand Down
46 changes: 45 additions & 1 deletion changelog/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ func diffItems(resourceType RootType, was, have *Item) (*Diff, error) {
if !haveValue.(bool) {
entry = "no longer beta"
}
case "enum":
entry = cmpList(wasValue.([]string), haveValue.([]string))
default:
// The rest of the fields will have diff-like entry
entry = fmt.Sprintf("%s ~~`%s`~~ -> `%s`", k, strValue(wasValue), strValue(haveValue))
entry = fmt.Sprintf("%s ~~`%s`~~ `%s`", k, strValue(wasValue), strValue(haveValue))

// Fixes formatting issues
entry = strings.ReplaceAll(entry, "``", "`")
Expand Down Expand Up @@ -183,3 +185,45 @@ func serializeDiff(list []*Diff) []string {
}
return strs
}

func cmpList[T any](was, have []T) string {
const (
remove int = 1 << iota
add
)

seen := make(map[string]int)
for _, v := range was {
seen[fmt.Sprintf("`%v`", v)] = remove
}

for _, v := range have {
seen[fmt.Sprintf("`%v`", v)] |= add
}

var added, removed []string
for k, v := range seen {
switch v {
case add:
added = append(added, k)
case remove:
removed = append(removed, k)
}
}

result := make([]string, 0)
if s := joinSorted(added); s != "" {
result = append(result, "add "+s)
}

if s := joinSorted(removed); s != "" {
result = append(result, "remove "+s)
}

return joinSorted(result)
}

func joinSorted(args []string) string {
sort.Strings(args)
return strings.Join(args, ", ")
}
38 changes: 36 additions & 2 deletions changelog/differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCompare(t *testing.T) {
}{
{
name: "change enums",
expect: "Change `foo` resource field `bar`: enum ~~`bar`, `baz`~~ -> `foo`, `baz`",
expect: "Change `foo` resource field `bar`: add `foo`, remove `bar`",
kind: ResourceRootType,
old: &Item{
Type: schema.TypeString,
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestCompare(t *testing.T) {
},
{
name: "change type",
expect: "Change `foo` resource field `bar`: type ~~`list`~~ -> `set`",
expect: "Change `foo` resource field `bar`: type ~~`list`~~ `set`",
kind: ResourceRootType,
old: &Item{
Type: schema.TypeList,
Expand Down Expand Up @@ -119,3 +119,37 @@ func TestSerializeDiff(t *testing.T) {
actual := serializeDiff(list)
assert.Empty(t, cmp.Diff(expect, actual))
}

func TestCmpList(t *testing.T) {
cases := []struct {
was, have []string
expect string
}{
{
was: []string{"a", "b", "c"},
have: []string{"a", "b", "c"},
expect: "",
},
{
was: []string{"a", "b", "c"},
have: []string{"a", "b", "c", "d", "f"},
expect: "add `d`, `f`",
},
{
was: []string{"a", "b", "c"},
have: []string{"a", "c"},
expect: "remove `b`",
},
{
was: []string{"a", "b", "c", "f"},
have: []string{"a", "b", "c", "d"},
expect: "add `d`, remove `f`",
},
}

for _, opt := range cases {
t.Run(opt.expect, func(t *testing.T) {
assert.Equal(t, opt.expect, cmpList(opt.was, opt.have))
})
}
}
6 changes: 4 additions & 2 deletions changelog/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ func softWrap(text string, size int) []string {

j := 0
result := make([]string, 1)
for i, w := range strings.Split(text, " ") {
words := strings.Split(text, " ")
for i, w := range words {
w = strings.ReplaceAll(w, "⍽", " ")
switch {
case i == 0:
result[j] += w
case len(result[j])+len(w) < size:
case len(result[j])+len(w) < size || i == len(words)-1:
// When the size is not reached, or it's the last word
result[j] += " " + w
default:
result = append(result, w) // nolint: makezero // By some reason linter doesn't understand it has length 1
Expand Down
5 changes: 2 additions & 3 deletions changelog/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ func TestLineWrapping(t *testing.T) {
"to map external service",
"user with internal aiven",
"user with",
"external_identity data",
"source",
"external_identity data source",
}
expectPoint := "- Add capability\n to map external service\n user with internal aiven\n user with\n external_identity data\n source"
expectPoint := "- Add capability\n to map external service\n user with internal aiven\n user with\n external_identity data source"
assert.Equal(t, expectList, softWrap(input, 25))
assert.Equal(t, expectPoint, addBullet("- ", expectList))
}
Expand Down
14 changes: 12 additions & 2 deletions changelog/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ func removeEnum(text string) string {
return reEnum.ReplaceAllString(text, "")
}

var reCode = regexp.MustCompile("`[^`]+`")
var reCode = regexp.MustCompile("`([^`]+)`")

func findEnums(description string) []string {
parts := strings.Split(description, userconfig.PossibleValuesPrefix)
if len(parts) != 2 {
return nil
}
values := reCode.FindAllString(parts[1], -1)
if len(values) == 0 {
return nil
}

// Removes ` from the beginning and end of the string
result := make([]string, len(values))
for i, v := range values {
result[i] = v[1 : len(v)-1]
}

return reCode.FindAllString(parts[1], -1)
return result
}

// strValue formats Go value into humanreadable string
Expand Down

0 comments on commit 85784bc

Please sign in to comment.