diff --git a/.github/workflows/check-precommit-applied.yml b/.github/workflows/check-precommit-applied.yml new file mode 100644 index 00000000..521faf66 --- /dev/null +++ b/.github/workflows/check-precommit-applied.yml @@ -0,0 +1,96 @@ +name: pre-commit + +on: + pull_request: + push: + branches: + - main + - develop + +jobs: + pre-commit: + runs-on: ubuntu-latest + + steps: + # Checkout full repo history to ensure diff calculation works + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v4 + with: + python-version: '3.12' + + # Cache pre-commit environments + - name: Cache pre-commit hooks + uses: actions/cache@v4 + with: + path: ~/.cache/pre-commit + key: pre-commit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }} + restore-keys: | + pre-commit-${{ runner.os }}- + + - name: Install pre-commit + run: pip install pre-commit + + - name: Run pre-commit on diff + id: run_precommit + env: + EVENT_NAME: ${{ github.event_name }} + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.sha }} + BEFORE_SHA: ${{ github.event.before }} + run: | + set -e + + echo "🔍 Determining diff range..." + + if [ "$EVENT_NAME" = "pull_request" ]; then + FROM_REF="$BASE_SHA" + TO_REF="$HEAD_SHA" + echo "PR event: checking diff from $FROM_REF to $TO_REF" + elif [ -n "$BEFORE_SHA" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then + FROM_REF="$BEFORE_SHA" + TO_REF="$HEAD_SHA" + echo "Push event: checking diff from $FROM_REF to $TO_REF" + else + # Edge case: unknown base; check all files + echo "⚠️ Unable to determine diff automatically — running on all files." + set +e + pre-commit run --all-files + EXIT_CODE=$? + set -e + echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT + exit 0 + fi + + # Run pre-commit on diff + set +e + pre-commit run --from-ref "$FROM_REF" --to-ref "$TO_REF" + EXIT_CODE=$? + set -e + + echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT + + - name: Check for uncommitted changes from pre-commit + run: | + if ! git diff --quiet; then + echo "❌ pre-commit hooks modified files." + echo " Please run the following locally:" + echo "" + echo " pre-commit install" + echo " pre-commit run --all-files" + echo "" + echo " Then commit and push the changes." + echo "" + git diff --color + exit 1 + fi + + echo "✔️ No changes detected. pre-commit checks passed." + + - name: Fail if hooks returned errors + if: steps.run_precommit.outputs.exit_code != '0' + run: | + echo "❌ pre-commit hooks reported issues." + exit 1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cb03c2c6..7b8588c6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,18 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - # Ruff version. rev: v0.12.10 hooks: - # Run the linter. + # Run the linter (fix issues automatically). - id: ruff-check - args: [ --fix ] + args: [--fix] + pass_filenames: true + stages: [commit] + # Run the formatter. - id: ruff-format - + pass_filenames: true + stages: [commit] + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: @@ -16,7 +20,10 @@ repos: - id: check-case-conflict - id: end-of-file-fixer - id: trailing-whitespace + - repo: https://github.com/kynan/nbstripout rev: 0.8.1 hooks: - id: nbstripout + pass_filenames: true + stages: [commit] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5a5018bd..092d2e7e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,13 +99,24 @@ pip install pytest pytest-cov black flake8 ### Submitting a Pull Request -1. **Push your branch** to your fork: +1. **Ensure pre-commit checks pass** (Important!) + ```bash + # Run pre-commit on all files + pre-commit run --all-files + + # Or run on specific files + pre-commit run --files path/to/your/files + ``` + + The pre-commit checks will automatically run in CI, but running them locally first saves time. + +2. **Push your branch** to your fork: ```bash git push origin feature/your-feature-name ``` 2. **Open a Pull Request** on GitHub: - - **Target branch**: + - **Target branch**: - Use `develop` for new features and non-critical bug fixes - Use `main` only for hotfixes or critical patches to the current stable release - Most contributions should target the `develop` branch diff --git a/workflow/envs/environment.yaml b/workflow/envs/environment.yaml index 6535e3f1..402b078f 100644 --- a/workflow/envs/environment.yaml +++ b/workflow/envs/environment.yaml @@ -74,3 +74,4 @@ dependencies: - mkdocs-awesome-nav - mike>=1.1.0 - python-markdown-math + - pre-commit>=4.0.0