From 8c62a2bc9709acb6ce6a562ab7df589474260d3a Mon Sep 17 00:00:00 2001 From: Dmitry Kisler Date: Fri, 4 Aug 2023 00:20:39 +0200 Subject: [PATCH] CI: add auto-update SDK upon API spec updates (#21) * ci: add the step to verify SDK code difference in case of new open spec to reduce noise alerts. Signed-off-by: Dmitry Kisler * ci: add auto-commit and PR step Signed-off-by: Dmitry Kisler * ci: add auto-commit and PR step Signed-off-by: Dmitry Kisler --------- Signed-off-by: Dmitry Kisler --- .github/workflows/check-api-spec-updates.yaml | 71 ++++++++++++++++--- Makefile | 5 +- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/.github/workflows/check-api-spec-updates.yaml b/.github/workflows/check-api-spec-updates.yaml index fe561a8..d11e3eb 100644 --- a/.github/workflows/check-api-spec-updates.yaml +++ b/.github/workflows/check-api-spec-updates.yaml @@ -6,11 +6,14 @@ on: workflow_dispatch: jobs: - compare_to_new_spec: + main: + permissions: + pull-requests: write + contents: write runs-on: ubuntu-latest - name: Compare to new API spec outputs: - isNew: ${{ steps.diff.outputs.isNew }} + isNew: ${{ steps.diff_sdk.outputs.isNew }} + prId: ${{ steps.pr.outputs.pull-request-number }} steps: - uses: actions/checkout@v3 with: @@ -20,7 +23,7 @@ jobs: curl -SLo openAPIDefinition_new.json "$SPEC_URL" env: SPEC_URL: https://dfv3qgd2ykmrx.cloudfront.net/api_spec/release/v2.json - - name: Diff + - name: Diff spec files id: diff run: | if [ $(diff openAPIDefinition_new.json openAPIDefinition.json | wc -l) -gt 0 ]; then @@ -28,12 +31,62 @@ jobs: else echo "isNew=false" >> $GITHUB_OUTPUT fi + - uses: actions/setup-go@v4 + if: steps.diff.outputs.isNew == 'true' + with: + go-version: 1.18 + - name: Generate SDK using new spec + id: diff_sdk + if: steps.diff.outputs.isNew == 'true' + run: | + echo "isNew=false" >> $GITHUB_OUTPUT + + mkdir tmp + make generate-sdk PATH_SPEC=${PWD}/openAPIDefinition_new.json PATH_SDK=${PWD}/tmp + + for obj in $(ls ${PWD}/tmp); do + echo check ${obj} + if [ $(diff ${obj} ${PWD}/tmp/${obj} | wc -l) -gt 0 ]; then + echo diff in SDK generated using new new spec, file ${obj} + echo "isNew=true" >> $GITHUB_OUTPUT + fi + done + + - name: MV new sdk to root + if: steps.diff_sdk.outputs.isNew == 'true' + run: | + mv ${PWD}/tmp/* . + mv openAPIDefinition_new.json openAPIDefinition.json + + - name: Create Pull Request + if: steps.diff_sdk.outputs.isNew == 'true' + uses: peter-evans/create-pull-request@v5 + id: pr + with: + add-paths: | + *.go + go.mod + openAPIDefinition.json + commit-message: | + feat: auto-updated SDK + signoff: true + branch: feat/auto-generated-new-api-spec + branch-suffix: timestamp + delete-branch: true + assignees: kislerdm + title: Auto-generated SDK upon new spec + body: | + ## What changed + New generated SDK + ## Why do we need it + To reflect API spec changes + notify: runs-on: ubuntu-latest name: Notify needs: - - compare_to_new_spec - if: needs.compare_to_new_spec.outputs.isNew == 'true' + - main + if: needs.main.outputs.isNew == 'true' steps: - name: Telegram uses: appleboy/telegram-action@master @@ -42,6 +95,7 @@ jobs: token: ${{ secrets.TELEGRAM_TOKEN }} message: | New Neon API spec has been detected. Please take action. + PR: https://github.com/kislerdm/neon-sdk-go/pull/${{ needs.main.outputs.prId }} The message was sent from https://github.com/kislerdm/neon-sdk-go Get in touch for details: admin@dkisler.com @@ -52,10 +106,9 @@ jobs: -H "Title: Neon API Spec Updated" \ -H "Priority: urgent" \ -H "Tags: warning" \ - -H "Actions: view, Open API Spec, https://api-docs.neon.tech/; \ - view, Open Repo, https://github.com/kislerdm/neon-sdk-go; \ - view, Send Email to maintainer, mailto:admin@dkisler.com?subject=[alert]Neon-API-Updated" \ + -H "Actions: view, Open Repo: https://github.com/kislerdm/neon-sdk-go" \ -d "New Neon API spec has been detected. Please take action. + PR: https://github.com/kislerdm/neon-sdk-go/pull/${{ needs.main.outputs.prId }} The message was sent from https://github.com/kislerdm/neon-sdk-go Get in touch for details: admin@dkisler.com" \ diff --git a/Makefile b/Makefile index e2205d1..9bc2e0d 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,9 @@ PATH_SDK := $(PWD) .PHONY: generate-sdk generate-sdk: ## Generates the SDK codebase using code generator. - @ make build DIR=generator - @ ./generator/bin/generator-$(OS)-$(ARCH) --output $(PATH_SDK) --input $(PATH_SPEC) + @ cd generator && \ + go mod tidy && \ + CGO_ENABLED=0 go run cmd/main.go --output $(PATH_SDK) --input $(PATH_SPEC) .PHONY: tests tests: ## Run tests.