diff --git a/.gitea/workflows/review.yml b/.gitea/workflows/review.yml new file mode 100644 index 0000000..bc58853 --- /dev/null +++ b/.gitea/workflows/review.yml @@ -0,0 +1,233 @@ +# 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 + 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 + 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_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 + env: + PROBE_WORKFLOW_REF: ${{ github.workflow_ref }} + PROBE_JOB_WORKFLOW_SHA: ${{ github.job_workflow_sha }} + PROBE_JOB_WORKFLOW_REF: ${{ github.job_workflow_ref }} + run: | + python3 --version + git --version + node --version + echo "probe workflow_ref=[$PROBE_WORKFLOW_REF]" + echo "probe job_workflow_sha=[$PROBE_JOB_WORKFLOW_SHA]" + echo "probe job_workflow_ref=[$PROBE_JOB_WORKFLOW_REF]" + echo "probe RUNNER_TEMP=[${RUNNER_TEMP:-}]" + env | grep -E '^GITHUB_' | grep -viE 'TOKEN' | sort | sed 's/^/probe /' + + - 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 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.extraHeader \ + GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN" + src="$(mktemp -d)" + git clone -q -- "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" "$src" + 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.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.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