Skip to content

PR Visual Snapshot Update Bot #25473

PR Visual Snapshot Update Bot

PR Visual Snapshot Update Bot #25473

name: PR Visual Snapshot Update Bot
on:
workflow_run:
workflows: [PR Build]
permissions:
actions: read
contents: write
issues: write
pull-requests: write
statuses: write
jobs:
pr-visual-snapshot-update-bot:
if: ${{github.event.workflow_run.conclusion == 'success'}}
runs-on: ubuntu-latest
steps:
- uses: haya14busa/action-workflow_run-status@v1
- name: Get PR Event
id: get-pr-event
uses: actions/github-script@v7
with:
script: |
const prs = context.payload.workflow_run.pull_requests;
if (prs.length > 0) {
core.setOutput('pullRequestNumber', prs[0].number);
core.setOutput('sourceHeadBranch', prs[0].head.ref);
} else {
core.setFailed('No PR found');
}
- name: Checkout
uses: actions/checkout@v3
with:
repository: ${{ github.repository }}
ref: ${{ github.event.workflow_run.head_sha }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install Dependencies
run: npm ci --ignore-scripts
- name: Create Temp Directory for Diff Artifacts
run: mkdir -p ${{runner.temp}}/artifacts/
- name: Download Changes
uses: actions/download-artifact@v4
with:
pattern: visual-snapshot-changes-*.diff
path: ${{runner.temp}}/artifacts/
run-id: ${{github.event.workflow_run.id}}
github-token: ${{ github.token }}
- name: Apply Changes
id: apply-changes
run: |
# apply changes
git apply ${{runner.temp}}/artifacts/visual-snapshot-changes-*.diff/visual-snapshot-changes-*.diff
# We force the cleanup script to revert to the version in HEAD (the trusted base).
git checkout HEAD -- scripts/delete-unused-screenshots.ts
# delete unused screenshots
npm run ts-node -- ./scripts/delete-unused-screenshots.ts
- name: Check for Changes
id: diff
run: |
git add .
if git diff-index --quiet HEAD
then
echo "status=clean" >> $GITHUB_OUTPUT
else
echo "status=dirty" >> $GITHUB_OUTPUT
fi
- name: Push Changes
if: ${{steps.diff.outputs.status == 'dirty'}}
id: push-changes
env:
HEAD_BRANCH: ${{ steps.get-pr-event.outputs.sourceHeadBranch }}
PR_NUMBER: ${{ steps.get-pr-event.outputs.pullRequestNumber }}
run: |
repositoryUrl=${{github.server_url}}/${{github.repository}}.git
updateBranchName=visual-snapshot-update/pr-$PR_NUMBER
# configure git
git config user.name "GitHub"
git config user.email "noreply@github.com"
# commit changes
git commit --message "chore: update visual snapshots for \`$HEAD_BRANCH\`"
# push changes
git push $repositoryUrl HEAD:refs/heads/$updateBranchName --force
# delete branch (it's not needed, only the commit needs to be in the repo)
git push $repositoryUrl -d $updateBranchName
# set commit-sha for next step
echo "commit-sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Comment
if: ${{steps.diff.outputs.status == 'dirty'}}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{steps.get-pr-event.outputs.pullRequestNumber}}
body: |
This PR introduces visual changes: ${{steps.push-changes.outputs.commit-sha}}
If these changes are intended and correct, please cherry-pick the above commit to this PR.
```
git checkout ${{steps.get-pr-event.outputs.sourceHeadBranch}}
git fetch ${{github.server_url}}/${{github.repository}}.git ${{steps.push-changes.outputs.commit-sha}}
git cherry-pick ${{steps.push-changes.outputs.commit-sha}}
git push
```
- name: Fail if Visual Snapshots Changed
if: ${{steps.diff.outputs.status == 'dirty'}}
run: exit 1