name: Publish
on:
  workflow_call:
    inputs:
      channel:
        default: stable
        required: true
        type: string
      version:
        required: true
        type: string
      target_commitish:
        required: true
        type: string
      prerelease:
        default: false
        required: true
        type: boolean
    secrets:
      ARCHIVE_REPO_TOKEN:
        required: false

permissions:
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/download-artifact@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"

      - name: Generate release notes
        run: |
          printf '%s' \
            '[![Installation](https://img.shields.io/badge/-Which%20file%20should%20I%20download%3F-white.svg?style=for-the-badge)]' \
              '(https://github.com/yt-dlp/yt-dlp#installation "Installation instructions") ' \
            '[![Documentation](https://img.shields.io/badge/-Docs-brightgreen.svg?style=for-the-badge&logo=GitBook&labelColor=555555)]' \
              '(https://github.com/yt-dlp/yt-dlp/tree/2023.03.04#readme "Documentation") ' \
            '[![Donate](https://img.shields.io/badge/_-Donate-red.svg?logo=githubsponsors&labelColor=555555&style=for-the-badge)]' \
              '(https://github.com/yt-dlp/yt-dlp/blob/master/Collaborators.md#collaborators "Donate") ' \
            '[![Discord](https://img.shields.io/discord/807245652072857610?color=blue&labelColor=555555&label=&logo=discord&style=for-the-badge)]' \
              '(https://discord.gg/H5MNcFW63r "Discord") ' \
            ${{ inputs.channel != 'nightly' && '"[![Nightly](https://img.shields.io/badge/Get%20nightly%20builds-purple.svg?style=for-the-badge)]" \
              "(https://github.com/yt-dlp/yt-dlp-nightly-builds/releases/latest \"Nightly builds\")"' || '' }} \
            > ./RELEASE_NOTES
          printf '\n\n' >> ./RELEASE_NOTES
          cat >> ./RELEASE_NOTES << EOF
          #### A description of the various files are in the [README](https://github.com/yt-dlp/yt-dlp#release-files)
          ---
          $(python ./devscripts/make_changelog.py -vv --collapsible)
          EOF
          printf '%s\n\n' '**This is an automated nightly pre-release build**' >> ./NIGHTLY_NOTES
          cat ./RELEASE_NOTES >> ./NIGHTLY_NOTES
          printf '%s\n\n' 'Generated from: https://github.com/${{ github.repository }}/commit/${{ inputs.target_commitish }}' >> ./ARCHIVE_NOTES
          cat ./RELEASE_NOTES >> ./ARCHIVE_NOTES

      - name: Archive nightly release
        env:
          GH_TOKEN: ${{ secrets.ARCHIVE_REPO_TOKEN }}
          GH_REPO: ${{ vars.ARCHIVE_REPO }}
        if: |
          inputs.channel == 'nightly' && env.GH_TOKEN != '' && env.GH_REPO != ''
        run: |
          gh release create \
            --notes-file ARCHIVE_NOTES \
            --title "yt-dlp nightly ${{ inputs.version }}" \
            ${{ inputs.version }} \
            artifact/*

      - name: Prune old nightly release
        if: inputs.channel == 'nightly' && !vars.ARCHIVE_REPO
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release delete --yes --cleanup-tag "nightly" || true
          git tag --delete "nightly" || true
          sleep 5  # Enough time to cover deletion race condition

      - name: Publish release${{ inputs.channel == 'nightly' && ' (nightly)' || '' }}
        env:
          GH_TOKEN: ${{ github.token }}
        if: (inputs.channel == 'nightly' && !vars.ARCHIVE_REPO) || inputs.channel != 'nightly'
        run: |
          gh release create \
            --notes-file ${{ inputs.channel == 'nightly' && 'NIGHTLY_NOTES' || 'RELEASE_NOTES' }} \
            --target ${{ inputs.target_commitish }} \
            --title "yt-dlp ${{ inputs.channel == 'nightly' && 'nightly ' || '' }}${{ inputs.version }}" \
            ${{ inputs.prerelease && '--prerelease' || '' }} \
            ${{ inputs.channel == 'nightly' && '"nightly"' || inputs.version }} \
            artifact/*