Skip to content

Commit 07ee679

Browse files
authored
Add Vale pre-commit Git hook (nhs-england-tools#146)
## Description This change adds a Vale pre-commit Git hook to the repository template. Additionally, the following refactoring tasks were done: - Use Vale natively, if available, otherwise fall back to Docker. - Maintain a consistent naming convention for GitHub actions and pre-commit Git hooks. ## Context Vale is a command-line tool that functions as a syntax-aware linter for prose. It's designed to help writers adhere to a specific writing style or set of guidelines. We automatically check the English usage in our documentation. ## Type of changes - [x] Refactoring (non-breaking change) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist - [x] I am familiar with the [contributing guidelines](../docs/CONTRIBUTING.md) - [x] I have followed the code style of the project - [ ] I have added tests to cover my changes - [ ] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent 894f8b6 commit 07ee679

File tree

6 files changed

+62
-22
lines changed

6 files changed

+62
-22
lines changed

.github/actions/lint-prose/action.yaml renamed to .github/actions/check-english-usage/action.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ runs:
66
- name: "Check English usage"
77
shell: bash
88
run: |
9+
export BRANCH_NAME=origin/${{ github.event.repository.default_branch }}
910
check=branch ./scripts/githooks/check-english-usage.sh

.github/workflows/stage-1-commit.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ jobs:
5656
- name: "Check file format"
5757
uses: ./.github/actions/check-file-format
5858
check-markdown-format:
59-
name: "Check markdown format"
59+
name: "Check Markdown format"
6060
runs-on: ubuntu-latest
6161
timeout-minutes: 2
6262
steps:
6363
- name: "Checkout code"
6464
uses: actions/checkout@v4
6565
with:
6666
fetch-depth: 0 # Full history is needed to compare branches
67-
- name: "Check markdown format"
67+
- name: "Check Markdown format"
6868
uses: ./.github/actions/check-markdown-format
69-
lint-prose:
69+
check-english-usage:
7070
name: "Check English usage"
7171
runs-on: ubuntu-latest
7272
timeout-minutes: 2
@@ -76,7 +76,7 @@ jobs:
7676
with:
7777
fetch-depth: 0 # Full history is needed to compare branches
7878
- name: "Check English usage"
79-
uses: ./.github/actions/lint-prose
79+
uses: ./.github/actions/check-english-usage
8080
lint-terraform:
8181
name: "Lint Terraform"
8282
runs-on: ubuntu-latest

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pre-commit 3.4.0
1515
# docker/ghcr.io/nhs-england-tools/github-runner-image 20230909-321fd1e-rt@sha256:ce4fd6035dc450a50d3cbafb4986d60e77cb49a71ab60a053bb1b9518139a646 # SEE: https://github.com/nhs-england-tools/github-runner-image/pkgs/container/github-runner-image
1616
# docker/hadolint/hadolint 2.12.0-alpine@sha256:7dba9a9f1a0350f6d021fb2f6f88900998a4fb0aaf8e4330aa8c38544f04db42 # SEE: https://hub.docker.com/r/hadolint/hadolint/tags
1717
# docker/hashicorp/terraform 1.5.6@sha256:180a7efa983386a27b43657ed610e9deed9e6c3848d54f9ea9b6cb8a5c8c25f5 # SEE: https://hub.docker.com/r/hashicorp/terraform/tags
18+
# docker/jdkato/vale v2.29.7@sha256:5ccfac574231b006284513ac3e4e9f38833989d83f2a68db149932c09de85149 # SEE: https://hub.docker.com/r/jdkato/vale/tags
1819
# docker/koalaman/shellcheck latest@sha256:e40388688bae0fcffdddb7e4dea49b900c18933b452add0930654b2dea3e7d5c # SEE: https://hub.docker.com/r/koalaman/shellcheck/tags
1920
# docker/mstruebing/editorconfig-checker 2.7.1@sha256:dd3ca9ea50ef4518efe9be018d669ef9cf937f6bb5cfe2ef84ff2a620b5ddc24 # SEE: https://hub.docker.com/r/mstruebing/editorconfig-checker/tags
2021
# docker/sonarsource/sonar-scanner-cli 5.0.1@sha256:494ecc3b5b1ee1625bd377b3905c4284e4f0cc155cff397805a244dee1c7d575 # SEE: https://hub.docker.com/r/sonarsource/sonar-scanner-cli/tags

scripts/config/pre-commit.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,39 @@ repos:
22
- repo: local
33
hooks:
44
- id: scan-secrets
5-
name: Scan Secrets
5+
name: Scan secrets
66
entry: ./scripts/githooks/scan-secrets.sh
77
args: ["check=staged-changes"]
88
language: script
99
pass_filenames: false
1010
- repo: local
1111
hooks:
1212
- id: check-file-format
13-
name: Check File Format
13+
name: Check file format
1414
entry: ./scripts/githooks/check-file-format.sh
1515
args: ["check=staged-changes"]
1616
language: script
1717
pass_filenames: false
1818
- repo: local
1919
hooks:
2020
- id: check-markdown-format
21-
name: Check Markdown Format
21+
name: Check Markdown format
2222
entry: ./scripts/githooks/check-markdown-format.sh
2323
args: ["check=staged-changes"]
2424
language: script
2525
pass_filenames: false
2626
- repo: local
2727
hooks:
28-
- id: check-terraform-format
29-
name: Check Terraform Format
28+
- id: check-english-usage
29+
name: Check English usage
30+
entry: ./scripts/githooks/check-english-usage.sh
31+
args: ["check=staged-changes"]
32+
language: script
33+
pass_filenames: false
34+
- repo: local
35+
hooks:
36+
- id: lint-terraform
37+
name: Lint Terraform
3038
entry: ./scripts/githooks/check-terraform-format.sh
3139
language: script
3240
pass_filenames: false

scripts/githooks/check-english-usage.sh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22

3-
set -e
3+
# WARNING: Please, DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
4+
5+
set -euo pipefail
46

57
# Git hook to check prose style
68
#
@@ -19,15 +21,12 @@ set -e
1921
# check=staged-changes: check only files staged for commit.
2022
# check=working-tree-changes: check modified, unstaged files. This is the default.
2123
# check=branch: check for all changes since branching from $BRANCH_NAME
22-
# ==============================================================================
23-
24-
image_version=v2.29.0@sha256:d4647754ea0d051d574bafe79edccaaa67f25a4c227b890a55dd83a117278590
2524

2625
# ==============================================================================
2726

2827
function main() {
2928

30-
cd $(git rev-parse --show-toplevel)
29+
cd "$(git rev-parse --show-toplevel)"
3130

3231
check=${check:-working-tree-changes}
3332
case $check in
@@ -48,18 +47,49 @@ function main() {
4847
;;
4948
esac
5049

50+
if command -v vale > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
51+
filter="$filter" run-vale-natively
52+
else
53+
filter="$filter" run-vale-in-docker
54+
fi
55+
}
56+
57+
# Run Vale natively.
58+
# Arguments (provided as environment variables):
59+
# filter=[git command to filter the files to check]
60+
function run-vale-natively() {
61+
62+
# shellcheck disable=SC2046
63+
vale \
64+
--config "$PWD/scripts/config/vale/vale.ini" \
65+
$($filter)
66+
}
67+
68+
# Run Vale in a Docker container.
69+
# Arguments (provided as environment variables):
70+
# filter=[git command to filter the files to check]
71+
function run-vale-in-docker() {
72+
73+
# shellcheck disable=SC1091
74+
source ./scripts/docker/docker.lib.sh
75+
76+
# shellcheck disable=SC2155
77+
local image=$(name=jdkato/vale docker-get-image-version-and-pull)
5178
# We use /dev/null here to stop `vale` from complaining that it's
5279
# not been called correctly if the $filter happens to return an
53-
# empty list. As long as there's a filename, even if it's one that
80+
# empty list. As long as there's a filename, even if it's one that
5481
# will be ignored, `vale` is happy.
82+
# shellcheck disable=SC2046,SC2086
5583
docker run --rm --platform linux/amd64 \
56-
--volume $PWD:/workdir \
84+
--volume "$PWD:/workdir" \
5785
--workdir /workdir \
58-
jdkato/vale:$image_version \
59-
--config scripts/config/vale/vale.ini \
86+
"$image" \
87+
--config /workdir/scripts/config/vale/vale.ini \
6088
$($filter) /dev/null
6189
}
6290

91+
# ==============================================================================
92+
6393
function is-arg-true() {
6494

6595
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
@@ -71,8 +101,8 @@ function is-arg-true() {
71101

72102
# ==============================================================================
73103

74-
is-arg-true "$VERBOSE" && set -x
104+
is-arg-true "${VERBOSE:-false}" && set -x
75105

76-
main $*
106+
main "$@"
77107

78108
exit 0

scripts/githooks/check-file-format.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function main() {
7676
# Run editorconfig natively.
7777
# Arguments (provided as environment variables):
7878
# dry_run_opt=[dry run option]
79-
# filter=[filter for files to check]
79+
# filter=[git command to filter the files to check]
8080
function run-editorconfig-natively() {
8181

8282
# shellcheck disable=SC2046,SC2086
@@ -87,7 +87,7 @@ function run-editorconfig-natively() {
8787
# Run editorconfig in a Docker container.
8888
# Arguments (provided as environment variables):
8989
# dry_run_opt=[dry run option]
90-
# filter=[filter for files to check]
90+
# filter=[git command to filter the files to check]
9191
function run-editorconfig-in-docker() {
9292

9393
# shellcheck disable=SC1091

0 commit comments

Comments
 (0)