name: Generate Release Draft

on:
  pull_request:
    types:
      - closed
    branches:
      - main

jobs:
  check_pr_title:
    if: >
      github.event.pull_request.merged == true &&
      github.event.pull_request.base.ref == 'main'
    runs-on: ubuntu-latest
    outputs:
      version_pr: ${{ steps.check.outputs.version_pr }}
    steps:
      - name: Check if PR title is version
        id: check
        run: |
          if [[ "${{ github.event.pull_request.title }}" =~ ^([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
            echo "::set-output name=version_pr::true"
          else
            echo "::set-output name=version_pr::false"
          fi

  generate_release_draft:
    needs: check_pr_title
    if:
      needs.check_pr_title.outputs.version_pr == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Generate Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.event.pull_request.title }}
          name: 🌟${{ github.event.pull_request.title }}
          body: ${{ github.event.pull_request.body }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
