-
Notifications
You must be signed in to change notification settings - Fork 34
Format only PR-changed files in web edit workflow #898
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
ntotten
wants to merge
1
commit into
main
Choose a base branch
from
ntotten/format-pr-files
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
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,92 @@ | ||
| name: Format (Web Edits) | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| format: | ||
| # Only attempt to push if the PR branch is in the same repo (not a fork) | ||
| if: ${{ github.event.pull_request.head.repo.full_name == github.repository }} | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Detect if PR contains Web UI commits | ||
| id: webui | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const { owner, repo } = context.repo; | ||
| const pr = context.payload.pull_request.number; | ||
|
|
||
| // List commits on the PR | ||
| const commits = await github.paginate( | ||
| github.rest.pulls.listCommits, | ||
| { owner, repo, pull_number: pr, per_page: 100 } | ||
| ); | ||
|
|
||
| // GitHub web editor commits are typically committed by `web-flow` | ||
| const hasWebFlow = commits.some(c => c.committer?.login === "web-flow"); | ||
|
|
||
| core.setOutput("is_webui", hasWebFlow ? "true" : "false"); | ||
|
|
||
| - name: Checkout PR branch | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' }} | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.event.pull_request.head.ref }} | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Setup Node | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' }} | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 20 | ||
| cache: npm | ||
|
|
||
| - name: Install deps | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' }} | ||
| run: npm ci | ||
|
|
||
| - name: Get changed files | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' }} | ||
| id: changed | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const { owner, repo } = context.repo; | ||
| const pr = context.payload.pull_request.number; | ||
|
|
||
| const files = await github.paginate( | ||
| github.rest.pulls.listFiles, | ||
| { owner, repo, pull_number: pr, per_page: 100 } | ||
| ); | ||
|
|
||
| // Only include added or modified files (not removed) | ||
| const paths = files | ||
| .filter(f => f.status !== "removed") | ||
| .map(f => f.filename); | ||
|
|
||
| core.setOutput("files", paths.join("\n")); | ||
|
|
||
| - name: Format changed files | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' && steps.changed.outputs.files != '' }} | ||
| run: | | ||
| echo "${{ steps.changed.outputs.files }}" | xargs npx prettier --write --ignore-unknown | ||
|
|
||
| - name: Commit & push (only if changes) | ||
| if: ${{ steps.webui.outputs.is_webui == 'true' }} | ||
| run: | | ||
| if git diff --quiet; then | ||
| echo "No changes." | ||
| exit 0 | ||
| fi | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
| git add -A | ||
| git commit -m "chore: format files (web UI PR)" | ||
| git push | ||
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.
Check warning
Code scanning / CodeQL
Checkout of untrusted code in trusted context Medium
Copilot Autofix
AI 3 months ago
In general, the safest pattern is to ensure that workflows which check out and execute untrusted PR code do not perform privileged actions (such as pushing commits) or handle secrets. Instead, they should only build/test/format and upload results as artifacts. A separate privileged workflow, triggered via
workflow_run, can then review or apply changes as needed using trusted code from the base repository, not from the untrusted PR head.For this specific workflow, the simplest fix that preserves existing functionality as much as possible and removes the “checkout of untrusted code in privileged context” is to stop explicitly checking out the PR head ref and instead check out the trusted base branch (the repository’s default base for the PR). That way, the code being executed (
npm ci,npx prettier) comes from the trusted base branch, while we still format and commit changes to the PR branch. This addresses the security concern (no untrusted code execution) while maintaining the job’s purpose (auto-format web-UI PRs in-place).Concretely, we will change the
with.refargument in the “Checkout PR branch” step to usegithub.event.pull_request.base.refinstead ofgithub.event.pull_request.head.ref. No additional imports or actions are required; everything else in the workflow remains the same. The job will still run only for same-repo branches and keep the same permissions and subsequent steps (npm ci,npx prettier,git push).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just make it so this doesnt run without approval for non members? Also we should filter so we only do this on PRs that are brances of this repo, not forks