Pre-Commit Install
Create .pre-commit-config.yaml at base of repo
Section titled “Create .pre-commit-config.yaml at base of repo”repos: - repo: https://github.com/psf/black rev: 24.10.0 hooks: - id: black args: [--line-length=120]
- repo: https://github.com/PyCQA/flake8 rev: 7.1.1 hooks: - id: flake8 args: [--max-line-length=120] additional_dependencies: - flake8-bugbear
- repo: https://github.com/adrienverge/yamllint rev: v1.35.1 hooks: - id: yamllint args: [--strict]
- repo: https://github.com/rstcheck/rstcheck rev: v6.2.4 hooks: - id: rstcheck name: Check reStructuredText files entry: rstcheck language: python types: [rst] require_serial: true
- repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-json - id: check-added-large-files - id: fix-encoding-pragma - id: debug-statements - id: check-merge-conflict - id: mixed-line-ending - id: forbid-new-submodules - id: check-executables-have-shebangs - id: check-yamlUpdate versions
Section titled “Update versions”pre-commit autoupdateInstall hooks
Section titled “Install hooks”pre-commit installApply to all files
Section titled “Apply to all files”pre-commit run --all-filesGithub action to ensure pre-commit was applied
Section titled “Github action to ensure pre-commit was applied”name: Lint RST Files
on: # yamllint disable-line rule:truthy push: branches: - master pull_request: branches: - master workflow_dispatch:
jobs: lint-rst: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4
- name: Install uv uses: astral-sh/setup-uv@v4 with: python-version: "3.13" enable-cache: true
- name: Install linters run: | uv run pre-commit install
- name: Lint RST files run: | uv run pre-commit run --all-filesGithub action to update pre commit
Section titled “Github action to update pre commit”---name: Pre-commit Autoupdate
on: schedule: - cron: "0 5 * * 1" workflow_dispatch:
jobs: update: runs-on: ubuntu-latest
steps: - uses: actions/checkout@v4 with: fetch-depth: 0
- name: Install uv based on the version defined in pyproject.toml uses: astral-sh/setup-uv@v7
- name: Run pre-commit autoupdate run: | uv run --group dev pre-commit autoupdate
- name: Commit and push changes run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com"
if git diff --quiet; then echo "No changes to commit" exit 0 fi
branch="update/pre-commit-$(date +%Y%m%d)" git checkout -b "$branch" git add .pre-commit-config.yaml git commit -m "chore: pre-commit autoupdate" git push -u origin "$branch"
- name: Create PR env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | if git rev-parse --verify HEAD >/dev/null 2>&1 && [ "$(git log origin/master..HEAD --oneline | wc -l)" -gt 0 ]; then gh pr create --fill --base master --head "$branch" else echo "No new commits to create PR" fi