name: Auto-request Copilot review on: # 仅对主仓非草稿 PR 操作元数据;本流程不检出或执行 PR 代码。 # GitHub 公仓默认禁用 pull_request_target,改用 pull_request 并仅对同仓 PR 执行写操作。 pull_request: types: [opened, reopened, ready_for_review] permissions: pull-requests: write jobs: request: if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest timeout-minutes: 2 steps: - name: Request Copilot reviewer env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} run: | set -e if gh api "repos/${{ github.repository }}/pulls/$PR_NUMBER" \ --jq '.requested_reviewers[].login' \ | grep -qE '^(Copilot|copilot-pull-request-reviewer\[bot\])$'; then echo "Copilot already requested as reviewer." exit 0 fi # 422 也可能是功能未启用等错误,必须核对真实审核记录,不能直接报成功。 if ! resp=$(gh api -X POST \ "repos/${{ github.repository }}/pulls/$PR_NUMBER/requested_reviewers" \ -f 'reviewers[]=copilot-pull-request-reviewer[bot]' 2>&1); then echo "$resp" if echo "$resp" | grep -q '"status":"422"'; then if gh api "repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews" --paginate \ | jq --arg head "$PR_HEAD_SHA" -r '.[] | select(.commit_id == $head) | .user.login' | grep -qE '^(Copilot|copilot-pull-request-reviewer\[bot\])$'; then echo "Copilot review exists for the current head." exit 0 fi fi exit 1 fi