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
57 changes: 39 additions & 18 deletions .github/workflows/guardian-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ name: Guardian Review (reusable)
# (actions/*, astral-sh/setup-uv, peter-evans/create-or-update-comment).
# Pin `uses:` and `tooling_ref` to the SAME commit SHA, never a tag: the
# tooling runs on the caller's runner, and a tag can move.
#
# The pins are current majors and run on node24, so a self-hosted runner must
# be v2.327.1 or later (GitHub-hosted runners already are). The job needs no
# GitHub CLI: the reaction and the base-branch lookup are
# actions/github-script, and the poster talks to the REST API over HTTPS (#742).

on:
workflow_call:
Expand Down Expand Up @@ -79,7 +84,7 @@ jobs:
steps:
# Here rather than in each caller, so no caller can forget it.
- name: Check commenter has write access
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({
Expand All @@ -93,19 +98,23 @@ jobs:
}

- name: React to trigger comment
env:
GH_TOKEN: ${{ github.token }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
gh api repos/$GITHUB_REPOSITORY/issues/comments/$COMMENT_ID/reactions \
-X POST -f content=eyes
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
# The comment the caller's `if` matched: `context.payload.comment`.
# The REST call, not `gh api` — a self-hosted runner need not have gh (#742).
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'eyes',
});

- name: Validate inputs and get base branch
- name: Validate inputs
env:
EVENT_NAME: ${{ github.event_name }}
PR_NUMBER: ${{ inputs.pr_number }}
SOURCE_ROOT: ${{ inputs.source_root }}
GH_TOKEN: ${{ github.token }}
run: |
if [ "$EVENT_NAME" != issue_comment ]; then
echo "Call this on issue_comment (a /guardian review comment), not $EVENT_NAME" && exit 1
Expand All @@ -124,16 +133,28 @@ jobs:
echo "source_root must be a plain path inside the repository: '$SOURCE_ROOT'" && exit 1 ;;
esac
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
BASE_BRANCH=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json baseRefName --jq '.baseRefName')
echo "BASE_BRANCH=$BASE_BRANCH" >> $GITHUB_ENV

# Read through the REST API, not `gh pr view`: gh is not on every runner
# (#742). PR_NUMBER was validated as digits-only in the step above, so
# the number cannot reach the API malformed.
- name: Get base branch
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: Number(process.env.PR_NUMBER),
});
core.exportVariable('BASE_BRANCH', pr.base.ref);

# The PR is data, never code (#730). Everything that runs — uv sync, the
# guardian scripts, cgis itself — comes from codegraph-brain at
# tooling_ref, checked out here; the PR lands in pr/ and is only diffed,
# read and parsed. This step and the next keep the job's provider keys
# out of reach of whoever wrote the PR.
- name: Checkout trusted tooling (codegraph-brain)
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
repository: zaebee/codegraph-brain
ref: ${{ inputs.tooling_ref }}
Expand All @@ -149,7 +170,7 @@ jobs:
}

- name: Checkout PR branch as data
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
repository: ${{ github.repository }}
# PR_NUMBER validated as digits-only in a previous step
Expand All @@ -174,7 +195,7 @@ jobs:
GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $cred" git fetch origin "$BASE_BRANCH"

- name: Install uv
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3

- name: Install dependencies
run: uv sync --frozen --extra guardian
Expand Down Expand Up @@ -216,7 +237,7 @@ jobs:
- name: Mint GitHub App token
id: app-token
if: vars.GUARDIAN_APP_CLIENT_ID != ''
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.GUARDIAN_APP_CLIENT_ID }}
private-key: ${{ secrets.GUARDIAN_APP_PRIVATE_KEY }}
Expand Down Expand Up @@ -258,22 +279,22 @@ jobs:
--base-branch "$BASE_BRANCH"

- name: Upload metrics artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: guardian-metrics-${{ env.PR_NUMBER }}
path: guardian_metrics.jsonl
retention-days: 7

- name: Upload finder recording artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: guardian-finder-${{ env.PR_NUMBER }}
path: guardian_finder.json
retention-days: 7

- name: Post review as PR comment
if: steps.guardian.outputs.posted_inline != 'true'
uses: peter-evans/create-or-update-comment@a111a2c3bacd7be7898ee22d0d71d9aec2bb972c # v4
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5
with:
token: ${{ steps.app-token.outputs.token || github.token }}
issue-number: ${{ env.PR_NUMBER }}
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/guardian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ jobs:

steps:
- name: Checkout main
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
ref: main
fetch-depth: 0

- name: Download metrics artifact
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
with:
name: guardian-metrics-${{ github.event.issue.number }}

Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:

steps:
- name: Check commenter has write access
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({
Expand Down Expand Up @@ -104,13 +104,13 @@ jobs:
echo "APPLIED=$APPLIED" >> $GITHUB_ENV

- name: Checkout main
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
ref: main
fetch-depth: 0

- name: Install uv
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3

- name: Install dependencies
run: uv sync --frozen
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ The details — and a pipeline graph CGIS regenerates from its own source on eve
* **Dogfooded & measured:** Guardian reviews CGIS's *own* pull requests, and a benchmark harness scores it against curated ground truth — so prompt changes are validated, not guessed.

```bash
# Build the graph, then review a PR with a local model (no API key)
# Build the graph, then review a PR with a local model (no API key).
# --inline posts over HTTPS with GH_TOKEN (or GITHUB_TOKEN); no gh CLI needed.
# The reusable workflow exports GH_TOKEN for you.
cgis ingest ./src --output graph.db
GUARDIAN_PROVIDER=ollama GUARDIAN_MODEL=qwen2.5-coder:14b \
uv run python scripts/guardian_review.py --pr 123 --db graph.db --inline
Expand Down
64 changes: 54 additions & 10 deletions src/cgis/guardian/github_poster.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
"""Posts a ReviewResult as one GitHub review with inline comments (spec §6)."""

import json
import subprocess
import os
import urllib.error
import urllib.request

from cgis.guardian.findings import Finding, ReviewResult
from cgis.guardian.render import render_inline_comment, render_review_body
from cgis.guardian.skeptic import visible_findings

#: The REST API the workflow token writes to. A fixed host, not a variable: a
#: caller-supplied URL would send the token somewhere else.
_API_ROOT = "https://api.github.com"
#: Bounded so a stalled connection cannot hold the job past the app token's
#: lifetime; an inline review is a few kilobytes.
_TIMEOUT_S = 30
#: GitHub rejects a request without one (403); urllib's default names only the
#: library, which says nothing useful in the audit log.
_USER_AGENT = "codegraph-brain-guardian"

#: Below this length a quote is too generic to substring-match safely — short
#: keywords/patterns (``)``, ``else:``, ``self.``, ``return``) would collide with
#: unrelated lines (``something_else:``, ``myself.foo``). Such a quote may only
Expand Down Expand Up @@ -98,6 +110,24 @@ def build_review(
return render_review_body(result, outside=outside, threshold=threshold) + footer, comments


def _token() -> str:
"""The GitHub token, read from the environment the way the gh CLI reads it.

GH_TOKEN is what the reusable workflow exports (the app token when one is
configured, else the caller's GITHUB_TOKEN); GITHUB_TOKEN is the fallback
for a local run of the script. Surrounding whitespace is stripped: a token
pasted with a trailing space or newline must not become an auth failure.
"""
token = (os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") or "").strip()
if not token:
_msg = (
"No GitHub token: set GH_TOKEN (or GITHUB_TOKEN) to post the review. "
"The reusable workflow exports GH_TOKEN for exactly this call."
)
raise RuntimeError(_msg)
return token


def post_inline_review(
*,
repo: str,
Expand All @@ -109,11 +139,13 @@ def post_inline_review(
diff_content: dict[str, dict[int, str]] | None = None,
threshold: int = 0,
) -> None:
"""POST one COMMENT review via `gh api` (auto-auth in Actions, spec §6.4).
"""POST one COMMENT review over plain HTTPS (spec §6.4).

Always event=COMMENT, never REQUEST_CHANGES — guardian is an advisor,
not a gate. Raises CalledProcessError on API rejection; the caller
decides the fallback (spec §6.5).
not a gate. The request needs no GitHub CLI: the token comes from the
environment and urllib sends the request, so a self-hosted runner needs
neither gh nor its config (#742). Raises RuntimeError on API rejection;
the caller decides the fallback (spec §6.5).
"""
body, comments = build_review(
result,
Expand All @@ -124,10 +156,22 @@ def post_inline_review(
threshold=threshold,
)
payload = {"event": "COMMENT", "body": body, "comments": comments}
subprocess.run(
["gh", "api", f"repos/{repo}/pulls/{pr}/reviews", "-X", "POST", "--input", "-"],
input=json.dumps(payload),
text=True,
check=True,
capture_output=True,
request = urllib.request.Request(
f"{_API_ROOT}/repos/{repo}/pulls/{pr}/reviews",
data=json.dumps(payload).encode("utf-8"),
headers={
"Authorization": f"Bearer {_token()}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/json",
"User-Agent": _USER_AGENT,
},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=_TIMEOUT_S) as response:
response.read()
except urllib.error.HTTPError as exc:
detail = exc.read().decode("utf-8", "replace")[:1000]
_msg = f"GitHub rejected the inline review ({exc.code} {exc.reason}): {detail}"
raise RuntimeError(_msg) from exc
2 changes: 1 addition & 1 deletion src/cgis/guardian/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ async def run_guardian(
try:
diff_text = collector.source.get_git_diff()
content = diff_line_content(diff_text)
await asyncio.to_thread( # subprocess `gh api` call — keep the loop responsive
await asyncio.to_thread( # blocking HTTPS call — keep the loop responsive
post_inline_review,
repo=inline_repo,
pr=pr,
Expand Down
Loading
Loading