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
1 change: 1 addition & 0 deletions .github/cursor-review/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ All optional except `workflows_ref` (required, no default) — pass them under
| `rereview_command` | `''` (off) | A comment command, e.g. `/review`, asking for another round on the **same commit**. Empty leaves the same-commit dedupe absolute. Needs the SAME value on [`cursor-review-auto-label.yml`](../../docs/callers/cursor-review-auto-label.md) plus `issue_comment` in that caller's triggers: that half re-applies the label (removing it first, since adding a label already present raises no event), this half stops the dedupe from skipping the round it starts. Setting one alone is a silent no-op. The command must be the first token of the first line, and the commenter's association must be `OWNER`, `MEMBER` or `COLLABORATOR` — not the opted-in roster. The request must postdate the review it overrides, so one request buys one round; `max_rounds` still applies. Exists because the documented alternative cannot work: the consolidated review is `COMMENTED`, which GitHub will not dismiss. See [the setup guide](../../docs/callers/cursor-review.md#asking-for-another-round-on-the-same-commit). |
| `max_rounds` | `5` | Per-PR review-round cap; `0` → none. Counts the posting identity's consolidated reviews since `needs-human-review` was last removed; at or over the cap the panel is skipped, the PR is labelled `needs-human-review`, one comment lists the latest round's open findings above the threshold, and the `approve_gate` output is `capped`. Fails open on a read error. See [the setup guide](../../docs/callers/cursor-review.md#round-cap-and-the-approve_gate-output). |
| `runs_on` | `'"ubuntu-latest"'` | JSON-encoded `runs-on` (same shape as `cursor-review-auto-label.yml`'s) for the four credential-free heavy jobs only: `diff-size`, `preflight`, the `review` panel and `consolidate`. The gate, ledger, Slack notices and every job holding the App token or `pull-requests: write` stay on GitHub-hosted `ubuntu-latest`. Those four run models with unsandboxed shell over PR code, so use a self-hosted pool only if each job gets a fresh pod (fresh filesystem and `$HOME`), holds no cloud identity or token, and can't reach private networks or the metadata server. The image must be linux/x64 with bash, git, curl, jq, python3, gh, tar and GNU coreutils. |
| `approver_environment` | `''` | The caller-repo environment holding `APPROVER_TOKEN` (an environment secret, base-branch-only deployment policy), declared on `post-review`, `dismiss-stale-approval` and `auto-retry` only, and only under `pull_request_target`. Pair with a `pull_request_target` caller — see the [trust model](../../docs/callers/cursor-review.md#trust-model). |

### `workflows_ref` must equal the `uses:` pin

Expand Down
72 changes: 72 additions & 0 deletions .github/cursor-review/tests/test_workflow_job_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,78 @@ def test_payload_is_handed_over_as_an_artifact(self):
self.assertIn("name: cursor-review-consolidated", post)
self.assertIn("actions/download-artifact@", post)

def test_environment_is_declared_only_on_the_approver_token_jobs(self):
# approver_environment confines APPROVER_TOKEN to a base-branch-only
# environment. Declaring it on a PR-checkout job would hand that
# environment's secrets to a job running models over PR code.
token_jobs = sorted(
name
for name, body in self.jobs.items()
if any("secrets.APPROVER_TOKEN" in line for line in code_lines(body))
)
self.assertEqual(
token_jobs, ["auto-retry", "dismiss-stale-approval", "post-review"]
)
env_jobs = sorted(
name
for name, body in self.jobs.items()
if any(re.match(r"^ environment:", line) for line in code_lines(body))
)
self.assertEqual(env_jobs, token_jobs)
for name in env_jobs:
body = code_lines(self.jobs[name])
self.assertIn(ENVIRONMENT_LINE, body, name)
self.assertFalse(
checks_out_pr_repo(self.jobs[name]),
f"`{name}` declares the approver environment and checks out PR code",
)
approve = read_lines(APPROVE_WORKFLOW)
self.assertEqual(
[line for line in approve if re.match(r"^\s+environment:", line)],
[ENVIRONMENT_LINE],
)

def test_every_pr_checkout_pins_a_ref(self):
# Under pull_request_target actions/checkout defaults to the BASE
# branch, so a PR checkout with no `ref:` would review the wrong tree.
pinned = 0
for name, body in self.jobs.items():
for step in split_steps(body):
lines = outside_run_blocks(code_lines(step))
if not any(CHECKOUT.match(line) for line in lines):
continue
if any(
(m := REPOSITORY.match(line)) and scalar(m.group(1)) == ASSETS_REPO
for line in lines
):
continue
refs = [line for line in lines if re.match(r"^\s+ref:\s*\S", line)]
self.assertTrue(refs, f"`{name}` checks out PR code with no `ref:`")
# The head SHA, not `refs/pull/<n>/merge`: that ref is mutable
# between jobs and absent on a conflicted PR.
self.assertEqual(
[r.split("ref:", 1)[1].strip() for r in refs],
["${{ github.event.pull_request.head.sha }}"],
name,
)
pinned += 1
self.assertGreaterEqual(pinned, 5)


def read_lines(path):
with open(path, encoding="utf-8") as f:
return f.read().split("\n")


# Declared only under pull_request_target: any other event runs on the PR's
# merge ref, which a base-branch-only deployment policy rejects.
ENVIRONMENT_LINE = (
" environment: ${{ github.event_name == 'pull_request_target'"
" && inputs.approver_environment || '' }}"
)

APPROVE_WORKFLOW = os.path.join(os.path.dirname(WORKFLOW), "cursor-approve.yml")


if __name__ == "__main__":
unittest.main(verbosity=2)
40 changes: 37 additions & 3 deletions .github/workflows/cursor-approve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ name: Cursor approve
# marked non-gating stop blocking auto-resolution, as
# cursor-review's own decide does. Default `full`: a marked
# thread blocks like any other (fail-closed)
# approver_environment the caller-repo environment holding APPROVER_TOKEN
# (an environment secret, deployment branches = the base
# branch only), declared on the `approve` job. Default '' =
# none. See docs/callers/cursor-review.md#trust-model.
# prior_rounds 0..3 (default 0 = off): how many earlier rounds of each
# axis's own verdicts it sees. decide writes them to the
# card as one base64 sentinel line; start reads them back
Expand Down Expand Up @@ -110,6 +114,13 @@ on:
prior_rounds:
type: number
default: 0
approver_environment:
description: >-
The caller-repo environment that holds APPROVER_TOKEN. With a
deployment-branch policy of the base branch only, the token is
unreachable from `pull_request` runs. Empty (default) = no environment.
type: string
default: ''
secrets:
APPROVER_TOKEN:
required: false
Expand All @@ -126,6 +137,11 @@ jobs:
name: 'cursor-approve (${{ inputs.phase }})'
runs-on: ubuntu-latest
timeout-minutes: 10
# The caller environment holding APPROVER_TOKEN; '' = none (the trust
# model in docs/callers/cursor-review.md#trust-model). Only under
# `pull_request_target`: any other event runs on `refs/pull/<n>/merge`,
# which a base-branch-only deployment policy rejects before a step runs.
environment: ${{ github.event_name == 'pull_request_target' && inputs.approver_environment || '' }}
permissions:
contents: read
actions: read
Expand All @@ -137,6 +153,8 @@ jobs:
PHASE: ${{ inputs.phase }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PRIOR_ROUNDS: ${{ inputs.prior_rounds }}
EVENT_NAME: ${{ github.event_name }}
APPROVER_ENVIRONMENT: ${{ inputs.approver_environment }}
run: |
case "$PHASE" in
start|decide) ;;
Expand All @@ -150,6 +168,20 @@ jobs:
if [ -z "$PR_NUMBER" ]; then
echo "::error::cursor-approve runs on a pull_request(_target) event only"; exit 1
fi
# Only `pull_request_target` loads the caller from the base branch; on
# any other event a PR can read APPROVER_TOKEN. Post no card and no
# approval as it — but still withdraw that identity's own marked
# approvals (`withdraw_only`), so one it posted earlier at this head
# does not keep satisfying branch protection.
if [ -n "$APPROVER_TOKEN" ] && [ "$EVENT_NAME" != "pull_request_target" ]; then
echo "::error::APPROVER_TOKEN on a '$EVENT_NAME' run is readable by any PR-head workflow, so this run posts no approval (it still withdraws earlier ones); call this reusable from pull_request_target with approver_environment (docs/callers/cursor-review.md#trust-model)"
echo "present=false" >> "$GITHUB_OUTPUT"
echo "withdraw_only=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ -n "$APPROVER_TOKEN" ] && [ -z "$APPROVER_ENVIRONMENT" ]; then
echo "::warning::APPROVER_TOKEN without approver_environment is a repo secret that any PR-head pull_request workflow can read; move it into a base-branch-only environment and pass approver_environment (docs/callers/cursor-review.md#trust-model)"
fi
if [ -z "$APPROVER_TOKEN" ]; then
echo "::warning::APPROVER_TOKEN is empty — cursor-approve posts no card and no approval (there is no fallback identity)."
echo "present=false" >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -215,7 +247,7 @@ jobs:
fi

- name: Load cursor-approve scripts
if: steps.token.outputs.present == 'true'
if: steps.token.outputs.present == 'true' || steps.token.outputs.withdraw_only == 'true'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: Comfy-Org/github-workflows
Expand All @@ -225,7 +257,7 @@ jobs:

- name: Resolve the approver login
id: login
if: steps.token.outputs.present == 'true'
if: steps.token.outputs.present == 'true' || steps.token.outputs.withdraw_only == 'true'
env:
GH_TOKEN: ${{ secrets.APPROVER_TOKEN }}
# Captured, not inlined into the echo: `echo "login=$(...)"` discards the
Expand All @@ -239,7 +271,9 @@ jobs:
echo "login=$login" >> "$GITHUB_OUTPUT"

- name: Withdraw approvals while the axes run
if: steps.token.outputs.present == 'true' && inputs.phase == 'start'
# Also, in either phase, on a trigger that may not approve (see the
# token step).
if: (steps.token.outputs.present == 'true' && inputs.phase == 'start') || steps.token.outputs.withdraw_only == 'true'
env:
GH_TOKEN: ${{ secrets.APPROVER_TOKEN }}
LOGIN: ${{ steps.login.outputs.login }}
Expand Down
Loading
Loading