# The reusable docs-review workflow. A repository calls it from its own workflow with # `uses: Civilytics/docs-review/.gitea/workflows/review.yml@v1`; README.md has the # caller and the inputs. # # Secrets reach only the steps that need them. The bot token is in the clone step and # the two steps that run the tool. The Claude credentials are in the drafting step only, # and that step runs only when `drafter` normalizes to `claude`. Every `if:` reads the # normalized drafter, never the raw input. name: docs-review on: workflow_call: inputs: drafter: description: "claude, local or none; anything else behaves as none" type: string required: false default: none docs: description: "Newline-separated paths or globs of the living documents" type: string required: true generated: description: "Newline-separated paths of documents that are rebuilt, never hand-edited" type: string required: false default: "" top_level: description: "The subset of docs the monthly pass re-reads in full" type: string required: false default: README.md review_only: description: "Newline-separated paths or globs within docs that the drafter may read but not edit" type: string required: false default: "" source_dirs: description: "Newline-separated directories whose new files count toward uncovered" type: string required: false default: "R\nscripts" workstream: description: "Scope in draft commit messages, docs(): ..." type: string required: false default: docs tracking_issue: description: "Issue number the monthly pass's commits cite" type: string required: false default: "" doc_guide: description: "Path to the document naming each listed document's reader and purpose" type: string required: false default: "" model: description: "Model for drafter: claude" type: string required: false default: claude-sonnet-5-5 # Gitea 28.3 does not tell a called workflow its own ref (github.workflow_ref and # job_workflow_sha are empty, and GITHUB_WORKFLOW_SHA is the caller's commit), so # the caller names the tool version twice. tool_ref: description: "Branch or tag of docs-review to run; keep it equal to the @ref in uses:" type: string required: false default: v1 secrets: DOCS_REVIEW_BOT_TOKEN: required: false CLAUDE_CODE_OAUTH_TOKEN: required: false ANTHROPIC_API_KEY: required: false defaults: run: shell: bash jobs: review: runs-on: ubuntu-latest # node:22 carries python3 (3.11) and git, and npm for the Claude Code install. container: node:22 # The drafter's own timeout is 25 minutes; the debounce adds 3. timeout-minutes: 40 env: TOOL_REPO: Civilytics/docs-review TOOL_REF: ${{ inputs.tool_ref }} EVENT: ${{ github.event_name }} ACTION: ${{ github.event.action }} PR: ${{ github.event.pull_request.number }} BASE: ${{ github.event.pull_request.base.ref }} HEAD_REF: ${{ github.event.pull_request.head.ref }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} INPUT_DRAFTER: ${{ inputs.drafter }} INPUT_DOCS: ${{ inputs.docs }} INPUT_GENERATED: ${{ inputs.generated }} INPUT_TOP_LEVEL: ${{ inputs.top_level }} INPUT_REVIEW_ONLY: ${{ inputs.review_only }} INPUT_SOURCE_DIRS: ${{ inputs.source_dirs }} INPUT_WORKSTREAM: ${{ inputs.workstream }} INPUT_TRACKING_ISSUE: ${{ inputs.tracking_issue }} INPUT_DOC_GUIDE: ${{ inputs.doc_guide }} INPUT_MODEL: ${{ inputs.model }} # The calling repository's files are untrusted. Every python3 call takes -P, so # Python imports nothing from the working directory and a docs_review/ or os.py in # a pull request cannot replace the tool. steps: - name: Versions run: | python3 --version git --version node --version - name: Fetch docs-review run: | set -eu tool="$RUNNER_TEMP/docs-review" git clone -q --depth 1 --branch "$TOOL_REF" -- \ "$GITHUB_SERVER_URL/$TOOL_REPO.git" "$tool" echo "docs-review $TOOL_REF at $(git -C "$tool" rev-parse --short HEAD)" echo "PYTHONPATH=$tool" >> "$GITHUB_ENV" - name: Normalize the drafter id: drafter run: | set -eu drafter="$(python3 -P -c 'import os; from docs_review.inputs import normalize_drafter; print(normalize_drafter(os.environ.get("INPUT_DRAFTER")))')" echo "drafter: ${INPUT_DRAFTER:-} -> $drafter" echo "drafter=$drafter" >> "$GITHUB_OUTPUT" # Full history, so merge bases and the docs branch's ancestry resolve. The token # travels as an http./.extraHeader in this command's environment, so it is # sent only to the Gitea server and never written to .git/config or a remote URL. - name: Clone the calling repository if: github.event_name != 'pull_request' || github.event.action != 'closed' env: DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }} run: | set -eu if [ -z "$DOCS_REVIEW_BOT_TOKEN" ]; then echo "::error::the DOCS_REVIEW_BOT_TOKEN secret is not set" exit 1 fi export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" src="$(mktemp -d)" if ! git clone -q -- "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" "$src"; then echo "::error::the bot token could not clone $GITHUB_REPOSITORY; it needs the" \ "write:repository scope and write access to the repository" exit 1 fi cd "$src" if [ "$EVENT" = "pull_request" ]; then git fetch -q origin "refs/pull/$PR/head" git fetch -q origin "+refs/heads/$BASE:refs/remotes/origin/$BASE" git checkout -q --detach "$HEAD_SHA" fi git log --oneline -1 cd - >/dev/null cp -a "$src/." . && rm -rf "$src" - name: Close the docs PR of a closed pull request if: github.event_name == 'pull_request' && github.event.action == 'closed' env: DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }} run: python3 -P -m docs_review closed --number "$PR" # Stage 1 runs on every PR event. With drafter none it is the whole run and posts # the comment; otherwise it only checks that the flags compute, and the drafting # step after the debounce posts. - name: Stage 1 if: >- (github.event_name == 'pull_request' && github.event.action != 'closed') || ((github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && steps.drafter.outputs.drafter == 'none') env: DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }} DOCS_REVIEW_STAGE: "1" run: | set -eu export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" if [ "$EVENT" = "pull_request" ]; then python3 -P -m docs_review pr --number "$PR" --base "$BASE" \ --head-ref "$HEAD_REF" --head-sha "$HEAD_SHA" else base="$(git rev-parse --abbrev-ref origin/HEAD)" python3 -P -m docs_review monthly --month "$(date -u +%Y-%m)" --base "${base#origin/}" fi # A re-push inside this wait cancels the run (the caller's concurrency group) # before anything is spent. - name: Debounce if: >- github.event_name == 'pull_request' && github.event.action != 'closed' && steps.drafter.outputs.drafter != 'none' run: sleep 180 # `local` runs here too, without the Claude credentials and without the install. - name: Draft if: >- steps.drafter.outputs.drafter != 'none' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.action != 'closed')) env: DRAFTER: ${{ steps.drafter.outputs.drafter }} DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }} CLAUDE_CODE_OAUTH_TOKEN: ${{ steps.drafter.outputs.drafter == 'claude' && secrets.CLAUDE_CODE_OAUTH_TOKEN || '' }} ANTHROPIC_API_KEY: ${{ steps.drafter.outputs.drafter == 'claude' && secrets.ANTHROPIC_API_KEY || '' }} CLAUDE_CODE_VERSION: 2.1.322 DISABLE_UPDATES: "1" CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1" run: | set -eu # A fresh HOME, so no settings, credentials or history from the image reach # Claude Code. export HOME="$(mktemp -d "$RUNNER_TEMP/home.XXXXXX")" # With no credential the tool posts the flags with a note and fails, so the # install would be wasted. if [ "$DRAFTER" = "claude" ] && [ -n "$CLAUDE_CODE_OAUTH_TOKEN$ANTHROPIC_API_KEY" ]; then npm install -g --no-fund --no-audit "@anthropic-ai/claude-code@$CLAUDE_CODE_VERSION" claude --version fi export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \ GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" if [ "$EVENT" = "pull_request" ]; then python3 -P -m docs_review pr --number "$PR" --base "$BASE" \ --head-ref "$HEAD_REF" --head-sha "$HEAD_SHA" else base="$(git rev-parse --abbrev-ref origin/HEAD)" python3 -P -m docs_review monthly --month "$(date -u +%Y-%m)" --base "${base#origin/}" fi