Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tool>/ 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 <tool>/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.
Expand Down
22 changes: 19 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,25 @@ jobs:
- uses: actions/checkout@v7
with:
persist-credentials: false
# Fails when a go.mod isn't at <tool>/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 <tool>/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"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 0 additions & 16 deletions .gitleaks.toml

This file was deleted.

6 changes: 3 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<tool>/go.mod` declares `github.com/OpenNSW/tools/<tool>`.
- Each tool is its own Go module in its own directory: `<tool>/go.mod` declares `github.com/OpenNSW/tools/<tool>`. 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 `<tool>/cmd/<command>/`.
- 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.
Expand All @@ -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/<tool>` for one. Every target takes `/<tool>`.
- `make test`: race-enabled tests in every tool module; `make test/<tool>` for one. `fmt`, `lint`, `license`, `license-check`, `tidy`, `build`, `test` and `vuln` all take `/<tool>`.
- `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 <dir> && <command>`, that runs in both sh and Windows `cmd.exe`; CI runs the Makefile on Windows.

## Compatibility

Expand Down
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Each tool lives in its own directory and is its own Go module:
└── cmd/<command>/ 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

Expand Down Expand Up @@ -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 `/<tool>` 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 `/<tool>` 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 |
|---|---|
Expand All @@ -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 <dir> && <command>`, with no shell loops, `if`, pipes or Unix-only tools. Keep it that way; CI runs it on Windows.

### EditorConfig

Expand All @@ -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 |
|---|---|---|
Expand All @@ -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 (`<tool>`) | `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 |
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 <dir> && <command>`, 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
Expand Down Expand Up @@ -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 <target>/<tool>, e.g. make test/migrator)
$(info fmt, lint, license, license-check, tidy, build, test and vuln also run)
$(info for one tool as <target>/<tool>, e.g. make test/migrator)
$(info Tools: $(MODULES))
@cd .

Expand Down