|
| 1 | +name: update-snapshots |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +defaults: |
| 8 | + run: |
| 9 | + # NOTE: A bit stricter than the default bash options used by GitHub Actions |
| 10 | + # (bash --noprofile --norc -e -o pipefail {0}) |
| 11 | + shell: bash --noprofile --norc -euo pipefail {0} |
| 12 | + |
| 13 | +# NOTE: Set concurrency for the current workflow to 1 |
| 14 | +concurrency: update-snapshots-${{ github.ref }}-${{ github.workflow }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + update-snapshots: |
| 18 | + if: | |
| 19 | + github.event.issue.pull_request != null && |
| 20 | + contains(github.event.comment.body, '/update-snapshots') |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + issues: write |
| 26 | + |
| 27 | + steps: |
| 28 | + |
| 29 | + - name: Notify PR about snapshot update |
| 30 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 31 | + with: |
| 32 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + script: | |
| 34 | + const { owner, repo } = context.repo; |
| 35 | + const prNumber = context.payload.issue.number; |
| 36 | + const runId = process.env.GITHUB_RUN_ID; |
| 37 | + const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${runId}`; |
| 38 | + const body = `Updating snapshots... [View workflow run](${runUrl})`; |
| 39 | +
|
| 40 | + await github.rest.issues.createComment({ |
| 41 | + issue_number: prNumber, |
| 42 | + owner: owner, |
| 43 | + repo: repo, |
| 44 | + body: body |
| 45 | + }); |
| 46 | +
|
| 47 | + - name: fetch pr branch name |
| 48 | + if: ${{ github.event.issue.pull_request }} |
| 49 | + id: pr |
| 50 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 51 | + with: |
| 52 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 53 | + script: | |
| 54 | + const prNumber = context.payload.issue.number; |
| 55 | + const { data: pr } = await github.rest.pulls.get({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + pull_number: prNumber |
| 59 | + }); |
| 60 | + core.setOutput('head_ref', pr.head.ref); |
| 61 | +
|
| 62 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 63 | + with: |
| 64 | + ref: ${{ steps.pr.outputs.head_ref }} |
| 65 | + fetch-depth: 0 |
| 66 | + |
| 67 | + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 68 | + with: |
| 69 | + node-version: 20 |
| 70 | + |
| 71 | + - name: Install dependencies |
| 72 | + run: npm ci |
| 73 | + |
| 74 | + - name: Update CDK and Jest snapshots |
| 75 | + run: make snapshots |
| 76 | + |
| 77 | + - name: Check for snapshot changes |
| 78 | + id: check_changes |
| 79 | + run: | |
| 80 | + if [[ -n "$(git status --porcelain)" ]]; then |
| 81 | + echo "changes_detected=true" >> $GITHUB_OUTPUT |
| 82 | + else |
| 83 | + echo "changes_detected=false" >> $GITHUB_OUTPUT |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Commit and push snapshot changes |
| 87 | + if: steps.check_changes.outputs.changes_detected == 'true' |
| 88 | + run: | |
| 89 | + git config user.name "github-actions[bot]" |
| 90 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 91 | +
|
| 92 | + # stage Jest snapshot changes |
| 93 | + git add src |
| 94 | +
|
| 95 | + # stage CDK snapshot changes |
| 96 | + git add __snapshots__ |
| 97 | +
|
| 98 | + git commit -m "chore: update snapshots triggered by comment [skip ci]" |
| 99 | +
|
| 100 | + # rebase onto the PR branch to avoid merge conflicts |
| 101 | + git fetch origin ${{ steps.pr.outputs.head_ref }} |
| 102 | + git rebase origin/${{ steps.pr.outputs.head_ref }} |
| 103 | +
|
| 104 | + # push updated snapshots |
| 105 | + git push origin HEAD:${{ steps.pr.outputs.head_ref }} |
0 commit comments