From 4fef994e383432195c13283dc467dffb64743c2e Mon Sep 17 00:00:00 2001 From: Niall Byrne <9848926+niall-byrne@users.noreply.github.com> Date: Sat, 4 Mar 2023 14:10:15 -0500 Subject: [PATCH] ci(GITHUB): add commit linting --- .github/workflows/self-test.yml | 1 + .../.github/scripts/pushed_commit_range.sh | 31 +++++---- .../.github/workflows/push.yml | 64 ++++++++++++++++++- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/.github/workflows/self-test.yml b/.github/workflows/self-test.yml index 0121df4a..d11d84c7 100644 --- a/.github/workflows/self-test.yml +++ b/.github/workflows/self-test.yml @@ -15,6 +15,7 @@ on: env: ANSIBLE_WORKBENCH_BRANCH_NAME_BASE: "master" ANSIBLE_WORKBENCH_BRANCH_NAME_DEVELOPMENT: "dev" + ANSIBLE_WORKBENCH_PUSH_FALLBACK_REV_RANGE: "8f9a7c315416747257b3ef2adfc425c63d85adf8..HEAD" PROJECT_NAME: "ansible-workbench" USER_NAME: "niall-byrne" TEMPLATED_NAME: "flower-generator" diff --git a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh index 01aebd11..33de1399 100644 --- a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh +++ b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh @@ -1,32 +1,41 @@ #!/bin/bash # .github/scripts/pushed_commit_range.sh -# Retrieves the range of the commits in a push, and sets the PUSHED_COMMIT_START environment variables. +# Retrieves the range of the commits in a push, and sets the PUSHED_COMMIT_START, PUSHED_COMMIT_REV_RANGE variables. -# GITHUB_CONTEXT: The github action context object as an environment variable. +# ANSIBLE_WORKBENCH_PUSH_FALLBACK_INDEX: Optionally set fallback behaviour when no changed commits are detected. (Default is the first commit in the project.) +# ANSIBLE_WORKBENCH_PUSH_FALLBACK_REV_RANGE: Optionally set fallback behaviour when no changed commits are detected. (Default is HEAD, all commits.) +# GITHUB_CONTEXT: The github action context object as an environment variable. # CI only script set -eo pipefail +ANSIBLE_WORKBENCH_PUSH_FALLBACK_INDEX="${ANSIBLE_WORKBENCH_PUSH_FALLBACK_INDEX-$(git rev-list --max-parents=0 HEAD)}" +ANSIBLE_WORKBENCH_PUSH_FALLBACK_REV_RANGE="${ANSIBLE_WORKBENCH_PUSH_FALLBACK_REV_RANGE-HEAD}" -get_all_commits() { - git rev-list --max-parents=0 HEAD +fallback_behaviour() { + echo "WARNING: Unable to determine number of changed commits." + echo "WARNING: Fallback values are being used instead." + PUSHED_COMMIT_START="${ANSIBLE_WORKBENCH_PUSH_FALLBACK_INDEX}" + PUSHED_COMMIT_REV_RANGE="${ANSIBLE_WORKBENCH_PUSH_FALLBACK_REV_RANGE}" } - main() { - PUSHED_COMMIT_START="HEAD~$(echo "${GITHUB_CONTEXT}" | jq '.event.commits | length')" + COMMIT_COUNT="$(echo "${GITHUB_CONTEXT}" | jq '.event.commits | length')" + + PUSHED_COMMIT_START="HEAD~${COMMIT_COUNT}" PUSHED_COMMIT_REV_RANGE="${PUSHED_COMMIT_START}..HEAD" - if [[ "${PUSHED_COMMIT_REV_RANGE}" == "HEAD~0" ]]; then - PUSHED_COMMIT_START="$(get_all_commits)" - PUSHED_COMMIT_REV_RANGE="HEAD" + echo "DEBUG: GitHub reports ${COMMIT_COUNT} commit(s) have changed." + + if [[ "${PUSHED_COMMIT_START}" == "HEAD~0" ]]; then + fallback_behaviour fi - if ! git rev-parse "${PUSHED_COMMIT_REV_RANGE}"; then - PUSHED_COMMIT_START="$(get_all_commits)" + if ! git rev-parse "${PUSHED_COMMIT_START}" >> /dev/null 2>&1; then + fallback_behaviour fi { diff --git a/{{cookiecutter.project_slug}}/.github/workflows/push.yml b/{{cookiecutter.project_slug}}/.github/workflows/push.yml index 67a964f1..9e33eeee 100644 --- a/{{cookiecutter.project_slug}}/.github/workflows/push.yml +++ b/{{cookiecutter.project_slug}}/.github/workflows/push.yml @@ -75,9 +75,69 @@ jobs: run: | ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: error reporting job status!" + commit_lint_test: + needs: [_create_configuration] + + runs-on: ubuntu-latest + strategy: + max-parallel: 4 + matrix: + python-version: ${{ fromJSON(needs._create_configuration.outputs.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 ./.github/scripts/setup.sh + source ./.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 ./.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 ./.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: | + ./.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: commit lint checks succeeded!" + + - name: Commit Lint Test -- Report Job Status on Failure + if: failure() + run: | + ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: commit lint checks failed!" + 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 %} - + needs: {% endraw %}[_create_configuration, _start_notification, commit_lint_test, 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: