name: Daily test

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

# Please keep the permissions minimal, as stubtest runs arbitrary code from pypi.
permissions:
  contents: read

env:
  PIP_DISABLE_PIP_VERSION_CHECK: 1

jobs:
  stubtest-stdlib:
    name: Check stdlib with stubtest
    if: github.repository == 'python/typeshed'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ["ubuntu-latest", "windows-latest", "macos-latest"]
        python-version: ["3.7", "3.8", "3.9", "3.10", "3.11-dev"]
      fail-fast: false

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }} on ${{ matrix.os }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Update pip
        run: python -m pip install -U pip
      - name: Install dependencies
        run: pip install $(grep tomli== requirements-tests.txt) $(grep mypy== requirements-tests.txt)
      - name: Run stubtest
        run: python tests/stubtest_stdlib.py

  stubtest-third-party:
    name: Check third party stubs with stubtest
    if: github.repository == 'python/typeshed'
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        shard-index: [0, 1, 2, 3]
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.9"
      - name: Install dependencies
        run: pip install $(grep tomli== requirements-tests.txt) termcolor
      - name: Install apt packages
        run: |
          sudo apt update
          sudo apt install -y $(python tests/get_apt_packages.py)
      - name: Run stubtest
        run: python tests/stubtest_third_party.py --num-shards 4 --shard-index ${{ matrix.shard-index }}

  # https://github.community/t/run-github-actions-job-only-if-previous-job-has-failed/174786/2
  create-issue-on-failure:
    name: Create an issue if stubtest failed
    runs-on: ubuntu-latest
    needs: [stubtest-stdlib, stubtest-third-party]
    if: ${{ github.repository == 'python/typeshed' && always() && (needs.stubtest-stdlib.result == 'failure' || needs.stubtest-third-party.result == 'failure') }}
    permissions:
      issues: write
    steps:
      - uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            await github.rest.issues.create({
              owner: "python",
              repo: "typeshed",
              title: `Stubtest failed on ${new Date().toDateString()}`,
              body: "Stubtest runs are listed here: https://github.com/python/typeshed/actions/workflows/daily.yml",
            })
