diff --git a/.github/workflows/copilot-review.yml b/.github/workflows/copilot-review.yml new file mode 100644 index 00000000..6ce43ede --- /dev/null +++ b/.github/workflows/copilot-review.yml @@ -0,0 +1,70 @@ +name: Copilot Code Review + +on: + pull_request: + types: [opened, synchronize, reopened] + pull_request_review_comment: + types: [created] + +permissions: + contents: read + pull-requests: write + +jobs: + copilot-review: + # Skip draft PRs; skip when Copilot itself triggers the event to prevent infinite loops + if: | + github.event.pull_request.draft == false && + !contains(fromJSON('["github-copilot[bot]","copilot-pull-request-reviewer[bot]"]'), github.actor) + runs-on: ubuntu-latest + steps: + - name: Dismiss previous Copilot review comments + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const pullNumber = context.payload.pull_request?.number ?? context.issue.number; + + const comments = await github.paginate( + github.rest.pulls.listReviewComments, + { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pullNumber, + } + ); + + const copilotBotLogins = new Set([ + 'github-copilot[bot]', + 'copilot-pull-request-reviewer[bot]', + ]); + + const copilotComments = comments.filter( + c => c.user.type === 'Bot' && copilotBotLogins.has(c.user.login) + ); + + for (const comment of copilotComments) { + await github.rest.pulls.deleteReviewComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: comment.id, + }); + } + + core.info(`Dismissed ${copilotComments.length} previous Copilot review comment(s).`); + + - name: Request Copilot review + # Only re-request a review on PR push/open events; Copilot re-reviews automatically + # when a user replies to one of its review comments. + if: github.event_name == 'pull_request' + continue-on-error: true + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + await github.rest.pulls.requestReviewers({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + reviewers: ['copilot'], + });