|
| 1 | +name: Claude Code |
| 2 | + |
| 3 | +# Reactive: does nothing until someone writes @claude. Pairs with |
| 4 | +# claude-pr-review.yml, which reviews every PR unprompted. |
| 5 | +# |
| 6 | +# >>> DO NOT MERGE THESE TWO INTO ONE JOB. <<< |
| 7 | +# They look redundant and are not. Supplying a `prompt` puts the action in AGENT |
| 8 | +# mode for every event it sees, so a single job carrying both triggers would |
| 9 | +# answer @claude comments in agent mode: no PR context, no tracking comment, and |
| 10 | +# nothing posted back. This file deliberately supplies no prompt, which is what |
| 11 | +# keeps it in TAG mode. Deleting one as a duplicate of the other has silently |
| 12 | +# broken @claude before. |
| 13 | +# |
| 14 | +# NOTE: `issue_comment` workflows always run from the default branch, so edits to |
| 15 | +# this file do nothing until they are on `main`. |
| 16 | +# |
| 17 | +# The 👀 reaction on an @claude comment comes from the GitHub App acknowledging the |
| 18 | +# mention. It is NOT evidence that anything ran -- the work happens here. |
| 19 | + |
| 20 | +on: |
| 21 | + issue_comment: |
| 22 | + types: [created] |
| 23 | + pull_request_review_comment: |
| 24 | + types: [created] |
| 25 | + issues: |
| 26 | + types: [opened, assigned] |
| 27 | + pull_request_review: |
| 28 | + types: [submitted] |
| 29 | + |
| 30 | +jobs: |
| 31 | + claude: |
| 32 | + # Two conditions, both required, and they must be checked per-event. |
| 33 | + # |
| 34 | + # 1. author_association of the person who ACTUALLY TRIGGERED IT. This repository is |
| 35 | + # public and this job has `contents: write` and blanket `Bash`, so without this |
| 36 | + # check any passer-by who types @claude gets an arbitrary-code-execution agent |
| 37 | + # run on the maintainer's OAuth token. The pairing matters: on `issue_comment`, |
| 38 | + # `github.event.issue.author_association` is the issue's *opener*, not the |
| 39 | + # commenter, so checking that field here would let a stranger trigger a run |
| 40 | + # merely by commenting on a maintainer's own issue. OWNER covers the repository |
| 41 | + # owner; COLLABORATOR/MEMBER cover anyone later given push access. |
| 42 | + # 2. the @claude mention itself. |
| 43 | + if: | |
| 44 | + (github.event_name == 'issue_comment' && |
| 45 | + contains(github.event.comment.body, '@claude') && |
| 46 | + contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) || |
| 47 | + (github.event_name == 'pull_request_review_comment' && |
| 48 | + contains(github.event.comment.body, '@claude') && |
| 49 | + contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) || |
| 50 | + (github.event_name == 'pull_request_review' && |
| 51 | + contains(github.event.review.body, '@claude') && |
| 52 | + contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.review.author_association)) || |
| 53 | + (github.event_name == 'issues' && |
| 54 | + (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')) && |
| 55 | + contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.issue.author_association)) |
| 56 | + runs-on: ubuntu-latest |
| 57 | + permissions: |
| 58 | + contents: write |
| 59 | + pull-requests: write |
| 60 | + issues: write |
| 61 | + id-token: write |
| 62 | + actions: read # Required for Claude to read CI results on PRs |
| 63 | + steps: |
| 64 | + - name: Checkout repository |
| 65 | + uses: actions/checkout@v4 |
| 66 | + with: |
| 67 | + fetch-depth: 1 |
| 68 | + |
| 69 | + - name: Run Claude Code |
| 70 | + id: claude |
| 71 | + uses: anthropics/claude-code-action@v1 |
| 72 | + with: |
| 73 | + claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} |
| 74 | + |
| 75 | + # This is an optional setting that allows Claude to read CI results on PRs. |
| 76 | + # It is also what makes the three `mcp__github_ci__*` tools available; tag |
| 77 | + # mode adds them to the allowlist on its own, so they are not listed below. |
| 78 | + additional_permissions: | |
| 79 | + actions: read |
| 80 | +
|
| 81 | + # No `prompt:` on purpose -- see the header. That is what keeps this in tag |
| 82 | + # mode, where Claude performs the instructions in the comment that tagged it. |
| 83 | + |
| 84 | + # Without these the log shows only "init" and "result". On a public |
| 85 | + # repository the job log and the artifacts below are world-readable, so this |
| 86 | + # is a deliberate trade: without them, a run that was starved by a denied |
| 87 | + # tool call is indistinguishable from one that had nothing to do. |
| 88 | + display_report: true |
| 89 | + show_full_output: true |
| 90 | + |
| 91 | + # >>> Do not go back to an enumerated command allowlist. <<< |
| 92 | + # Earlier versions of this file listed individual Bash commands (puppet-lint, |
| 93 | + # rake spec, ./bin/run_tests.sh, nox, poetry, ...). Anything unlisted is a |
| 94 | + # SILENT denial: the run goes green having done nothing, which is exactly how |
| 95 | + # the PR-review job failed before it was rebuilt. Skill and Task matter for |
| 96 | + # the same reason -- a skill or a subagent that cannot be invoked is not an |
| 97 | + # error, just an absence. |
| 98 | + # |
| 99 | + # Unlike the review job, this one can push, so it is the more powerful of the |
| 100 | + # two. What keeps blanket Bash acceptable is who can reach it at all -- see |
| 101 | + # the `if:` above and the comment on it. |
| 102 | + # |
| 103 | + # In tag mode this list is MERGED with the action's own base tools (Read/Glob/ |
| 104 | + # Grep/LS, the comment-update tool, the CI tools, and git add/commit/push/rm); |
| 105 | + # it does not replace them. Naming an `mcp__github_*` tool here is also what |
| 106 | + # installs the corresponding MCP server, which is why the inline-comment tool |
| 107 | + # is listed even though this job rarely reaches for it. |
| 108 | + # |
| 109 | + # WebSearch and WebFetch are DISALLOWED by default in tag mode; naming them |
| 110 | + # here is what lifts that. They are worth having -- "@claude why does this |
| 111 | + # library do X" is most of what the mention job gets asked -- and the `if:` |
| 112 | + # above is what makes it an acceptable trade, since an agent that can fetch |
| 113 | + # arbitrary URLs and also push is only reachable by someone with push access. |
| 114 | + claude_args: | |
| 115 | + --allowedTools "mcp__github_inline_comment__create_inline_comment,Read,Glob,Grep,Edit,Write,Task,Skill,TodoWrite,WebSearch,WebFetch,Bash" |
| 116 | +
|
| 117 | + # The action discards its transcript with the runner. Without this there is no way |
| 118 | + # to explain a green run that did nothing. |
| 119 | + - name: Collect Claude logs |
| 120 | + if: always() |
| 121 | + env: |
| 122 | + EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }} |
| 123 | + run: | |
| 124 | + mkdir -p "$RUNNER_TEMP/claude-logs" |
| 125 | + if [ -n "$EXECUTION_FILE" ] && [ -f "$EXECUTION_FILE" ]; then |
| 126 | + cp "$EXECUTION_FILE" "$RUNNER_TEMP/claude-logs/execution-output.json" |
| 127 | + fi |
| 128 | + if [ -d "$HOME/.claude/projects" ]; then |
| 129 | + cp -r "$HOME/.claude/projects" "$RUNNER_TEMP/claude-logs/sessions" |
| 130 | + fi |
| 131 | + find "$RUNNER_TEMP/claude-logs" -type f -printf '%s\t%p\n' | sort -rn |
| 132 | +
|
| 133 | + - name: Upload Claude logs |
| 134 | + if: always() |
| 135 | + uses: actions/upload-artifact@v7 |
| 136 | + with: |
| 137 | + name: claude-mention-logs-${{ github.run_id }}-${{ github.run_attempt }} |
| 138 | + path: ${{ runner.temp }}/claude-logs/ |
| 139 | + if-no-files-found: warn |
| 140 | + retention-days: 14 |
| 141 | + |
| 142 | + # Same guard as claude-pr-review.yml: a denied tool call is how one of these |
| 143 | + # jobs passes while having done nothing. There is no comment to annotate here |
| 144 | + # -- in tag mode the action maintains its own tracking comment -- so the |
| 145 | + # numbers go to the job summary and a denial fails the check. |
| 146 | + - name: Summarise the run and fail on permission denials |
| 147 | + if: always() |
| 148 | + env: |
| 149 | + EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }} |
| 150 | + run: | |
| 151 | + set -uo pipefail |
| 152 | +
|
| 153 | + if [ -z "${EXECUTION_FILE:-}" ] || [ ! -s "$EXECUTION_FILE" ]; then |
| 154 | + echo "::warning title=Claude did not run::No execution file was produced, so the agent never started. Expected if this PR edits .github/workflows/claude-*.yml (workflow validation skip)." |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | +
|
| 158 | + RESULT=$(jq -c '[.[] | select(.type == "result")] | last // empty' "$EXECUTION_FILE") |
| 159 | + if [ -z "$RESULT" ]; then |
| 160 | + echo "::warning title=No result block::The execution file exists but contains no result object." |
| 161 | + exit 0 |
| 162 | + fi |
| 163 | +
|
| 164 | + DURATION_MS=$(jq -r '.duration_ms // 0' <<<"$RESULT") |
| 165 | + TURNS=$(jq -r '.num_turns // 0' <<<"$RESULT") |
| 166 | + COST=$(jq -r '.total_cost_usd // 0' <<<"$RESULT") |
| 167 | + DENIALS=$(jq -r '.permission_denials_count // 0' <<<"$RESULT") |
| 168 | + IS_ERROR=$(jq -r '.is_error // false' <<<"$RESULT") |
| 169 | +
|
| 170 | + HUMAN=$(awk -v ms="$DURATION_MS" 'BEGIN { |
| 171 | + s = int(ms / 1000) |
| 172 | + if (s < 60) { printf "%ds", s } else { printf "%dm %ds", int(s / 60), s % 60 } |
| 173 | + }') |
| 174 | + COST_FMT=$(awk -v c="$COST" 'BEGIN { printf "%.4f", c }') |
| 175 | +
|
| 176 | + { |
| 177 | + echo "### Claude (@mention)" |
| 178 | + echo |
| 179 | + echo "| | |" |
| 180 | + echo "|---|---|" |
| 181 | + echo "| Duration | $HUMAN |" |
| 182 | + echo "| Turns | $TURNS |" |
| 183 | + echo "| Cost | \$$COST_FMT |" |
| 184 | + echo "| Permission denials | $DENIALS |" |
| 185 | + } >> "$GITHUB_STEP_SUMMARY" |
| 186 | +
|
| 187 | + STATUS=0 |
| 188 | + if [ "$DENIALS" != "0" ]; then |
| 189 | + echo "::error title=Claude was denied $DENIALS tool call(s)::A denied tool means the run was starved, even though it exited cleanly. Add the missing tool to --allowedTools. See the claude-mention-logs-* artifact." |
| 190 | + STATUS=1 |
| 191 | + fi |
| 192 | + if [ "$IS_ERROR" = "true" ]; then |
| 193 | + echo "::error title=Claude reported an error::The run's result block has is_error=true." |
| 194 | + STATUS=1 |
| 195 | + fi |
| 196 | + exit $STATUS |
0 commit comments