name: Release Start

on:
  workflow_dispatch:
    inputs:
      channel:
        required: true
        type: choice
        description: Channel of the release (alpha/beta/stable)
        options:
        - alpha
        - beta
        - stable
      version:
        required: true
        description: version of the release (e.g. 2022.1.0)
      iteration:
        required: false
        description: iteration of the release, used for non-stable releases (version-channel.iteration) (e.g. 0, 1, 2, 3 ...)
        default: '0'
      major-npm:
        required: false
        type: boolean
        description: increment the major version of the NPM packages
        default: false

env:
  RELEASE_VERSION: ${{ github.event.inputs.version }}${{ github.event.inputs.channel != 'stable' && format('-{0}.{1}', github.event.inputs.channel, github.event.inputs.iteration) || '' }}
  RELEASE_BRANCH: release/${{ github.event.inputs.version }}${{ github.event.inputs.channel != 'stable' && format('-{0}.{1}', github.event.inputs.channel, github.event.inputs.iteration) || '' }}

jobs:
  setup-release-branch:
    runs-on: ubuntu-latest
    steps:
      - name: Create Branch # Create a branch if it doesn't exist
        uses: peterjgrainger/action-create-branch@v2.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          branch: ${{ env.RELEASE_BRANCH }}

      - name: Checkout branch # Check out the new branch
        uses: actions/checkout@v3
        with:
          ref: ${{ env.RELEASE_BRANCH }}
          persist-credentials: false

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'

      - name: Configure Git user
        id: configure_git_user
        uses: Homebrew/actions/git-user-config@master
        with:
          username: ${{ (github.event_name == 'workflow_dispatch' && github.actor) || 'insomnia-infra' }}

      - name: Run bootstrap
        run: npm run bootstrap

      - name: Lerna version (stable)
        if: github.event.inputs.channel == 'stable'
        run: npm run version -- --yes "${{ github.event.inputs.major-npm != 'true' && 'minor' || 'major' }}"

      - name: Lerna version (alpha/beta)
        if: github.event.inputs.channel != 'stable'
        run: npm run version -- --yes --preid "${{ github.event.inputs.channel }}" "${{ github.event.inputs.iteration == '0' && (github.event.inputs.major-npm != 'true' && 'preminor' || 'premajor') || 'prerelease' }}"

      - name: Config version
        run: APP_VERSION="${{ env.RELEASE_VERSION }}" npm run app-bump-version

      # https://github.com/lerna/lerna/issues/2832
      - name: Update package-lock.json files
        run: npx lerna clean --yes && npx lerna bootstrap --no-ci --ignore-scripts -- --package-lock-only --no-audit

      - name: Commit
        run: git commit -am "Bump app version to ${{ env.RELEASE_VERSION }} and lerna package versions"

      - name: Push changes
        run: |
          remote_repo="https://${GITHUB_ACTOR}:${RELEASE_GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
          git push "${remote_repo}" --follow-tags
        env:
          RELEASE_GH_TOKEN: ${{ secrets.RELEASE_GH_TOKEN }}

  create-pull-request:
    needs: setup-release-branch
    runs-on: ubuntu-latest
    steps:
      - name: Checkout branch # Check out the new branch
        uses: actions/checkout@v3
        with:
          ref: ${{ env.RELEASE_BRANCH }}
          fetch-depth: 0

      - name: Run the Action
        uses: devops-infra/action-pull-request@v0.4.2
        with:
          github_token: ${{ secrets.RELEASE_GH_TOKEN }}
          source_branch: ${{ env.RELEASE_BRANCH}}
          target_branch: develop
          title: ":rocket: ${{ env.RELEASE_VERSION}}"
          body: |
            **Automated pull request**
            Artifacts build in progress...
          draft: false
