diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..374ba7b --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,60 @@ +--- +name: pre-commit +on: + pull_request: + push: +jobs: + pre-commit: + runs-on: ubuntu-latest + env: + RAW_LOG: pre-commit.log + CS_XML: pre-commit.xml + steps: + - run: sudo apt-get update && sudo apt-get install cppcheck + if: false + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + if: false + with: + cache: pip + python-version: 3.12.1 + - run: python -m pip install pre-commit regex + - uses: actions/cache/restore@v4 + with: + path: | + ~/.cache/pre-commit/ + bash_unit + key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') + }} + - name: Run pre-commit hooks + env: + SKIP: no-commit-to-branch + run: | + set -o pipefail + pre-commit gc + pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG} + - name: Convert Raw Log to annotations + uses: mdeweerd/logToCheckStyle@v2024.2.3 + if: ${{ failure() }} + with: + in: ${{ env.RAW_LOG }} + # Out can be omitted if you do not need the xml output + # out: ${{ env.CS_XML }} + + - uses: actions/cache/save@v4 + if: ${{ always() }} + with: + path: | + ~/.cache/pre-commit/ + bash_unit + key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') + }} + - name: Provide log as artifact + uses: actions/upload-artifact@v4 + if: ${{ always() }} + with: + name: precommit-logs + path: | + ${{ env.RAW_LOG }} + ${{ env.CS_XML }} + retention-days: 2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5002f5b --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,86 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: no-commit-to-branch + args: [--branch, master] + - id: check-yaml + args: [--unsafe] + - id: check-json + - id: mixed-line-ending + - id: trailing-whitespace + exclude_types: [markdown] + - id: end-of-file-fixer + - id: check-merge-conflict + - id: check-executables-have-shebangs + - id: check-shebang-scripts-are-executable + exclude: ^tests/test_.*$ + - id: fix-byte-order-marker + - id: check-case-conflict + + # Beautify shell scripts + - repo: https://github.com/lovesegfault/beautysh.git + rev: v6.2.1 + hooks: + - id: beautysh + exclude: (?x)^(tests/test_(.*))$ + args: [-i, "2"] + + # Run local script + - repo: local + hooks: + - id: local-precommit-script + name: Run local script before commit if it exists + language: system + entry: bash -c '[ ! -x local.sh ] || ./local.sh' + pass_filenames: false + + # Prettier (format code, only for non common files) + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + stages: [manual] + exclude_types: + - executable + - binary + - shell + - markdown + + # Check format of yaml files + - repo: https://github.com/adrienverge/yamllint.git + rev: v1.35.1 + hooks: + - id: yamllint + args: + - --no-warnings + - -d + - '{extends: relaxed, rules: {line-length: {max: 120}}}' + + # Execute codespell to fix typo errors (setup of codespell into dev/tools/codespell/) + - repo: https://github.com/codespell-project/codespell + rev: v2.2.6 + hooks: + - id: codespell + args: + - --ignore-words-list=master,als + - --builtin=clear,rare,informal,usage,code,names,en-GB_to_en-US + exclude_types: [image] + + # Check some shell scripts + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.9.0.6 + hooks: + - id: shellcheck + args: [-W, '100'] + + # Run tests + - repo: local + hooks: + - id: tests + stages: [manual] + name: Run tests + language: system + entry: bash -c "./bash_unit tests/*" + pass_filenames: false