name: Release

on:
  push:
    branches:
      - main
      - next
      - "1-legacy"
      - "2-legacy"
      - "3-legacy"
      - "4-legacy"
      - "5-legacy"
      - "6-legacy"

defaults:
  run:
    shell: bash

env:
  FORCE_COLOR: true

jobs:
  changelog:
    name: Changelog PR or Release
    if: ${{ github.repository_owner == 'withastro' }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Setup PNPM
        uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8

      - name: Setup Node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24.15.0

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

      - name: Build Packages
        run: pnpm run build --force

      - name: Create Release Pull Request or Publish
        id: changesets
        uses: changesets/action@6a0a831ff30acef54f2c6aa1cbbc1096b066edaf # v1.7.0
        with:
          # Note: pnpm install after versioning is necessary to refresh lockfile
          version: pnpm run version
          publish: pnpm exec changeset publish
          commit: "[ci] release"
          title: "[ci] release"
        env:
          # Needs access to push to main
          GITHUB_TOKEN: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
          # Use OIDC for npm authentication instead of NPM_TOKEN
          NPM_TOKEN: '' # https://github.com/changesets/changesets/issues/1152#issuecomment-3190884868

      - name: Check if astro-vscode was published
        id: vscode-published
        if: steps.changesets.outputs.published == 'true'
        run: |
          if echo '${{ steps.changesets.outputs.publishedPackages }}' | jq -e '.[] | select(.name == "astro-vscode")' > /dev/null; then
            echo "published=true" >> $GITHUB_OUTPUT
          else
            echo "published=false" >> $GITHUB_OUTPUT
          fi

      - name: Prepare vscode folder
        if: steps.vscode-published.outputs.published == 'true'
        working-directory: ./packages/language-tools/vscode
        run: |
          npm run build:grammar
          mv node_modules/astro-ts-plugin-bundle ./astro-ts-plugin-bundle # move the ts-plugin out of node_modules to avoid deletion
          rm -rf node_modules
          # Also delete root package.json and node_modules to avoid vsce picking up any monorepo info
          mv ../../../package.json ../../../package.temp.json
          rm -rf ../../../node_modules
          sleep 60s # wait for npm registry to update
          npm i --workspaces=false # vsce does not support pnpm, so we need to pretend this is not a monorepo and use npm
          mv ./astro-ts-plugin-bundle ./node_modules/astro-ts-plugin-bundle

      - name: Publish to VSCode Marketplace
        if: steps.vscode-published.outputs.published == 'true'
        working-directory: ./packages/language-tools/vscode
        run: |
          npx vsce publish -p ${{ secrets.VSCE_TOKEN }} --target win32-x64 win32-arm64 linux-x64 linux-arm64 linux-armhf darwin-x64 darwin-arm64 alpine-x64 alpine-arm64

      - name: Publish to OpenVSX
        if: steps.vscode-published.outputs.published == 'true'
        working-directory: ./packages/language-tools/vscode
        run: |
          npx ovsx publish -p ${{ secrets.OVSX_TOKEN }} --target win32-x64 win32-arm64 linux-x64 linux-arm64 linux-armhf darwin-x64 darwin-arm64 alpine-x64 alpine-arm64
          
      - name: Restore root package.json and node_modules
        if: steps.vscode-published.outputs.published == 'true'
        run: |
          mv ./package.temp.json ./package.json
          pnpm install --frozen-lockfile
          
      - name: Generate Announcement
        id: message
        if: steps.changesets.outputs.published == 'true'
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
        run: node .github/scripts/announce.mjs '${{ steps.changesets.outputs.publishedPackages }}'

      - name: Send message on Discord
        if: steps.changesets.outputs.published == 'true'
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
        uses: Ilshidur/action-discord@d2594079a10f1d6739ee50a2471f0ca57418b554 # 0.4.0
        with:
          args: "${{ steps.message.outputs.DISCORD_MESSAGE }}"

      - name: Dispatch post-release event
        if: steps.changesets.outputs.published == 'true' && github.ref_name == 'main'
        uses: peter-evans/repository-dispatch@28959ce8df70de7be546dd1250a005dd32156697 # v4.0.1
        with:
          token: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
          event-type: release-published
