name: Version & Publish

on:
  push:
    branches:
      - main

permissions:
  contents: write
  id-token: write

jobs:
  version:
    name: Version & Publish Extension
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: latest

      - name: Get pnpm store directory
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install root dependencies
        run: pnpm install --frozen-lockfile

      - name: Install extension dependencies
        working-directory: apps/extension
        run: pnpm install --frozen-lockfile

      - name: Install vsce and ovsx globally
        run: pnpm add -g @vscode/vsce ovsx

      - name: Make release script executable
        run: chmod +x scripts/release.sh

      - name: Create Release Pull Request or Publish
        uses: changesets/action@v1
        with:
          publish: ./scripts/release.sh
          title: "Release: Extension Version Packages"
          commit: "chore: release extension packages"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          VSCE_PAT: ${{ secrets.VSCE_PAT }}
          OVSX_PAT: ${{ secrets.OVSX_PAT }} 