name: Run mypy_primer

permissions: {}

on:
  pull_request:
    paths:
      - "crates/ty*/**"
      - "crates/ruff_db"
      - "crates/ruff_python_ast"
      - "crates/ruff_python_parser"
      - ".github/workflows/mypy_primer.yaml"
      - ".github/workflows/mypy_primer_comment.yaml"
      - "Cargo.lock"
      - "!**.md"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

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

jobs:
  mypy_primer:
    name: Run mypy_primer
    runs-on: depot-ubuntu-22.04-32
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          path: ruff
          fetch-depth: 0
          persist-credentials: false

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@bd01e18f51369d5a26f1651c3cb451d3417e3bba # v6.3.1

      - uses: Swatinem/rust-cache@98c8021b550208e191a6a3145459bfc9fb29c4c0 # v2.8.0
        with:
          workspaces: "ruff"

      - name: Install Rust toolchain
        run: rustup show

      - name: Run mypy_primer
        shell: bash
        env:
          PRIMER_SELECTOR: crates/ty_python_semantic/resources/primer/good.txt
          DIFF_FILE: mypy_primer.diff
        run: |
          cd ruff
          scripts/mypy_primer.sh
          echo ${{ github.event.number }} > ../pr-number

      - name: Upload diff
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: mypy_primer_diff
          path: mypy_primer.diff

      - name: Upload pr-number
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: pr-number
          path: pr-number

  memory_usage:
    name: Run memory statistics
    runs-on: depot-ubuntu-22.04-32
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          path: ruff
          fetch-depth: 0
          persist-credentials: false

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@bd01e18f51369d5a26f1651c3cb451d3417e3bba # v6.3.1

      - uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8
        with:
          workspaces: "ruff"

      - name: Install Rust toolchain
        run: rustup show

      - name: Run mypy_primer
        shell: bash
        env:
          TY_MAX_PARALLELISM: 1 # for deterministic memory numbers
          TY_MEMORY_REPORT: mypy_primer
          PRIMER_SELECTOR: crates/ty_python_semantic/resources/primer/memory.txt
          DIFF_FILE: mypy_primer_memory.diff
        run: |
          cd ruff
          scripts/mypy_primer.sh

      - name: Upload diff
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: mypy_primer_memory_diff
          path: mypy_primer_memory.diff
