name: doc-bridge-gate
description: Run doc-bridge index freshness and configured gates on pull requests
branding:
  icon: book
  color: blue

inputs:
  config-path:
    description: Path to doc-bridge.config.json (default discovers from repo root)
    required: false
    default: ''
  gate:
    description: Optional single gate id (default runs all configured gates)
    required: false
    default: ''
  node-version:
    description: Node.js version for ak-docs
    required: false
    default: '22'
  package-version:
    description: Exact @agentskit/doc-bridge npm version (kept in sync with this Action release)
    required: false
    default: '1.4.1'

runs:
  using: composite
  steps:
    - name: Setup Node
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v4
      with:
        node-version: ${{ inputs.node-version }}

    - name: Install ak-docs
      shell: bash
      env:
        DOC_BRIDGE_PACKAGE_VERSION: ${{ inputs.package-version }}
      run: |
        if [[ ! "$DOC_BRIDGE_PACKAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
          echo "::error title=Invalid package-version::Use an exact semver version such as 1.2.1"
          exit 2
        fi
        # When this Action runs against the Doc Bridge repository itself (CI dogfood),
        # install the workspace package so gates exercise the PR under test — not a
        # stale published npm version that may lag the local ecosystem contract.
        if [ -f package.json ] && [ "$(node -p "try{require('./package.json').name}catch{''}")" = "@agentskit/doc-bridge" ]; then
          npm install -g .
        else
          npm install -g "@agentskit/doc-bridge@${DOC_BRIDGE_PACKAGE_VERSION}"
        fi

    - name: Run gates
      shell: bash
      env:
        DOC_BRIDGE_CONFIG_PATH: ${{ inputs.config-path }}
        DOC_BRIDGE_GATE_ID: ${{ inputs.gate }}
      run: |
        if [ -n "$DOC_BRIDGE_GATE_ID" ] && [ -n "$DOC_BRIDGE_CONFIG_PATH" ]; then
          ak-docs gate run "$DOC_BRIDGE_GATE_ID" --config "$DOC_BRIDGE_CONFIG_PATH"
        elif [ -n "$DOC_BRIDGE_GATE_ID" ]; then
          ak-docs gate run "$DOC_BRIDGE_GATE_ID"
        elif [ -n "$DOC_BRIDGE_CONFIG_PATH" ]; then
          ak-docs gate run --config "$DOC_BRIDGE_CONFIG_PATH"
        else
          ak-docs gate run
        fi

    - name: Doctor coverage (annotation)
      shell: bash
      continue-on-error: true
      env:
        DOC_BRIDGE_CONFIG_PATH: ${{ inputs.config-path }}
      run: |
        if [ -n "$DOC_BRIDGE_CONFIG_PATH" ]; then
          REPORT="$(ak-docs doctor --text --config "$DOC_BRIDGE_CONFIG_PATH" 2>&1 || true)"
        else
          REPORT="$(ak-docs doctor --text 2>&1 || true)"
        fi
        echo "$REPORT"
        SCORE="$(echo "$REPORT" | sed -n 's/^Score: \([0-9]*\)\/.*/\1/p' | head -1)"
        if [ -n "$SCORE" ]; then
          echo "::notice title=doc-bridge coverage::Score ${SCORE}/100 — run ak-docs doctor locally for next actions"
        fi
