|
| 1 | +name: Codex review (reusable) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + codex_review_token: |
| 7 | + description: >- |
| 8 | + Fine-grained token of the repository owner (Pull requests: read and write). |
| 9 | + Codex ignores review requests from bots, so the request is posted as the owner. |
| 10 | + required: true |
| 11 | + |
| 12 | +permissions: {} |
| 13 | + |
| 14 | +defaults: |
| 15 | + run: |
| 16 | + shell: bash |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: codex-review-${{ github.repository }}-${{ github.event.pull_request.number || github.ref }} |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + should-review: |
| 24 | + name: Decide whether to review |
| 25 | + runs-on: ubuntu-latest |
| 26 | + timeout-minutes: 1 |
| 27 | + permissions: {} |
| 28 | + # Same rule as claude-review: same-repository pull requests that a human or furedea-agent opened. |
| 29 | + # This job always succeeds so the caller job is never reported as skipped. |
| 30 | + outputs: |
| 31 | + run: >- |
| 32 | + ${{ github.event_name == 'pull_request' |
| 33 | + && github.event.pull_request.head.repo.full_name == github.repository |
| 34 | + && (github.event.pull_request.user.type != 'Bot' |
| 35 | + || github.event.pull_request.user.login == 'furedea-agent[bot]') }} |
| 36 | + steps: |
| 37 | + - name: No-op |
| 38 | + run: "true" |
| 39 | + |
| 40 | + review: |
| 41 | + name: Codex review |
| 42 | + needs: should-review |
| 43 | + if: needs.should-review.outputs.run == 'true' |
| 44 | + runs-on: ubuntu-latest |
| 45 | + timeout-minutes: 25 |
| 46 | + permissions: {} |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ secrets.codex_review_token }} |
| 49 | + REPO: ${{ github.repository }} |
| 50 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 51 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 52 | + steps: |
| 53 | + - name: Request a review |
| 54 | + id: request |
| 55 | + run: | |
| 56 | + echo "requested_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >>"$GITHUB_OUTPUT" |
| 57 | + gh pr comment "$PR_NUMBER" --repo "$REPO" --body "@codex review" |
| 58 | +
|
| 59 | + # Codex keeps one summary comment per pull request and records its state in |
| 60 | + # <!-- codex-review-state:v1 {...} -->. Wait until that comment is updated after this |
| 61 | + # request (so a re-run never reads an old verdict) and its state is for HEAD_SHA. |
| 62 | + # A failed API call is retried on the next poll instead of ending the wait. |
| 63 | + - name: Wait for the review of the head commit |
| 64 | + id: wait |
| 65 | + env: |
| 66 | + REQUESTED_AT: ${{ steps.request.outputs.requested_at }} |
| 67 | + run: | |
| 68 | + deadline=$((SECONDS + 20 * 60)) |
| 69 | + while ((SECONDS < deadline)); do |
| 70 | + state="$(gh api "repos/$REPO/issues/$PR_NUMBER/comments" --paginate \ |
| 71 | + --jq '.[] | select(.user.login == "chatgpt-codex-connector[bot]" and .updated_at >= env.REQUESTED_AT) | .body' \ |
| 72 | + | sed -n 's/.*<!-- codex-review-state:v1 \(.*\) -->.*/\1/p' | tail -n 1)" || state="" |
| 73 | + sha="$(jq -r '.code_review.commit_sha // empty' <<<"$state" 2>/dev/null || true)" |
| 74 | + status="$(jq -r '.code_review.status // empty' <<<"$state" 2>/dev/null || true)" |
| 75 | + if [[ "$sha" == "$HEAD_SHA" && ( "$status" == "completed" || "$status" == "failed" ) ]]; then |
| 76 | + echo "status=$status" >>"$GITHUB_OUTPUT" |
| 77 | + echo "unresolved=$(jq -r '.code_review.has_unresolved_findings' <<<"$state")" >>"$GITHUB_OUTPUT" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | + sleep 30 |
| 81 | + done |
| 82 | + echo "status=timeout" >>"$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + - name: Fail unless the review passed |
| 85 | + if: always() |
| 86 | + env: |
| 87 | + STATUS: ${{ steps.wait.outputs.status }} |
| 88 | + UNRESOLVED: ${{ steps.wait.outputs.unresolved }} |
| 89 | + run: | |
| 90 | + case "$STATUS:$UNRESOLVED" in |
| 91 | + completed:false) echo "Codex review of $HEAD_SHA: no unresolved findings." >>"$GITHUB_STEP_SUMMARY" ;; |
| 92 | + completed:*) message="Codex left unresolved findings on $HEAD_SHA. Fix them, or resolve a refuted one with evidence in its thread." ;; |
| 93 | + failed:*) message="Codex could not review $HEAD_SHA (for example, usage limits). Read its summary comment on the pull request." ;; |
| 94 | + *) message="Codex did not review $HEAD_SHA within 20 minutes. Check the Codex connector settings and that the token is valid (\`repo review-token rotate\`)." ;; |
| 95 | + esac |
| 96 | + if [[ -n "${message:-}" ]]; then |
| 97 | + echo "$message" >>"$GITHUB_STEP_SUMMARY" |
| 98 | + echo "::error::$message" |
| 99 | + exit 1 |
| 100 | + fi |
0 commit comments