name: Update Browser Versions
on:
  schedule:
    - cron: '0 8 * * *' #  every day at 8am UTC (3/4am EST/EDT)
jobs:
  update-browser-versions:
    runs-on: ubuntu-latest
    env:
      CYPRESS_BOT_APP_ID: ${{ secrets.CYPRESS_BOT_APP_ID }}
      BASE_BRANCH: develop
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          token: ${{ secrets.DEVELOP_PUSH_TOKEN }}
      - name: Set committer info
        ## attribute the commit to cypress-bot: https://github.community/t/logging-into-git-as-a-github-app/115916
        run: |
          git config --local user.email "${{ env.CYPRESS_BOT_APP_ID }}+cypress-bot[bot]@users.noreply.github.com"
          git config --local user.name "cypress-bot[bot]"
      - name: Checkout base branch
        run: |
          git fetch origin
          git checkout ${{ env.BASE_BRANCH }}
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14
      - name: Check for new Chrome versions
        id: get-versions
        uses: actions/github-script@v4
        with:
          script: |
            const { getVersions } = require('./scripts/github-actions/update-browser-versions.js')

            getVersions({ core })
      - name: Determine name of new branch and if it already exists
        id: check-branch
        env:
          BRANCH_NAME: update-chrome-stable-from-${{ steps.get-versions.outputs.current_stable_version }}-beta-from-${{ steps.get-versions.outputs.current_beta_version }}
        run: |
          echo "::set-output name=branch_name::${{ env.BRANCH_NAME }}"
          echo "::set-output name=branch_exists::$(git show-ref --verify --quiet refs/remotes/origin/${{ env.BRANCH_NAME }} && echo 'true')"
      - name: Check need for PR or branch update
        id: check-need-for-pr
        run: |
          echo "::set-output name=needs_pr::${{ steps.get-versions.outputs.has_update == 'true' && steps.check-branch.outputs.branch_exists != 'true' }}"
          echo "::set-output name=needs_branch_update::${{ steps.get-versions.outputs.has_update == 'true' && steps.check-branch.outputs.branch_exists == 'true' }}"
      ## Update available and a branch/PR already exists
      - name: Checkout existing branch
        if: ${{ steps.check-need-for-pr.outputs.needs_branch_update == 'true' }}
        run: git checkout ${{ steps.check-branch.outputs.branch_name }}
      - name: Check need for update on existing branch
        if: ${{ steps.check-need-for-pr.outputs.needs_branch_update == 'true' }}
        id: check-need-for-branch-update
        uses: actions/github-script@v4
        with:
          script: |
            const { checkNeedForBranchUpdate } = require('./scripts/github-actions/update-browser-versions.js')

            checkNeedForBranchUpdate({
              core,
              latestStableVersion: '${{ steps.get-versions.outputs.latest_stable_version }}',
              latestBetaVersion: '${{ steps.get-versions.outputs.latest_beta_version }}',
            })
      ## Update available and a PR doesn't already exist
      - name: Checkout new branch
        if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' }}
        run: git checkout -b ${{ steps.check-branch.outputs.branch_name }} ${{ env.BASE_BRANCH }}
      ## Both
      - name: Update Browser Versions File
        if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' || steps.check-need-for-branch-update.outputs.has_newer_update == 'true' }}
        uses: actions/github-script@v4
        with:
          script: |
            const { updateBrowserVersionsFile } = require('./scripts/github-actions/update-browser-versions.js')

            updateBrowserVersionsFile({
              latestStableVersion: '${{ steps.get-versions.outputs.latest_stable_version }}',
              latestBetaVersion: '${{ steps.get-versions.outputs.latest_beta_version }}',
            })
      - name: Commit the changes
        if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' || steps.check-need-for-branch-update.outputs.has_newer_update == 'true' }}
        run: |
          git commit -am "chore: ${{ steps.get-versions.outputs.description }}"
      - name: Push branch to remote
        if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' || steps.check-need-for-branch-update.outputs.has_newer_update == 'true' }}
        run: git push origin ${{ steps.check-branch.outputs.branch_name }}
      ## Update available and a branch/PR already exists
      - name: Update PR Title
        if: ${{ steps.check-need-for-pr.outputs.needs_branch_update == 'true' }}
        uses: actions/github-script@v4
        with:
          script: |
            const { updatePRTitle } = require('./scripts/github-actions/update-browser-versions.js')

            await updatePRTitle({
              context,
              github,
              baseBranch: '${{ env.BASE_BRANCH }}',
              branchName: '${{ steps.check-branch.outputs.branch_name }}',
              description: '${{ steps.get-versions.outputs.description }}',
            })
      # Update available and a PR doesn't already exist
      - name: Create Pull Request
        if: ${{ steps.check-need-for-pr.outputs.needs_pr == 'true' }}
        uses: actions/github-script@v4
        with:
          script: |
            const { createPullRequest } = require('./scripts/github-actions/update-browser-versions.js')

            await createPullRequest({
              context,
              github,
              baseBranch: '${{ env.BASE_BRANCH }}',
              branchName: '${{ steps.check-branch.outputs.branch_name }}',
              description: '${{ steps.get-versions.outputs.description }}',
            })
