-
Notifications
You must be signed in to change notification settings - Fork 10
API Contract: Automate Schema Updates #892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
642dcc4
poc: test oasfiff API changelog
Prajna1999 bb5ebdb
Merge branch 'main' into chore/diffs-in-api-contract
Prajna1999 192650f
fix: in CI generate and compare diffs on the fly
Prajna1999 954c594
fix: in CI generate and compare diffs on the fly
Prajna1999 69e3590
Merge remote-tracking branch 'refs/remotes/origin/chore/diffs-in-api-…
Prajna1999 81e2cb4
check CI
Prajna1999 d61c828
fix: base cwd for .env.example
Prajna1999 9eb4ffe
fix: path for Diff breaking changes step
Prajna1999 e352cf7
test: remove job_id from LLMJobImmediatePublic
Prajna1999 0b092d7
chore: better siren and labeling to bypass
Prajna1999 b90bb97
fix: conditional failure of breaking change
Prajna1999 c40815b
chore: prettify md files
Prajna1999 8264a02
test
Prajna1999 e2fcd4d
garnishing sticky message in the PR comment
Prajna1999 f385144
chore: test with non_breaking changes
Prajna1999 5fc4c82
chore: revert all schema changes
Prajna1999 f6a4a0f
test: breaking changes
Prajna1999 8dab138
Merge branch 'main' into chore/diffs-in-api-contract
Prajna1999 9781016
cosmetic UI changes
Prajna1999 b0266d4
test with creds
Prajna1999 60a88c5
another test
Prajna1999 984c4cc
chore: non breaking gree
Prajna1999 19b3b28
Merge branch 'main' into chore/diffs-in-api-contract
Ayush8923 985e738
chore: remove test breaking changes
Prajna1999 257b1cc
Merge remote-tracking branch 'refs/remotes/origin/chore/diffs-in-api-…
Prajna1999 9be2496
Update .github/workflows/oasdiff.yaml
Prajna1999 9020324
Update .github/workflows/oasdiff.yaml
Prajna1999 7653cc8
Update .github/workflows/oasdiff.yaml
Prajna1999 51350dd
Update .github/workflows/oasdiff.yaml
Prajna1999 bceb063
Update .github/workflows/oasdiff.yaml
Prajna1999 6dea5a7
fix: comments
Prajna1999 d1bfa4b
fix: oasdiff undefined
Prajna1999 e321c02
fix: workflow not triggering
Prajna1999 eb833cb
fix: logic fix at generate comment
Prajna1999 7c99e96
Merge branch 'main' into chore/diffs-in-api-contract
Ayush8923 2e87b88
chore: remove test fields
Prajna1999 c31cfe1
Merge remote-tracking branch 'refs/remotes/origin/chore/diffs-in-api-…
Prajna1999 6c49b2b
Update backend/scripts/kaapi_export_openapi.py
Ayush8923 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| # Generate an API changelog by diffing the OpenAPI schema between base and HEAD. | ||
| name: API Breaking Changes Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: ["main"] | ||
| # Re-run when the approval label is added/removed so merge gating updates immediately. | ||
| types: [opened, synchronize, reopened, labeled, unlabeled] | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
Prajna1999 marked this conversation as resolved.
|
||
|
|
||
| # One run per PR. A new commit (or label toggle) cancels the in-flight run instead | ||
| concurrency: | ||
| group: api-changes-pr-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| breaking-changes: | ||
| runs-on: ubuntu-latest | ||
|
Ayush8923 marked this conversation as resolved.
|
||
| timeout-minutes: 15 | ||
| steps: | ||
| - name: Checkout PR head | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| path: head | ||
| fetch-depth: 0 | ||
| persist-credentials: false | ||
|
|
||
| - name: Checkout base ref | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.base_ref }} | ||
| path: base | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| persist-credentials: false | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.12" | ||
|
|
||
| - name: Install uv | ||
| uses: astral-sh/setup-uv@v3 | ||
|
Prajna1999 marked this conversation as resolved.
|
||
|
|
||
| - name: Prepare env files | ||
| run: | | ||
| cp head/.env.test.example head/.env.test | ||
| cp head/.env.test.example base/.env.test | ||
|
|
||
| - name: Export base OpenAPI schema | ||
| working-directory: base/backend | ||
| env: | ||
| ENVIRONMENT: testing | ||
| run: | | ||
| uv sync --frozen | ||
| if [ -f scripts/kaapi_export_openapi.py ]; then | ||
| uv run python scripts/kaapi_export_openapi.py "$GITHUB_WORKSPACE/base-openapi.yaml" | ||
| else | ||
| # Base ref predates this workflow; copy the HEAD script in to bootstrap. | ||
| cp "$GITHUB_WORKSPACE/head/backend/scripts/kaapi_export_openapi.py" scripts/ | ||
| uv run python scripts/kaapi_export_openapi.py "$GITHUB_WORKSPACE/base-openapi.yaml" | ||
| fi | ||
|
|
||
| - name: Export HEAD OpenAPI schema | ||
| working-directory: head/backend | ||
| env: | ||
| ENVIRONMENT: testing | ||
| run: | | ||
| uv sync --frozen | ||
| uv run python scripts/kaapi_export_openapi.py "$GITHUB_WORKSPACE/head-openapi.yaml" | ||
|
|
||
| - name: Generate diff (JSON) | ||
| env: | ||
| OASDIFF_IMAGE: tufin/oasdiff:latest | ||
| run: | | ||
| set -euo pipefail | ||
|
|
||
| run_diff() { | ||
| local cmd="$1" out="$2" | ||
| docker run --rm -v "$PWD:/specs" "$OASDIFF_IMAGE" \ | ||
| "$cmd" /specs/base-openapi.yaml /specs/head-openapi.yaml \ | ||
| --format json > "$out" || true | ||
| if ! jq -e . "$out" >/dev/null 2>&1; then | ||
| echo "[]" > "$out" | ||
| fi | ||
| } | ||
| run_diff changelog changelog.json | ||
| run_diff breaking breaking.json | ||
|
|
||
| echo "breaking=$(jq 'length' breaking.json) changelog=$(jq 'length' changelog.json)" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| - name: Debug raw oasdiff output | ||
| run: | | ||
| echo "=== breaking.json ===" | ||
| cat breaking.json || echo "(missing)" | ||
| echo | ||
| echo "=== changelog.json ===" | ||
| cat changelog.json || echo "(missing)" | ||
|
|
||
| - name: Build PR comment body | ||
| if: always() | ||
| run: | | ||
| plural() { if [ "$1" -ne 1 ]; then echo s; fi; } | ||
|
|
||
| # Render an oasdiff JSON array as a markdown table. | ||
| # Severity dot: red only for breaking (ERR), green for everything else | ||
| # (WARN / INFO — non-breaking additions like optional params). | ||
| to_table() { | ||
| jq -r ' | ||
| def dot(lvl): | ||
| if lvl >= 2 then "🔴" | ||
| else "🟢" end; | ||
| if (. // []) | length == 0 then "_No entries._" | ||
| else | ||
| "| | Method | Path | Change |", | ||
| "|:-:|:-:|:--|:--|", | ||
| (.[] | "| \(dot(.level)) | `\(.operation // "—")` | `\(.path // "—")` | \((.text // "") | gsub("[\r\n]+"; " ")) |") | ||
| end | ||
| ' "$1" | ||
| } | ||
|
|
||
| BREAKING=$(jq 'length' breaking.json) | ||
| CHANGES=$(jq 'length' changelog.json) | ||
|
|
||
| if [ "$BREAKING" -gt 0 ]; then | ||
| STATUS="🔴 **${BREAKING} breaking change$(plural $BREAKING)**" | ||
| ALERT='> [!CAUTION]' | ||
| HEADLINE="Downstream consumers may need an update before merging." | ||
| BREAKING_OPEN=" open" | ||
| elif [ "$CHANGES" -gt 0 ]; then | ||
| STATUS="🟢 **${CHANGES} non-breaking change$(plural $CHANGES)**" | ||
| ALERT='> [!TIP]' | ||
| HEADLINE="Safe to merge from an API-contract perspective." | ||
| BREAKING_OPEN="" | ||
| else | ||
| STATUS="⚪ **No API surface changes**" | ||
| ALERT='> [!NOTE]' | ||
| HEADLINE="This PR does not modify the API contract." | ||
| BREAKING_OPEN="" | ||
| fi | ||
|
|
||
| { | ||
| echo "## OpenAPI changes ${STATUS}" | ||
| echo | ||
| echo "${ALERT}" | ||
| echo "> ${HEADLINE}" | ||
| echo | ||
|
|
||
| if [ "$BREAKING" -gt 0 ]; then | ||
| echo "<details${BREAKING_OPEN}>" | ||
| echo "<summary><b>Breaking changes</b> · <code>${BREAKING}</code></summary>" | ||
| echo | ||
| to_table breaking.json | ||
| echo | ||
| echo "</details>" | ||
| echo | ||
| fi | ||
|
|
||
| if [ "$CHANGES" -gt 0 ]; then | ||
| echo "<details>" | ||
| echo "<summary><b>Full changelog</b> · <code>${CHANGES}</code></summary>" | ||
| echo | ||
| to_table changelog.json | ||
| echo | ||
| echo "</details>" | ||
| echo | ||
| fi | ||
|
|
||
| echo "<sub><code>${GITHUB_BASE_REF}</code> ↔ <code>${GITHUB_SHA::8}</code> · generated by <a href=\"https://github.com/oasdiff/oasdiff\">oasdiff</a></sub>" | ||
| } > comment.md | ||
| cat comment.md >> "$GITHUB_STEP_SUMMARY" | ||
|
|
||
| - name: Post sticky PR comment | ||
| uses: marocchino/sticky-pull-request-comment@v3 | ||
| with: | ||
| header: oasdiff | ||
| path: comment.md | ||
|
|
||
| - name: Enforce breaking-change gate | ||
| # Skip the hard fail when a reviewer has explicitly acknowledged the breaking change | ||
| # by applying the `breaking-change-approved` label to the PR. | ||
| if: ${{ !contains(github.event.pull_request.labels.*.name, 'breaking-change-approved') }} | ||
| run: | | ||
| set -euo pipefail | ||
| count=$(jq 'length' breaking.json) | ||
| if [ "$count" -gt 0 ]; then | ||
| echo "::error::${count} breaking change(s) detected. Apply the 'breaking-change-approved' label to override the gate." | ||
| exit 1 | ||
| fi | ||
| echo "No breaking changes detected." | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Export the FastAPI app's OpenAPI schema to a YAML file. | ||
|
|
||
| Usage: | ||
| uv run python -m scripts.kaapi_export_openapi [output_path] | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import yaml | ||
|
|
||
| from app.main import app | ||
|
|
||
|
|
||
| def main() -> None: | ||
| output = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("openapi.yaml") | ||
| schema = app.openapi() | ||
| output.write_text(yaml.safe_dump(schema, sort_keys=False)) | ||
| print(f"[export_openapi] Wrote {output} ({output.stat().st_size} bytes)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.