diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..2eeac69 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,27 @@ +name: ci + +on: + pull_request: + push: + branches: [main] + +jobs: + check: + name: build and test + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: false + + - name: Run checks + run: ./scripts/check.sh + + - name: Check dependency freshness + run: ./scripts/check-deps.sh diff --git a/README.md b/README.md index e82115b..09493af 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,12 @@ mise run lint # Install locally 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 @@ -534,6 +540,7 @@ stamp/ ├── internal/ │ ├── changeset/ # Stamp file parsing, writing, and slug generation │ ├── changelog/ # CHANGELOG.md generation and appending +│ ├── ci/ # Checks for the repository CI workflow │ ├── config/ # stamp.toml loading, validation, and saving │ ├── git/ # Git operations (tag, commit, push, branch management) │ ├── gitea/ # Gitea API client (releases, PR comments, release PRs) @@ -549,4 +556,4 @@ stamp/ ## License -MIT \ No newline at end of file +MIT diff --git a/docs/workflow.md b/docs/workflow.md index 90b4c25..7053087 100644 --- a/docs/workflow.md +++ b/docs/workflow.md @@ -18,6 +18,43 @@ It works with any project type and supports monorepos. --- +## Continuous Integration + +Every pull request and every push to `main` runs the `ci` workflow in +[`.gitea/workflows/ci.yml`](../.gitea/workflows/ci.yml). It has a single `check` +job with two steps: + +1. [`scripts/check.sh`](../scripts/check.sh) first validates the workflow and + the dependency-policy scripts themselves, then fails if any Go file is not + `gofmt`-formatted, and finally runs `go mod verify`, `go vet ./...`, + `go build ./...`, and `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 of every pinned module. When the proxy is disabled it uses + the dependency facts prepared by the execution profile (the file named by + `MAINTAINER_DEPENDENCY_FACTS`), which record the newest release observed on + `proxy.golang.org` and when. If neither source is available the script + exits with status 2 instead of assuming the pins are current; the local + module cache is never used as evidence of what the newest release is. + +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: + +```sh +./scripts/check.sh +./scripts/check-deps.sh # or: mise run deps +``` + +[`internal/ci/workflow_test.go`](../internal/ci/workflow_test.go) parses the +workflow and fails `go test ./...` if it stops running on pull requests or +stops invoking those scripts; [`scripts/test-ci-workflow.sh`](../scripts/test-ci-workflow.sh) +and [`scripts/test-check-deps.sh`](../scripts/test-check-deps.sh) cover the same +behaviour without needing a Go module download. + +--- + ## Two Release Workflows ### Direct release @@ -483,4 +520,4 @@ Tag format: `@v` for monorepos (e.g. `my-app@v1.3.0`), or `v " for every module required by go.mod. +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 } + ' +} + +# behind reports whether version $1 is older than version $2. +behind() { + [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" = "$2" ] +} + +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 2 + 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 (module proxy)" + exit 0 +fi + +# Offline: fall back to the dependency facts prepared by the execution profile. +# The facts file is refreshed by the execution profile, so a reader can race +# with an atomic rewrite; give it a moment before declaring the facts missing. +if [ -n "$facts_file" ]; then + for _ in 1 2 3 4 5 6 7 8 9 10; do + [ -r "$facts_file" ] && break + sleep 0.2 + done +fi +if [ -z "$facts_file" ] || [ ! -r "$facts_file" ]; then + echo 'cannot verify dependency freshness: the module proxy is disabled (GOPROXY=off)' \ + 'and MAINTAINER_DEPENDENCY_FACTS does not name a readable dependency-facts file' >&2 + exit 2 +fi +field() { + grep -o "\"$1\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" "$facts_file" | head -n 1 | sed 's/.*:[[:space:]]*"\(.*\)"/\1/' +} + +schema="$(grep -o '"schema_version"[[:space:]]*:[[:space:]]*[0-9]*' "$facts_file" | head -n 1 | grep -o '[0-9]*$' || true)" +source="$(field source)" +freshness="$(field freshness_status)" +scope="$(field query_scope)" +observed_at="$(field observed_at)" +digest="$(field manifest_digest)" + +if [ "$schema" != 1 ]; then + echo "cannot verify dependency freshness: unsupported dependency-facts schema (${schema:-missing})" >&2 + exit 2 +fi +if [ "$source" != 'https://proxy.golang.org' ]; then + echo "cannot verify dependency freshness: unexpected dependency-facts source '${source}'" >&2 + exit 2 +fi +if [ "$freshness" != 'available' ]; then + echo "cannot verify dependency freshness: dependency facts are not marked available ('${freshness}')" >&2 + exit 2 +fi +if [ "$scope" != 'module_path_latest' ]; then + echo "cannot verify dependency freshness: dependency facts use query scope '${scope}', want module_path_latest" >&2 + exit 2 +fi +if [ -z "$observed_at" ]; then + echo 'cannot verify dependency freshness: dependency facts have no observation time' >&2 + exit 2 +fi + +# One record per observed module: name, latest version and item status. +records="$(grep -o '{[^{}]*"latest"[^{}]*' "$facts_file" || true)" +if [ -z "$records" ]; then + echo 'cannot verify dependency freshness: dependency facts contain no module observations' >&2 + exit 2 +fi + +latest_for() { + local want="$1" record name latest status + while IFS= read -r record; do + name="$(printf '%s\n' "$record" | sed -n 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" + [ "$name" = "$want" ] || continue + latest="$(printf '%s\n' "$record" | sed -n 's/.*"latest"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" + status="$(printf '%s\n' "$record" | sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" + [ "$status" = 'observed' ] || return 1 + printf '%s\n' "$latest" + return 0 + done <<<"$records" + return 1 +} + +stale='' +unverified='' +while read -r path version; do + if ! latest="$(latest_for "$path")"; then + unverified+=" $path $version (not covered by the dependency facts)"$'\n' + continue + fi + if behind "$version" "$latest"; then + stale+=" $path $version (latest observed: $latest)"$'\n' + elif [ "$version" != "$latest" ]; then + unverified+=" $path $version (pin is newer than the latest observation $latest)"$'\n' + fi +done <<<"$pins" + +if [ -n "$stale" ]; then + echo 'These go.mod pins are behind the latest observed releases:' >&2 + printf '%s' "$stale" >&2 + exit 1 +fi +if [ -n "$unverified" ]; then + echo 'These go.mod pins could not be checked against the dependency facts:' >&2 + printf '%s' "$unverified" >&2 + exit 1 +fi + +echo "ok: $count go.mod requirements match the latest releases observed by the execution profile" +echo "source: $source, observed at $observed_at (module_path_latest)" +if [ -n "$digest" ]; then + echo "note: dependency-facts manifest digest: $digest" +fi diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..7128232 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# +# check.sh runs every verification step required before a change can land. +# `.gitea/workflows/ci.yml` invokes it on every pull request and every push to +# main, so this script is the single source of truth for what "CI passed" means. + +set -euo pipefail + +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +echo '==> ci workflow' +./scripts/test-ci-workflow.sh + +echo '==> dependency check tests' +./scripts/test-check-deps.sh + +echo '==> gofmt' +unformatted="$(gofmt -l .)" +if [ -n "$unformatted" ]; then + echo 'These files are not gofmt-formatted:' >&2 + printf '%s\n' "$unformatted" >&2 + exit 1 +fi + +echo '==> go mod verify' +go mod verify + +echo '==> go vet' +go vet ./... + +echo '==> go build' +go build ./... + +echo '==> go test' +go test -cover ./... diff --git a/scripts/test-check-deps.sh b/scripts/test-check-deps.sh new file mode 100755 index 0000000..de1fa79 --- /dev/null +++ b/scripts/test-check-deps.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +# +# test-check-deps.sh exercises scripts/check-deps.sh without a module proxy or +# network access. It builds throwaway manifests in a temporary directory and +# asserts the script's exit status and diagnostics. + +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +script="$here/check-deps.sh" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +failures=0 +expect_status() { + local want="$1" got + shift + set +e + "$@" >"$tmp/out" 2>&1 + got=$? + set -e + if [ "$got" -ne "$want" ]; then + echo "FAIL: expected exit $want, got $got: $*" >&2 + cat "$tmp/out" >&2 + failures=$((failures + 1)) + fi +} + +# fixture sets up $tmp/repo/go.mod and prints the go.mod path. +fixture() { + mkdir -p "$tmp/repo" + cat >"$tmp/repo/go.mod" <<'EOF' +module example.com/fixture + +go 1.27.1 + +require ( + github.com/a/b v1.0.0 + github.com/c/d v1.3.0 +) +EOF +} + +# facts writes a dependency-facts document to $tmp/facts.json. +# Arguments: +facts() { + local freshness="$1" + shift + local items='' + local item + for item in "$@"; do + [ -z "$items" ] || items+=',' + items+="$item" + done + cat >"$tmp/facts.json" <&2; failures=$((failures + 1)); } + +# 2. A stale pin is reported and fails. +fixture +facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.2.0","status":"observed"},{"name":"github.com/c/d","current":"v1.3.0","latest":"v1.3.0","status":"observed"}' +expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" +grep -q 'github.com/a/b v1.0.0 (latest observed: v1.2.0)' "$tmp/out" || { echo 'FAIL: stale module not named' >&2; failures=$((failures + 1)); } + +# 3. A pin that is newer than the observation cannot be verified. +fixture +facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.0.0","status":"observed"}' +expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" +grep -q 'not covered by the dependency facts' "$tmp/out" || { echo 'FAIL: uncovered module not reported' >&2; failures=$((failures + 1)); } + +# 4. Without a proxy and without facts nothing is verified. +fixture +expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/missing.json" GOPROXY=off "$script" +grep -q 'cannot verify dependency freshness' "$tmp/out" || { echo 'FAIL: unverified run did not explain itself' >&2; failures=$((failures + 1)); } + +# 5. Incomplete dependency facts are not trusted. +fixture +facts unavailable "$up_to_date" +expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" + +# 6. Items that were not observed are not treated as current. +fixture +facts available '{"name":"github.com/a/b","current":"v1.0.0","latest":"v1.0.0","status":"not_found"},{"name":"github.com/c/d","current":"v1.3.0","latest":"v1.3.0","status":"observed"}' +expect_status 1 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/facts.json" GOPROXY=off "$script" + +# 7. A facts document from a different source is rejected. +fixture +sed 's#https://proxy.golang.org#https://example.invalid#' "$tmp/facts.json" >"$tmp/other.json" +expect_status 2 env CHECK_DEPS_ROOT="$tmp/repo" MAINTAINER_DEPENDENCY_FACTS="$tmp/other.json" GOPROXY=off "$script" + +if [ "$failures" -ne 0 ]; then + echo "$failures check-deps test(s) failed" >&2 + exit 1 +fi +echo 'ok: check-deps.sh behaviour is covered' diff --git a/scripts/test-ci-workflow.sh b/scripts/test-ci-workflow.sh new file mode 100755 index 0000000..e53b907 --- /dev/null +++ b/scripts/test-ci-workflow.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# +# test-ci-workflow.sh checks the properties of .gitea/workflows/ci.yml that +# gate pull requests. It runs without Go modules so CI configuration can be +# validated even when the module proxy is unavailable; the equivalent Go test +# in internal/ci/workflow_test.go parses the same file as YAML. + +set -euo pipefail + +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +workflow="$root/.gitea/workflows/ci.yml" + +if [ ! -f "$workflow" ]; then + echo "missing workflow: $workflow" >&2 + exit 1 +fi + +failures=0 +require_line() { + if ! grep -qF "$1" "$workflow"; then + echo "workflow is missing: $1" >&2 + failures=$((failures + 1)) + fi +} + +require_line 'on:' +require_line ' pull_request:' +require_line ' push:' +require_line 'branches: [main]' +require_line 'run: ./scripts/check.sh' +require_line 'run: ./scripts/check-deps.sh' +require_line 'go-version-file: go.mod' + +# The workflow must be a single action so "all checks passed" has one meaning. +jobs="$(awk '/^jobs:/{j=1;next} j && /^ [a-z0-9_-]+:$/{n++} END{print n+0}' "$workflow")" +if [ "$jobs" -ne 1 ]; then + echo "workflow must define exactly one job, found $jobs" >&2 + failures=$((failures + 1)) +fi + +if [ "$failures" -ne 0 ]; then + exit 1 +fi +echo 'ok: the pull request workflow runs the repository checks'