name: Daily property test run

on:
  workflow_dispatch:
  schedule:
    - cron: "0 12 * * *"
  pull_request:
    paths:
      - ".github/workflows/daily_property_tests.yml"

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_TERM_COLOR: always
  RUSTUP_MAX_RETRIES: 10
  FORCE_COLOR: 1

jobs:
  property_tests:
    name: Property tests
    runs-on: ubuntu-latest
    timeout-minutes: 20
    # Don't run the cron job on forks:
    if: ${{ github.repository == 'astral-sh/ty' || github.event_name != 'schedule' }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
          repository: astral-sh/ruff
      - name: "Install Rust toolchain"
        run: rustup show
      - name: "Install mold"
        uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
      - name: Build ty
        # A release build takes longer (2 min vs 1 min), but the property tests run much faster in release
        # mode (1.5 min vs 14 min), so the overall time is shorter with a release build.
        run: cargo build --locked --release --package ty_python_semantic --tests
      - name: Run property tests
        shell: bash
        run: |
          export QUICKCHECK_TESTS=100000
          for _ in {1..5}; do
            cargo test --locked --release --package ty_python_semantic -- --ignored list::property_tests
            cargo test --locked --release --package ty_python_semantic -- --ignored types::property_tests::stable
          done

  create-issue-on-failure:
    name: Create an issue if the daily property test run surfaced any bugs
    runs-on: ubuntu-latest
    needs: property_tests
    if: ${{ github.repository == 'astral-sh/ty' && always() && github.event_name == 'schedule' && needs.property_tests.result == 'failure' }}
    permissions:
      issues: write
    steps:
      - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            await github.rest.issues.create({
              owner: "astral-sh",
              repo: "ty",
              title: `Daily property test run failed on ${new Date().toDateString()}`,
              body: "Run listed here: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
              labels: ["bug", "type properties"],
            })
