Make dependency checks offline-safe and guard the CI workflow with tests

scripts/check-deps.sh previously ran `go list -m all`, which fails with
"module lookup disabled by GOPROXY=off" whenever the module cache does not
hold the full module graph, even though the build only needs part of it.
Read the pins with `go mod edit -json` instead: the online branch asks the
module proxy about only the pinned modules, and the offline branch compares
those pins with the newest versions in the module cache. Neither path loads
the whole graph, and an empty cache is reported instead of failing.

Run the dependency check from the single check job that already gates pull
requests, so one action covers both the code/tests and dependency freshness.

Add internal/ci tests that parse .gitea/workflows/ci.yml, fail if the
workflow stops running on pull requests or stops invoking the check scripts,
and fail if those scripts are not executable. Document the two-step job and
the new mise tasks.
This commit is contained in:
software-maintainer committed 2026-10-28 08:57:59 +00:00
1 parent 22e4c96192
commit 050936c6fd
5 files changed
+225 -47

No files matched your search

+4
View File
@@ -9,6 +9,7 @@ jobs:
check: check:
name: build and test name: build and test
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 20
steps: steps:
- name: Check out repository - name: Check out repository
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -21,3 +22,6 @@ jobs:
- name: Run checks - name: Run checks
run: ./scripts/check.sh run: ./scripts/check.sh
- name: Check dependency freshness
run: ./scripts/check-deps.sh
+8 -1
View File
@@ -524,6 +524,12 @@ mise run lint
# Install locally # Install locally
mise run install mise run install
# Run the same checks CI runs (format, vet, build, tests)
mise run check
# Check that every dependency in go.mod is the newest release
mise run deps
``` ```
## Project Structure ## Project Structure
@@ -534,6 +540,7 @@ stamp/
├── internal/ ├── internal/
│ ├── changeset/ # Stamp file parsing, writing, and slug generation │ ├── changeset/ # Stamp file parsing, writing, and slug generation
│ ├── changelog/ # CHANGELOG.md generation and appending │ ├── changelog/ # CHANGELOG.md generation and appending
│ ├── ci/ # Checks for the repository CI workflow
│ ├── config/ # stamp.toml loading, validation, and saving │ ├── config/ # stamp.toml loading, validation, and saving
│ ├── git/ # Git operations (tag, commit, push, branch management) │ ├── git/ # Git operations (tag, commit, push, branch management)
│ ├── gitea/ # Gitea API client (releases, PR comments, release PRs) │ ├── gitea/ # Gitea API client (releases, PR comments, release PRs)
@@ -549,4 +556,4 @@ stamp/
## License ## License
MIT MIT
+16 -12
View File
@@ -21,26 +21,30 @@ It works with any project type and supports monorepos.
## Continuous Integration ## Continuous Integration
Every pull request and every push to `main` runs the `ci` workflow in Every pull request and every push to `main` runs the `ci` workflow in
[`.gitea/workflows/ci.yml`](../.gitea/workflows/ci.yml). Its single `check` job [`.gitea/workflows/ci.yml`](../.gitea/workflows/ci.yml). It has a single `check`
runs [`scripts/check.sh`](../scripts/check.sh), which: job with two steps:
1. fails if any Go file is not `gofmt`-formatted, 1. [`scripts/check.sh`](../scripts/check.sh) fails if any Go file is not
2. runs `go mod verify` to confirm the dependency modules match `go.sum`, `gofmt`-formatted, then runs `go mod verify`, `go vet ./...`,
3. runs `go vet ./...` and `go build ./...`, `go build ./...`, and `go test -cover ./...`.
4. runs `go test -cover ./...`. 2. [`scripts/check-deps.sh`](../scripts/check-deps.sh) fails when a module
pinned in `go.mod` has a newer release. It asks the module proxy for the
newest release and falls back to the newest versions in the local module
cache when the proxy is disabled.
The job fails when any check fails, so a pull request cannot land with broken
code or tests, or with stale dependencies.
Run the same checks locally with: Run the same checks locally with:
```sh ```sh
./scripts/check.sh ./scripts/check.sh
./scripts/check-deps.sh # or: mise run deps
``` ```
The job fails when any check fails, so a pull request cannot land with broken [`internal/ci/workflow_test.go`](../internal/ci/workflow_test.go) parses the
code or tests. workflow and fails `go test ./...` if it stops running on pull requests or
stops invoking those scripts.
Dependency freshness is checked separately with `mise run deps` (or
[`scripts/check-deps.sh`](../scripts/check-deps.sh)), which fails when a module
pinned in `go.mod` has a newer release available.
--- ---
+113
View File
@@ -0,0 +1,113 @@
// Package ci tests the repository's Gitea Actions configuration.
package ci
import (
"os"
"path/filepath"
"sort"
"testing"
"gopkg.in/yaml.v3"
)
// workflowPath is the workflow that gates every pull request.
const workflowPath = "../../.gitea/workflows/ci.yml"
func TestPullRequestWorkflow(t *testing.T) {
data, err := os.ReadFile(filepath.FromSlash(workflowPath))
if err != nil {
t.Fatalf("read %s: %v", workflowPath, err)
}
var doc yaml.Node
if err := yaml.Unmarshal(data, &doc); err != nil {
t.Fatalf("parse %s: %v", workflowPath, err)
}
if len(doc.Content) != 1 || doc.Content[0].Kind != yaml.MappingNode {
t.Fatalf("%s must be a single YAML mapping", workflowPath)
}
workflow := mapping(doc.Content[0])
on := workflow["on"]
if on == nil {
t.Fatal("workflow has no on: trigger")
}
if mapping(on)["pull_request"] == nil {
t.Error("workflow must run on every pull request")
}
jobsNode := workflow["jobs"]
if jobsNode == nil {
t.Fatal("workflow has no jobs")
}
jobs := mapping(jobsNode)
if len(jobs) != 1 {
t.Errorf("workflow must be one action, got %d jobs: %v", len(jobs), keys(jobs))
}
job := jobs["check"]
if job == nil {
t.Fatalf("workflow has no check job, got %v", keys(jobs))
}
if got := scalar(mapping(job)["runs-on"]); got == "" {
t.Error("check job has no runs-on label")
}
steps := mapping(job)["steps"]
if steps == nil || steps.Kind != yaml.SequenceNode {
t.Fatal("check job has no steps")
}
runs := map[string]bool{}
for _, step := range steps.Content {
runs[scalar(mapping(step)["run"])] = true
}
for _, script := range []string{"./scripts/check.sh", "./scripts/check-deps.sh"} {
if !runs[script] {
t.Errorf("check job does not run %s", script)
}
}
}
// mapping returns the mapping nodes of n keyed by their scalar key.
func mapping(n *yaml.Node) map[string]*yaml.Node {
out := map[string]*yaml.Node{}
if n == nil || n.Kind != yaml.MappingNode {
return out
}
for i := 0; i < len(n.Content)-1; i += 2 {
out[n.Content[i].Value] = n.Content[i+1]
}
return out
}
// scalar returns the value of a scalar node, or the empty string.
func scalar(n *yaml.Node) string {
if n == nil || n.Kind != yaml.ScalarNode {
return ""
}
return n.Value
}
// keys returns the sorted keys of m for error messages.
func keys(m map[string]*yaml.Node) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}
func TestCheckScriptsAreExecutable(t *testing.T) {
for _, script := range []string{"../../scripts/check.sh", "../../scripts/check-deps.sh"} {
info, err := os.Stat(filepath.FromSlash(script))
if err != nil {
t.Errorf("stat %s: %v", script, err)
continue
}
if info.Mode()&0o111 == 0 {
t.Errorf("%s is not executable; the ci workflow runs it directly", script)
}
}
}
+84 -34
View File
@@ -1,39 +1,32 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# check-deps.sh verifies that every module pinned in go.mod is the newest # check-deps.sh fails when a module required by go.mod is not the newest
# version available. # version available.
# #
# When the module proxy is reachable, `go list -m -u` is authoritative and any # When the module proxy is reachable, `go list -m -u` is authoritative: it
# module with a newer release fails the check. When the proxy is disabled (for # asks the proxy for the newest release of each pinned module. When the proxy
# example in an offline build), the script falls back to comparing each pin # is disabled (offline builds, sandboxes), the script compares the pins with
# with the newest version present in the local module cache, which still # the newest versions recorded in the local module cache instead.
# catches pins that were left behind after a newer version was downloaded. #
# The script never loads the full module graph, so it works even when the cache
# only holds the modules needed by the build.
set -euo pipefail set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.." cd "$(dirname "${BASH_SOURCE[0]}")/.."
echo '==> module graph is loadable' # requirements prints "<path> <version>" for every module required by go.mod.
go list -m -e all >/dev/null requirements() {
go mod edit -json | awk -F'"' '
/^\t"Require": \[/ { in_require = 1 }
in_require && /^\t\],?$/ { in_require = 0 }
in_require && /"Path":/ { path = $4 }
in_require && /"Version":/ { print path, $4 }
'
}
echo '==> modules are up to date' # escape_path applies the case encoding used by the module cache for paths with
updates="$(go list -m -u -e -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' all | awk 'NF == 3')" # upper-case letters (for example BurntSushi becomes !burnt!sushi).
if [ -n "$updates" ]; then
echo 'These modules have newer versions available:' >&2
printf '%s\n' "$updates" >&2
exit 1
fi
if [ "$(go env GOPROXY)" != "off" ]; then
exit 0
fi
# The module proxy is disabled, so `go list -m -u` cannot report updates.
# Compare the pins in go.mod with the versions in the local module cache.
cache="$(go env GOMODCACHE)/cache/download"
# escape_path applies the case encoding used by the module cache for paths that
# contain upper-case letters (for example BurntSushi becomes !burnt!sushi).
escape_path() { escape_path() {
local path="$1" out='' char i local path="$1" out='' char i
for ((i = 0; i < ${#path}; i++)); do for ((i = 0; i < ${#path}; i++)); do
@@ -47,28 +40,85 @@ escape_path() {
printf '%s' "$out" printf '%s' "$out"
} }
# newest prints the newest version in the stdin version list. Version lists in
# the module cache are unsorted and include pre-releases, so sort them with
# `sort -V` and then prefer a release to its own pre-releases
# (v1.2.0 outranks v1.2.0-rc1, while v1.2.1-0.20240101 outranks v1.2.0).
newest() {
local versions latest base
versions="$(grep -E '^v[0-9]' || true)"
if [ -z "$versions" ]; then
return 1
fi
latest="$(printf '%s\n' "$versions" | sort -V | tail -n 1)"
base="${latest%%-*}"
if printf '%s\n' "$versions" | grep -qxF "$base"; then
latest="$base"
fi
printf '%s\n' "$latest"
}
pins="$(requirements)"
if [ -z "$pins" ]; then
echo 'go.mod requires no modules' >&2
exit 1
fi
count="$(printf '%s\n' "$pins" | wc -l | tr -d ' ')"
if [ "$(go env GOPROXY)" != "off" ]; then
mods=()
while read -r path version; do
mods+=("$path@$version")
done <<<"$pins"
if ! listing="$(go list -m -u -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' "${mods[@]}")"; then
echo 'could not query the module proxy for newer versions' >&2
exit 1
fi
updates="$(printf '%s\n' "$listing" | awk 'NF == 3')"
if [ -n "$updates" ]; then
echo 'These modules have newer versions available:' >&2
printf '%s\n' "$updates" >&2
exit 1
fi
echo "ok: $count go.mod requirements are up to date"
exit 0
fi
cache="$(go env GOMODCACHE)/cache/download"
checked=0 checked=0
unchecked=0
stale='' stale=''
while read -r path version; do while read -r path version; do
list="$cache/$(escape_path "$path")/@v/list" list="$cache/$(escape_path "$path")/@v/list"
if [ ! -f "$list" ]; then if [ ! -f "$list" ]; then
unchecked=$((unchecked + 1))
continue continue
fi fi
checked=$((checked + 1)) checked=$((checked + 1))
latest="$(sort -V "$list" | tail -n 1)" latest="$(newest <"$list")"
if [ "$latest" != "$version" ]; then if [ "$latest" = "$version" ]; then
stale+="$path $version (newest in cache: $latest)"$'\n' continue
fi fi
done < <(go mod edit -json | awk -F'"' '/"Path":/ { path = $4 } /"Version":/ { print path, $4 }') # Only complain when the cached version really is newer than the pin; a
# pin that is newer than everything cached is simply not verifiable here.
if [ "$(printf '%s\n%s\n' "$version" "$latest" | sort -V | tail -n 1)" = "$latest" ]; then
stale+=" $path $version (newest cached: $latest)"$'\n'
fi
done <<<"$pins"
if [ -n "$stale" ]; then if [ -n "$stale" ]; then
echo 'These go.mod pins are not the newest cached versions:' >&2 echo 'These go.mod pins are behind the newest cached version:' >&2
printf '%s' "$stale" >&2 printf '%s' "$stale" >&2
exit 1 exit 1
fi fi
if [ "$checked" -eq 0 ]; then if [ "$checked" -eq 0 ]; then
echo 'note: proxy disabled and module cache is empty; freshness not checked' >&2 echo "note: GOPROXY=off and $cache is empty; dependency freshness was not checked" >&2
else exit 0
echo "all $checked go.mod pins match the newest versions in the module cache"
fi fi
if [ "$unchecked" -ne 0 ]; then
echo "note: $unchecked of $count go.mod requirements are missing from the module cache" >&2
fi
echo "ok: $checked go.mod requirements match the newest versions in the module cache"