# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This workflow handles linting, formatting, and static analysis checks for the codebase.
name: Quality

on:
  # Allows running this workflow manually from the Actions tab
  workflow_dispatch:

  # Triggers the workflow on push events to main
  push:
    branches:
      - main

  # Triggers the workflow on pull request events targeting main
  pull_request:
    branches:
      - main

# Ensures that only the latest commit for a PR or branch is built, canceling older runs.
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions: {}

jobs:
  # This job runs pre-commit hooks to check code style and formatting.
  pre-commit-checks:
    permissions:
      contents: read
    name: Run Pre-commit Hooks (Lint, Format & Static Analysis)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1
        with:
          persist-credentials: false

      - name: Set up Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97  # v6
        with:
          python-version: '3.12'

      - name: Run pre-commit hooks
        uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd  # v3.0.1
        with:
          extra_args: --all-files --show-diff-on-failure --color=always

  # This job runs the examples in our docstrings and validates the doctest allowlist.
  # See docs/source/writing_docstrings.mdx for the standard these enforce.
  doc-checks:
    permissions:
      contents: read
    name: Run Documentation Checks (Doctests)
    runs-on: ubuntu-latest
    env:
      # Examples that need a physical robot, a serial port or a Hub download are skipped by content.
      # Everything else has to actually run. See src/lerobot/utils/doctest_utils.py.
      SKIP_HARDWARE_DOCTEST: "1"
      SKIP_CUDA_DOCTEST: "1"
    steps:
      - name: Checkout code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1
        with:
          persist-credentials: false

      - name: Setup uv and Python
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990  # v8.3.2
        with:
          enable-cache: true
          version: "0.11.30"
          python-version: "3.12"

      - name: Install dependencies
        run: uv sync --locked --extra test --extra dataset

      - name: Check the doctest list is sorted and its paths exist
        run: make check-doctest-list

      - name: Check documented arguments match their signatures
        run: make check-docstrings

      - name: Check docstring coverage has not regressed
        run: uv run --with interrogate interrogate --config=pyproject.toml

      - name: Run doctests
        run: make doctest
