From b10696265f04b624ff98834d95f7751176ef83ac Mon Sep 17 00:00:00 2001 From: jo Date: Fri, 17 Jun 2022 17:07:44 +0200 Subject: [PATCH] ci: check and dispatch api schema changes - check if api schema is outdated - do not cancel in progress workflow Since this workflow commit to an external repo, it might squash multiple changes into a single commit. - dispatch each schema update to client repo - cache pip dependencies --- .github/workflows/api-schema.yml | 104 +++++++++++++++++++++++++------ api/Makefile | 5 +- 2 files changed, 87 insertions(+), 22 deletions(-) diff --git a/.github/workflows/api-schema.yml b/.github/workflows/api-schema.yml index e1fb64efe..2a097ecdc 100644 --- a/.github/workflows/api-schema.yml +++ b/.github/workflows/api-schema.yml @@ -3,40 +3,104 @@ name: API schema on: push: branches: [main] + paths: + - .github/workflows/** + - api/** + + pull_request: + branches: [main] + paths: + - .github/workflows/** + - api/** concurrency: group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true jobs: - generate: - if: github.repository_owner == 'libretime' - name: Generate - + check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-python@v4 with: python-version: "3.x" - - name: Clone client repo - uses: actions/checkout@v3 + - uses: actions/cache@v3 with: - repository: "${{ github.repository_owner }}/client" - path: client-repo - ssh-key: "${{ secrets.API_CLIENT_DEPLOY_KEY }}" + path: ~/.cache/pip + key: ${{ runner.os }}-pip-api-${{ hashFiles('api/**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip-api - - name: Generate schema - run: | - make schema - cp schema.yml ../client-repo/schema.yml + - name: Install + run: make install working-directory: api - - name: Push schema changes to API clients - uses: stefanzweifel/git-auto-commit-action@v4 + - name: Get pull request commit range + if: github.event_name == 'pull_request' + run: echo "COMMIT_RANGE=origin/${{ github.base_ref }}..${{ github.sha }}" >> $GITHUB_ENV + + - name: Get push commit range + if: github.event_name == 'push' + run: echo "COMMIT_RANGE=${{ github.event.before }}..${{ github.sha }}" >> $GITHUB_ENV + + - name: Check if schema is outdated + run: | + for commit in $(git rev-list --reverse --no-merges ${{ env.COMMIT_RANGE }}); do + git checkout $commit + + make --quiet schema + git add schema.yml + git diff-index --quiet HEAD -- || { + echo "ERROR: Schema is outdated for commit $commit" + git show --quiet + git diff -- schema.yml + exit 1 + } + done + working-directory: api + + dispatch: + if: > + github.repository_owner == 'libretime' + && github.event_name == 'push' + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 with: - commit_message: "chore: update schema for ${{ github.repository }}@${{ github.sha }}" - commit_user_name: "github-actions[bot]" - commit_user_email: "github-actions[bot]@users.noreply.github.com" - repository: ./client-repo + fetch-depth: 0 + + - uses: actions/checkout@v3 + with: + repository: libretime/client + path: client + ssh-key: "${{ secrets.API_CLIENT_DEPLOY_KEY }}" + + - name: Dispatch schema update commits + run: | + git config --global user.email "libretime-bot" + git config --global user.name "libretime-bot@users.noreply.github.com" + + for commit in $(git rev-list --reverse --no-merges ${{ github.event.before }}..${{ github.sha }} -- api/schema.yml); do + cp api/schema.yml client/ + + git show \ + --quiet \ + --format="%B%n${{ github.repository }}@%H" \ + "$commit" \ + > commit-message + + pushd client/ + git add schema.yml + git diff-index --quiet HEAD -- || { + git commit --file=../commit-message + git push + } + popd + + rm commit-message + done diff --git a/api/Makefile b/api/Makefile index 6ff3aeb05..65b8779ed 100644 --- a/api/Makefile +++ b/api/Makefile @@ -15,7 +15,8 @@ test: $(VENV) source $(VENV)/bin/activate DJANGO_SETTINGS_MODULE=libretime_api.settings.testing pytest -n $(CPU_CORES) --color=yes -v libretime_api +SCHEMA_FILE ?= schema.yml schema: $(VENV) source $(VENV)/bin/activate - DJANGO_SETTINGS_MODULE=libretime_api.settings.testing libretime-api spectacular --file schema.yml - if command -v npx > /dev/null; then npx prettier --write schema.yml; fi + DJANGO_SETTINGS_MODULE=libretime_api.settings.testing libretime-api spectacular --file $(SCHEMA_FILE) + if command -v npx > /dev/null; then npx prettier --write $(SCHEMA_FILE); fi