# Whenever a commit is pushed to master (ideally via a pull-request!)
# this action will create the next release, which means:
# 1. tag master with the proper version
# 2. create a new draft release (pre-released if a nightly build)
# 3. add release notes

name: 🏭 Create Release

on:
  push:
    branches:
      - master
  # TODO - future work
  # workflow_dispatch:
  #   inputs:
  #     isStable:
  #       description: 'Should it be a stable release?'
  #       required: true
  #       default: 'false'
  #     versionTag:
  #       description: 'The version to tag with'
  #       required: true

jobs:
  cut-release:
    if: github.repository == 'PCSX2/pcsx2'
    runs-on: ubuntu-latest
    name: "Create Tag and Release"
    steps:
      - uses: actions/checkout@v3

      # Docs - https://github.com/mathieudutour/github-tag-action
      - name: Bump Version and Push Tag
        id: tag_version
        uses: mathieudutour/github-tag-action@v6.1
        with:
          # Workflows cannot trigger other workflows implicitly
          # - https://github.community/t/github-actions-workflow-not-triggering-with-tag-push/17053/7
          github_token: ${{ secrets.BOT_PAT }}
          tag_prefix: v
          default_bump: patch

      # TODO - we could do this and remove the node.js script, but auto-generated notes only work
      # with PRs -- not commits (determine how much we care).
      # - name: Create Draft Release
      #   env:
      #     GITHUB_TOKEN: ${{ secrets.BOT_PAT }}
      #   run: |
      #     echo "Creating release with tag - ${{ steps.tag_version.outputs.new_tag }}"
      #     gh release create ${{ steps.tag_version.outputs.new_tag }} --draft --generate-notes -title ${{ steps.tag_version.outputs.new_tag }}

      - name: Generate Release Notes
        env:
          OWNER: PCSX2
          REPO: pcsx2
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COMMIT_SHA: ${{ github.SHA }}
        run: |
          cd ./.github/workflows/scripts/releases/generate-release-notes
          npm ci
          node index.js
          mv ./release-notes.md ${GITHUB_WORKSPACE}/release-notes.md

      - name: Create a GitHub Release
        uses: softprops/action-gh-release@v1
        if: steps.tag_version.outputs.new_tag
        with:
          body_path: ./release-notes.md
          draft: true
          prerelease: true
          tag_name: ${{ steps.tag_version.outputs.new_tag }}
