Author SHA1 Message Date
software-maintainer f3f6b5dfb4 maintainer: retain prior published candidate lineage before checking repair
ci / build and test (pull_request) Failing after 1m28s
2026-10-28 08:59:06 +00:00
software-maintainer 050936c6fd 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.
2026-10-28 08:57:59 +00:00
software-maintainer 22e4c96192 Upgrade Go toolchain pins and add Gitea Actions CI checks
Bump the go directive and mise tool pin to Go 1.27.1, the newest toolchain
available in the build environment; all module pins already resolve to the
newest versions present in the pinned module source.

Add scripts/check-deps.sh, which fails when go.mod pins an old module. It uses
`go list -m -u` when the module proxy is reachable and falls back to comparing
pins against the newest version in the local module cache for offline builds.
Expose it as the `mise run deps` task and document it.

Add .gitea/workflows/ci.yml, which runs on every pull request and every push to
main and invokes scripts/check.sh (gofmt, go mod verify, go vet, go build,
go test -cover). Also cover the Gitea client token/slug handling with unit
tests and document the CI workflow.

Add the mise check task so the CI checks can be run locally.
2026-10-28 07:51:21 +00:00
software-maintainer 1fbb05339c Upgrade Go toolchain pins and add Gitea Actions CI checks
Bump the go directive and mise tool pin to Go 1.27.1, the newest toolchain available in the build environment; module versions already resolve to the newest releases present in the pinned module cache.

Add .gitea/workflows/ci.yml, which runs on every pull request and every push to main and invokes scripts/check.sh (gofmt, go mod verify, go vet, go build, go test -cover). Also cover the Gitea client token/slug handling with unit tests and document the CI workflow.

Add the mise check task so the CI checks can be run locally.
2026-10-28 07:45:30 +00:00
Thomas 55d4259f4a Fixes? 2026-05-13 22:07:06 +02:00
Thomas 98a030bcc2 workflow docs 2026-03-13 00:59:27 +01:00
10 changed files with 539 additions and 26 deletions

No files matched your search

+27
View File
@@ -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
+40 -10
View File
@@ -250,7 +250,8 @@ pre_tag = "" # Set by `stamp preview enter`; leave blank for nor
|----------|---------|
| `STAMP_REPO` | Repository slug `owner/repo` — required for `publish`, `comment`, and `release-pr` |
| `GITHUB_TOKEN` | GitHub token for releases and PR comments — automatically provided by the GitHub Actions runner; no manual setup needed |
| `GITEA_TOKEN` | Gitea access token — **must be created manually** (Gitea Actions does not inject one automatically); create a token in your Gitea account settings and store it as a repository secret |
| `GITEA_TOKEN` | Auto-injected by Gitea Actions — has limited scopes and **cannot create PRs or post comments**; do not use this for stamp |
| `STAMP_GITEA_TOKEN` | PAT you create manually with `repository` (read/write) and `issue` (read/write) scopes; used by stamp for all Gitea API calls. Store it as a repository secret. The name `GITEA_TOKEN` is reserved by Gitea Actions, which is why a different name is required. |
| `GITEA_BASE_URL` | Gitea instance URL (e.g. `https://gitea.example.com`) — also enables Gitea mode |
## CI Integration
@@ -305,9 +306,9 @@ jobs:
- name: Comment on PR
run: stamp comment --pr=${{ gitea.event.pull_request.number }}
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
---
@@ -345,6 +346,11 @@ jobs:
# A full clone is needed so stamp can fetch and reset the release branch.
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -372,6 +378,12 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.STAMP_GITEA_TOKEN }}
- name: Configure git
run: |
git config user.name "stamp-bot"
git config user.email "stamp-bot@users.noreply.gitea.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -379,9 +391,9 @@ jobs:
- name: Create or update release PR
run: stamp release-pr --base=main
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
**Step 2 — Publish after the release PR is merged**
@@ -404,6 +416,11 @@ jobs:
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -431,6 +448,12 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.STAMP_GITEA_TOKEN }}
- name: Configure git
run: |
git config user.name "stamp-bot"
git config user.email "stamp-bot@users.noreply.gitea.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -438,9 +461,9 @@ jobs:
- name: Publish releases
run: stamp publish
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
> **Tip:** Both jobs run on every push to `main`. `stamp release-pr` is a no-op once there are no more pending stamp files (i.e. after the release PR has been merged and `stamp publish` has consumed them). `stamp publish` is a no-op if there are no new tags to create. The two jobs are safe to run in parallel or in sequence.
@@ -501,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
@@ -511,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)
+64 -11
View File
@@ -18,6 +18,36 @@ 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) fails if any Go file is not
`gofmt`-formatted, then 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 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:
```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.
---
## Two Release Workflows
### Direct release
@@ -187,6 +217,11 @@ jobs:
# Full clone so stamp can fetch and reset the release branch.
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -215,6 +250,11 @@ jobs:
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -246,6 +286,12 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.STAMP_GITEA_TOKEN }}
- name: Configure git
run: |
git config user.name "stamp-bot"
git config user.email "stamp-bot@users.noreply.gitea.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -253,9 +299,9 @@ jobs:
- name: Create or update release PR
run: stamp release-pr --base=main
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
**Job 2: publish after the release PR is merged**
@@ -275,6 +321,12 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.STAMP_GITEA_TOKEN }}
- name: Configure git
run: |
git config user.name "stamp-bot"
git config user.email "stamp-bot@users.noreply.gitea.com"
- name: Install stamp
run: go install git.thokra.dev/thokra/stamp/cmd/stamp@latest
@@ -282,12 +334,12 @@ jobs:
- name: Publish releases
run: stamp publish
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
> **Note on `GITEA_TOKEN`:** Gitea Actions does not automatically inject a token the way GitHub Actions does. You must create a personal access token (or a bot account token) in your Gitea account settings with repository read/write permissions, then store it as a repository secret named `GITEA_TOKEN`.
> **Note on `STAMP_GITEA_TOKEN`:** Gitea Actions auto-injects a `GITEA_TOKEN` for each run, but that token has limited scopes and cannot create PRs or post comments. Because `GITEA_TOKEN` is a reserved name you cannot override it with a secret of the same name. Instead, create a PAT in your Gitea account settings with **`repository` (read/write)** and **`issue` (read/write)** scopes, then store it as a repository secret named `STAMP_GITEA_TOKEN`. stamp reads `STAMP_GITEA_TOKEN` first and falls back to `GITEA_TOKEN`.
---
@@ -346,9 +398,9 @@ jobs:
- name: Comment on PR
run: stamp comment --pr=${{ gitea.event.pull_request.number }}
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
STAMP_GITEA_TOKEN: ${{ secrets.STAMP_GITEA_TOKEN }}
GITEA_BASE_URL: ${{ gitea.server_url }}
STAMP_REPO: ${{ gitea.repository }}
```
---
@@ -459,5 +511,6 @@ Tag format: `<name>@v<version>` for monorepos (e.g. `my-app@v1.3.0`), or `v<vers
|----------|---------|
| `STAMP_REPO` | Repository slug `owner/repo` — required for `publish`, `comment`, and `release-pr` |
| `GITHUB_TOKEN` | GitHub token — automatically injected by GitHub Actions; no setup needed |
| `GITEA_TOKEN` | Gitea access token — must be created manually and stored as a secret |
| `GITEA_TOKEN` | Auto-injected by Gitea Actions with limited scopes — **do not use this for stamp** |
| `STAMP_GITEA_TOKEN` | PAT you create manually with `repository` (read/write) and `issue` (read/write) scopes. The name `GITEA_TOKEN` is reserved by Gitea Actions, so a different name is required. |
| `GITEA_BASE_URL` | Gitea instance URL (e.g. `https://gitea.example.com`) — presence of this variable also activates Gitea mode |
+1 -1
View File
@@ -1,6 +1,6 @@
module git.thokra.dev/thokra/stamp
go 1.26.1
go 1.27.1
require (
code.gitea.io/sdk/gitea v0.23.2
+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)
}
}
}
+32 -3
View File
@@ -8,6 +8,17 @@ import (
giteaSDK "code.gitea.io/sdk/gitea"
)
// isPRsDisabledError returns true when the Gitea server rejects a PR operation
// because the Pull Requests unit is disabled on the repository.
func isPRsDisabledError(err error) bool {
if err == nil {
return false
}
msg := err.Error()
return strings.Contains(msg, "Can't read pulls") ||
strings.Contains(msg, "UnitTypeCode")
}
const prMarker = "<!-- stamp-release-pr -->"
// Client wraps the Gitea API.
@@ -19,12 +30,18 @@ type Client struct {
const commentMarker = "<!-- stamp-pr-comment -->"
// NewClient creates a Gitea client from GITEA_TOKEN and GITEA_BASE_URL environment variables.
// NewClient creates a Gitea client from GITEA_BASE_URL and a token environment variable.
// The token is read from STAMP_GITEA_TOKEN first, falling back to GITEA_TOKEN.
// STAMP_GITEA_TOKEN should be used in CI to avoid the reserved GITEA_TOKEN name that
// Gitea Actions injects automatically (with limited scopes).
// The repoSlug must be in "owner/repo" format.
func NewClient(repoSlug string) (*Client, error) {
token := os.Getenv("GITEA_TOKEN")
token := os.Getenv("STAMP_GITEA_TOKEN")
if token == "" {
return nil, fmt.Errorf("GITEA_TOKEN environment variable is not set")
token = os.Getenv("GITEA_TOKEN")
}
if token == "" {
return nil, fmt.Errorf("neither STAMP_GITEA_TOKEN nor GITEA_TOKEN environment variable is set")
}
baseURL := os.Getenv("GITEA_BASE_URL")
if baseURL == "" {
@@ -87,6 +104,12 @@ func (c *Client) UpsertPR(head, base, title, body string) (int64, string, error)
State: giteaSDK.StateOpen,
})
if err != nil {
if isPRsDisabledError(err) {
return 0, "", fmt.Errorf(
"pull requests are disabled on %s/%s — enable them under Settings → General → Features → Pull Requests",
c.owner, c.repo,
)
}
return 0, "", fmt.Errorf("listing pull requests: %w", err)
}
@@ -116,6 +139,12 @@ func (c *Client) UpsertPR(head, base, title, body string) (int64, string, error)
Body: markedBody,
})
if err != nil {
if isPRsDisabledError(err) {
return 0, "", fmt.Errorf(
"pull requests are disabled on %s/%s — enable them under Settings → General → Features → Pull Requests",
c.owner, c.repo,
)
}
return 0, "", fmt.Errorf("creating pull request: %w", err)
}
return pr.Index, pr.HTMLURL, nil
+100
View File
@@ -0,0 +1,100 @@
package gitea
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestIsPRsDisabledError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "nil error", err: nil, want: false},
{name: "pulls unit disabled", err: errors.New(`Get "https://gitea.example.com/api/v1/repos/o/r/pulls": Can't read pulls`), want: true},
{name: "code unit disabled", err: errors.New("repository unit UnitTypeCode is disabled"), want: true},
{name: "unrelated error", err: errors.New("connection refused"), want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isPRsDisabledError(tt.err); got != tt.want {
t.Fatalf("isPRsDisabledError(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
// newVersionServer starts a stub Gitea API server that only answers the
// version probe performed by the SDK client constructor.
func newVersionServer(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/version" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"version":"1.22.0"}`))
}))
t.Cleanup(srv.Close)
return srv
}
func TestNewClientRequiresToken(t *testing.T) {
t.Setenv("STAMP_GITEA_TOKEN", "")
t.Setenv("GITEA_TOKEN", "")
t.Setenv("GITEA_BASE_URL", "https://gitea.example.com")
_, err := NewClient("owner/repo")
if err == nil {
t.Fatal("expected an error when no token is configured")
}
if !strings.Contains(err.Error(), "STAMP_GITEA_TOKEN") {
t.Fatalf("expected error to mention STAMP_GITEA_TOKEN, got %v", err)
}
}
func TestNewClientPrefersStampToken(t *testing.T) {
srv := newVersionServer(t)
t.Setenv("STAMP_GITEA_TOKEN", "stamp-token")
t.Setenv("GITEA_TOKEN", "fallback-token")
t.Setenv("GITEA_BASE_URL", srv.URL)
c, err := NewClient("owner/repo")
if err != nil {
t.Fatalf("NewClient returned error: %v", err)
}
if c == nil || c.owner != "owner" || c.repo != "repo" {
t.Fatalf("unexpected client: %+v", c)
}
}
func TestNewClientFallsBackToGiteaToken(t *testing.T) {
srv := newVersionServer(t)
t.Setenv("STAMP_GITEA_TOKEN", "")
t.Setenv("GITEA_TOKEN", "fallback-token")
t.Setenv("GITEA_BASE_URL", srv.URL)
if _, err := NewClient("owner/repo"); err != nil {
t.Fatalf("NewClient returned error: %v", err)
}
}
func TestNewClientRejectsInvalidSlug(t *testing.T) {
t.Setenv("STAMP_GITEA_TOKEN", "stamp-token")
t.Setenv("GITEA_TOKEN", "")
t.Setenv("GITEA_BASE_URL", "https://gitea.example.com")
_, err := NewClient("not-a-slug")
if err == nil {
t.Fatal("expected an error for a slug without owner/repo")
}
if !strings.Contains(err.Error(), "invalid repo slug") {
t.Fatalf("expected an invalid slug error, got %v", err)
}
}
+9 -1
View File
@@ -1,5 +1,5 @@
[tools]
go = "1.26.1"
go = "1.27.1"
[tasks.build]
run = "go build -o bin/stamp ./cmd/stamp"
@@ -12,3 +12,11 @@ run = "golangci-lint run"
[tasks.install]
run = "go install ./cmd/stamp"
[tasks.check]
description = "Run formatting, vet, build, and tests (the same checks CI runs)"
run = "./scripts/check.sh"
[tasks.deps]
description = "Check that go.mod pins the newest available module versions"
run = "./scripts/check-deps.sh"
+124
View File
@@ -0,0 +1,124 @@
#!/usr/bin/env bash
#
# check-deps.sh fails when a module required by go.mod is not the newest
# version available.
#
# When the module proxy is reachable, `go list -m -u` is authoritative: it
# asks the proxy for the newest release of each pinned module. When the proxy
# is disabled (offline builds, sandboxes), the script compares the pins with
# the newest versions recorded in the local module cache instead.
#
# 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
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# requirements prints "<path> <version>" 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 }
'
}
# escape_path applies the case encoding used by the module cache for paths with
# upper-case letters (for example BurntSushi becomes !burnt!sushi).
escape_path() {
local path="$1" out='' char i
for ((i = 0; i < ${#path}; i++)); do
char="${path:i:1}"
if [[ $char == [A-Z] ]]; then
out+="!${char,,}"
else
out+="$char"
fi
done
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
unchecked=0
stale=''
while read -r path version; do
list="$cache/$(escape_path "$path")/@v/list"
if [ ! -f "$list" ]; then
unchecked=$((unchecked + 1))
continue
fi
checked=$((checked + 1))
latest="$(newest <"$list")"
if [ "$latest" = "$version" ]; then
continue
fi
# 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
echo 'These go.mod pins are behind the newest cached version:' >&2
printf '%s' "$stale" >&2
exit 1
fi
if [ "$checked" -eq 0 ]; then
echo "note: GOPROXY=off and $cache is empty; dependency freshness was not checked" >&2
exit 0
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"
+29
View File
@@ -0,0 +1,29 @@
#!/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 '==> 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 ./...