name: Draft release

on:
  #presume that pull_request_target is safe at this point, since the PR was approved and merged
  #we need write access to prepare the release & create comments
  pull_request_target:
    types:
      - closed
    branches:
      - stable
      - minor-next
      - major-next
      - "legacy/*"
    paths:
      - "src/VersionInfo.php"
  push:
    tags:
      - "*"

env:
  PHP_VERSION: "8.2"

jobs:
  skip:
    name: Check whether to ignore this tag
    runs-on: ubuntu-22.04

    outputs:
      skip: ${{ steps.exists.outputs.exists == 'true' }}

    steps:
      - name: Check if release already exists
        id: exists
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          exists=false
          if [[ "${{ github.ref_type }}" == "tag" ]]; then
            tag="$(echo "${{ github.ref }}" | cut -d/ -f3-)"
            if gh release view "$tag" --repo "${{ github.repository }}"; then
              exists=true
            fi
          fi
          echo exists=$exists >> $GITHUB_OUTPUT

  check:
    needs: [skip]
    if: needs.skip.outputs.skip != 'true'
    name: Check release
    uses: ./.github/workflows/draft-release-pr-check.yml

  trigger-post-release-workflow:
    name: Trigger post-release RestrictedActions workflow
    needs: [check]
    if: needs.check.outputs.valid == 'true' && github.ref_type != 'tag' #can't do post-commit for a tag

    runs-on: ubuntu-22.04

    steps:
      - name: Generate access token
        id: generate-token
        uses: actions/create-github-app-token@v3
        with:
          app-id: ${{ vars.RESTRICTED_ACTIONS_DISPATCH_ID }}
          private-key: ${{ secrets.RESTRICTED_ACTIONS_DISPATCH_KEY }}
          owner: ${{ github.repository_owner }}
          repositories: RestrictedActions

      - name: Dispatch post-release restricted action
        uses: peter-evans/repository-dispatch@v4
        with:
          token: ${{ steps.generate-token.outputs.token }}
          repository: ${{ github.repository_owner }}/RestrictedActions
          event-type: pocketmine_mp_post_release
          client-payload: '{"branch": "${{ github.ref }}"}'

  draft:
    name: Create GitHub draft release
    needs: [check]
    if: needs.check.outputs.valid == 'true' || github.ref_type == 'tag' #ignore validity check for tags

    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true

      - name: Setup PHP
        uses: shivammathur/setup-php@2.37.0
        with:
          php-version: ${{ env.PHP_VERSION }}

      - name: Restore Composer package cache
        uses: actions/cache@v5
        with:
          path: |
            ~/.cache/composer/files
            ~/.cache/composer/vcs
          key: "composer-v2-cache-${{ hashFiles('./composer.lock') }}"
          restore-keys: |
            composer-v2-cache-

      - name: Install Composer dependencies
        run: composer install --no-dev --prefer-dist --no-interaction --ignore-platform-reqs

      - name: Calculate build number
        id: build-number
        run: |
          BUILD_NUMBER=$((2300+$GITHUB_RUN_NUMBER)) #to stay above jenkins
          echo "Build number: $BUILD_NUMBER"
          echo BUILD_NUMBER=$BUILD_NUMBER >> $GITHUB_OUTPUT

      - name: Minify BedrockData JSON files
        run: php vendor/pocketmine/bedrock-data/.minify_json.php

      - name: Build PocketMine-MP.phar
        run: php -dphar.readonly=0 build/server-phar.php --git ${{ github.sha }} --build ${{ steps.build-number.outputs.BUILD_NUMBER }}

      - name: Get PocketMine-MP release version
        id: get-pm-version
        run: |
          PM_VERSION=$(php build/dump-version-info.php base_version)
          echo PM_VERSION=$PM_VERSION >> $GITHUB_OUTPUT
          echo PM_MAJOR=$(php build/dump-version-info.php major_version) >> $GITHUB_OUTPUT
          echo MCPE_VERSION=$(php build/dump-version-info.php mcpe_version) >> $GITHUB_OUTPUT
          echo MCPE_DISPLAY_VERSION=$(php build/dump-version-info.php mcpe_display_version) >> $GITHUB_OUTPUT
          echo CHANGELOG_FILE_NAME=$(php build/dump-version-info.php changelog_file_name) >> $GITHUB_OUTPUT
          echo CHANGELOG_MD_HEADER=$(php build/dump-version-info.php changelog_md_header) >> $GITHUB_OUTPUT
          echo PRERELEASE=$(php build/dump-version-info.php prerelease) >> $GITHUB_OUTPUT

          if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
            tag="$(echo "${{ github.ref }}" | cut -d/ -f3-)"
          else
            tag="$PM_VERSION"
          fi
          echo TAG_NAME=$tag >> $GITHUB_OUTPUT

      - name: Generate PHP binary download URL
        id: php-binary-url
        run: |
          echo PHP_BINARY_URL="${{ github.server_url }}/${{ github.repository_owner }}/PHP-Binaries/releases/tag/pm${{ steps.get-pm-version.outputs.PM_MAJOR }}-php-${{ env.PHP_VERSION }}-latest" >> $GITHUB_OUTPUT

      - name: Generate build info
        run: |
          php build/generate-build-info-json.php \
            ${{ github.sha }} \
            ${{ steps.get-pm-version.outputs.TAG_NAME }} \
            ${{ github.repository }} \
            ${{ steps.build-number.outputs.BUILD_NUMBER }} \
            ${{ github.run_id }} \
            ${{ steps.php-binary-url.outputs.PHP_BINARY_URL }} \
            > build_info.json

      - name: Generate core permission doc for doc.pmmp.io
        run: php tools/generate-permission-doc.php rst

      - name: Upload release artifacts
        uses: actions/upload-artifact@v7
        with:
          name: release_artifacts
          path: |
            ${{ github.workspace }}/PocketMine-MP.phar
            ${{ github.workspace }}/start.*
            ${{ github.workspace }}/build_info.json
            ${{ github.workspace }}/core-permissions.rst

      - name: Create draft release
        uses: ncipollo/release-action@v1.21.0
        id: create-draft
        with:
          artifacts: ${{ github.workspace }}/PocketMine-MP.phar,${{ github.workspace }}/start.*,${{ github.workspace }}/build_info.json,${{ github.workspace }}/core-permissions.rst
          commit: ${{ github.sha }}
          draft: true
          prerelease: ${{ steps.get-pm-version.outputs.PRERELEASE }}
          name: PocketMine-MP ${{ steps.get-pm-version.outputs.PM_VERSION }}
          tag: ${{ steps.get-pm-version.outputs.TAG_NAME }}
          token: ${{ secrets.GITHUB_TOKEN }}
          body: |
            **For Minecraft: Bedrock Edition ${{ steps.get-pm-version.outputs.MCPE_VERSION }} (display version ${{ steps.get-pm-version.outputs.MCPE_DISPLAY_VERSION }})**

            Please see the [changelogs](${{ github.server_url }}/${{ github.repository }}/blob/${{ steps.get-pm-version.outputs.PM_VERSION }}/changelogs/${{ steps.get-pm-version.outputs.CHANGELOG_FILE_NAME }}#${{ steps.get-pm-version.outputs.CHANGELOG_MD_HEADER }}) for details.

            :information_source: Download the recommended PHP binary [here](${{ steps.php-binary-url.outputs.PHP_BINARY_URL }}).

            :warning: Found a bug? Report it on our [issue tracker](${{ github.server_url }}/${{ github.repository }}/issues). **We can't fix bugs if you don't report them.**

      - name: Post draft release URL on PR
        if: github.event_name == 'pull_request_target'
        uses: thollander/actions-comment-pull-request@v3
        with:
          message: "${{ vars.DRAFT_RELEASE_NOTIFICATION_MENTION }} [Draft release ${{ steps.get-pm-version.outputs.PM_VERSION }}](${{ steps.create-draft.outputs.html_url }}) has been created for commit ${{ github.sha }}. Please review and publish it."