# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: CC0-1.0

# This file is maintained in https://github.com/WeblateOrg/meta/
name: Pre-commit check

on:
  push:
    branches-ignore:
    - renovate/**
    - weblate
  pull_request:

permissions:
  contents: read

jobs:
  pre-commit:
    runs-on: ubuntu-24.04

    steps:
    - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      with:
        persist-credentials: false
    - name: Get cache tag
      id: get-date
      run: |
        echo "cache_tag=$(/bin/date --utc '+%Y%m')" >> "$GITHUB_OUTPUT"
        echo "previous_cache_tag=$(/bin/date --date='1 month ago' --utc '+%Y%m')" >> "$GITHUB_OUTPUT"
      shell: bash
    - uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
      id: pre-commit-cache
      with:
        path: ~/.cache/pre-commit
        key: ${{ runner.os }}-pre-commit-${{ steps.get-date.outputs.cache_tag }}-${{ hashFiles('.pre-commit-config.yaml') }}
        restore-keys: |
          ${{ runner.os }}-pre-commit-${{ steps.get-date.outputs.cache_tag }}
          ${{ runner.os }}-pre-commit-${{ steps.get-date.outputs.previous_cache_tag }}
          ${{ runner.os }}-pre-commit-
    - name: Setup Python
      uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
      with:
        python-version: '3.14'
    - uses: astral-sh/setup-uv@681c641aba71e4a1c380be3ab5e12ad51f415867 # v7.1.6
      with:
        enable-cache: false
    - name: detect method
      id: detect
      run: |
        if test -f requirements-lint.txt ; then
          echo "method=requirements" >> "$GITHUB_OUTPUT"
        elif test -f pyproject.toml && grep -q dependency-groups pyproject.toml ; then
          echo "method=pep735" >> "$GITHUB_OUTPUT"
        elif test -f pyproject.toml && grep -q dependency-groups pyproject.toml ; then
          echo "method=pyproject" >> "$GITHUB_OUTPUT"
        else
          echo "method=uvx" >> "$GITHUB_OUTPUT"
        fi
    - name: pre-commit (PEP 735)
      if: steps.detect.outputs.method == 'pep735'
      run: |
        uv run --only-group pre-commit pre-commit run --all --show-diff-on-failure
        uv run --only-group pre-commit pre-commit gc
      env:
        RUFF_OUTPUT_FORMAT: github
        REUSE_OUTPUT_FORMAT: github
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: pre-commit (uvx)
      if: steps.detect.outputs.method == 'uvx'
      run: |
        uvx pre-commit run --all --show-diff-on-failure
        uvx pre-commit gc
      env:
        RUFF_OUTPUT_FORMAT: github
        REUSE_OUTPUT_FORMAT: github
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Install dependencies
      if: steps.detect.outputs.method == 'requirements'
      run: uv pip install --system -r requirements-lint.txt
    - name: Install dependencies
      if: steps.detect.outputs.method == 'pyproject'
      run: uv pip install --system "$(sed -n 's/.*"\(pre-commit==\([^"]*\)\)".*/\1/p' pyproject.toml)"
    - name: pre-commit (installed)
      if: steps.detect.outputs.method == 'requirements' || steps.detect.outputs.method == 'pyproject'
      run: |
        pre-commit run --all --show-diff-on-failure
        pre-commit gc
      env:
        RUFF_OUTPUT_FORMAT: github
        REUSE_OUTPUT_FORMAT: github
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: diff
      run: git diff
      if: always()
    - uses: pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0
      if: always()
      with:
        msg: 'chore(pre-commit): apply code formatting'
