name: Make new pre-release (beta)
on: workflow_dispatch
jobs:
  createReleasePR:
    name: Create PR for release
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      RELEASE_BRANCH: "release/beta"

    steps:
      # Setup release branch
      - name: Delete existing branch
        uses: dawidd6/action-delete-branch@v3
        with:
          github_token: ${{ env.GITHUB_TOKEN }}
          branches: ${{ env.RELEASE_BRANCH }}
        continue-on-error: true

      - name: Create release branch
        uses: peterjgrainger/action-create-branch@v2.2.0
        with:
          branch: ${{ env.RELEASE_BRANCH }}

      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: ${{ env.RELEASE_BRANCH }}

      # Push commit from release-it: version bump, changelog, build
      - uses: actions/setup-node@v2

      - name: NPM authentication
        run: npm config set //registry.npmjs.org/:_authToken ${{ secrets.NPM_TOKEN }}

      - name: git config
        run: |
          git config user.name "${GITHUB_ACTOR}"
          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

      - name: Install packages
        run: npm ci

      - name: Bump version, build
        run: |
          npm run version-release:beta

      # Create PR for release branch
      - name: Create Pull Request
        id: cpr
        uses: repo-sync/pull-request@v2
        with:
          destination_branch: "beta"
          source_branch: ${{ env.RELEASE_BRANCH }}
          github_token: ${{ env.GITHUB_TOKEN }}
          pr_draft: false
          pr_label: release
          pr_title: "chore(release): publish beta"
          pr_body: |
            🤖 **Preparing to release as [beta]**

      - name: Show PR details
        run: |
          echo ${{steps.cpr.outputs.pr_url}}
          echo ${{steps.open-pr.outputs.pr_number}}
          echo ${{steps.open-pr.outputs.has_changed_files}}
