Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 41 additions & 137 deletions .github/workflows/_ai-pr-review-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ on:
required: false
type: 'string'
default: ''
pr_number_review:
description: 'PR number from pull_request_review/review_comment event'
required: false
type: 'string'
default: ''
pr_number_dispatch:
description: 'PR number from workflow_dispatch event'
required: false
Expand All @@ -44,16 +39,6 @@ on:
required: false
type: 'string'
default: ''
review_body:
description: 'Review body (for pull_request_review events)'
required: false
type: 'string'
default: ''
review_comment_body:
description: 'Review comment body (for pull_request_review_comment events)'
required: false
type: 'string'
default: ''
app_id:
description: 'GitHub App ID — used to conditionally generate an app token'
required: false
Expand All @@ -77,34 +62,21 @@ jobs:
runs-on: 'ubuntu-latest'

steps:
- name: 'Checkout PR code (pull_request)'
if: |-
${{ inputs.event_name == 'pull_request' }}
# SECURITY: checks out the BASE repo, never `refs/pull/N/head`, and does
# so unconditionally. Under `pull_request_target` this job holds full
# secrets and a write-scoped token, so checking out the pull request's
# code would hand its author both. Nothing here needs that code — the
# diff and metadata arrive through `gh`, and the only thing read off
# disk is `.github/scripts/post_review_comments.py`.
#
# Unconditional because the four per-event variants this replaced all
# keyed on event names that `pull_request_target` is not one of, so none
# would have matched: no checkout, no repo for `gh` to infer, no script.
- name: 'Checkout the base repo (never the PR head)'
uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1' # ratchet:actions/checkout@v4
with:
fetch-depth: 1 # PR diff and metadata come from the gh API; git history is not needed.

- name: 'Checkout PR code (workflow_dispatch)'
if: |-
${{ inputs.event_name == 'workflow_dispatch' }}
uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1' # ratchet:actions/checkout@v4
with:
fetch-depth: 1

- name: 'Checkout PR code (issue_comment)'
if: |-
${{ inputs.event_name == 'issue_comment' }}
uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1' # ratchet:actions/checkout@v4
with:
fetch-depth: 1

- name: 'Checkout PR code (pull_request_review & pull_request_review_comment)'
if: |-
${{ inputs.event_name == 'pull_request_review' || inputs.event_name == 'pull_request_review_comment' }}
uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1' # ratchet:actions/checkout@v4
with:
fetch-depth: 1

# SECURITY: strip the workspace config agy reads, so the only
# configuration it sees is the global config written to $HOME below —
# outside the workspace, and so not PR-author-writable. `.agents/` holds
Expand Down Expand Up @@ -134,10 +106,10 @@ jobs:
permission-issues: 'write'
permission-pull-requests: 'write'

- name: 'Get PR details (pull_request & workflow_dispatch)'
- name: 'Get PR details (pull_request_target & workflow_dispatch)'
id: 'get_pr'
if: |-
${{ inputs.event_name == 'pull_request' || inputs.event_name == 'workflow_dispatch' }}
${{ inputs.event_name == 'pull_request_target' || inputs.event_name == 'workflow_dispatch' }}
env:
GH_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
EVENT_NAME: '${{ inputs.event_name }}'
Expand Down Expand Up @@ -193,7 +165,7 @@ jobs:
echo "pr_number=${PR_NUMBER}" >> "${GITHUB_OUTPUT}"

ADDITIONAL_INSTRUCTIONS="$(
echo "${COMMENT_BODY}" | sed 's/.*@gemini-cli \/review//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
echo "${COMMENT_BODY}" | sed 's/.*@ai-review//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
)"
# Use a UUID-derived delimiter so a multi-line comment body, PR title,
# or file path containing a bare "EOF" on its own line cannot escape
Expand All @@ -215,58 +187,13 @@ jobs:
done
{ echo "changed_files<<${DELIM}"; echo "${CHANGED_FILES}"; echo "${DELIM}"; } >> "${GITHUB_OUTPUT}"

- name: 'Get PR details (pull_request_review & pull_request_review_comment)'
id: 'get_pr_review'
if: |-
${{ inputs.event_name == 'pull_request_review' || inputs.event_name == 'pull_request_review_comment' }}
# One reason a PR can be unreviewable, and it is a hard API limit rather
# than a policy. Seven later steps hang off this step's `skip` output.
- name: 'Check whether the PR diff is fetchable'
id: 'diff_size_guard'
env:
GH_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
EVENT_NAME: '${{ inputs.event_name }}'
REVIEW_BODY: '${{ inputs.review_body }}'
COMMENT_BODY: '${{ inputs.review_comment_body }}'
PR_NUMBER: '${{ inputs.pr_number_review }}'
run: |-
set -euo pipefail

echo "pr_number=${PR_NUMBER}" >> "${GITHUB_OUTPUT}"

if [[ "${EVENT_NAME}" == "pull_request_review" ]]; then
TRIGGER_BODY="${REVIEW_BODY}"
else
TRIGGER_BODY="${COMMENT_BODY}"
fi

ADDITIONAL_INSTRUCTIONS="$(
echo "${TRIGGER_BODY}" | sed 's/.*@gemini-cli \/review//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
)"
# Same UUID-delimited heredoc as the issue_comment path above —
# applies to all multi-line outputs, not just additional_instructions.
DELIM="EOF_$(uuidgen)"
{ echo "additional_instructions<<${DELIM}"; echo "${ADDITIONAL_INSTRUCTIONS}"; echo "${DELIM}"; } >> "${GITHUB_OUTPUT}"

for attempt in 1 2 3; do
PR_DATA="$(gh pr view "${PR_NUMBER}" --json title,additions,deletions,changedFiles,baseRefName,headRefName)" && break
echo "::warning::gh pr view failed (attempt ${attempt}/3); retrying in 5s..."
sleep 5
done
{ echo "pr_data<<${DELIM}"; echo "${PR_DATA}"; echo "${DELIM}"; } >> "${GITHUB_OUTPUT}"

for attempt in 1 2 3; do
CHANGED_FILES="$(gh pr diff "${PR_NUMBER}" --name-only)" && break
echo "::warning::gh pr diff failed (attempt ${attempt}/3); retrying in 5s..."
sleep 5
done
{ echo "changed_files<<${DELIM}"; echo "${CHANGED_FILES}"; echo "${DELIM}"; } >> "${GITHUB_OUTPUT}"

# Both reasons a PR can be unreviewable are decided here, because seven
# later steps hang off this one step's `skip` output. The id stays
# `new_files_only_guard` so none of them have to move.
- name: 'Check whether this PR can be reviewed'
id: 'new_files_only_guard'
env:
GH_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number || steps.get_pr_review.outputs.pr_number }}'
EVENT_NAME: '${{ inputs.event_name }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number }}'
run: |-
set -euo pipefail

Expand All @@ -276,29 +203,17 @@ jobs:
fi

# IMPORTANT: `gh api --paginate --jq EXPR` applies EXPR to EACH page's
# response separately and concatenates the results. The `/pulls/N/files`
# endpoint paginates at 30 files per page, so a previous form
# --jq '[.[] | select(.status != "added")] | length'
# emitted one integer per page for any PR with more than 30 changed
# files, and the multi-line capture broke the `-eq` comparison below
# under `set -euo pipefail` — the guard would crash on every large PR.
# Emit one line per file and count the lines instead.
gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" \
--paginate --jq '.[] | .status' > pr_file_statuses.txt
# response separately and concatenates the results, so an aggregating
# filter like `| length` emits one integer PER PAGE and breaks any
# numeric comparison on the result. Emit one line per file and count
# the lines instead.
#
# `tr -d` because `wc -l < file` right-pads the count on BSD wc, and
# TOTAL_COUNT is interpolated into the PR comment below. Arithmetic
# would tolerate the padding; the reader would not.
TOTAL_COUNT="$(wc -l < pr_file_statuses.txt | tr -d '[:space:]')"
# awk, not `grep -cv ... || true`. grep exits 1 on no match, which
# `set -e` reads as failure and an all-added PR triggers, so it needs
# `|| true` — but that also swallows grep's exit >1 on a REAL error,
# leaving the count empty, and `[[ "" -eq 0 ]]` is true in bash. The
# guard would then skip the review as "only adds new files" for a PR
# it never managed to read. awk exits 0 with no matches and non-zero
# only on a real error, which pipefail turns into a loud failure.
NON_ADDED_COUNT="$(awk '$0 != "added"' pr_file_statuses.txt | wc -l | tr -d '[:space:]')"

# A HARD API LIMIT, not a policy, so it applies on every event.
TOTAL_COUNT="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" \
--paginate --jq '.[] | .filename' | wc -l | tr -d '[:space:]')"

# `gh pr diff` reads GET /repos/{owner}/{repo}/pulls/{n} with the diff
# media type, which answers HTTP 406 "PullRequest.diff too_large" above
# 300 files. 406 is deterministic, so the fetch step's three retries
Expand All @@ -314,17 +229,6 @@ jobs:
exit 0
fi

# A POLICY skip, so only on the automatic trigger — a maintainer
# invoking a review by hand has already decided it is worth one.
# Every file 'added' means a new recipe drop, and human admin
# approval is the real quality gate for those.
if [[ "${EVENT_NAME}" == 'pull_request' && "${NON_ADDED_COUNT}" -eq 0 ]]; then
echo "skip=true" >> "${GITHUB_OUTPUT}"
gh pr comment "${PR_NUMBER}" \
--body "This PR only adds new files — automated AI review was skipped. New recipe submissions are reviewed by a maintainer during the approval process."
exit 0
fi

echo "skip=false" >> "${GITHUB_OUTPUT}"

# Fetch the diff HERE rather than letting the reviewer shell out for it.
Expand All @@ -342,10 +246,10 @@ jobs:
# step output round-trips through the environment, where Linux caps a
# single variable at the same 131072 bytes that caps an argv string.
- name: 'Fetch the PR diff for the reviewer'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
env:
GH_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number || steps.get_pr_review.outputs.pr_number }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number }}'
run: |-
set -euo pipefail

Expand Down Expand Up @@ -382,7 +286,7 @@ jobs:
# this step provides by exporting GOOGLE_APPLICATION_CREDENTIALS.
- name: 'Authenticate to Google Cloud (Workload Identity Federation)'
id: 'auth'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
uses: 'google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093' # ratchet:google-github-actions/auth@v3.0.0
with:
project_id: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
Expand All @@ -398,7 +302,7 @@ jobs:
# validated against that catalog the run dies with the badly misleading
# "model <name> is not recognized as a known model".
- name: 'Add the quota project to the ADC credential'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
env:
GOOGLE_CLOUD_PROJECT: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
run: |-
Expand All @@ -411,7 +315,7 @@ jobs:
# The installer always fetches the latest build — it has no version flag,
# so the old `gemini_cli_version` pin has no equivalent here.
- name: 'Install Antigravity CLI (agy)'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
run: |-
set -euo pipefail
curl -fsSL https://antigravity.google/cli/install.sh | bash
Expand All @@ -434,7 +338,7 @@ jobs:
# a review that produced nothing still looks like a pass. Leaving room
# for routine improvisation is cheaper than a silently voided review.
- name: 'Configure agy (tool permissions)'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
run: |-
set -euo pipefail

Expand All @@ -459,14 +363,14 @@ jobs:
SETTINGS_EOF

- name: 'Run AI PR Review'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
id: 'agy_pr_review'
env:
AGY_ADC_AUTH: 'true'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number || steps.get_pr_review.outputs.pr_number }}'
PR_DATA: '${{ steps.get_pr.outputs.pr_data || steps.get_pr_comment.outputs.pr_data || steps.get_pr_review.outputs.pr_data }}'
CHANGED_FILES: '${{ steps.get_pr.outputs.changed_files || steps.get_pr_comment.outputs.changed_files || steps.get_pr_review.outputs.changed_files }}'
ADDITIONAL_INSTRUCTIONS: '${{ steps.get_pr.outputs.additional_instructions || steps.get_pr_comment.outputs.additional_instructions || steps.get_pr_review.outputs.additional_instructions }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number }}'
PR_DATA: '${{ steps.get_pr.outputs.pr_data || steps.get_pr_comment.outputs.pr_data }}'
CHANGED_FILES: '${{ steps.get_pr.outputs.changed_files || steps.get_pr_comment.outputs.changed_files }}'
ADDITIONAL_INSTRUCTIONS: '${{ steps.get_pr.outputs.additional_instructions || steps.get_pr_comment.outputs.additional_instructions }}'
GOOGLE_CLOUD_PROJECT: '${{ vars.GOOGLE_CLOUD_PROJECT }}'
GOOGLE_CLOUD_LOCATION: '${{ vars.GOOGLE_CLOUD_LOCATION }}'
REPOSITORY: '${{ github.repository }}'
Expand Down Expand Up @@ -629,11 +533,11 @@ jobs:
# right here, so a bad position can be dropped deterministically instead
# of taking every other comment down with it.
- name: 'Post the review'
if: steps.new_files_only_guard.outputs.skip != 'true'
if: steps.diff_size_guard.outputs.skip != 'true'
id: 'post_review'
env:
GH_TOKEN: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number || steps.get_pr_review.outputs.pr_number }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number }}'
REVIEW_LABEL: '${{ inputs.review_label }}'
run: |-
set -euo pipefail
Expand Down Expand Up @@ -692,7 +596,7 @@ jobs:
# sees it, so any quote in the expanded value can break out of the
# string literal and become executable code.
env:
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number || steps.get_pr_review.outputs.pr_number }}'
PR_NUMBER: '${{ steps.get_pr.outputs.pr_number || steps.get_pr_comment.outputs.pr_number }}'
REVIEW_LABEL: '${{ inputs.review_label }}'
with:
github-token: '${{ steps.generate_token.outputs.token || secrets.GITHUB_TOKEN_SECRET }}'
Expand Down
Loading
Loading