name: Monitor self-hosted runners
on:
  schedule:
    # Every weekday at 12:00pm
    - cron: "0 12 * * 1-5"
  workflow_dispatch:

jobs:
  check:
    name: Monitor and send alert
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Get self-hosted runner status
        id: monitor
        continue-on-error: true
        run: |
          COUNT=$(node .github/workflows/utils/get-self-hosted-runner-status.js online)
          echo "Online self-hosted runners: $COUNT"

          # If all runners are online, return success
          if [[ "$COUNT" =~ ^All[[:space:]]\([[:digit:]]+\/[[:digit:]]+\)$ ]]; then exit 0; fi

          # Else, set outputs and fail
          DETAIL=$(node .github/workflows/utils/get-self-hosted-runner-status.js)
          echo "Detailed status: $DETAIL"
          echo "COUNT=$COUNT" >> $GITHUB_ENV
          echo "DETAIL<<EOF" >> $GITHUB_ENV
          echo "$DETAIL" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
          exit 1

      - name: Send alert by mail if needed
        uses: dawidd6/action-send-mail@v3
        if: steps.monitor.outcome == 'failure' && github.event_name == 'schedule'
        with:
          server_address: smtp.gmail.com
          server_port: 465
          username: ${{secrets.PNJ_MAIL_USERNAME}}
          password: ${{secrets.PNJ_MAIL_PASSWORD}}
          subject: Self-Hosted Offline Alert
          to: infra@berty.tech
          from: Github Actions
          priority: high
          body: |
            Online self-hosted runners: ${{ env.COUNT }}
            Detail:
              ${{ env.DETAIL }}
