name: E2E (Copilot)

# Live end-to-end check: drive the real GitHub Copilot CLI (`copilot`) through
# oneharness and assert the JSON contract (scripts/e2e-copilot.sh). Unlike
# ci.yml this needs network, a model call, and the COPILOT_GITHUB_TOKEN secret,
# so it lives in its own workflow gated to trusted contexts only.
#
# Security: a secret must never be reachable by untrusted code. GitHub already
# withholds secrets from fork pull requests; the job `if` below is defense in
# depth — it runs only in the canonical repo and never for a PR whose head is a
# fork, so this workflow can be edited in a fork PR without exposing the token.

on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: e2e-copilot-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  # In CI the harness is installed and auth verified up front, so any e2e skip
  # means detection/install/spawn broke (e.g. an unresolved .cmd shim on Windows)
  # — fail instead of going green having tested nothing. See scripts/e2e-lib.sh.
  OH_E2E_NO_SKIP: 1

jobs:
  live:
    name: copilot live e2e (${{ matrix.os }})
    if: >-
      github.repository == 'nickderobertis/oneharness' &&
      (github.event_name != 'pull_request' ||
       github.event.pull_request.head.repo.full_name == github.repository)
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4

      - name: Verify the API token is present
        env:
          COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_GITHUB_TOKEN }}
        run: |
          if [ -z "${COPILOT_GITHUB_TOKEN:-}" ]; then
            echo "::error::COPILOT_GITHUB_TOKEN secret is not set. Create a fine-grained PAT with the 'Copilot Requests' permission at https://github.com/settings/personal-access-tokens/new, then sync it (just secrets-sync) or set it: gh secret set COPILOT_GITHUB_TOKEN --repo ${{ github.repository }}"
            exit 1
          fi

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@v2
        with:
          tool: just

      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - name: Install Copilot CLI
        run: |
          npm install -g @github/copilot
          copilot --version

      - name: Live e2e against the real CLI
        env:
          COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_GITHUB_TOKEN }}
          COPILOT_E2E_MODEL: ${{ vars.COPILOT_E2E_MODEL }}
        run: just live-copilot
