name: Release

on:
  workflow_dispatch:

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

jobs:
  build-and-test:
    uses: ./.github/workflows/build-and-test.yml
    permissions:
      actions: write
      contents: read
      pages: write
      id-token: write

  build-release-assets:
    uses: ./.github/workflows/packages.yml
    permissions:
      contents: read

  release:
    name: Release
    needs: [build-and-test, build-release-assets]
    runs-on: ubuntu-latest
    permissions:
      contents: write
      issues: write
      pull-requests: write
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: "lts/*"

      - name: Download release artifacts
        uses: actions/download-artifact@v8
        with:
          pattern: panache-*
          merge-multiple: true
          path: target/release-assets

      - name: Stage installer scripts
        run: |
          cp scripts/panache-installer.sh target/release-assets/panache-installer.sh
          cp scripts/panache-installer.ps1 target/release-assets/panache-installer.ps1

      - run: npm ci --prefix editors/code --no-audit --no-fund

      - run: npm audit signatures --prefix editors/code

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
          VSCE_PAT: ${{ secrets.VSCE_PAT }}
          OVSX_PAT: ${{ secrets.OVSX_PAT }}
        run: |
          ln -sf editors/code/package.json package.json
          npx --yes \
            --package @semantic-release/changelog \
            --package @semantic-release/git \
            --package conventional-changelog-conventionalcommits \
            --package semantic-release-cargo \
            --package semantic-release-replace-plugin \
            --package semantic-release-vsce \
            semantic-release
