name: Release Notification

on:
  release:
    types: [published]

  workflow_dispatch:
    inputs:
      tag_name:
        required: true
        type: string
        description: The release tag to generate a notification for

env:
  TAG: ${{ github.event.release.tag_name || github.event.inputs.tag_name }}

jobs:
  notify:
    name: Discord Notification
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v4

      - name: Setup Inputs
        run: |
          cat >> $GITHUB_ENV <<"EOL"
          INPUT_TAG="${{ env.TAG }}"
          INPUT_ORG_REPO="${{ github.repository }}"
          EOL

      - name: Get Release Info
        uses: octokit/request-action@v2.x
        id: release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          route: GET /repos/{org_repo}/releases/tags/{tag}

      # Write changelog to an environment variable to avoid certain characters from being processed
      # by the shell
      - name: Get Information from Release
        env:
          DATA: ${{ steps.release.outputs.data }}
        run: |
          jq -r '.assets[].browser_download_url' <<<"$DATA" > assets.txt
          jq -r '.body' <<<"$DATA" > changelog.txt

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Install Discord Webhook Python Module
        run: pip install "discord-webhook==1.0.0"

      - name: Send Discord Notification
        run: >
          python ci/notify/discord_notify.py
          --version ${{ env.TAG }}
          --repo ${{ github.repository }}
          --webhook-url ${{ secrets.DISCORD_WEBHOOK }}
          --changelog changelog.txt
          --assets assets.txt