From e53c83bc49d9c6c34f42f5ade16ab01bdad8824f Mon Sep 17 00:00:00 2001 From: Dafydd Jones Date: Mon, 21 Sep 2020 22:51:32 +0100 Subject: [PATCH] feat(pre-commit): add `pre-commit` --- ssf/defaults.yaml | 3 ++ ssf/files/default/.pre-commit-config.yaml | 55 ++++++++++++++++++++ ssf/files/default/.rstcheck.cfg | 3 ++ ssf/files/default/.travis.yml | 22 ++++---- ssf/files/default/bin/install-hooks | 16 ++++++ ssf/files/tofs_.github/docs/CONTRIBUTING.rst | 16 ++++++ ssf/formulas.yaml | 7 +++ 7 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 ssf/files/default/.pre-commit-config.yaml create mode 100644 ssf/files/default/.rstcheck.cfg create mode 100755 ssf/files/default/bin/install-hooks diff --git a/ssf/defaults.yaml b/ssf/defaults.yaml index 4e9e276b..5050195c 100644 --- a/ssf/defaults.yaml +++ b/ssf/defaults.yaml @@ -17,6 +17,7 @@ ssf_node_anchors: specific_directories: [] specific_ssf: - '/.github/workflows/': '@saltstack-formulas/ssf' + - '/bin/install-hooks': '@saltstack-formulas/ssf' - '/bin/kitchen': '@saltstack-formulas/ssf' - '/docs/AUTHORS.rst': '@saltstack-formulas/ssf' - '/docs/CHANGELOG.rst': '@saltstack-formulas/ssf' @@ -27,6 +28,8 @@ ssf_node_anchors: - '/test/integration/**/README.md': '@saltstack-formulas/ssf' - '/.gitignore': '@saltstack-formulas/ssf' - '/.cirrus.yml': '@saltstack-formulas/ssf' + - '/.pre-commit-config.yaml': '@saltstack-formulas/ssf' + - '/.rstcheck.cfg': '@saltstack-formulas/ssf' - '/.rubocop.yml': '@saltstack-formulas/ssf' - '/.salt-lint': '@saltstack-formulas/ssf' - '/.travis.yml': '@saltstack-formulas/ssf' diff --git a/ssf/files/default/.pre-commit-config.yaml b/ssf/files/default/.pre-commit-config.yaml new file mode 100644 index 00000000..3dc79faf --- /dev/null +++ b/ssf/files/default/.pre-commit-config.yaml @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# vim: ft=yaml +--- +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +default_stages: [commit] +repos: + - repo: https://github.com/dafyddj/commitlint-pre-commit-hook + rev: v2.3.0 + hooks: + - id: commitlint + name: Check commit message using commitlint + description: Lint commit message against @commitlint/config-conventional rules + stages: [commit-msg] + additional_dependencies: ['@commitlint/config-conventional@8.3.4'] + - id: commitlint-travis + stages: [manual] + additional_dependencies: ['@commitlint/config-conventional@8.3.4'] + always_run: true + - repo: https://github.com/adithyabsk/mirrors-rubocop + rev: v0.91.0 + hooks: + - id: rubocop + name: Check Ruby files with rubocop + args: [--debug] + always_run: true + pass_filenames: false + - repo: https://github.com/jumanjihouse/pre-commit-hooks + rev: 2.1.3 + hooks: + - id: shellcheck + name: Check shell scripts with shellcheck + files: ^.*\.(sh|bash|ksh)$ + types: [] + args: [] + - repo: https://github.com/adrienverge/yamllint.git + rev: v1.23.0 + hooks: + - id: yamllint + name: Check YAML syntax with yamllint + args: [--strict, '.'] + always_run: true + pass_filenames: false + - repo: https://github.com/warpnet/salt-lint + rev: v0.3.0 + hooks: + - id: salt-lint + name: Check Salt files using salt-lint + files: ^.*\.(sls|jinja|j2|tmpl|tst)$ + - repo: https://github.com/myint/rstcheck + rev: 3f929574 + hooks: + - id: rstcheck + name: Check reST files using rstcheck + args: [--report=warning] diff --git a/ssf/files/default/.rstcheck.cfg b/ssf/files/default/.rstcheck.cfg new file mode 100644 index 00000000..05856dc7 --- /dev/null +++ b/ssf/files/default/.rstcheck.cfg @@ -0,0 +1,3 @@ +[rstcheck] +report=error +ignore_language=rst diff --git a/ssf/files/default/.travis.yml b/ssf/files/default/.travis.yml index 4c0fb0c9..11cb2c82 100644 --- a/ssf/files/default/.travis.yml +++ b/ssf/files/default/.travis.yml @@ -114,11 +114,6 @@ jobs: {{ comment_linters }} - language: 'node_js' node_js: 'lts/*' - {%- if semrel_formula in ['template'] %} - cache: - directories: - - $HOME/.cache/pre-commit - {%- endif %} env: 'Lint' name: '{{ name_linters }}' before_install: 'skip' @@ -149,12 +144,19 @@ jobs: {#- Note: `--verbose` doesn't work yet, upstream issue created: #} {#- - https://github.com/conventional-changelog/commitlint/issues/1695 #} - commitlint-travis{{- ' --verbose' if semrel_formula == 'ssf' else '' }} - {%- if semrel_formula in ['template'] %} + # Run `pre-commit` linters in a single job + - language: 'python' + env: 'Lint_pre-commit' + name: 'Lint: pre-commit' + before_install: 'skip' + cache: + directories: + - $HOME/.cache/pre-commit + script: # Install and run `pre-commit` - - pip install pre-commit - - pre-commit run --all-files --verbose - - pre-commit run --hook-stage manual --verbose commitlint-travis - {%- endif %} + - {{ pip_cmd }} install pre-commit==2.7.1 + - pre-commit run --all-files --color always --verbose + - pre-commit run --color always --hook-stage manual --verbose commitlint-travis {%- if not travis.use_single_job_for_linters %} # Run the `rubocop` linter in a separate job that is allowed to fail # Once these lint errors are fixed, this can be merged into a single job diff --git a/ssf/files/default/bin/install-hooks b/ssf/files/default/bin/install-hooks new file mode 100755 index 00000000..840bb6c5 --- /dev/null +++ b/ssf/files/default/bin/install-hooks @@ -0,0 +1,16 @@ +#!/usr/bin/env sh +set -o nounset # Treat unset variables as an error and immediately exit +set -o errexit # If a command fails exit the whole script + +if [ "${DEBUG:-false}" = "true" ]; then + set -x # Run the entire script in debug mode +fi + +if ! command -v pre-commit >/dev/null 2>&1; then + echo "pre-commit not found: please install or check your PATH" >&2 + echo "See https://pre-commit.com/#installation" >&2 + exit 1 +fi + +pre-commit install --install-hooks +pre-commit install --hook-type commit-msg --install-hooks diff --git a/ssf/files/tofs_.github/docs/CONTRIBUTING.rst b/ssf/files/tofs_.github/docs/CONTRIBUTING.rst index 8fb88519..ab319dfc 100644 --- a/ssf/files/tofs_.github/docs/CONTRIBUTING.rst +++ b/ssf/files/tofs_.github/docs/CONTRIBUTING.rst @@ -55,6 +55,22 @@ So based on the example above: * The ``(scope):`` will be shown in bold text without the brackets. * The ``subject`` follows the ``scope`` as standard text. +pre-commit +^^^^^^^^^^ + +`pre-commit `_ is configured for this formula, which you may +optionally use to ease the steps involved in submitting your changes, including +checking the formatting of your commit messages. + +First install the ``pre-commit`` package manager locally using the appropriate +`method `_, then run ``bin/install-hooks`` in the +formula's root directory and now ``pre-commit`` will run automatically on each +``git commit``. :: + + $ bin/install-hooks + pre-commit installed at .git/hooks/pre-commit + pre-commit installed at .git/hooks/commit-msg + Linting commit messages in Travis CI ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/ssf/formulas.yaml b/ssf/formulas.yaml index a3e1a6dc..aafb07f0 100644 --- a/ssf/formulas.yaml +++ b/ssf/formulas.yaml @@ -510,6 +510,9 @@ ssf_node_anchors: semrel_files: &semrel_files_default .github/workflows/commitlint.yml: &file__--github__workflows__commitlint--yml <<: *file_default + bin/install-hooks: &file__bin__install-hooks + <<: *file_default + mode: 755 bin/kitchen: &file__bin__kitchen <<: *file_default mode: 755 @@ -532,6 +535,10 @@ ssf_node_anchors: .cirrus.yml: &file__--cirrus--yml <<: *file_default template: 'jinja' + .pre-commit-config.yaml: &file__--pre-commit-config--yaml + <<: *file_default + .rstcheck.cfg: &file__--rstcheck--cfg + <<: *file_default .rubocop.yml: &file__--rubocop--yml <<: *file_default template: 'jinja'