Skip to content

Commit 26a766f

Browse files
authored
Allow Claude to write (#7294)
* Split Claude automation into two workflows with separate trust boundaries: claude-write.yml for write-capable issue automation and follow-up edits on Claude-owned PRs, and claude-review.yml for read-only PR review. * Sets up a GitHub app (vortex-claude) so PRs pushed by Claude can run CI * Only claude-write can access the GitHub App private key, and only through the claude-automation environment. No other workflow gets that key. * claude-write uses a short-lived GitHub App installation token, not GITHUB_TOKEN, so PRs opened by Claude trigger normal pull_request CI. * claude-write runs only for trusted collaborators with write/maintain/admin access. * claude-write is issue-driven by default. It may also run from a PR conversation comment, but only when that PR is a same-repo PR originally opened by the Vortex Claude GitHub App (CLAUDE_APP_LOGIN). * claude-write refuses fork PR content entirely. There is no promotion path for forks. * claude-review has no GitHub App private key and no write-capable repo token. It uses the built-in GITHUB_TOKEN only for reading repo state and posting review comments. * claude-review can review same-repo PRs, but it must not create commits, push branches, open PRs, or update PR branches. * PR conversation comments on Claude-owned PRs are routed to claude-write; claude-review explicitly stands down on that path to avoid duplicate runs. * Both workflows reject bot-triggered invocations and gate access before running Claude logic. * Both workflows use persist-credentials: false on checkout so git operations do not silently fall back to the built-in GITHUB_TOKEN. * Fork PRs are always refused for Claude automation because fork content is treated as untrusted prompt input. --------- Signed-off-by: Nicholas Gates <[email protected]>
1 parent 223d1df commit 26a766f

3 files changed

Lines changed: 472 additions & 66 deletions

File tree

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
name: Claude Code (Review)
2+
3+
# Setup
4+
# - This workflow does not need the GitHub App private key.
5+
# - Do not attach the `claude-automation` environment here.
6+
# - Store `CLAUDE_CODE_OAUTH_TOKEN` as a repository or organization Actions secret.
7+
# - Create a repository or organization Actions variable:
8+
# - CLAUDE_APP_LOGIN
9+
# Set this to the bot login for the GitHub App, usually `<app-slug>[bot]`.
10+
# - It may use the default GITHUB_TOKEN to read repository data and post review
11+
# comments, but it must never be able to push commits or open branches.
12+
#
13+
# Why this workflow exists separately
14+
# - PR review traffic is a different trust boundary from issue automation.
15+
# - This workflow is intentionally read-only with respect to repository contents.
16+
# - Fork PRs are refused outright. We do not "promote" or manually bless fork
17+
# content into Claude. If a contributor wants Claude to implement something,
18+
# a maintainer should restate the task on an issue and use claude-write.yml.
19+
# - PR conversation comments on PRs already opened by the Claude App are handled by
20+
# claude-write.yml so maintainers can ask Claude to make follow-up changes there.
21+
22+
concurrency:
23+
# `issue_comment` events on PRs expose the PR number via `github.event.issue.number`,
24+
# so this falls back there when `github.event.pull_request.number` is unset.
25+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.issue.number || github.run_id }}
26+
cancel-in-progress: true
27+
28+
on:
29+
issue_comment:
30+
types: [created]
31+
pull_request_review_comment:
32+
types: [created]
33+
pull_request_review:
34+
types: [submitted]
35+
36+
jobs:
37+
gate:
38+
name: Gate PR Trigger
39+
runs-on: ubuntu-latest
40+
permissions:
41+
contents: read
42+
issues: read
43+
pull-requests: read
44+
outputs:
45+
should_run: ${{ steps.gate.outputs.should_run }}
46+
reason: ${{ steps.gate.outputs.reason }}
47+
pull_number: ${{ steps.gate.outputs.pull_number }}
48+
checkout_ref: ${{ steps.gate.outputs.checkout_ref }}
49+
actor_has_write: ${{ steps.gate.outputs.actor_has_write }}
50+
steps:
51+
- name: Check whether this PR event is allowed to reach Claude
52+
id: gate
53+
uses: actions/github-script@v7
54+
with:
55+
github-token: ${{ github.token }}
56+
script: |
57+
const sender = context.payload.sender?.login ?? '';
58+
const senderType = context.payload.sender?.type ?? '';
59+
const trustedClaudeLogin = process.env.CLAUDE_APP_LOGIN ?? '';
60+
61+
async function getPermission(username) {
62+
try {
63+
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
username,
67+
});
68+
return data.permission;
69+
} catch (error) {
70+
if (error.status === 404) {
71+
return 'none';
72+
}
73+
throw error;
74+
}
75+
}
76+
77+
let mentioned = false;
78+
let pullNumber = null;
79+
80+
if (context.eventName === 'issue_comment') {
81+
mentioned = (context.payload.comment?.body ?? '').includes('@claude');
82+
if (context.payload.issue?.pull_request) {
83+
pullNumber = context.payload.issue.number;
84+
}
85+
} else if (context.eventName === 'pull_request_review_comment') {
86+
mentioned = (context.payload.comment?.body ?? '').includes('@claude');
87+
pullNumber = context.payload.pull_request?.number ?? null;
88+
} else if (context.eventName === 'pull_request_review') {
89+
mentioned = (context.payload.review?.body ?? '').includes('@claude');
90+
pullNumber = context.payload.pull_request?.number ?? null;
91+
}
92+
93+
if (pullNumber) {
94+
core.setOutput('pull_number', String(pullNumber));
95+
}
96+
97+
let reason = '';
98+
99+
if (!mentioned) {
100+
reason = 'not_mentioned';
101+
} else if (!pullNumber) {
102+
reason = 'not_a_pr_event';
103+
} else if (senderType === 'Bot') {
104+
reason = 'bot_sender_refused';
105+
}
106+
107+
let actorHasWrite = 'false';
108+
if (!reason) {
109+
const permission = await getPermission(sender);
110+
core.setOutput('actor_permission', permission);
111+
actorHasWrite = ['admin', 'maintain', 'write'].includes(permission) ? 'true' : 'false';
112+
if (actorHasWrite !== 'true') {
113+
reason = 'actor_lacks_write';
114+
}
115+
}
116+
117+
if (!reason && context.eventName === 'issue_comment' && !trustedClaudeLogin) {
118+
reason = 'missing_claude_app_login';
119+
}
120+
121+
if (!reason) {
122+
let pr = null;
123+
const response = await github.rest.pulls.get({
124+
owner: context.repo.owner,
125+
repo: context.repo.repo,
126+
pull_number: pullNumber,
127+
});
128+
pr = response.data;
129+
130+
const headRepo = pr.head.repo;
131+
const isFork = !headRepo || headRepo.fork || headRepo.full_name !== `${context.repo.owner}/${context.repo.repo}`;
132+
core.setOutput('checkout_ref', pr.head.sha);
133+
134+
if (isFork) {
135+
reason = 'fork_pr_refused';
136+
} else if (
137+
context.eventName === 'issue_comment' &&
138+
trustedClaudeLogin &&
139+
(pr.user?.login ?? '') === trustedClaudeLogin
140+
) {
141+
reason = 'claude_pr_uses_write_workflow';
142+
}
143+
144+
if (!reason) {
145+
const files = await github.paginate(github.rest.pulls.listFiles, {
146+
owner: context.repo.owner,
147+
repo: context.repo.repo,
148+
pull_number: pullNumber,
149+
per_page: 100,
150+
});
151+
// Refuse PRs that modify `.github/` because workflow files define the
152+
// automation policy this review job is enforcing.
153+
if (files.some(f => f.filename.startsWith('.github/'))) {
154+
reason = 'modifies_github_dir';
155+
}
156+
}
157+
}
158+
159+
core.setOutput('actor_has_write', actorHasWrite);
160+
core.setOutput('should_run', !reason ? 'true' : 'false');
161+
core.setOutput('reason', reason || 'allowed');
162+
env:
163+
CLAUDE_APP_LOGIN: ${{ vars.CLAUDE_APP_LOGIN }}
164+
165+
explain-refusal:
166+
name: Explain Refusal
167+
needs: gate
168+
if: |
169+
needs.gate.outputs.actor_has_write == 'true' &&
170+
(
171+
needs.gate.outputs.reason == 'fork_pr_refused' ||
172+
needs.gate.outputs.reason == 'modifies_github_dir' ||
173+
needs.gate.outputs.reason == 'missing_claude_app_login'
174+
)
175+
runs-on: ubuntu-latest
176+
permissions:
177+
issues: write
178+
pull-requests: write
179+
steps:
180+
- name: Comment with the refusal reason
181+
uses: actions/github-script@v7
182+
with:
183+
script: |
184+
const reason = ${{ toJSON(needs.gate.outputs.reason) }};
185+
const messages = {
186+
fork_pr_refused: [
187+
"Claude review automation is disabled for fork pull requests.",
188+
"",
189+
"Why:",
190+
"- fork content is untrusted input",
191+
"- this repository does not allow Claude to run against fork content",
192+
"- there is no promotion path for forks",
193+
"",
194+
"If maintainers want Claude to implement a change, restate the task on an issue and use the issue-driven Claude workflow instead."
195+
].join("\n"),
196+
modifies_github_dir: [
197+
"Claude review automation is disabled for pull requests that modify `.github/` files.",
198+
"",
199+
"Why:",
200+
"- workflow and action files are part of the automation policy",
201+
"- this review workflow refuses to evaluate automation changes from the same PR",
202+
"",
203+
"Ask a human reviewer to inspect workflow changes directly."
204+
].join("\n"),
205+
missing_claude_app_login: [
206+
"Claude review automation is misconfigured for issue-comment triggers.",
207+
"",
208+
"Why:",
209+
"- `CLAUDE_APP_LOGIN` is not set",
210+
"- without that bot login, the review workflow cannot safely route comments on Claude-owned PRs to the write workflow",
211+
"",
212+
"Set the `CLAUDE_APP_LOGIN` Actions variable, then retry the command."
213+
].join("\n"),
214+
};
215+
216+
await github.rest.issues.createComment({
217+
owner: context.repo.owner,
218+
repo: context.repo.repo,
219+
issue_number: Number(${{ needs.gate.outputs.pull_number }}),
220+
body: messages[reason],
221+
});
222+
223+
claude:
224+
name: Run Claude PR Review
225+
needs: gate
226+
if: needs.gate.outputs.should_run == 'true'
227+
runs-on: ubuntu-latest
228+
timeout-minutes: 45
229+
permissions:
230+
contents: read
231+
issues: write
232+
pull-requests: write
233+
actions: read
234+
steps:
235+
- name: Checkout same-repo PR contents
236+
uses: actions/checkout@v6
237+
with:
238+
ref: ${{ needs.gate.outputs.checkout_ref }}
239+
fetch-depth: 1
240+
# Keep git credentials out of the workspace. This workflow is review-only
241+
# and should never push changes.
242+
persist-credentials: false
243+
244+
- name: Run Claude Code in review-only mode
245+
id: claude
246+
uses: anthropics/claude-code-action@6cad158a175744eb2e76f7f5fd108ec63145598c
247+
with:
248+
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
249+
# This workflow deliberately uses the built-in token because it only needs
250+
# to read repository state and post review comments. It cannot write repo
251+
# contents, and it has no access to the GitHub App private key.
252+
github_token: ${{ github.token }}
253+
254+
additional_permissions: |
255+
actions: read
256+
257+
claude_args: |
258+
--allowedTools "Read,Grep,Glob,Bash(git diff:*),Bash(git show:*),Bash(git log:*),Bash(head:*),Bash(jq:*),Bash(rg:*)"
259+
--system-prompt "You are the repository's read-only Claude review workflow. Review the current same-repo pull request and respond in GitHub. Never modify files, never create commits, never push branches, and never open or update pull requests. Fork pull requests are blocked before this job starts."

0 commit comments

Comments
 (0)