Files
claude-code-gitea-action/examples/gitea-structured-output.yml
reza d71f4db3d7 fix: inline --json-schema file paths so structured output works on CLI 2.1.160 (closes #3)
The docs recommend the file-path form (`--json-schema /path/to/schema.json`)
as primary, but the default-pinned Claude CLI 2.1.160 only accepts an inline
JSON string. Passing a path makes the CLI exit 0 with empty output, so the
action's fail-loud branch trips on every run.

Resolve the value before spawning the CLI: a file-path schema is read
in-process and passed inline (validated + compacted), while an already-inline
schema is left untouched. Because the value is handed to spawn() as an argv
element (never through a shell), a schema with `$ref`/`$defs` round-trips
intact. Unreadable files and invalid JSON now fail with a clear error instead
of the CLI's silent empty output.

Docs (README, example workflow, action.yml) are corrected to explain that the
file-path form is auto-inlined and works on the default CLI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 10:45:40 +10:00

68 lines
2.4 KiB
YAML

# Example: schema-validated PR review verdict via `claude_args` + `structured_output`.
#
# Instead of having the agent write its verdict to a file, this asks Claude for a
# result that conforms to a JSON Schema and reads it back from the action output.
# The default-installed Claude CLI (2.1.160) supports `--json-schema`; the
# action reads the schema file below and passes it to the CLI inline, so the
# file-path form works out of the box (see README → "Structured output"). If
# you consume this action through a pre-baked runner image
# (path_to_claude_code_executable), that image's CLI must still support the
# `--json-schema` flag.
name: Claude PR Review (structured output)
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
# The schema the verdict must conform to. Using a file (rather than an
# inline string) keeps the value free of spaces and `$` characters, so a
# schema with $ref/$defs survives shell tokenization intact.
- name: Write review schema
shell: bash
run: |
mkdir -p .gitea
cat > .gitea/review-schema.json <<'JSON'
{
"type": "object",
"additionalProperties": false,
"required": ["decision", "summary"],
"properties": {
"decision": { "type": "string", "enum": ["approve", "request_changes", "comment"] },
"summary": { "type": "string" }
}
}
JSON
- name: Run Claude review
id: review
uses: ryan/claude-code-gitea-action@gitea
with:
gitea_token: ${{ secrets.GITEA_TOKEN }}
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
direct_prompt: |
Review the changes in this pull request and return your verdict
strictly matching the provided JSON schema.
claude_args: --json-schema ${{ github.workspace }}/.gitea/review-schema.json
- name: Act on the verdict
shell: bash
env:
VERDICT: ${{ steps.review.outputs.structured_output }}
run: |
echo "Decision: $(echo "$VERDICT" | jq -r '.decision')"
echo "Summary: $(echo "$VERDICT" | jq -r '.summary')"