Skip to content

Commit 15499cb

Browse files
Refactor shell scripts (nhs-england-tools#137)
## Description This is the first part of the refactoring effort to ensure that the shell scripts: - Follow a consistent convention, - Pass the ShellCheck linting test (please run `make shellscript-lint-all`), and - Work either with the pre-installed CLI tooling or run commands using Docker containers. ## Context Information for a reviewer: - The first two commits, [a4517db](nhs-england-tools@a4517db) and [7b94f14](nhs-england-tools@7b94f14), implement stylistic and linting recommendations. - The last two commits, [f8b9e84](nhs-england-tools@f8b9e84) and [22f7700](nhs-england-tools@22f7700), refactor how tools like Gitleaks and SonarScanner are executed. ## Type of changes - [x] Refactoring (non-breaking change) - [ ] 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. --------- Co-authored-by: Alex Young <[email protected]>
1 parent 5f28100 commit 15499cb

File tree

19 files changed

+236
-142
lines changed

19 files changed

+236
-142
lines changed

.github/actions/check-markdown-format/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ runs:
77
shell: bash
88
run: |
99
export BRANCH_NAME=origin/${{ github.event.repository.default_branch }}
10-
./scripts/githooks/check-markdown-format.sh
10+
check=branch ./scripts/githooks/check-markdown-format.sh

.github/actions/scan-secrets/action.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ runs:
66
- name: "Scan secrets"
77
shell: bash
88
run: |
9-
export ALL_FILES=true # Do not change this line, as new patterns may be added or history may be rewritten
10-
./scripts/githooks/scan-secrets.sh
9+
# Please do not change this `check=whole-history` setting, as new patterns may be added or history may be rewritten.
10+
check=whole-history ./scripts/githooks/scan-secrets.sh

.tool-versions

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ pre-commit 3.4.0
66
# ==============================================================================
77
# The section below is reserved for Docker image versions.
88

9-
# terraform, SEE: https://hub.docker.com/r/hashicorp/terraform/tags
10-
# docker/hashicorp/terraform 1.5.6@sha256:180a7efa983386a27b43657ed610e9deed9e6c3848d54f9ea9b6cb8a5c8c25f5
11-
12-
# shellcheck, SEE: https://hub.docker.com/r/koalaman/shellcheck/tags
13-
# docker/koalaman/shellcheck latest@sha256:e40388688bae0fcffdddb7e4dea49b900c18933b452add0930654b2dea3e7d5c
14-
15-
# hadolint, SEE: https://hub.docker.com/r/hadolint/hadolint/tags
16-
# docker/hadolint/hadolint 2.12.0-alpine@sha256:7dba9a9f1a0350f6d021fb2f6f88900998a4fb0aaf8e4330aa8c38544f04db42
17-
18-
# ghcr.io/nhs-england-tools/github-runner-image, SEE: https://github.com/nhs-england-tools/github-runner-image/pkgs/container/github-runner-image
19-
# docker/ghcr.io/nhs-england-tools/github-runner-image 20230909-321fd1e-rt@sha256:ce4fd6035dc450a50d3cbafb4986d60e77cb49a71ab60a053bb1b9518139a646
9+
# TODO: Move this section - consider using a different file for the repository template dependencies.
10+
# docker/ghcr.io/gitleaks/gitleaks v8.18.0@sha256:fd2b5cab12b563d2cc538b14631764a1c25577780e3b7dba71657d58da45d9d9 # SEE: https://github.com/gitleaks/gitleaks/pkgs/container/gitleaks
11+
# 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
12+
# docker/hadolint/hadolint 2.12.0-alpine@sha256:7dba9a9f1a0350f6d021fb2f6f88900998a4fb0aaf8e4330aa8c38544f04db42 # SEE: https://hub.docker.com/r/hadolint/hadolint/tags
13+
# docker/hashicorp/terraform 1.5.6@sha256:180a7efa983386a27b43657ed610e9deed9e6c3848d54f9ea9b6cb8a5c8c25f5 # SEE: https://hub.docker.com/r/hashicorp/terraform/tags
14+
# docker/koalaman/shellcheck latest@sha256:e40388688bae0fcffdddb7e4dea49b900c18933b452add0930654b2dea3e7d5c # SEE: https://hub.docker.com/r/koalaman/shellcheck/tags
15+
# docker/sonarsource/sonar-scanner-cli 5.0.1@sha256:494ecc3b5b1ee1625bd377b3905c4284e4f0cc155cff397805a244dee1c7d575 # SEE: https://hub.docker.com/r/sonarsource/sonar-scanner-cli/tags

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ The following software packages, or their equivalents, are expected to be instal
4848
- [docker](https://www.docker.com/) container runtime or a compatible tool, e.g. [podman](https://podman.io/),
4949
- [asdf](https://asdf-vm.com/) version manager,
5050
- [GNU make](https://www.gnu.org/software/make/) 3.82 or later,
51-
- [GNU coreutils](https://www.gnu.org/software/coreutils/) and [GNU binutils](https://www.gnu.org/software/binutils/) may be required to build dependencies like Python, which may need to be compiled during installation. For macOS users, this has been scripted and automated by the `dotfiles` project; please see this [script](https://github.com/nhs-england-tools/dotfiles/blob/main/assets/20-install-base-packages.macos.sh) for details.
51+
- [GNU coreutils](https://www.gnu.org/software/coreutils/) and [GNU binutils](https://www.gnu.org/software/binutils/) may be required to build dependencies like Python, which may need to be compiled during installation. For macOS users, this has been scripted and automated by the `dotfiles` project; please see this [script](https://github.com/nhs-england-tools/dotfiles/blob/main/assets/20-install-base-packages.macos.sh) for details,
52+
- [jq](https://jqlang.github.io/jq/) a lightweight and flexible command-line JSON processor.
5253

5354
> [!NOTE]<br>
5455
> The version of GNU make available by default on macOS is earlier than 3.82. You will need to upgrade it or certain `make` tasks will fail. On macOS, you will need [homebrew](https://brew.sh/) installed, then to install `make`, like so:

docs/adr/assets/ADR-003/examples/bash/script.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function main() {
1717
function get-jwt-token() {
1818

1919
header=$(echo -n '{"alg":"RS256","typ":"JWT"}' | base64 | tr -d '=' | tr -d '\n=' | tr -- '+/' '-_')
20-
payload=$(echo -n '{"iat":'$(date +%s)',"exp":'$(($(date +%s)+600))',"iss":"'$GITHUB_APP_ID'"}' | base64 | tr -d '\n=' | tr -- '+/' '-_')
20+
payload=$(echo -n '{"iat":'"$(date +%s)"',"exp":'$(($(date +%s)+600))',"iss":"'"$GITHUB_APP_ID"'"}' | base64 | tr -d '\n=' | tr -- '+/' '-_')
2121
signature=$(echo -n "$header.$payload" | openssl dgst -binary -sha256 -sign "$GITHUB_APP_PK_FILE" | openssl base64 | tr -d '\n=' | tr -- '+/' '-_')
2222

2323
echo "$header.$payload.$signature"
@@ -30,17 +30,17 @@ function get-installation-id() {
3030
-H "Accept: application/vnd.github.v3+json" \
3131
https://api.github.com/app/installations)
3232

33-
echo "$(echo $installations_response | jq '.[] | select(.account.login == "'"$GITHUB_ORG"'") .id')"
33+
echo "$installations_response" | jq '.[] | select(.account.login == "'"$GITHUB_ORG"'") .id'
3434
}
3535

3636
function get-access-token() {
3737

3838
token_response=$(curl -sX POST \
3939
-H "Authorization: Bearer $jwt_token" \
4040
-H "Accept: application/vnd.github.v3+json" \
41-
https://api.github.com/app/installations/$installation_id/access_tokens)
41+
"https://api.github.com/app/installations/$installation_id/access_tokens")
4242

43-
echo "$(echo $token_response | jq .token -r)"
43+
echo "$token_response" | jq .token -r
4444
}
4545

4646
main

scripts/config/pre-commit.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ repos:
44
- id: scan-secrets
55
name: Scan Secrets
66
entry: ./scripts/githooks/scan-secrets.sh
7+
args: ["check=staged-changes"]
78
language: script
89
pass_filenames: false
910
- repo: local
@@ -19,6 +20,7 @@ repos:
1920
- id: check-markdown-format
2021
name: Check Markdown Format
2122
entry: ./scripts/githooks/check-markdown-format.sh
23+
args: ["check=staged-changes"]
2224
language: script
2325
pass_filenames: false
2426
- repo: local

scripts/docker/docker.lib.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function docker-get-image-version-and-pull() {
168168
local versions_file="${TOOL_VERSIONS:=$(git rev-parse --show-toplevel)/.tool-versions}"
169169
local version="latest"
170170
if [ -f "$versions_file" ]; then
171-
line=$(grep "docker/${name} " "$versions_file" | sed "s/^#\s*//; s/\s*#.*$//" | grep "${match_version:-'.*'}")
171+
line=$(grep "docker/${name} " "$versions_file" | sed "s/^#\s*//; s/\s*#.*$//" | grep "${match_version:-".*"}")
172172
[ -n "$line" ] && version=$(echo "$line" | awk '{print $2}')
173173
fi
174174

@@ -177,7 +177,7 @@ function docker-get-image-version-and-pull() {
177177
local digest="$(echo "$version" | sed 's/^.*@//')"
178178

179179
# Check if the image exists locally already
180-
if ! docker images | awk '{ print $1 ":" $2 }' | grep "^${name}:${tag}$"; then
180+
if ! docker images | awk '{ print $1 ":" $2 }' | grep -q "^${name}:${tag}$"; then
181181
if [ "$digest" != "latest" ]; then
182182
# Pull image by the digest sha256 and tag it
183183
docker pull \

scripts/docker/dockerfile-linter.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function docker-run-hadolint() {
4646
# shellcheck disable=SC1091
4747
source ./scripts/docker/docker.lib.sh
4848

49-
image=$(name=hadolint/hadolint docker-get-image-version-and-pull)
49+
# shellcheck disable=SC2155
50+
local image=$(name=hadolint/hadolint docker-get-image-version-and-pull)
5051
# shellcheck disable=SC2001
5152
docker run --rm --platform linux/amd64 \
5253
--volume "$PWD:/workdir" \

scripts/docker/tests/docker.test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function test-docker-get-image-version-and-pull() {
144144

145145
# ==============================================================================
146146

147-
function is_arg_true() {
147+
function is-arg-true() {
148148

149149
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
150150
return 0
@@ -155,7 +155,7 @@ function is_arg_true() {
155155

156156
# ==============================================================================
157157

158-
is_arg_true "${VERBOSE:-false}" && set -x
158+
is-arg-true "${VERBOSE:-false}" && set -x
159159

160160
main "$@"
161161

scripts/githooks/check-file-format.sh

Lines changed: 13 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
# Pre-commit git hook to check the EditorConfig rules compliance over changed
68
# files. It ensures all non-binary files across the codebase are formatted
@@ -17,7 +19,6 @@ set +e
1719
# 0 - All files are formatted correctly
1820
# 1 - Files are not formatted correctly
1921
#
20-
#
2122
# The `check` parameter controls which files are checked, so you can
2223
# limit the scope of the check according to what is appropriate at the
2324
# point the check is being applied.
@@ -45,9 +46,10 @@ image_version=2.7.1@sha256:dd3ca9ea50ef4518efe9be018d669ef9cf937f6bb5cfe2ef84ff2
4546

4647
function main() {
4748

48-
cd $(git rev-parse --show-toplevel)
49+
cd "$(git rev-parse --show-toplevel)"
4950

50-
is-arg-true "$dry_run" && dry_run_opt="--dry-run"
51+
# shellcheck disable=SC2154
52+
is-arg-true "${dry_run:-false}" && dry_run_opt="--dry-run"
5153

5254
check=${check:-working-tree-changes}
5355
case $check in
@@ -68,16 +70,17 @@ function main() {
6870
;;
6971
esac
7072

71-
7273
# We use /dev/null here as a backstop in case there are no files in the state
73-
# we choose. If the filter comes back empty, adding `/dev/null` onto it has
74+
# we choose. If the filter comes back empty, adding `/dev/null` onto it has
7475
# the effect of preventing `ec` from treating "no files" as "all the files".
7576
docker run --rm --platform linux/amd64 \
76-
--volume=$PWD:/check \
77+
--volume "$PWD":/check \
7778
mstruebing/editorconfig-checker:$image_version \
78-
sh -c "ec --exclude '.git/' $dry_run_opt \$($filter) /dev/null"
79+
sh -c "ec --exclude '.git/' ${dry_run_opt:-} \$($filter) /dev/null"
7980
}
8081

82+
# ==============================================================================
83+
8184
function is-arg-true() {
8285

8386
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
@@ -89,9 +92,8 @@ function is-arg-true() {
8992

9093
# ==============================================================================
9194

95+
is-arg-true "${VERBOSE:-false}" && set -x
9296

93-
is-arg-true "$VERBOSE" && set -x
94-
95-
main $*
97+
main "$@"
9698

9799
exit 0

0 commit comments

Comments
 (0)