# Author: Nishant Gaurav
# Note: Do not change

name: Release Genesis to NPM
on:
  workflow_dispatch:
    inputs:
      release-type:
        description: 'Select Release type (prerelease appends beta in build number)'
        type: choice
        options:
          - patch
          - minor
          - major
          - prerelease
        required: true
jobs:
  release:
    runs-on: ubuntu-latest
    # Check access controls on release and tagging
    steps:
      - name: Check Permissions
        id: check-permissions
        env:
          ALLOWED_USERS: superrover, Anant-Raj
        if: ${{ !contains(env.ALLOWED_USERS, github.actor) }}
        run: |
          echo "You don't have correct permissions to do this release"
          exit 1

      # Checkout project repository
      # Added fetch-depth to fetch complete history of repo. TODO(Nishant): Customize?
      # TODO(Nishant): update with a org bot token instead of personal token
      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.NPM_PUBLISH_USER_ACCESS_TOKEN }}
          fetch-depth: '0'

      # Setup Node.js environment
      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          registry-url: https://registry.npmjs.org/
          node-version: '16'

      # Install dependencies (required by Run tests step)
      - name: Install dependencies
        run: yarn install

      # ToDo(Nishant): Activate this once we have chromatic tests ready.
      #- name: Run tests
      #  run: yarn test

      # Configure Git. TODO(Nishant): update with a org bot instead of personal detail
      - name: Git configuration
        run: |
          git config --global user.email "86281150+superrover@users.noreply.github.com"
          git config --global user.name "Nishant Gaurav"
          git fetch --all
          git checkout master
          git pull origin master

      # Bump package version
      # Use tag latest
      - name: Bump release version
        if: startsWith(github.event.inputs.release-type, 'pre') != true
        run: |
          yarn build:changelog --release-as $RELEASE_TYPE
          echo "RELEASE_TAG=latest" >> $GITHUB_ENV
        env:
          RELEASE_TYPE: ${{ github.event.inputs.release-type }}

      # Bump package pre-release version
      # Use tag beta for pre-release versions
      - name: Bump pre-release version
        if: startsWith(github.event.inputs.release-type, 'pre')
        run: |
          yarn build:changelog --$RELEASE_TYPE beta
          echo "RELEASE_TAG=beta" >> $GITHUB_ENV
        env:
          RELEASE_TYPE: ${{ github.event.inputs.release-type }}

      # Push changes
      - name: Push CHANGELOG.md and created release tag
        run: |
          git push --follow-tags origin master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # Publish version to public repository
      - name: Publish
        run: yarn publish --verbose --access public --tag ${{ env.RELEASE_TAG }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
