[tox]
minversion = 1.6
envlist = pre-commit
skipsdist = true

[testenv:jenkins-jobs]
deps =
    jenkins-job-builder=={env:JJB_VERSION:6.4.1}
commands =
    jenkins-jobs {posargs:--help}

[testenv:jjb-test]
deps =
    jenkins-job-builder=={env:JJB_VERSION:6.4.1}
commands =
    jenkins-jobs --conf {env:JJB_CONF:jenkins.ini} {posargs} test -o /tmp jjb/

[testenv:pre-commit-install]
basepython = python3
deps = pre-commit
commands =
    pre-commit install
    pre-commit install --hook-type commit-msg

[testenv:pre-commit-uninstall]
basepython = python3
deps = pre-commit
commands =
    pre-commit uninstall
    pre-commit uninstall --hook-type commit-msg

[testenv:pre-commit]
description = Precommit checks for black, gitlint, etc.
allowlist_externals =
    /bin/sh
deps =
    pre-commit
passenv = HOME
commands =
    pre-commit run --all-files --show-diff-on-failure
    /bin/sh -c 'if ! git config --get user.name > /dev/null; then \
        git config --global --add user.name "CI"; \
        touch .git/REMOVE_USERNAME; fi'
    /bin/sh -c 'if ! git config --get user.email > /dev/null; then \
        git config --global --add user.email "ci@example.org"; \
        touch .git/REMOVE_USEREMAIL; fi'
    /bin/sh -c "if [ -f .git/COMMIT_EDITMSG ]; then \
        cp .git/COMMIT_EDITMSG .git/COMMIT_MSGTOX; else \
        git log HEAD -n1 --pretty=%B > .git/COMMIT_MSGTOX; fi"
    pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_MSGTOX
    /bin/sh -c "rm -f .git/COMMIT_MSGTOX"
    /bin/sh -c "if [ -f .git/REMOVE_USERNAME ]; then \
        git config --global --unset user.name; \
        rm -f .git/REMOVE_USERNAME; fi"
    /bin/sh -c "if [ -f .git/REMOVE_USEREMAIL ]; then \
        git config --global --unset user.email; \
        rm -f .git/REMOVE_USEREMAIL; fi"
