name: sync-sandbox-agent-skill

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  generate:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Generate skill artifacts
        run: node scripts/skill-generator/generate.js

      - name: Sync to skills repo
        env:
          GH_TOKEN: ${{ secrets.RIVET_GITHUB_PAT }}
        run: |
          if [ -z "$GH_TOKEN" ]; then
            echo "::error::RIVET_GITHUB_PAT secret is not set"
            exit 1
          fi

          # Validate token before proceeding
          if ! gh auth status 2>/dev/null; then
            echo "::error::RIVET_GITHUB_PAT is invalid or expired. Rotate the token at https://github.com/settings/tokens"
            exit 1
          fi

          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          # Clone public repo, configure auth via gh credential helper
          gh auth setup-git
          git clone https://github.com/rivet-dev/skills.git /tmp/rivet-skills

          mkdir -p /tmp/rivet-skills/skills/sandbox-agent
          rm -rf /tmp/rivet-skills/skills/sandbox-agent/*
          cp -R scripts/skill-generator/dist/* /tmp/rivet-skills/skills/sandbox-agent/

          cd /tmp/rivet-skills
          git add skills/sandbox-agent

          if git diff --cached --quiet; then
            echo "No skill changes to publish"
            exit 0
          fi

          git commit -m "chore: update sandbox-agent skill"
          git push
