|
| 1 | +name: OpenAPI Diff |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - src/lib/** |
| 7 | + - .github/workflows/openapi-diff.yaml |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + stable_version: |
| 11 | + description: 'Stable Unleash version to compare against (e.g. unleash-server:6.9.3 or unleash-enterprise:6.10.0)' |
| 12 | + required: false |
| 13 | + default: 'unleash-server:latest' |
| 14 | + |
| 15 | +jobs: |
| 16 | + generate-openapi-stable: |
| 17 | + name: Generate OpenAPI (stable) |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + - name: Start Unleash test instance |
| 22 | + run: | |
| 23 | + docker compose -f .github/docker-compose.test.yml up -d --wait -t 90 |
| 24 | + env: |
| 25 | + FRONTEND_TEST_LICENSE: ${{ secrets.FRONTEND_TEST_LICENSE }} |
| 26 | + UNLEASH_VERSION: ${{ github.event.inputs.stable_version || 'unleash-server:main-edge' }} |
| 27 | + CHECK_VERSION: 'false' |
| 28 | + - name: Wait for Unleash to be ready |
| 29 | + run: | |
| 30 | + for i in {1..30}; do |
| 31 | + if curl -sf http://localhost:3000/health; then |
| 32 | + echo "Unleash is up!"; |
| 33 | + exit 0 |
| 34 | + fi |
| 35 | + echo "Waiting for Unleash..."; |
| 36 | + sleep 2 |
| 37 | + done |
| 38 | + echo "Unleash did not become ready in time." |
| 39 | + exit 1 |
| 40 | + - name: Download OpenAPI spec from (${{ github.event.inputs.stable_version || 'tip of main' }}) |
| 41 | + run: curl -sSL -o openapi-stable.json "localhost:3000/docs/openapi.json" |
| 42 | + - name: Upload openapi-stable.json |
| 43 | + uses: actions/upload-artifact@v4 |
| 44 | + with: |
| 45 | + name: openapi-stable |
| 46 | + path: openapi-stable.json |
| 47 | + |
| 48 | + generate-openapi-current: |
| 49 | + name: Generate OpenAPI (current branch) |
| 50 | + runs-on: ubuntu-latest |
| 51 | + services: |
| 52 | + # Label used to access the service container |
| 53 | + postgres: |
| 54 | + # Docker Hub image |
| 55 | + image: postgres |
| 56 | + # Provide the password for postgres |
| 57 | + env: |
| 58 | + POSTGRES_PASSWORD: postgres |
| 59 | + POSTGRES_INITDB_ARGS: "--no-sync" |
| 60 | + # Set health checks to wait until postgres has started |
| 61 | + ports: |
| 62 | + - 5432:5432 |
| 63 | + options: >- |
| 64 | + --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 |
| 65 | + steps: |
| 66 | + - uses: actions/checkout@v4 |
| 67 | + - name: Install node |
| 68 | + uses: actions/setup-node@v4 |
| 69 | + with: |
| 70 | + node-version: 22.x |
| 71 | + - name: Install dependencies |
| 72 | + run: | |
| 73 | + yarn install --immutable |
| 74 | + - name: Start Unleash test instance |
| 75 | + run: | |
| 76 | + # fake frontend build |
| 77 | + mkdir frontend/build |
| 78 | + touch frontend/build/index.html |
| 79 | + touch frontend/build/favicon.ico |
| 80 | + # end fake frontend build |
| 81 | +
|
| 82 | + # start unleash in background |
| 83 | + NODE_ENV=openapi yarn dev:backend & |
| 84 | + env: |
| 85 | + DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres |
| 86 | + DATABASE_SSL: 'false' |
| 87 | + CHECK_VERSION: 'false' |
| 88 | + - name: Wait for Unleash to be ready |
| 89 | + run: | |
| 90 | + for i in {1..30}; do |
| 91 | + if curl -sf http://localhost:4242/health; then |
| 92 | + echo "Unleash is up!"; |
| 93 | + break; |
| 94 | + fi |
| 95 | + echo "Waiting for Unleash..."; |
| 96 | + sleep 2 |
| 97 | + done |
| 98 | + - name: Download OpenAPI spec (current branch) |
| 99 | + run: curl -sSL -o openapi-current.json localhost:4242/docs/openapi.json |
| 100 | + - name: Upload openapi-current.json |
| 101 | + uses: actions/upload-artifact@v4 |
| 102 | + with: |
| 103 | + name: openapi-current |
| 104 | + path: openapi-current.json |
| 105 | + |
| 106 | + openapi-diff: |
| 107 | + name: OpenAPI Diff |
| 108 | + runs-on: ubuntu-latest |
| 109 | + needs: [generate-openapi-current, generate-openapi-stable] |
| 110 | + if: github.event_name == 'pull_request' |
| 111 | + steps: |
| 112 | + - name: Download openapi-current.json |
| 113 | + uses: actions/download-artifact@v4 |
| 114 | + with: |
| 115 | + name: openapi-current |
| 116 | + - name: Download openapi-stable.json |
| 117 | + uses: actions/download-artifact@v4 |
| 118 | + with: |
| 119 | + name: openapi-stable |
| 120 | + - name: Run OpenAPI diff |
| 121 | + id: diff |
| 122 | + run: | |
| 123 | + docker run --rm -t -v $(pwd):/specs:ro tufin/oasdiff changelog --format markdown /specs/openapi-stable.json /specs/openapi-current.json > openapi-diff.txt || true |
| 124 | + # then output in a format that can be used in GitHub Actions |
| 125 | + docker run --rm -t -v $(pwd):/specs:ro tufin/oasdiff changelog --format githubactions /specs/openapi-stable.json /specs/openapi-current.json |
| 126 | + - name: Comment on PR with OpenAPI diff |
| 127 | + uses: actions/github-script@v7 |
| 128 | + with: |
| 129 | + script: | |
| 130 | + const fs = require('fs'); |
| 131 | + const diff = fs.readFileSync('openapi-diff.txt', 'utf8'); |
| 132 | + const diffLines = diff.split('\n').filter(line => line.trim() !== '' && !line.startsWith('#')).length; |
| 133 | + const marker = '### OpenAPI Diff'; |
| 134 | + // Get all comments on the PR |
| 135 | + const { data: comments } = await github.rest.issues.listComments({ |
| 136 | + issue_number: context.issue.number, |
| 137 | + owner: context.repo.owner, |
| 138 | + repo: context.repo.repo, |
| 139 | + }); |
| 140 | + // Find existing OpenAPI Diff comment |
| 141 | + const existing = comments.find(c => c.body && c.body.includes(marker) && c.user.type === 'Bot'); |
| 142 | + let body; |
| 143 | + if (diffLines > 300) { |
| 144 | + body = `${marker} too long, check the this task output for details.`; |
| 145 | + console.log(diff); |
| 146 | + } else if (diffLines > 0) { |
| 147 | + body = `${marker}\n\n\`\`\`diff\n${diff}\n\`\`\``; |
| 148 | + } else { |
| 149 | + body = null; |
| 150 | + } |
| 151 | + if (body) { |
| 152 | + if (existing) { |
| 153 | + await github.rest.issues.updateComment({ |
| 154 | + comment_id: existing.id, |
| 155 | + owner: context.repo.owner, |
| 156 | + repo: context.repo.repo, |
| 157 | + body, |
| 158 | + }); |
| 159 | + } else { |
| 160 | + await github.rest.issues.createComment({ |
| 161 | + issue_number: context.issue.number, |
| 162 | + owner: context.repo.owner, |
| 163 | + repo: context.repo.repo, |
| 164 | + body, |
| 165 | + }); |
| 166 | + } |
| 167 | + } else { |
| 168 | + console.log('No significant changes detected in OpenAPI spec.'); |
| 169 | + if (existing) { |
| 170 | + await github.rest.issues.deleteComment({ |
| 171 | + comment_id: existing.id, |
| 172 | + owner: context.repo.owner, |
| 173 | + repo: context.repo.repo, |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
0 commit comments