|
| 1 | +name: Visual changes bot |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [main] |
| 5 | + pull_request: |
| 6 | + branches: [main] |
| 7 | +jobs: |
| 8 | + update-screenshots: |
| 9 | + name: Update screenshots |
| 10 | + runs-on: ubuntu-latest-16-cores |
| 11 | + permissions: |
| 12 | + id-token: 'write' |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Get branch of PR |
| 17 | + uses: xt0rted/pull-request-comment-branch@v3 |
| 18 | + id: comment-branch |
| 19 | + |
| 20 | + - name: Checkout PR branch |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + ref: ${{ steps.comment-branch.outputs.head_ref }} |
| 24 | + - uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: lts/* |
| 27 | + registry-url: 'https://npm.pkg.github.com' |
| 28 | + |
| 29 | + - name: Install dependencies |
| 30 | + run: npm ci |
| 31 | + env: |
| 32 | + NODE_AUTH_TOKEN: ${{ secrets.READER_TOKEN }} |
| 33 | + - run: npm run build:storybook |
| 34 | + |
| 35 | + - name: Run Playwright update screenshots |
| 36 | + run: docker run --rm -v $(pwd):/workspace -w /workspace --ipc=host mcr.microsoft.com/playwright:v1.51.0-noble /bin/bash -c "CI=true npx playwright test --update-snapshots" |
| 37 | + |
| 38 | + - name: Commit and push updated screenshots |
| 39 | + if: always() |
| 40 | + id: auto-commit-action |
| 41 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 42 | + with: |
| 43 | + commit_message: 'Update screenshots' |
| 44 | + |
| 45 | + - name: Handle PR Review |
| 46 | + uses: actions/github-script@v7 |
| 47 | + with: |
| 48 | + script: | |
| 49 | + try { |
| 50 | + const { repo, owner } = context.repo; |
| 51 | + |
| 52 | + // Get PR number from event context |
| 53 | + let prNumber; |
| 54 | + if (context.payload.pull_request) { |
| 55 | + prNumber = context.payload.pull_request.number; |
| 56 | + } else { |
| 57 | + console.log('No PR number found in context'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + console.log('PR number:', prNumber); |
| 62 | + |
| 63 | + // Add a small delay to ensure files are available |
| 64 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 65 | + |
| 66 | + const changedFiles = await github.rest.pulls.listFiles({ |
| 67 | + owner, |
| 68 | + repo, |
| 69 | + pull_number: prNumber |
| 70 | + }); |
| 71 | + |
| 72 | + console.log('Total files in PR:', changedFiles.data.length); |
| 73 | + console.log('All files in PR:', changedFiles.data.map(f => `${f.filename} (${f.status})`)); |
| 74 | + |
| 75 | + // Separate files by status |
| 76 | + const modifiedOrAddedPngs = changedFiles.data.filter(file => |
| 77 | + file.filename.endsWith('.png') && |
| 78 | + (file.status === 'modified' || file.status === 'added') |
| 79 | + ); |
| 80 | + |
| 81 | + const removedPngs = changedFiles.data.filter(file => |
| 82 | + file.filename.endsWith('.png') && |
| 83 | + file.status === 'removed' |
| 84 | + ); |
| 85 | + |
| 86 | + console.log('Modified/Added PNG files:', modifiedOrAddedPngs.map(f => `${f.filename} (${f.status})`)); |
| 87 | + console.log('Removed PNG files:', removedPngs.map(f => `${f.filename} (${f.status})`)); |
| 88 | + |
| 89 | + if (changedFiles.data.length === 0) { |
| 90 | + console.log('Warning: No files detected in PR. This might be a timing issue.'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // Only consider files as "changed" if they are modified or added |
| 95 | + const hasScreenshotChanges = modifiedOrAddedPngs.length > 0; |
| 96 | + console.log('Has active screenshot changes:', hasScreenshotChanges); |
| 97 | + |
| 98 | + const reviews = await github.rest.pulls.listReviews({ |
| 99 | + owner, |
| 100 | + repo, |
| 101 | + pull_number: prNumber |
| 102 | + }); |
| 103 | + |
| 104 | + const REVIEW_MARKER = '<!-- playwright-screenshot-bot -->'; |
| 105 | + |
| 106 | + const botReview = reviews.data.find(review => |
| 107 | + review.user.login === 'github-actions[bot]' && |
| 108 | + review.state === 'CHANGES_REQUESTED' && |
| 109 | + review.body.includes(REVIEW_MARKER) |
| 110 | + ); |
| 111 | + |
| 112 | + console.log('Found screenshot bot review:', botReview ? 'yes' : 'no'); |
| 113 | + |
| 114 | + if (hasScreenshotChanges && !botReview) { |
| 115 | + console.log('Creating new review'); |
| 116 | + const filesUrl = `https://github.com/${owner}/${repo}/pull/${prNumber}/files?file-filters[]=.png&file-filters[]=^playwright/`; |
| 117 | + await github.rest.pulls.createReview({ |
| 118 | + owner, |
| 119 | + repo, |
| 120 | + pull_number: prNumber, |
| 121 | + body: `${REVIEW_MARKER}\n## 👀 Visual changes detected\n\nPlease [review the screenshot changes](${filesUrl}) before merging.`, |
| 122 | + event: 'REQUEST_CHANGES' |
| 123 | + }); |
| 124 | + } else if (!hasScreenshotChanges && botReview) { |
| 125 | + console.log('Dismissing review because no PNG changes found'); |
| 126 | + await github.rest.pulls.dismissReview({ |
| 127 | + owner, |
| 128 | + repo, |
| 129 | + pull_number: prNumber, |
| 130 | + review_id: botReview.id, |
| 131 | + message: `${REVIEW_MARKER}\nVisual changes have been reverted` |
| 132 | + }); |
| 133 | + } else { |
| 134 | + console.log('No action needed:', |
| 135 | + hasScreenshotChanges ? 'Has changes' : 'No changes', |
| 136 | + botReview ? 'Has review' : 'No review' |
| 137 | + ); |
| 138 | + } |
| 139 | + } catch (error) { |
| 140 | + console.log('Error:', error); |
| 141 | + console.log('Error details:', error.message); |
| 142 | + core.setFailed('Failed to handle review'); |
| 143 | + } |
0 commit comments