PR comment for OpenAPI Diff output #88
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Lint and validate OpenAPI specs | |
on: | |
- pull_request | |
jobs: | |
lint: | |
name: Lint OpenAPI definition | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out head branch | |
uses: actions/checkout@v4 | |
- name: Run OpenAPI Lint Action | |
uses: mhiew/redoc-lint-github-action@v4 | |
with: | |
args: 'openapi/task_execution_service.openapi.yaml' | |
validate: | |
name: Validate OpenAPI definition | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out head branch | |
uses: actions/checkout@v4 | |
- name: Run OpenAPI Validate Action | |
uses: char0n/swagger-editor-validate@v1 | |
with: | |
definition-file: openapi/task_execution_service.openapi.yaml | |
diff: | |
name: Show OpenAPI differences relative to target branch | |
runs-on: ubuntu-latest | |
outputs: | |
diff_generated: ${{ steps.upload-log.outputs.artifact_id }} | |
steps: | |
- name: Check out head branch | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.ref }} | |
path: head | |
- name: Check out base branch | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.base.ref }} | |
path: base | |
- name: Create output directory | |
run: mkdir -p diff-artifacts/ | |
- name: Run openapi-diff tool | |
run: | | |
docker run --rm \ | |
-v $(pwd)/head:/head:ro \ | |
-v $(pwd)/base:/base:ro \ | |
-v $(pwd)/diff-artifacts:/local \ | |
openapitools/openapi-diff:2.0.1 \ | |
/head/openapi/task_execution_service.openapi.yaml \ | |
/base/openapi/task_execution_service.openapi.yaml \ | |
--markdown /local/diff.md | |
- name: Check if OpenAPI Diff failed | |
id: check-diff | |
run: | | |
if [ -f diff-artifacts/error.log ]; then | |
echo "failed=true" >> $GITHUB_ENV | |
else | |
echo "failed=false" >> $GITHUB_ENV | |
fi | |
- name: Create PR comment if OpenAPI Diff failed | |
if: env.failed == 'true' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const path = require('path'); | |
const logPath = path.join(process.cwd(), 'diff-artifacts', 'error.log'); | |
let errorContent = "No error log found."; | |
try { | |
errorContent = fs.readFileSync(logPath, 'utf8'); | |
} catch (err) { | |
errorContent = "Error reading the log file: " + err.message; | |
} | |
const commentBody = ` | |
## OpenAPI Diff Action Failed | |
The OpenAPI Diff Action encountered an error. Here is the log: | |
\`\`\` | |
${errorContent} | |
\`\`\` | |
`; | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody, | |
}); | |
- name: Get PR number | |
id: get-pr-number | |
run: | | |
echo "${{ github.event.pull_request.number }}" > diff-artifacts/pr_number | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: diff-artifacts | |
path: diff-artifacts/ | |
if-no-files-found: ignore | |
permissions: | |
contents: read | |
pull-requests: write |