Skip to content

Commit 65b4b98

Browse files
authored
Merge pull request #798 from stackhpc/add-pre-commit-hooks
feat: add pre-commit hooks
2 parents 0546050 + d277afb commit 65b4b98

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- repo: https://github.com/sirwart/ripsecrets
9+
rev: v0.1.7
10+
hooks:
11+
- id: ripsecrets

doc/source/contributor/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ This guide is for contributors of the StackHPC Kayobe configuration project.
1111
release-notes
1212
environments/index
1313
package-updates
14+
pre-commit

doc/source/contributor/pre-commit.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
================
2+
Pre-commit Hooks
3+
================
4+
5+
StackHPC Kayobe configuration carries support for
6+
`pre-commit hooks <https://pre-commit.com/>`_ which simplify the use of git
7+
hooks enabling the identification and repairing of broken or poor code
8+
before committing.
9+
These hooks are designed to make working within SKC easier and less error prone.
10+
11+
Currently the following hooks are provided:
12+
13+
- ``check-yaml``: perform basic yaml syntax linting
14+
- ``end-of-file-fixer``: identify and automatically fix missing newline
15+
- ``trailing-whitespace``: identify and automatically fix excessive white space
16+
- ``ripsecrets``: identify and prevent secrets from being committed to the branch
17+
18+
.. warning::
19+
The hook ``ripsecrets`` is capable of preventing the accidental leaking of secrets
20+
such as those found within `secrets.yml` or `passwords.yml`.
21+
However if the secret is contained within a file on it's own and lacks a certain level
22+
of entropy then the secret will not be identified as such as and maybe leaked as a result.
23+
24+
Installation of `pre-commit` hooks is handled via the `install-pre-commit-hooks` playbook
25+
found within the Ansible directory.
26+
Either run the playbook manually or add the playbook as a hook within Kayobe config such as
27+
within `control-host-bootstrap/post.d`.
28+
Once done you should find `pre-commit` is available within the `kayobe` virtualenv.
29+
30+
To run the playbook using the following command
31+
32+
- ``kayobe playbook run ${KAYOBE_CONFIG_PATH}/ansible/install-pre-commit-hooks.yml``
33+
34+
Whereas to run the playbook when control host bootstrap runs ensure it registered as symlink using the following command
35+
36+
- ``mkdir -p ${KAYOBE_CONFIG_PATH}/hooks/control-host-bootstrap/post.d``
37+
- ``ln -s ${KAYOBE_CONFIG_PATH}/ansible/install-pre-commit-hooks.yml ${KAYOBE_CONFIG_PATH}/hooks/control-host-bootstrap/post.d/install-pre-commit-hooks.yml``
38+
39+
All that remains is the installation of the hooks themselves which can be accomplished either by
40+
running `pre-commit run` or using `git commit` when you have changes that need to be committed.
41+
This will trigger a brief installation process of the hooks which may take a few minutes.
42+
This a one time process and will not be required again unless new hooks are added or existing ones are updated.
43+
44+
.. note::
45+
Currently if you run ``pre-commit run --all-files`` it will make a series of changes to
46+
release notes that lack new lines as well configuration files that ``check-yaml`` does not
47+
approve of.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: Install pre-commit hooks
3+
hosts: localhost
4+
gather_facts: false
5+
vars:
6+
pre_commit_version: 3.5.0
7+
tasks:
8+
- name: Install pre-commit hooks
9+
block:
10+
- name: Install pre-commit hooks into kayobe virtual env
11+
ansible.builtin.pip:
12+
name: pre-commit
13+
version: "{{ pre_commit_version }}"
14+
virtualenv: "{{ lookup('ansible.builtin.env', 'VIRTUAL_ENV') | default(omit, true) }}"
15+
register: pip_install
16+
17+
- name: Register pre-commit hooks with git
18+
ansible.builtin.command:
19+
cmd: "{{ lookup('ansible.builtin.env', 'VIRTUAL_ENV') | default(lookup('ansible.builtin.env', 'HOME') ~ '/.local', true) }}/bin/pre-commit install"
20+
args:
21+
chdir: "{{ playbook_dir | dirname | dirname | dirname }}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
features:
3+
- |
4+
Add playbook to install pre-commit hooks and register them with git.
5+
The hooks currently configured to be installed will check yaml syntax,
6+
fix new line at end of file and remove excess whitespace. This is
7+
currently opt-in which can be achieved by running `install-pre-commit-hooks`
8+
playbook.

0 commit comments

Comments
 (0)