PR Visual Snapshot Update Bot #14195
This file contains 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
name: PR Visual Snapshot Update Bot | |
on: | |
workflow_run: | |
workflows: [PR Build] | |
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: Checkout | |
uses: actions/checkout@v3 | |
with: | |
repo: ${{github.event.workflow_run.head_repository.full_name}} | |
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 | |
- name: Get PR Event | |
id: get-pr-event | |
uses: potiuk/get-workflow-origin@v1_5 | |
with: | |
token: ${{secrets.GITHUB_TOKEN}} | |
sourceRunId: ${{github.event.workflow_run.id}} | |
- name: Download Changes | |
uses: dawidd6/action-download-artifact@v2 | |
with: | |
name: visual-snapshot-changes-.+\.diff | |
name_is_regexp: true | |
path: . | |
workflow: ${{github.event.workflow_run.workflow_id}} | |
run_id: ${{github.event.workflow_run.id}} | |
if_no_artifact_found: ignore | |
- name: Apply Changes | |
id: apply-changes | |
run: | | |
# apply changes | |
git apply visual-snapshot-changes-*.diff/visual-snapshot-changes-*.diff | |
rm visual-snapshot-changes-*.diff/visual-snapshot-changes-*.diff | |
# 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 | |
run: | | |
repositoryUrl=${{github.server_url}}/${{github.repository}}.git | |
updateBranchName=visual-snapshot-update/pr-${{steps.get-pr-event.outputs.pullRequestNumber}} | |
# configure git | |
git config user.name "GitHub" | |
git config user.email "noreply@github.com" | |
# commit changes | |
git commit --message 'chore: update visual snapshots for `${{steps.get-pr-event.outputs.sourceHeadBranch}}`' | |
# 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 |