name: Update Changelog RC

on:
  push:
    paths:
      - ".changeset/**"
    branches:
      - main
      # - next

jobs:
  daily:
    if: ${{ 'chakra-ui/chakra-ui' == github.repository }}
    runs-on: ubuntu-latest
    steps:
      - name: Cancel previous runs
        uses: styfle/cancel-workflow-action@0.9.1
        with:
          access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get Yarn cache path
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Checkout
        uses: actions/checkout@master
        with:
          ref: main

      - name: Enable node
        uses: actions/setup-node@master
        with:
          node-version: 14.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 && yarn bootstrap

      - name: Setup Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Generate the new changelog
        run: yarn changelog:gen

      - name: Generate Twitter Post
        run: yarn twitter:gen

      - name: Commit changelog
        run: |
          git add .
          git diff --staged --quiet || git commit -m "docs(changelog): $GITHUB_SHA"

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          delete-branch: true
          title: "docs: update .changelogrc"
          branch: docs/changelog-updates
