Skip to content

Commit 53b2c1e

Browse files
cp-at-mitpre-commit-ci[bot]github-advanced-security[bot]
authored
open api diff action (#3064)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 3257153 commit 53b2c1e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

.github/workflows/openapi-diff.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: OpenAPI Diff
2+
on: [pull_request]
3+
permissions:
4+
contents: read
5+
pull-requests: write
6+
jobs:
7+
openapi-diff:
8+
runs-on: ubuntu-22.04
9+
steps:
10+
- name: Checkout HEAD
11+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
12+
with:
13+
ref: ${{ github.head_ref }}
14+
path: head
15+
- name: Checkout BASE
16+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
17+
with:
18+
ref: ${{ github.base_ref }}
19+
path: base
20+
- name: Generate oasdiff changelog
21+
id: oasdif_changelog
22+
run: | # Capture changelog as a multiline output
23+
echo "changelog<<EOF" > $GITHUB_OUTPUT
24+
for spec in base/openapi/specs/*.yaml; do
25+
base_spec="$spec"
26+
head_spec="head/openapi/specs/$(basename "$spec")"
27+
if [ -f "$head_spec" ]; then
28+
echo "## Changes for $(basename "$spec"):" >> $GITHUB_OUTPUT
29+
docker run --rm \
30+
--workdir ${{ github.workspace }} \
31+
--volume ${{ github.workspace }}:${{ github.workspace }}:rw \
32+
-e GITHUB_WORKSPACE=${{ github.workspace }} \
33+
tufin/oasdiff changelog \
34+
"$base_spec" \
35+
"$head_spec" \
36+
>> $GITHUB_OUTPUT
37+
echo "" >> $GITHUB_OUTPUT
38+
fi
39+
done
40+
echo "EOF" >> $GITHUB_OUTPUT
41+
- name: Get summary
42+
id: oasdif_summary
43+
run: |
44+
echo summary=$(echo "${{ steps.oasdif_changelog.outputs.changelog }}" | head -1 ) \
45+
>> $GITHUB_OUTPUT
46+
- name: Find existing comment
47+
id: find_comment
48+
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3
49+
with:
50+
token: ${{ secrets.GITHUB_TOKEN }}
51+
repository: ${{ github.repository }}
52+
issue-number: ${{ github.event.pull_request.number }}
53+
body-includes: "## OpenAPI Changes"
54+
- name: Post changes as comment
55+
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4
56+
# Even if no changes, make sure to update old comment if it was found.
57+
if: steps.oasdif_summary.outputs.summary || steps.find_comment.outputs.comment-id
58+
with:
59+
token: ${{ secrets.GITHUB_TOKEN }}
60+
edit-mode: "replace"
61+
repository: ${{ github.repository }}
62+
issue-number: ${{ github.event.pull_request.number }}
63+
comment-id: ${{ steps.find_comment.outputs.comment-id }}
64+
body: |
65+
## OpenAPI Changes
66+
67+
<details>
68+
<summary>Show/hide ${{ steps.oasdif_summary.outputs.summary || 'No detectable change.' }}</summary>
69+
70+
```
71+
${{ steps.oasdif_changelog.outputs.changelog }}
72+
```
73+
74+
Unexpected changes? Ensure your branch is up-to-date with `main` (consider rebasing).
75+
</details>
76+
- name: Check for breaking changes
77+
id: oasdif_breaking
78+
run: |
79+
for spec in base/openapi/specs/*.yaml; do
80+
base_spec="$spec"
81+
head_spec="head/openapi/specs/$(basename "$spec")"
82+
if [ -f "$head_spec" ]; then
83+
echo "Checking $(basename "$spec") for breaking changes..."
84+
docker run --rm \
85+
--workdir ${{ github.workspace }} \
86+
--volume ${{ github.workspace }}:${{ github.workspace }}:ro \
87+
-e GITHUB_WORKSPACE=${{ github.workspace }} \
88+
tufin/oasdiff breaking \
89+
--fail-on ERR \
90+
--format githubactions \
91+
"$base_spec" \
92+
"$head_spec"
93+
fi
94+
done

0 commit comments

Comments
 (0)