name: tinymist::announce
on:
  workflow_call:
    inputs:
      tag:
        description: Release Tag
        required: true
        type: string
  workflow_dispatch:
    inputs:
      tag:
        description: Release Tag
        required: true
        type: string
permissions:
  "contents": "write"

env:
  isNightly: ${{ ((!((!contains(inputs.tag, 'rc') && (endsWith(inputs.tag, '0') || endsWith(inputs.tag, '2') || endsWith(inputs.tag, '4') || endsWith(inputs.tag, '6') || endsWith(inputs.tag, '8')))))) }}

jobs:
  build:
    runs-on: "ubuntu-22.04"
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Install dist
        # we specify bash to get pipefail; it guards against the `curl` command
        # failing. otherwise `sh` won't catch that `curl` returned non-0
        shell: bash
        run: |
          dist_url="$(sed -n 's/^cargo-dist-url-override = "\(.*\)"$/\1/p' dist-workspace.toml)"
          test -n "$dist_url"
          curl --proto '=https' --tlsv1.2 -LsSf "$dist_url/cargo-dist-installer.sh" | sh
      - name: Install parse changelog
        uses: taiki-e/install-action@parse-changelog
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'yarn'
      - name: Install deps
        run: yarn install
      - id: announce
        name: "Generate announcement"
        run: |
          yarn draft-release ${{ inputs.tag }}
          echo "draft-release ran successfully"
      - name: "Upload announcement changelog"
        uses: actions/upload-artifact@v4
        with:
          name: announcement-changelog.md
          path: target/announcement-changelog.md
      - name: "Upload announcement"
        uses: actions/upload-artifact@v4
        with:
          name: announcement-dist.md
          path: target/announcement-dist.md
      - name: "Upload announcement"
        uses: actions/upload-artifact@v4
        with:
          name: announcement.gen.md
          path: target/announcement.gen.md
      - name: Create GitHub Release
        env:
          PRERELEASE_FLAG: "${{ (fromJson(env.isNightly) && '--prerelease') || '' }}"
          ANNOUNCEMENT_TITLE: "${{ steps.announce.outputs.tag }}"
          RELEASE_COMMIT: "${{ github.sha }}"
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo "Creating release for ${{ steps.announce.outputs.tag }} with PRERELEASE_FLAG=$PRERELEASE_FLAG (isNightly=$isNightly)"
          gh release create "${{ steps.announce.outputs.tag }}" $PRERELEASE_FLAG --title "$ANNOUNCEMENT_TITLE" --notes-file target/announcement.gen.md --draft=true
