diff --git a/.github/workflows/copilot-review-on-comment.yml b/.github/workflows/copilot-review-on-comment.yml index 3294a8e..3505cf4 100644 --- a/.github/workflows/copilot-review-on-comment.yml +++ b/.github/workflows/copilot-review-on-comment.yml @@ -24,7 +24,14 @@ on: jobs: assign-copilot: - if: github.event_name == 'pull_request_target' && github.event.action == 'opened' + # Skip bot-authored PRs (dependabot etc.) here -- tg-autopilot itself is + # a User, not a Bot, so this doesn't affect its own PRs. Draft status is + # checked live inside the step below instead of trusting this event's + # own payload. + if: | + github.event_name == 'pull_request_target' && + github.event.action == 'opened' && + github.event.pull_request.user.type != 'Bot' runs-on: ubuntu-latest steps: # Poll rather than check-once -- Copilot can take over a minute to @@ -37,9 +44,30 @@ jobs: run: | set -euo pipefail + # Draft status re-checked live via a plain API call, not the + # "opened" event's own payload, which can be stale. Retried briefly + # in case of a short read delay right after PR creation. + IS_DRAFT="false" + for attempt in 1 2 3; do + IS_DRAFT=$(curl -s -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/$REPO/pulls/$PR" | grep -oE '"draft":\s*(true|false)' | grep -oE '(true|false)') + [ "$IS_DRAFT" = "true" ] && break + sleep 4 + done + if [ "$IS_DRAFT" = "true" ]; then + echo "PR is a draft -- skipping Copilot review request." + exit 0 + fi + + # curl, not `gh api`, which can serve a cached GET right after our + # own mutation and falsely report "not present yet". copilot_present() { - gh api "repos/$REPO/pulls/$PR/requested_reviewers" --jq '.users[].login' | grep -qx 'copilot-pull-request-reviewer\[bot\]' && return 0 - gh api "repos/$REPO/pulls/$PR/reviews" --jq '.[].user.login' | grep -qx 'copilot-pull-request-reviewer\[bot\]' && return 0 + curl -s -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/$REPO/pulls/$PR/requested_reviewers" \ + | grep -qE '"login":\s*"copilot-pull-request-reviewer\[bot\]"' && return 0 + curl -s -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/$REPO/pulls/$PR/reviews" \ + | grep -qE '"login":\s*"copilot-pull-request-reviewer\[bot\]"' && return 0 return 1 } @@ -111,9 +139,15 @@ jobs: PR="${{ github.event.issue.number }}" REPO="${{ github.repository }}" + # curl, not `gh api`, which can serve a cached GET right after our + # own mutation and falsely report "not present yet". copilot_present() { - gh api "repos/$REPO/pulls/$PR/requested_reviewers" --jq '.users[].login' | grep -qx 'copilot-pull-request-reviewer\[bot\]' && return 0 - gh api "repos/$REPO/pulls/$PR/reviews" --jq '.[].user.login' | grep -qx 'copilot-pull-request-reviewer\[bot\]' && return 0 + curl -s -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/$REPO/pulls/$PR/requested_reviewers" \ + | grep -qE '"login":\s*"copilot-pull-request-reviewer\[bot\]"' && return 0 + curl -s -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/$REPO/pulls/$PR/reviews" \ + | grep -qE '"login":\s*"copilot-pull-request-reviewer\[bot\]"' && return 0 return 1 }