name: Main Checks

on:
  workflow_dispatch:
  push:
    branches:
      - main

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Automatically cancel in-progress actions on the same branch
concurrency:
  group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:
  congrats:
    name: congratsbot
    if: ${{ github.repository_owner == 'withastro' && github.event.head_commit.message != '[ci] format' }}
    uses: withastro/automation/.github/workflows/congratsbot.yml@main
    with:
      EMOJIS: '🎉,🎊,🧑‍🚀,🥳,🙌,🚀,👏,<:houston_golden:1068575433647456447>,<:astrocoin:894990669515489301>,<:astro_pride:1130501345326157854>'
    secrets:
      DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_CONGRATS }}

  check_for_update:
    name: Check for Updates
    runs-on: ubuntu-latest
    outputs:
      run_job: ${{ steps.check_files.outputs.run_job }}
    steps:
      - uses: actions/checkout@v4

      - name: Setup PNPM
        uses: pnpm/action-setup@v2

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install

      - name: Check Modified
        run: pnpm exec changeset status --output ./status.json

      - name: Check Output
        id: check_files
        run: |
          output=`echo $(cat status.json)`
          if [[ $output = '{ "changesets": [], "releases": [] }' ]]
          then
            echo 'No changeset found'
            echo "run_job=true" >> $GITHUB_OUTPUT
          else
            echo 'changes found, push to latest skipped'
            echo "run_job=false" >> $GITHUB_OUTPUT
          fi

  update:
    name: Update the latest branch
    needs: check_for_update
    if: needs.check_for_update.outputs.run_job == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: latest
