-
Notifications
You must be signed in to change notification settings - Fork 14
ci: auto-fix schema on /fix-schema comment #481
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
Open
margaretjgu
wants to merge
5
commits into
main
Choose a base branch
from
ci/schema-auto-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f9bda77
ci: auto-fix schema on /fix-schema comment
margaretjgu c043a2f
Merge branch 'main' into ci/schema-auto-fix
margaretjgu 040c6a8
ci: restrict /fix-schema to maintainers and same-repo branches
margaretjgu 7c57098
ci: pin checkout to validated SHA to prevent TOCTOU
margaretjgu 3ba5dc5
Merge branch 'main' into ci/schema-auto-fix
margaretjgu 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
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,73 @@ | ||
| name: Auto-fix schema | ||
|
|
||
| on: | ||
| issue_comment: | ||
| types: [created] | ||
|
|
||
| jobs: | ||
| fix: | ||
| name: Regenerate and commit schema | ||
| # Only run for maintainer comments: this job checks out and builds the PR | ||
| # branch with a write token, so it must never be triggerable by outsiders. | ||
| if: | | ||
| github.event.issue.pull_request != null && | ||
| contains(github.event.comment.body, '/fix-schema') && | ||
| contains(fromJSON('["MEMBER", "OWNER", "COLLABORATOR"]'), github.event.comment.author_association) | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
| env: | ||
| NODE_OPTIONS: --max-old-space-size=6144 | ||
| steps: | ||
| - name: Get PR branch | ||
| id: pr | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| PR_NUMBER: ${{ github.event.issue.number }} | ||
| run: | | ||
| PR_JSON=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json headRefName,headRefOid,isCrossRepository) | ||
| if [ "$(echo "$PR_JSON" | jq -r .isCrossRepository)" = "true" ]; then | ||
| echo "Refusing to run against a fork branch." >&2 | ||
| exit 1 | ||
| fi | ||
| echo "branch=$(echo "$PR_JSON" | jq -r .headRefName)" >> "$GITHUB_OUTPUT" | ||
| echo "sha=$(echo "$PR_JSON" | jq -r .headRefOid)" >> "$GITHUB_OUTPUT" | ||
|
|
||
| # Check out the exact SHA validated above (not the branch name) so the | ||
| # ref cannot be swapped between validation and execution (TOCTOU). | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7 | ||
| with: | ||
| ref: ${{ steps.pr.outputs.sha }} | ||
|
|
||
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6 | ||
| with: | ||
| node-version: 24.x | ||
|
|
||
| - name: Install | ||
| run: npm ci | ||
|
|
||
| - name: Build | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| run: npm run build | ||
|
|
||
| - name: Regenerate CLI schema | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| run: npm run build:schema | ||
|
|
||
| - name: Commit and push | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| PR_NUMBER: ${{ github.event.issue.number }} | ||
| BRANCH: ${{ steps.pr.outputs.branch }} | ||
| SHA: ${{ steps.pr.outputs.sha }} | ||
| run: | | ||
| if git diff --quiet docs/cli/schema.json; then | ||
| gh pr comment "$PR_NUMBER" --body "Schema is already up to date - nothing to commit." | ||
| else | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
| git add docs/cli/schema.json | ||
| git commit -m "chore: regenerate CLI schema" | ||
| # Fail instead of clobbering if the branch moved since validation. | ||
| git push origin "HEAD:refs/heads/$BRANCH" --force-with-lease="refs/heads/$BRANCH:$SHA" | ||
| gh pr comment "$PR_NUMBER" --body "Schema regenerated and committed." | ||
| fi | ||
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.