name: Check PR
description: "Evaluate a pull request against Rho repo policy."

inputs:
  pr-number:
    description: "GitHub PR number. If empty, changed files are read from the event or git diff fallback."
    required: false
    default: ""
  repo:
    description: "Repository in owner/repo format. Defaults to github.repository."
    required: false
    default: ""
  root:
    description: "Repository root passed to rho repo check-pr."
    required: false
    default: "."
  base-ref:
    description: "Base ref for git diff fallback."
    required: false
    default: ""
  event-path:
    description: "GitHub event JSON path. Defaults to GITHUB_EVENT_PATH."
    required: false
    default: ""
  auto-close:
    description: "Close PRs when Rho returns blocked with close_recommended=true."
    required: false
    default: "false"
  fail-on-blocked:
    description: "Fail the check when the Rho result is blocked."
    required: false
    default: "true"
  fail-on-pending:
    description: "Fail the check when the Rho result is pending."
    required: false
    default: "true"

outputs:
  status:
    description: "Rho policy status: pass, pending, or blocked."
    value: ${{ steps.run.outputs.status }}
  close-recommended:
    description: "Whether Rho recommends closing the PR."
    value: ${{ steps.run.outputs.close_recommended }}
  result:
    description: "Full JSON result from rho repo check-pr."
    value: ${{ steps.run.outputs.result }}

runs:
  using: composite
  steps:
    - id: run
      shell: bash
      env:
        INPUT_PR_NUMBER: ${{ inputs.pr-number }}
        INPUT_REPO: ${{ inputs.repo }}
        INPUT_ROOT: ${{ inputs.root }}
        INPUT_BASE_REF: ${{ inputs.base-ref }}
        INPUT_EVENT_PATH: ${{ inputs.event-path }}
        INPUT_AUTO_CLOSE: ${{ inputs.auto-close }}
        INPUT_FAIL_ON_BLOCKED: ${{ inputs.fail-on-blocked }}
        INPUT_FAIL_ON_PENDING: ${{ inputs.fail-on-pending }}
      run: |
        set -euo pipefail

        repo="${INPUT_REPO:-${GITHUB_REPOSITORY:-}}"
        pr_number="$INPUT_PR_NUMBER"
        event_path="${INPUT_EVENT_PATH:-${GITHUB_EVENT_PATH:-}}"
        changed_files=()

        if [[ -n "$pr_number" && -n "$repo" ]]; then
          mapfile -t changed_files < <(
            gh api "repos/${repo}/pulls/${pr_number}/files" \
              --paginate \
              --jq '.[].filename'
          )
        fi

        args=(repo check-pr --root "$INPUT_ROOT" --format json)
        if [[ -n "$event_path" ]]; then
          args+=(--event "$event_path")
        fi
        if [[ ${#changed_files[@]} -eq 0 && -n "$INPUT_BASE_REF" ]]; then
          args+=(--base-ref "$INPUT_BASE_REF")
        fi
        for file in "${changed_files[@]}"; do
          args+=(--changed-file "$file")
        done

        result="$(rho "${args[@]}")"
        status="$(python3 -c 'import json,sys; print(json.load(sys.stdin)["status"])' <<<"$result")"
        close_recommended="$(python3 -c 'import json,sys; print(str(json.load(sys.stdin)["close_recommended"]).lower())' <<<"$result")"
        reason="$(python3 -c 'import json,sys; print(json.load(sys.stdin)["reason"])' <<<"$result")"

        {
          echo "status=$status"
          echo "close_recommended=$close_recommended"
          echo "result<<RHO_RESULT_JSON"
          echo "$result"
          echo "RHO_RESULT_JSON"
        } >> "$GITHUB_OUTPUT"

        {
          echo "## Rho PR Policy"
          echo
          echo "- Status: \`$status\`"
          echo "- Close recommended: \`$close_recommended\`"
          echo "- Reason: $reason"
          echo
          echo '```json'
          echo "$result"
          echo '```'
        } >> "$GITHUB_STEP_SUMMARY"

        if [[ "$status" == "blocked" && "$INPUT_AUTO_CLOSE" == "true" && "$close_recommended" == "true" && -n "$pr_number" && -n "$repo" ]]; then
          gh pr comment "$pr_number" --repo "$repo" --body "Rho policy blocked this PR: ${reason}"
          gh pr close "$pr_number" --repo "$repo"
        fi

        if [[ "$status" == "blocked" && "$INPUT_FAIL_ON_BLOCKED" == "true" ]]; then
          exit 1
        fi
        if [[ "$status" == "pending" && "$INPUT_FAIL_ON_PENDING" == "true" ]]; then
          exit 1
        fi
