Skip to content

Commit

Permalink
CI: add auto-update SDK upon API spec updates (#21)
Browse files Browse the repository at this point in the history
* ci: add the step to verify SDK code difference in case of new open spec to reduce noise alerts.

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* ci: add auto-commit and PR step

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

* ci: add auto-commit and PR step

Signed-off-by: Dmitry Kisler <admin@dkisler.com>

---------

Signed-off-by: Dmitry Kisler <admin@dkisler.com>
  • Loading branch information
kislerdm authored Aug 3, 2023
1 parent 0cee2b6 commit 8c62a2b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
71 changes: 62 additions & 9 deletions .github/workflows/check-api-spec-updates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -20,20 +23,70 @@ 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
echo "isNew=true" >> $GITHUB_OUTPUT
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
Expand All @@ -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
Expand All @@ -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" \
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8c62a2b

Please sign in to comment.