name: Nightly tests

on:
  workflow_dispatch:
  schedule:
    - cron: "0 10 * * *"

permissions:
  contents: read

jobs:
  test-linux:
    if: github.repository == 'nodejs/undici'
    uses: ./.github/workflows/test.yml
    with:
      node-version: 23-nightly
      runs-on: ubuntu-latest
    secrets: inherit

  test-windows:
    if: github.repository == 'nodejs/undici'
    uses: ./.github/workflows/test.yml
    with:
      node-version: 23-nightly
      runs-on: windows-latest
    secrets: inherit

  test-macos:
    if: github.repository == 'nodejs/undici'
    uses: ./.github/workflows/test.yml
    with:
      node-version: 23-nightly
      runs-on: macos-latest
    secrets: inherit

  report-failure:
    if: ${{ always() && (needs.test-linux.result == 'failure' && needs.test-windows.result == 'failure' && needs.test-macos.result == 'failure') }}
    needs:
      - test-linux
      - test-windows
      - test-macos
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Create or update issue
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            const ISSUE_TITLE = "Nightly tests are failing"

            const actionRunUrl = "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"

            const issueContext = {
              owner: context.repo.owner,
              repo: context.repo.repo
            }

            let issue = (await github.rest.issues.listForRepo({
              state: "open",
              creator: "github-actions[bot]",
              ...issueContext
            })).data.find((issue) => issue.title === ISSUE_TITLE)

            if(!issue) {
              issue = (await github.rest.issues.create({
                title: ISSUE_TITLE,
                ...issueContext
              })).data
            }

            await github.rest.issues.createComment({
              issue_number: issue.number,
              body: `Tests against nightly failed, see: ${actionRunUrl}`,
              ...issueContext
            });
