name: PR Workflow

on:
  pull_request:
    types: [opened, synchronize, ready_for_review, edited, closed]
    branches:
      - main
      # - next

jobs:
  build:
    name: Build, Lint and Test
    runs-on: ubuntu-latest
    steps:
      - name: Get Yarn cache path
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Checkout branch
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Load Yarn cache
        uses: actions/cache@v3
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install dependencies
        run: yarn install --frozen-lockfile --ignore-engines
        env:
          CI: true

      - name: Build packages
        run: yarn build

      - name: Lint types and code
        run: yarn lint

      - name: Run tests
        run: yarn test:ci

  release:
    name: Release Snapshot
    if: ${{ false }} # disable for now. See https://github.com/chakra-ui/chakra-ui/issues/5603#issuecomment-1043351537
    runs-on: ubuntu-latest
    steps:
      - name: Get Yarn cache path
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Checkout Repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Load Yarn cache
        uses: actions/cache@v3
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install dependencies
        run: yarn install --frozen-lockfile --ignore-engines

      - name: Build packages
        run: yarn build

      - name: Build prop docs
        run: yarn build:prop-docs

      - name: Configure npm
        run: |
          cat << EOF > "$HOME/.npmrc"
            //registry.npmjs.org/:_authToken=$NPM_TOKEN
          EOF
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Check if PR author is Org member
        id: membercheck
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            let member
            try {
              const response = await github.rest.repos.checkCollaborator({
                username: context.actor,
                owner: 'chakra-ui',
                repo: 'chakra-ui',
              })
              member = response.data === undefined
              console.log({ actor: context.actor, isMember: member })
            } catch {
              member = false
            }
            return member

      - name: Release to @pr channel
        if: steps.membercheck.outputs.result == 'true'
        run: |
          yarn changeset version --snapshot pr
          yarn changeset publish --tag pr
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Get released version
        id: version
        if: steps.membercheck.outputs.result == 'true'
        run:
          echo ::set-output name=version::$(node -p
          "require('./packages/react/package.json').version")

      - name: Create comment
        if: steps.membercheck.outputs.result == 'true'
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          header: "pr-release"
          message: |
            #### :package: A new release has been made for this pull request

            To play around with this release, you can install `@chakra-ui/react@${{ steps.version.outputs.version }}` in your project

            > Latest commit: ${{ github.event.pull_request.head.sha }}
