|
| 1 | +name: API Specification |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - "src/**/*.php" |
| 7 | + - "config/**" |
| 8 | + - "composer.json" |
| 9 | + - "composer.lock" |
| 10 | + - "public/api-spec-v1.yaml" |
| 11 | + - "public/api-spec-v1.json" |
| 12 | + - "docker-compose.yml" |
| 13 | + |
| 14 | +env: |
| 15 | + COMPOSE_USER: runner |
| 16 | + |
| 17 | +jobs: |
| 18 | + api-spec-export: |
| 19 | + name: Ensure API specification is up to date |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v6 |
| 24 | + |
| 25 | + - name: Create docker network |
| 26 | + run: docker network create frontend |
| 27 | + |
| 28 | + # https://taskfile.dev/installation/#github-actions |
| 29 | + - uses: go-task/setup-task@v1 |
| 30 | + |
| 31 | + - name: Export API specification |
| 32 | + run: | |
| 33 | + task site:update |
| 34 | + task api:spec:export |
| 35 | +
|
| 36 | + - name: Check for uncommitted changes |
| 37 | + id: git-diff-spec |
| 38 | + continue-on-error: true |
| 39 | + run: | |
| 40 | + git diff --diff-filter=ACMRT --exit-code public/api-spec-v1.yaml public/api-spec-v1.json |
| 41 | +
|
| 42 | + - name: Comment PR if spec is outdated |
| 43 | + if: steps.git-diff-spec.outcome == 'failure' |
| 44 | + env: |
| 45 | + GH_TOKEN: ${{ github.token }} |
| 46 | + run: | |
| 47 | + gh pr comment ${{ github.event.pull_request.number }} \ |
| 48 | + --body "$(cat <<'EOF' |
| 49 | + ## API specification not up to date |
| 50 | +
|
| 51 | + The committed API specification files do not match the exported output. |
| 52 | +
|
| 53 | + Please run the following command, then commit and push the changes: |
| 54 | +
|
| 55 | + ```shell |
| 56 | + docker compose exec phpfpm composer update-api-spec |
| 57 | + ``` |
| 58 | + EOF |
| 59 | + )" \ |
| 60 | + --create-if-none --edit-last |
| 61 | +
|
| 62 | + - name: Fail if spec is outdated |
| 63 | + if: steps.git-diff-spec.outcome == 'failure' |
| 64 | + run: exit 1 |
| 65 | + |
| 66 | + api-spec-breaking-changes: |
| 67 | + name: Detect breaking changes in API specification |
| 68 | + runs-on: ubuntu-latest |
| 69 | + needs: [api-spec-export] |
| 70 | + permissions: |
| 71 | + pull-requests: write |
| 72 | + steps: |
| 73 | + - name: Checkout |
| 74 | + uses: actions/checkout@v6 |
| 75 | + |
| 76 | + - name: Fetch base branch for comparison |
| 77 | + run: git fetch --depth=1 origin ${{ github.base_ref }} |
| 78 | + |
| 79 | + - name: Detect breaking changes |
| 80 | + id: breaking |
| 81 | + continue-on-error: true |
| 82 | + uses: oasdiff/oasdiff-action/breaking@main |
| 83 | + with: |
| 84 | + base: "origin/${{ github.base_ref }}:public/api-spec-v1.yaml" |
| 85 | + revision: "public/api-spec-v1.yaml" |
| 86 | + fail-on: ERR |
| 87 | + |
| 88 | + - name: Generate changelog |
| 89 | + id: changelog |
| 90 | + continue-on-error: true |
| 91 | + uses: oasdiff/oasdiff-action/changelog@main |
| 92 | + with: |
| 93 | + base: "origin/${{ github.base_ref }}:public/api-spec-v1.yaml" |
| 94 | + revision: "public/api-spec-v1.yaml" |
| 95 | + format: markdown |
| 96 | + output-to-file: changelog.md |
| 97 | + |
| 98 | + - name: Comment PR - no changes |
| 99 | + if: steps.breaking.outcome == 'success' && hashFiles('changelog.md') == '' |
| 100 | + env: |
| 101 | + GH_TOKEN: ${{ github.token }} |
| 102 | + run: | |
| 103 | + gh pr comment ${{ github.event.pull_request.number }} \ |
| 104 | + --body "## API Specification |
| 105 | +
|
| 106 | + No changes detected in API specification." \ |
| 107 | + --create-if-none --edit-last |
| 108 | +
|
| 109 | + - name: Comment PR - non-breaking changes |
| 110 | + if: steps.breaking.outcome == 'success' && hashFiles('changelog.md') != '' |
| 111 | + env: |
| 112 | + GH_TOKEN: ${{ github.token }} |
| 113 | + run: | |
| 114 | + { |
| 115 | + echo "## API Specification - Non-breaking changes" |
| 116 | + echo "" |
| 117 | + cat changelog.md |
| 118 | + } > comment.md |
| 119 | + gh pr comment ${{ github.event.pull_request.number }} \ |
| 120 | + --body-file comment.md \ |
| 121 | + --create-if-none --edit-last |
| 122 | +
|
| 123 | + - name: Comment PR - breaking changes |
| 124 | + if: steps.breaking.outcome == 'failure' |
| 125 | + env: |
| 126 | + GH_TOKEN: ${{ github.token }} |
| 127 | + run: | |
| 128 | + { |
| 129 | + echo "## API Specification - Breaking changes detected" |
| 130 | + echo "" |
| 131 | + if [ -s changelog.md ]; then |
| 132 | + cat changelog.md |
| 133 | + else |
| 134 | + echo "The breaking changes action detected incompatible changes. Review the action logs for details." |
| 135 | + fi |
| 136 | + } > comment.md |
| 137 | + gh pr comment ${{ github.event.pull_request.number }} \ |
| 138 | + --body-file comment.md \ |
| 139 | + --create-if-none --edit-last |
| 140 | +
|
| 141 | + - name: Fail if breaking changes detected |
| 142 | + if: steps.breaking.outcome == 'failure' |
| 143 | + run: exit 1 |
0 commit comments