name: Announce Release
on:
  release:
    # This is intentionally not `published` to avoid announcing pre-releases
    types: [released]
  workflow_dispatch:
    inputs:
      tag_name:
        description: 'The tag name of the release'
        required: true
jobs:
  release:
    runs-on: ubuntu-slim
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6

      - name: Announce Release
        run: ./.github/scripts/announce-release.sh
        env:
          GH_TOKEN: ${{ github.token }}
          REPO: ${{ github.repository }}
          TAG_NAME: ${{ github.event.release.tag_name || inputs.tag_name }}
          URL: ${{ secrets.RELEASE_ANNOUNCEMENT_URL }}
          ROLE_ID: ${{ secrets.RELEASE_ANNOUNCEMENT_ROLE_ID }}
          USERNAME: ${{ secrets.RELEASE_ANNOUNCEMENT_USERNAME }}
          AVATAR_URL: ${{ secrets.RELEASE_ANNOUNCEMENT_AVATAR_URL }}

  notify_issues:
    runs-on: ubuntu-slim
    permissions:
      contents: read
      issues: write
      pull-requests: read
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
        with:
          sparse-checkout: .github/scripts

      - name: Resolve previous release tag
        id: prev
        run: ./.github/scripts/resolve-previous-release-tag.sh
        env:
          GH_TOKEN: ${{ github.token }}
          REPO: ${{ github.repository }}
          TAG_NAME: ${{ github.event.release.tag_name || inputs.tag_name }}

      - name: Notify issues about release
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
        env:
          TAG_NAME: ${{ github.event.release.tag_name || inputs.tag_name }}
          PREVIOUS_TAG_NAME: ${{ steps.prev.outputs.tag }}
        with:
          script: |
            const notify = require('./.github/scripts/release-notify-issues.js');
            await notify({
              github,
              context,
              core,
              tagName: process.env.TAG_NAME,
              previousTagName: process.env.PREVIOUS_TAG_NAME,
            });
