From 6e235c178a741f7943b7e297aa62357d2f830e6e Mon Sep 17 00:00:00 2001 From: Niall Byrne <9848926+niall-byrne@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:25:29 -0500 Subject: [PATCH] feat(PRE-COMMIT): add template development hooks --- .github/workflows/self-test.yml | 75 ++++++++++++++++++- .pre-commit-config.yaml | 37 +++++++++ pyproject.toml | 20 +++++ .../.github/scripts/pushed_commit_range.sh | 19 ++--- .../.github/workflows/push.yml | 4 +- .../.pre-commit/yaml-lint.sh | 21 ++++++ 6 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml create mode 100755 {{cookiecutter.project_slug}}/.pre-commit/yaml-lint.sh diff --git a/.github/workflows/self-test.yml b/.github/workflows/self-test.yml index 2cdd88a7..0121df4a 100644 --- a/.github/workflows/self-test.yml +++ b/.github/workflows/self-test.yml @@ -92,6 +92,69 @@ jobs: run: | ./template/{{cookiecutter.project_slug}}/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: error reporting job status!" + commit_lint_test: + needs: [_create_configuration] + + runs-on: ubuntu-latest + env: + ANSIBLE_WORKBENCH_SKIP_POETRY: 0 + ANSIBLE_WORKBENCH_SKIP_PRECOMMIT: 1 + strategy: + max-parallel: 4 + matrix: + python-version: ${{ fromJson(needs._create_configuration.outputs.configuration)._GITHUB_CI_PYTHON_VERSIONS }} + + steps: + - name: Commit Lint Test -- Checkout Repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Commit Lint Test -- Setup Environment + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/setup.sh + source ./{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + + - name: Commit Lint Test -- Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Commit Lint Test -- Install Poetry + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/poetry.sh "install-poetry" + + - name: Commit Lint Test -- Initialize Cache Locations + run: | + mkdir -p ~/.cache/pypoetry/virtualenvs + + - name: Commit Lint Test -- Mount Poetry Cache + uses: actions/cache@v3 + with: + key: poetry-${{ hashFiles('./pyproject.toml') }}-${{ runner.os }}-${{ env.CACHE_TTL }} + path: ~/.cache/pypoetry/virtualenvs + + - name: Commit Lint Test -- Install Requirements + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/poetry.sh "install-project" + + - name: Commit Lint Test -- Lint Pushed Commits + run: | + poetry run cz check --rev-range "${PUSHED_COMMIT_REV_RANGE}" + + - name: Commit Lint Test -- Report Job Status on Success + if: env.VERBOSE_NOTIFICATIONS == '1' + run: | + ./{{cookiecutter.project_slug}}/.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: commit lint checks succeeded!" + + - name: Commit Lint Test -- Report Job Status on Failure + if: failure() + run: | + ./{{cookiecutter.project_slug}}/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: commit lint checks failed!" + create_release: needs: [push_repository_test] @@ -387,7 +450,7 @@ jobs: ./template/{{cookiecutter.project_slug}}/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: pre-commit hook test has failed!" push_repository_test: - needs: [_create_configuration, _start_notification, documentation_test, molecule_lint_test, precommit_test, security_test, shellcheck_test, toml_lint_test, yaml_lint_test] + needs: [_create_configuration, _start_notification, commit_lint_test, documentation_test, molecule_lint_test, precommit_test, security_test, shellcheck_test, toml_lint_test, yaml_lint_test] runs-on: ubuntu-latest env: @@ -520,7 +583,7 @@ jobs: uses: trufflesecurity/trufflehog@v3.27.1 with: path: . - base: ${{ env.PUSHED_COMMIT_RANGE }} + base: ${{ env.PUSHED_COMMIT_START }} head: ${{ env.BRANCH_OR_TAG }} - name: Security Test Repo -- Report Job Status on Success @@ -630,7 +693,13 @@ jobs: run: | source ./template/.github/scripts/template.sh - - name: Toml Lint Test -- Run Linter + - name: Toml Lint Test -- Run Linter On Template + run: | + cd "template" + find . -type f -name "*.toml" -exec tomll "{}" \; + git diff --exit-code + + - name: Toml Lint -- Run Linter On Rendered Template run: | cd "${TEMPLATED_NAME}" find . -type f -name "*.toml" -exec tomll "{}" \; diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..148412c2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,37 @@ +--- +repos: + - repo: local + hooks: + - id: commit-lint + name: commit-lint + description: "Check whether the commit message follows committing rules." + entry: ./{{cookiecutter.project_slug}}/.pre-commit/commit-lint.sh + language: system + stages: [commit-msg] + - id: shell-lint + name: shell-lint + description: "Lint the project's shell scripts." + entry: shellcheck + files: "^.+\\.sh$" + exclude: "^{{cookiecutter.project_slug}}/.+$" + language: system + pass_filenames: true + stages: [commit] + - id: toml-lint + name: toml-lint + description: "Lint the project's TOML files." + entry: ./{{cookiecutter.project_slug}}/.pre-commit/toml-lint.sh + files: "^.+\\.toml$" + language: system + stages: [commit] + pass_filenames: true + - id: yaml-lint + name: yaml-lint + description: "Lint the project's YAML files." + entry: ./{{cookiecutter.project_slug}}/.pre-commit/yaml-lint.sh + args: ["-c", "./{{cookiecutter.project_slug}}/.yamllint.yml"] + files: "^.+\\.(yaml|yml)$" + exclude: "^{{cookiecutter.project_slug}}/.+$" + language: system + pass_filenames: true + stages: [commit] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..96fdab17 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ + +[build-system] + build-backend = "poetry.core.masonry.api" + requires = ["poetry-core"] + +[tool] + + [tool.poetry] + authors = ["Niall Byrne "] + description = "Ansible Role Development Environment." + license = "MPL-2" + name = "ansible-workbench" + readme = "README.md" + version = "0.1.6" + + [tool.poetry.dependencies] + commitizen = "^2.42.1" + pre-commit = "^3.1.0" + python = ">=3.9.0,<4.0" + yamllint = "^1.29.0" diff --git a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh index 3b878ef9..01aebd11 100644 --- a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh +++ b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh @@ -1,7 +1,7 @@ #!/bin/bash # .github/scripts/pushed_commit_range.sh -# Retrieves the range of the commits in a push, and sets the PUSHED_COMMIT_RANGE environment variables. +# Retrieves the range of the commits in a push, and sets the PUSHED_COMMIT_START environment variables. # GITHUB_CONTEXT: The github action context object as an environment variable. @@ -17,20 +17,21 @@ get_all_commits() { main() { - PUSHED_COMMIT_RANGE="HEAD~$(echo "$GITHUB_CONTEXT" | jq '.event.commits | length')" + PUSHED_COMMIT_START="HEAD~$(echo "${GITHUB_CONTEXT}" | jq '.event.commits | length')" + PUSHED_COMMIT_REV_RANGE="${PUSHED_COMMIT_START}..HEAD" - if [[ "${PUSHED_COMMIT_RANGE}" == "HEAD~0" ]]; then - PUSHED_COMMIT_RANGE="$(get_all_commits)" + if [[ "${PUSHED_COMMIT_REV_RANGE}" == "HEAD~0" ]]; then + PUSHED_COMMIT_START="$(get_all_commits)" + PUSHED_COMMIT_REV_RANGE="HEAD" fi - if ! git rev-parse "${PUSHED_COMMIT_RANGE}"; then - PUSHED_COMMIT_RANGE="$(get_all_commits)" + if ! git rev-parse "${PUSHED_COMMIT_REV_RANGE}"; then + PUSHED_COMMIT_START="$(get_all_commits)" fi { - echo "PUSHED_COMMIT_RANGE<> "${GITHUB_ENV}" } diff --git a/{{cookiecutter.project_slug}}/.github/workflows/push.yml b/{{cookiecutter.project_slug}}/.github/workflows/push.yml index 990e3e87..67a964f1 100644 --- a/{{cookiecutter.project_slug}}/.github/workflows/push.yml +++ b/{{cookiecutter.project_slug}}/.github/workflows/push.yml @@ -77,7 +77,7 @@ jobs: create_release: needs: {% endraw %}[_create_python_versions, _start_notification, documentation_test, molecule_lint_test, molecule_test, security_test,{% if cookiecutter.optional_toml_linting == 'true' %} toml_lint_test,{% endif %} yaml_lint_test]{% raw %} - + runs-on: ubuntu-latest steps: @@ -349,7 +349,7 @@ jobs: uses: {% endraw %}{{ cookiecutter._GITHUB_ACTION_TRUFFLEHOG }}{% raw %} with: path: . - base: ${{ env.PUSHED_COMMIT_RANGE }} + base: ${{ env.PUSHED_COMMIT_START }} head: ${{ env.BRANCH_OR_TAG }} - name: Security Test -- Report Job Status on Success diff --git a/{{cookiecutter.project_slug}}/.pre-commit/yaml-lint.sh b/{{cookiecutter.project_slug}}/.pre-commit/yaml-lint.sh new file mode 100755 index 00000000..21d68047 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.pre-commit/yaml-lint.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# scripts/pre-commit/yaml-lint.sh +# Runs yamllint on the specified files. + +# @: An array of yaml files to lint. + +# pre-commit script. + +set -eo pipefail + +main () { + + # shellcheck source=./.pre-commit/.poetry-compatible.sh + source "$(dirname -- "${BASH_SOURCE[0]}")/.poetry-compatible.sh" + + run_command yamllint -f standard "$@" + +} + +main "$@"