|
| 1 | +# This workflow ensures that PR authors have acknowledged and reviewed the code suggestions made by the PR agent |
| 2 | +# by checking if the author has marked a self-review checkbox in the comments and approves the PR if it is checked. |
| 3 | + |
| 4 | +name: Verify Author Self-Review |
| 5 | + |
| 6 | +on: |
| 7 | + # Since `issue_comment` cannot directly trigger a PR workflow, this must be done via the main branch workflow. |
| 8 | + issue_comment: |
| 9 | + types: [edited] |
| 10 | + |
| 11 | +jobs: |
| 12 | + verify-author-self-review: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Check for Author self-review in comments and Approve PR |
| 17 | + uses: actions/github-script@v7 |
| 18 | + with: |
| 19 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 20 | + script: | |
| 21 | + const issue_number = context.payload.issue.number; |
| 22 | +
|
| 23 | + if (!context.payload.issue.pull_request) { |
| 24 | + console.log('This comment is not on a pull request.'); |
| 25 | + return; |
| 26 | + } |
| 27 | +
|
| 28 | + console.log(`Pull Request Number: ${issue_number}`); |
| 29 | +
|
| 30 | + const { data: comments } = await github.rest.issues.listComments({ |
| 31 | + owner: context.repo.owner, |
| 32 | + repo: context.repo.repo, |
| 33 | + issue_number: issue_number |
| 34 | + }); |
| 35 | +
|
| 36 | + const foundChecked = comments.some(comment => |
| 37 | + comment.body.trim().includes('- [x] **Author self-review**:') |
| 38 | + ); |
| 39 | +
|
| 40 | + console.log(`foundChecked: ${foundChecked}`); |
| 41 | +
|
| 42 | + if (foundChecked) { |
| 43 | + // Check if the PR has already been approved by kojo-bot |
| 44 | + const { data: reviews } = await github.rest.pulls.listReviews({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + pull_number: issue_number |
| 48 | + }); |
| 49 | +
|
| 50 | + console.log('Reviewers:'); |
| 51 | + reviews.forEach(review => { |
| 52 | + console.log(`- ${review.user.login} (State: ${review.state})`); |
| 53 | + }); |
| 54 | +
|
| 55 | + const botApproval = reviews.some(review => |
| 56 | + review.user.login === 'github-actions[bot]' && review.state === 'APPROVED' |
| 57 | + ); |
| 58 | +
|
| 59 | + if (botApproval) { |
| 60 | + console.log('Pull request has already been approved by kojo-bot. Skipping approval.'); |
| 61 | + return; |
| 62 | + } |
| 63 | +
|
| 64 | + // Approve the pull request if the self-review is checked and not already approved |
| 65 | + await github.rest.pulls.createReview({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + pull_number: issue_number, |
| 69 | + event: 'APPROVE', |
| 70 | + }); |
| 71 | + console.log('Pull request approved.'); |
| 72 | + } |
0 commit comments