name: Create Pull Request
on:
  issue_comment:
    types: [ created ]

# require public member
# private member is treated as CONTRIBUTOR
permissions:
  contents: write # for checkout and commit
  pull-requests: write  # for create pr

jobs:
  createPullRequest:
    if: |
      github.event_name == 'issue_comment' &&
      (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') &&
      startsWith(github.event.comment.body, '/create release')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: 'https://npm.pkg.github.com'
      - name: Install
        run: yarn install
      - name: Git Identity
        run: |
          git config --global user.name '${GITHUB_ACTOR}'
          git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_ACTOR: ${{ github.actor }}
      - name: Versionup commit
        run: |
          yarn run versionup --yes
      - name: Set current CHANGELOG to output
        id: changelog
        shell: bash -ex {0}
        run: |
          version=$(node -p 'require("./lerna.json").version')
          echo "::set-output name=version::${version}"
      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "Update v${{ steps.changelog.outputs.version }}"
          committer: GitHub <noreply@github.com>
          author: "${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>"
          title: 'v${{ steps.changelog.outputs.version }}'
          body: |
            ## v${{ steps.changelog.outputs.version }}

            ### Breaking Changes

            - [ ] TODO

            ### Features

            - [ ] TODO

            ### Bug Fixes

            - [ ] TODO
          labels: "Type: Release"
          branch: "release/next"
          request-to-parent: false
      - name: Check outputs
        run: |
          echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
          echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
