You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d283f2e
Browse filesBrowse the repository at this point in the historyBrowse files
refactor: move the routine call to Python so every answer is tested
The fire step was shell and curl, which could only be checked by firing
the real routine. It is now standard-library Python (3.10 syntax), inline
in the reusable workflow with id "send", and tests/test_fire.py runs it
against the stub on 127.0.0.1. The routine API's address is a constant in
the script; the tests swap it through the harness's substitutions, so no
input or event field can change where the token goes.
Kept as before: the token is checked first, then the trigger id against
trig_[A-Za-z0-9]+; one POST to the same endpoint with the same four
headers; the body is {"text": <decision text>} written by a JSON encoder;
short timeouts (10 s to connect and per read, 30 s in all); no retry;
continue-on-error keeps a failure from turning the check red.
Pinned by the tests, with these deliberate differences from curl:
- Only a 2xx answer counts as sent. curl's --fail-with-body passed a 3xx;
a redirect is now an error and is not followed.
- A failure is one ::error:: annotation with the HTTP status and at most
200 characters of the answer, on one line, with the token taken out.
curl printed the whole answer as plain text, so an answer that repeated
the token printed it.
- A decision with no string text is an error and nothing is sent. jq
would have sent {"text": null}.
- The step writes sent=true only when the request was accepted, which
the react job (#16) will read.
Refs #15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Jmn7ot2xooXpcuW44QqtU
# Python's standard library only (3.10 features), so nothing is installed; tests/test_fire.py runs this script.
391
+
shell: python3 {0}
389
392
run: |
390
-
if [ -z "$CLAUDE_ROUTINE_TOKEN" ]; then
391
-
echo "::error::CLAUDE_ROUTINE_TOKEN is empty: pass it to the reusable workflow as a secret"
392
-
exit 1
393
-
fi
393
+
# Only a 2xx answer counts as sent. Anything else is an error annotation and a failed step, which the job's
394
+
# continue-on-error keeps from turning the check red.
395
+
import http.client
396
+
import json
397
+
import os
398
+
import re
399
+
import signal
400
+
import sys
401
+
import urllib.error
402
+
import urllib.request
403
+
import uuid
404
+
405
+
# The routine API's address. It is fixed here: no input or event field can change where the token goes.
406
+
API_BASE_URL = "https://api.anthropic.com"
407
+
# Seconds to wait to connect, and then for each read of the answer.
408
+
TIMEOUT_SECONDS = 10
409
+
# Seconds the whole request may take, however slowly the answer arrives.
410
+
TOTAL_SECONDS = 30
411
+
# The most of an error answer's body shown in the annotation.
412
+
EXCERPT_CHARACTERS = 200
413
+
394
414
# The id goes into the URL, so anything but a trigger id is refused.
395
-
if ! [[ "$TRIGGER_ID" =~ ^trig_[A-Za-z0-9]+$ ]]; then
396
-
echo "::error::routine-trigger-id is not a trigger id (trig_…): pass vars.CLAUDE_ROUTINE_TRIGGER_ID, set under Settings > Secrets and variables > Actions > Variables"
397
-
exit 1
398
-
fi
399
-
# jq writes the body, so nothing in the pull request's fields can break the JSON.
Copy file name to clipboardExpand all lines: README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ All three secrets, `CLAUDE_ROUTINE_TOKEN`, `CLAUDE_REVIEW_APP_ID` and `CLAUDE_RE
52
52
A run has two jobs:
53
53
54
54
-`decide` reads the event and decides. It writes a one-line summary, such as `Claude review: skipped (draft)`, to the run's summary page. It does not fail: every step that can fail is allowed to, and the decision then becomes a skip with the reason `decision-failed`.
55
-
-`fire` sends the request to the routine trigger. It runs only when `decide` decided to fire. A failed request shows as a failed step, but the job and the check stay green.
55
+
-`fire` sends the request to the routine trigger. It runs only when `decide` decided to fire. It sends the request once, with no retry, and does not follow a redirect. Only a `2xx` answer counts as sent. A failed request shows as a failed step with an error giving the HTTP status, but the job and the check stay green.
56
56
57
57
`fire` holds no permissions.
58
58
Only one `fire` runs per pull request at a time: a second waits rather than cancelling the first.
@@ -101,7 +101,7 @@ An event that has no pull request author, such as a payload whose `user` is `nul
101
101
102
102
## Python steps
103
103
104
-
Some steps, such as the membership lookup, are written in Python.
104
+
Some steps, such as the membership lookup and the request to the routine trigger, are written in Python.
105
105
They use only Python's standard library and keep to Python 3.10, so nothing is installed when they run.
106
106
Like the request decision, each one is kept inline in the reusable workflow, with `shell: python3 {0}` and an `id`, because a reusable workflow cannot read files from this repository.
107
107
The unit tests in `tests/` read each step's script out of the workflow, so they test exactly what ships.
@@ -129,7 +129,7 @@ request-decision/test
129
129
130
130
### Running the unit tests
131
131
132
-
The unit tests need `python3` (3.10 or later), `bash` and `yq`, and no network: a test that needs GitHub's API starts a stand-in server on 127.0.0.1.
132
+
The unit tests need `python3` (3.10 or later), `bash` and `yq`, and no network: a test that needs GitHub's API or the routine trigger starts a stand-in server on 127.0.0.1.
`tests/harness.py` runs one step of the reusable workflow on its own:
141
141
142
142
-`run_step(job, step_id, env, api_url=...)` reads the step's script out of the workflow with `yq`, runs it with the step's shell, and returns what it left: its exit code, standard output and error, its outputs (`.outputs`), its summary (`.summary`) and its `::error::` annotations (`.errors()`). The step sees only the `env` the test gives it, which must be names the step declares in its `env`, plus the runner's own variables, such as `GITHUB_OUTPUT`.
143
-
-`StubServer(routes)` stands in for GitHub's API. `routes` maps a path to a status and body, to `StubServer.HANG` for an answer that never comes, or to `StubServer.DROP` for a connection closed without one. It records every request.
144
-
-`substitutions` replaces text in the script before it runs, such as a timeout constant lowered so a test does not wait. Each piece of text must appear exactly once in the script.
143
+
-`StubServer(routes)` stands in for GitHub's API or the routine trigger. `routes` maps a path to a status and body, to `StubServer.HANG` for an answer that never comes, or to `StubServer.DROP` for a connection closed without one, for `GET` and `POST` alike. It records every request's method, path and headers in `.requests`, and its body in `.bodies`.
144
+
-`substitutions` replaces text in the script before it runs, such as a timeout constant lowered so a test does not wait, or the routine API's fixed address swapped for the stand-in server's. Each piece of text must appear exactly once in the script.
145
145
146
146
1. Add a test method to the step's test file, such as `tests/test_lookup.py`, or a new `tests/test_<step>.py` for a new step. Name it for what the step does, such as `test_a_pending_membership_is_not_a_member`.
147
147
2. Run the unit tests and check the new test passes.
0 commit comments