# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT
#
# copilot-setup-steps.yml
# Pre-install tools and dependencies for GitHub Copilot Coding Agent
# Reference: https://docs.github.com/en/copilot/how-tos/use-copilot-agents/coding-agent/customize-the-agent-environment

name: "Copilot Setup Steps"

# Copilot coding agent runs these steps internally before starting work.
# Use workflow_dispatch to manually validate the setup when desired.
on:
  workflow_dispatch:

# Minimal permissions
permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  # Job MUST be named 'copilot-setup-steps' to be recognized by Copilot
  copilot-setup-steps:
    runs-on: ubuntu-latest

    # Minimal permissions; Copilot receives its own token for operations
    permissions:
      contents: read

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4.2.2
        with:
          persist-credentials: false

      - name: Set up Node.js
        uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v4.1.0
        with:
          node-version: "20"
          cache: "npm"

      # continue-on-error allows Copilot to start work even if dependencies fail,
      # so it can fix package.json/package-lock.json issues itself
      - name: Install JavaScript dependencies
        continue-on-error: true
        run: npm ci

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.11"

      - name: Install actionlint
        env:
          ACTIONLINT_VERSION: '1.7.10'
          ACTIONLINT_SHA256: 'f4c76b71db5755a713e6055cbb0857ed07e103e028bda117817660ebadb4386f'
        run: |
          curl -sLO "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz"
          echo "${ACTIONLINT_SHA256}  actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" | sha256sum -c -
          tar -xzf "actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" actionlint
          sudo install actionlint /usr/local/bin/actionlint
          rm actionlint "actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz"
          actionlint --version

      - name: Install PowerShell modules
        shell: pwsh
        run: |
          Install-Module -Name PowerShell-Yaml -Force -Scope CurrentUser
          Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
          Install-Module -Name Pester -RequiredVersion 5.7.1 -Force -Scope CurrentUser

      - name: Verify tool availability
        run: |
          echo "=== Tool Versions ==="
          node --version
          npm --version
          python3 --version
          pwsh --version
          shellcheck --version
          actionlint --version
          echo ""
          echo "=== npm Scripts Available ==="
          npm run --list
