diff --git a/.githooks/pre-commit b/.githooks/pre-commit index d5ac2e2..4f1c4b2 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -76,27 +76,34 @@ if skip editorconfig; then warn "SKIP=editorconfig"; else fi fi -# Staged .go, go.mod and go.sum files: Added/Copied/Modified/Renamed, NUL-safe. -STAGED=() -while IFS= read -r -d '' f; do STAGED+=("$f"); done \ - < <(git diff --cached --name-only -z --diff-filter=ACMR -- '*.go' '*/go.mod' '*/go.sum') - -if [ "${#STAGED[@]}" -eq 0 ]; then - ok "no staged Go files; skipping go checks"; exit 0 -fi - -# Each tool module is a top-level directory with a go.mod. Group the staged -# files by module; a .go file outside every module is refused. +# Each tool module is a top-level directory with a go.mod, and modules don't +# nest. Group the staged .go, go.mod and go.sum files by module (NUL-safe): +# added, copied, modified and renamed files are checked and fixed below; a +# deleted file only marks its module for the checks. MODULES=() STAGED_GO=() -for f in "${STAGED[@]}"; do +add_module() { + case " ${MODULES[*]-} " in *" $1 "*) ;; *) MODULES+=("$1") ;; esac +} +while IFS= read -r -d '' f; do mod="${f%%/*}" if [[ "$mod" == "$f" || ! -f "$mod/go.mod" ]]; then die "$f is not in a tool module. Put each tool in / with its own go.mod." fi - [[ "$f" == *.go ]] && STAGED_GO+=("$f") - case " ${MODULES[*]-} " in *" $mod "*) ;; *) MODULES+=("$mod") ;; esac -done + if [[ "$f" == */go.mod || "$f" == */go.sum ]] && [[ "${f#*/}" == */* ]]; then + die "$f is a nested module. Each tool has one go.mod, at /go.mod." + fi + if [[ "$f" == *.go ]]; then STAGED_GO+=("$f"); fi + add_module "$mod" +done < <(git diff --cached --name-only -z --diff-filter=ACMR -- '*.go' '*/go.mod' '*/go.sum') +while IFS= read -r -d '' f; do + mod="${f%%/*}" + if [[ "$mod" != "$f" && -f "$mod/go.mod" ]]; then add_module "$mod"; fi +done < <(git diff --cached --name-only -z --diff-filter=D -- '*.go' '*/go.mod' '*/go.sum') + +if [ "${#MODULES[@]}" -eq 0 ]; then + ok "no staged Go changes; skipping go checks"; exit 0 +fi info "modules: ${MODULES[*]}" # 3. License headers: add the SPDX header where missing, then re-stage. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e77718e..59a6a74 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,10 +21,25 @@ jobs: - uses: actions/checkout@v7 with: persist-credentials: false + # Fails when a go.mod isn't at /go.mod or a .go file is outside + # every tool module, because the jobs below would skip that code. - name: List the tool modules id: find run: | - dirs=$(find . -mindepth 2 -maxdepth 2 -name go.mod -printf '%h\n' | sed 's|^\./||' | sort | jq -R . | jq -cs .) + nested=$(git ls-files -- 'go.mod' '*/go.mod' | awk -F/ 'NF != 2') + if [ -n "$nested" ]; then + echo "::error::Each tool has one go.mod, at /go.mod. Move or remove:" $nested + exit 1 + fi + modules=$(git ls-files -- '*/go.mod' | cut -d/ -f1 | sort -u) + stray=$(git ls-files -- '*.go' | MODULES="$modules" awk -F/ ' + BEGIN { n = split(ENVIRON["MODULES"], m, "\n"); for (i = 1; i <= n; i++) in_mod[m[i]] = 1 } + NF == 1 || !($1 in in_mod)') + if [ -n "$stray" ]; then + echo "::error::These Go files are outside every tool module:" $stray + exit 1 + fi + dirs=$(printf '%s' "$modules" | jq -R -s -c 'split("\n") | map(select(length > 0))') echo "Modules: $dirs" echo "dirs=$dirs" >> "$GITHUB_OUTPUT" @@ -97,7 +112,8 @@ jobs: run: go run golang.org/x/vuln/cmd/govulncheck@latest ./... # Runs the Makefile in cmd.exe, as GNU make does on Windows when no sh is on - # the PATH, so its recipes stay portable. + # the PATH, so its recipes stay portable. It uses the latest stable Go, which + # builds every module whatever Go version its go.mod needs. windows: name: Make on Windows needs: modules @@ -109,7 +125,7 @@ jobs: persist-credentials: false - uses: actions/setup-go@v7 with: - go-version-file: ${{ fromJSON(needs.modules.outputs.dirs)[0] }}/go.mod + go-version: stable cache-dependency-path: '*/go.sum' cache: true - name: Install GNU make diff --git a/.gitleaks.toml b/.gitleaks.toml deleted file mode 100644 index 826c8b1..0000000 --- a/.gitleaks.toml +++ /dev/null @@ -1,16 +0,0 @@ -# gitleaks configuration — https://github.com/gitleaks/gitleaks#configuration -# Used by the pre-commit hook (`gitleaks git --staged`) and CI (`gitleaks dir`). -[extend] -useDefault = true - -# Storage object keys in tests (e.g. ".pdf") are file identifiers, not -# credentials, but trip the high-noise `generic-api-key` rule because the variable -# is named `key`. Scope this allowlist to that one rule and to test files only, so -# high-signal rules (private keys, cloud tokens, JWTs, ...) still apply everywhere, -# including in tests. -[[allowlists]] -description = "Storage object keys in *_test.go are file identifiers, not secrets" -targetRules = ["generic-api-key"] -paths = [ - '''.*_test\.go$''', -] diff --git a/AGENTS.md b/AGENTS.md index 203fde7..687fd85 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -4,7 +4,7 @@ OpenNSW tools are command-line tools that National Single Window deployments and their CI run. Libraries belong in `OpenNSW/core`, and application code stays in each deployment's repository. -- Each tool is its own Go module in its own directory: `/go.mod` declares `github.com/OpenNSW/tools/`. +- Each tool is its own Go module in its own directory: `/go.mod` declares `github.com/OpenNSW/tools/`. Modules don't nest, and every `.go` file is in a tool module; CI fails otherwise. - A tool's library code sits at the root of its directory, and each command in `/cmd//`. - Each tool has a `README.md` describing its commands and configuration, and a row in the root `README.md`. - A tool never imports another tool's module or a deployment's repository. It reuses core's modules (`database`, `configyaml`, ...) rather than copying them. @@ -15,10 +15,10 @@ Use Go 1.26+, GNU make, gitleaks, golangci-lint v2.15.0 and editorconfig-checker - `make setup`: install golangci-lint, addlicense and editorconfig-checker, and enable the git hooks in `.githooks`. - `make check`: secret scan and `.editorconfig` check of the whole repo, then license headers, lint, tidy check and build in every tool module. -- `make test`: race-enabled tests in every tool module; `make test/` for one. Every target takes `/`. +- `make test`: race-enabled tests in every tool module; `make test/` for one. `fmt`, `lint`, `license`, `license-check`, `tidy`, `build`, `test` and `vuln` all take `/`. - `make fmt` and `make license`: apply formatting and SPDX headers. -Keep each Makefile recipe line a single command that runs in both sh and Windows `cmd.exe`; CI runs the Makefile on Windows. +Keep each Makefile recipe line one command, or `cd && `, that runs in both sh and Windows `cmd.exe`; CI runs the Makefile on Windows. ## Compatibility diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ce2f656..b528c7e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,7 +14,7 @@ Each tool lives in its own directory and is its own Go module: └── cmd// each command the tool provides ``` -Each module has its own dependencies and its own releases. A tool never imports another tool's module or a deployment's repository. Library code that several tools or applications need belongs in [OpenNSW/core](https://github.com/OpenNSW/core). +Modules don't nest: each tool has one `go.mod`, and every `.go` file is in a tool module. Each module has its own dependencies and its own releases. A tool never imports another tool's module or a deployment's repository. Library code that several tools or applications need belongs in [OpenNSW/core](https://github.com/OpenNSW/core). ## Adding a tool @@ -52,7 +52,7 @@ This installs golangci-lint, addlicense and editorconfig-checker, and runs `git ### Make targets -Each target runs in every tool module, except `editorconfig` and `secrets`, which check the whole repository. Add `/` to run a module target in one module, e.g. `make test/migrator`. Run `make` on its own to list the targets. +The Go targets run in every tool module, and `editorconfig` and `secrets` check the whole repository. Add `/` to `fmt`, `lint`, `license`, `license-check`, `tidy`, `build`, `test` or `vuln` to run it in one module, e.g. `make test/migrator`. Run `make` on its own to list the targets. | Target | What it does | |---|---| @@ -68,7 +68,7 @@ Each target runs in every tool module, except `editorconfig` and `secrets`, whic | `make vuln` | Scans dependencies with govulncheck | | `make secrets` | Scans the working tree with gitleaks | -The Makefile runs the same in sh and in Windows `cmd.exe`: each recipe line is one command, with no shell loops, conditionals, pipes or Unix-only tools. Keep it that way; CI runs it on Windows. +The Makefile runs the same in sh and in Windows `cmd.exe`: each recipe line is one command, or `cd && `, with no shell loops, `if`, pipes or Unix-only tools. Keep it that way; CI runs it on Windows. ### EditorConfig @@ -79,7 +79,7 @@ The Makefile runs the same in sh and in Windows `cmd.exe`: each recipe line is o ### Git hooks -The pre-commit hook runs against staged changes: the EditorConfig check on every staged file, and the Go checks in each tool module with staged `.go`, `go.mod` or `go.sum` files: +The pre-commit hook runs against staged changes: the EditorConfig check on every staged file, and the Go checks in each tool module with staged `.go`, `go.mod` or `go.sum` files, deleted ones included. It refuses a `.go` file outside every tool module, and a nested `go.mod`: | Step | Tool | Behaviour | |---|---|---| @@ -101,9 +101,9 @@ Bypass the hooks with `git commit --no-verify` or `git push --no-verify`, or ski | Job | What it checks | |---|---| -| Find modules | Lists the tool modules: every top-level directory with a `go.mod` | +| Find modules | Lists the tool modules, every top-level directory with a `go.mod`. Fails if a `.go` file is outside them or a `go.mod` is nested | | Module (``) | `go.mod` tidy, golangci-lint, license headers, tests with `-race` and gosec (informational), with the Go version in `go.mod`; then govulncheck with the latest stable Go | -| Make on Windows | The Makefile under `cmd.exe`: `editorconfig`, `license-check`, `lint`, `tidy`, `build` and `test` | +| Make on Windows | The Makefile under `cmd.exe`, with the latest stable Go: `editorconfig`, `license-check`, `lint`, `tidy`, `build` and `test` | | EditorConfig | Every file against `.editorconfig` | | Secret Scan | gitleaks | | CI | Passes only when every job above passed. Branch protection requires it | diff --git a/Makefile b/Makefile index 9aa0f8f..e33315d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -# Every recipe line is a single command that runs the same in sh and in -# Windows cmd.exe: no shell loops, conditionals, pipes or Unix-only tools. -# Lists and loops use GNU make functions instead. +# Every recipe line is one command, or `cd && `, and runs the +# same in sh and in Windows cmd.exe: no shell loops, if, pipes or Unix-only +# tools. Lists and loops use GNU make functions instead. GOLANGCI_VERSION := v2.15.0 # The newest editorconfig-checker that builds with Go 1.26; later releases @@ -39,7 +39,8 @@ help: $(info secrets Scan the working tree for secrets with gitleaks (whole repo)) $(info check secrets, editorconfig, license-check, lint, tidy and build) $(info ) - $(info Run a target for one tool with /, e.g. make test/migrator) + $(info fmt, lint, license, license-check, tidy, build, test and vuln also run) + $(info for one tool as /, e.g. make test/migrator) $(info Tools: $(MODULES)) @cd .