name: Lint

on:
  pull_request:
  workflow_call:

concurrency:
  group: lint-${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_number || github.ref }}
  cancel-in-progress: true

defaults:
  run:
    shell: bash -l -eo pipefail {0}

jobs:
  lint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.11']
    steps:
      - name: Check out repo
        uses: actions/checkout@v7

      - name: Setup python
        uses: actions/setup-python@v7
        with:
          python-version: ${{ matrix.python-version }}

      - name: Update pip
        run: python -m pip install --upgrade pip

      - name: Install Lychee (v0.24.1)
        run: |
          VERSION="v0.24.1"
          echo "Installing Lychee version: $VERSION"

          URL="https://github.com/lycheeverse/lychee/releases/download/lychee-${VERSION}/lychee-x86_64-unknown-linux-gnu.tar.gz"
          FILENAME="lychee-${VERSION}.tar.gz"

          # Load the archive, extract into temp folder, move the binary, delete the rest
          mkdir -p lychee_install
          curl -sL "$URL" -o "$FILENAME"
          tar -xzf "$FILENAME" -C lychee_install --strip-components=1
          sudo mv lychee_install/lychee /usr/local/bin/
          rm -rf lychee_install "$FILENAME"

          # Verify installation
          echo "Installed Lychee version: $(lychee --version)"

      - name: Restore Lychee cache
        uses: actions/cache@v6
        with:
          path: .lycheecache
          key: cache-lychee-${{ github.sha }}
          restore-keys: cache-lychee-

      - name: Install dependencies and lint utilities
        run: |
          python -m pip install -r requirements.txt -r requirements-dev.txt
          python -m pip install --force-reinstall --pre --index-url https://download.pytorch.org/whl/nightly/cu132 torch
          # pyrefly type-checks torchtitan/components/checkpointer/torch_checkpointing.py,
          # so the library has to be importable here. --no-deps, and after the
          # nightly install above: torch_checkpointing pins torch>=2.6.0, which
          # would otherwise pull a stable wheel over the nightly. Mirrors the
          # install in .ci/docker/common/install_conda.sh.
          python -m pip install --no-deps "git+https://github.com/meta-pytorch/torch_checkpointing.git@main"
          pre-commit install-hooks

      - name: Get changed files
        id: changed-files
        if: github.event_name == 'pull_request'
        uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v45.0.6

      - name: Lint modified files
        if: github.event_name == 'pull_request'
        env:
          # Lychee will checks all links in .md and .py files in the next step
          SKIP: lychee-link-checker
        run: pre-commit run --show-diff-on-failure --files ${{ steps.changed-files.outputs.all_changed_files }}

      - name: Lint all files
        if: github.event_name != 'pull_request'
        env:
          # Lychee checks all links in the next step.
          SKIP: lychee-link-checker
        run: pre-commit run --show-diff-on-failure --all-files

      - name: Check links with Lychee
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          pre-commit run lychee-link-checker --all-files
