Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
feat(PRE-COMMIT): add configured pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Mar 10, 2023
1 parent 38ff447 commit fe3460b
Show file tree
Hide file tree
Showing 18 changed files with 509 additions and 44 deletions.
18 changes: 14 additions & 4 deletions .github/scripts/template.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#!/bin/bash

NAME=${1:-"Pro Buddy Dev"}
EMAIL=${2:-"[email protected]"}
# .github/scripts/template.sh
# Perform automated templating.

# 1: Optional TOML Formatting
# 2: Git Username
# 3: Git Email

# CI only script.

set -eo pipefail

OPTIONAL_TOML_LINTING=${1:-"1"}
NAME=${2:-"Pro Buddy Dev"}
EMAIL=${3:-"[email protected]"}

main() {

git config --global user.name "${NAME}"
git config --global user.email "${EMAIL}"

echo -e '\n\n\n\n\n\n\n\n\n' | cookiecutter template/
echo -e "\n\n${OPTIONAL_TOML_LINTING}\n\n\n\n\n\n\n\n" | cookiecutter template/

}

main
main "$@"
80 changes: 80 additions & 0 deletions .github/scripts/test_precommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

# .github/scripts/test_precommit.sh
# Performs tests on the pre-commit hooks.

# 1: The name of a pre-commit test scenario. (See 'main' below.)
# TEMPLATED_NAME: The name of the rendered test project.

# CI only script.

set -eo pipefail

test_commit_lint() {
util_git_reset
touch empty_file.txt
git stage empty_file.txt
git commit -m 'test - pre-commit: improperly formatted commit' || exit 0
util_fail_test
}

test_molecule_lint() {
util_git_reset
echo "" >> tasks/main.yml
git stage tasks/main.yml
git commit -m 'test(PRE-COMMIT): fail due to ansible-lint' > error.log 2>&1 || grep "empty-lines" error.log > /dev/null && exit 0
util_fail_test
}

test_toml_lint_1() {
util_git_reset
sed -i.bak 's/authors =/ authors = /g' pyproject.toml
git stage pyproject.toml
git commit -m 'test(PRE-COMMIT): fail due to tomll' || exit 0
util_fail_test
}

test_toml_lint_2() {
util_git_reset
sed -i.bak 's/>=3.9.0,<4.0/>=3.9.1,<4.0/g' pyproject.toml
git stage pyproject.toml
git commit -m 'test(PRE-COMMIT): upgrade python without issue'
}

util_fail_test() {
echo "This commit should have failed."
exit 127
}

util_git_reset() {
git reset HEAD
git clean -fd
git checkout .
}

main() {

pushd "${TEMPLATED_NAME}"
case $1 in
commit-lint)
test_commit_lint
;;
molecule-lint)
test_molecule_lint
;;
toml-lint-1)
test_toml_lint_1
;;
toml-lint-2)
test_toml_lint_2
;;
*)
echo "Invalid test scenario."
exit 127
;;
esac
popd

}

main "$@"
Loading

0 comments on commit fe3460b

Please sign in to comment.