|
| 1 | +name: Pull Request Checks |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + pull-requests: read |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: pull-request-checks-${{ github.event.pull_request.number }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + check: |
| 17 | + name: Check description and visual evidence |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Check pull request details |
| 21 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 22 | + with: |
| 23 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + script: | |
| 25 | + const MAX_PR_BODY_LENGTH = 1000; |
| 26 | + const MARKER = '<!-- pull-request-details-check -->'; |
| 27 | +
|
| 28 | + const { owner, repo } = context.repo; |
| 29 | +
|
| 30 | + const issueNumber = context.payload.pull_request.number; |
| 31 | + const body = context.payload.pull_request.body || ''; |
| 32 | +
|
| 33 | + const problems = []; |
| 34 | +
|
| 35 | + if (Array.from(body).length > MAX_PR_BODY_LENGTH) { |
| 36 | + problems.push( |
| 37 | + `- The description is too long. Please keep it under ${MAX_PR_BODY_LENGTH} characters.` |
| 38 | + ); |
| 39 | + } |
| 40 | +
|
| 41 | + if ( |
| 42 | + !/(?:https?:\/\/[^\s)"']+\.(?:png|jpe?g|gif|webp|avif|mp4|mov|webm|m4v)(?:[?#][^\s)"']*)?|https?:\/\/github\.com\/user-attachments\/assets\/[^\s)"']+)/i.test( |
| 43 | + body |
| 44 | + ) |
| 45 | + ) { |
| 46 | + problems.push( |
| 47 | + '- Screenshot or video evidence is missing. Make sure to include one if it affects the UI.' |
| 48 | + ); |
| 49 | + } |
| 50 | +
|
| 51 | + const existingComment = ( |
| 52 | + await github.paginate(github.rest.issues.listComments, { |
| 53 | + owner, |
| 54 | + repo, |
| 55 | + issue_number: issueNumber, |
| 56 | + per_page: 100, |
| 57 | + }) |
| 58 | + ).find( |
| 59 | + (comment) => |
| 60 | + comment.user?.login === 'github-actions[bot]' && |
| 61 | + comment.body?.includes(MARKER) |
| 62 | + ); |
| 63 | +
|
| 64 | + if (problems.length === 0) { |
| 65 | + if (existingComment) { |
| 66 | + await github.rest.issues.deleteComment({ |
| 67 | + owner, |
| 68 | + repo, |
| 69 | + comment_id: existingComment.id, |
| 70 | + }); |
| 71 | + } |
| 72 | +
|
| 73 | + return; |
| 74 | + } |
| 75 | +
|
| 76 | + const commentBody = [ |
| 77 | + MARKER, |
| 78 | + 'Found potential problems with the pull request:', |
| 79 | + '', |
| 80 | + problems.join('\n'), |
| 81 | + ].join('\n'); |
| 82 | +
|
| 83 | + if (existingComment) { |
| 84 | + await github.rest.issues.updateComment({ |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + comment_id: existingComment.id, |
| 88 | + body: commentBody, |
| 89 | + }); |
| 90 | + } else { |
| 91 | + await github.rest.issues.createComment({ |
| 92 | + owner, |
| 93 | + repo, |
| 94 | + issue_number: issueNumber, |
| 95 | + body: commentBody, |
| 96 | + }); |
| 97 | + } |
0 commit comments