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.
This commit is contained in:
1 parent
55d4259f4a
commit
22e4c96192
7 files changed
+263
-3
No files matched your search
@@ -0,0 +1,23 @@
|
|||||||
|
name: ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
name: build and test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
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
|
||||||
+27
-1
@@ -18,6 +18,32 @@ 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). Its single `check` job
|
||||||
|
runs [`scripts/check.sh`](../scripts/check.sh), which:
|
||||||
|
|
||||||
|
1. fails if any Go file is not `gofmt`-formatted,
|
||||||
|
2. runs `go mod verify` to confirm the dependency modules match `go.sum`,
|
||||||
|
3. runs `go vet ./...` and `go build ./...`,
|
||||||
|
4. runs `go test -cover ./...`.
|
||||||
|
|
||||||
|
Run the same checks locally with:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./scripts/check.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
The job fails when any check fails, so a pull request cannot land with broken
|
||||||
|
code or tests.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Two Release Workflows
|
## Two Release Workflows
|
||||||
|
|
||||||
### Direct release
|
### Direct release
|
||||||
@@ -483,4 +509,4 @@ Tag format: `<name>@v<version>` for monorepos (e.g. `my-app@v1.3.0`), or `v<vers
|
|||||||
| `GITHUB_TOKEN` | GitHub token — automatically injected by GitHub Actions; no setup needed |
|
| `GITHUB_TOKEN` | GitHub token — automatically injected by GitHub Actions; no setup needed |
|
||||||
| `GITEA_TOKEN` | Auto-injected by Gitea Actions with limited scopes — **do not use this for stamp** |
|
| `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. |
|
| `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 |
|
| `GITEA_BASE_URL` | Gitea instance URL (e.g. `https://gitea.example.com`) — presence of this variable also activates Gitea mode |
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
module git.thokra.dev/thokra/stamp
|
module git.thokra.dev/thokra/stamp
|
||||||
|
|
||||||
go 1.26.1
|
go 1.27.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.gitea.io/sdk/gitea v0.23.2
|
code.gitea.io/sdk/gitea v0.23.2
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
[tools]
|
[tools]
|
||||||
go = "1.26.1"
|
go = "1.27.1"
|
||||||
|
|
||||||
[tasks.build]
|
[tasks.build]
|
||||||
run = "go build -o bin/stamp ./cmd/stamp"
|
run = "go build -o bin/stamp ./cmd/stamp"
|
||||||
@@ -12,3 +12,11 @@ run = "golangci-lint run"
|
|||||||
|
|
||||||
[tasks.install]
|
[tasks.install]
|
||||||
run = "go install ./cmd/stamp"
|
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"
|
||||||
Executable
+74
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# check-deps.sh verifies that every module pinned in go.mod is the newest
|
||||||
|
# version available.
|
||||||
|
#
|
||||||
|
# When the module proxy is reachable, `go list -m -u` is authoritative and any
|
||||||
|
# module with a newer release fails the check. When the proxy is disabled (for
|
||||||
|
# example in an offline build), the script falls back to comparing each pin
|
||||||
|
# with the newest version present in the local module cache, which still
|
||||||
|
# catches pins that were left behind after a newer version was downloaded.
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
cd "$(dirname "${BASH_SOURCE[0]}")/.."
|
||||||
|
|
||||||
|
echo '==> module graph is loadable'
|
||||||
|
go list -m -e all >/dev/null
|
||||||
|
|
||||||
|
echo '==> modules are up to date'
|
||||||
|
updates="$(go list -m -u -e -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' all | awk 'NF == 3')"
|
||||||
|
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() {
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
checked=0
|
||||||
|
stale=''
|
||||||
|
while read -r path version; do
|
||||||
|
list="$cache/$(escape_path "$path")/@v/list"
|
||||||
|
if [ ! -f "$list" ]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
checked=$((checked + 1))
|
||||||
|
latest="$(sort -V "$list" | tail -n 1)"
|
||||||
|
if [ "$latest" != "$version" ]; then
|
||||||
|
stale+="$path $version (newest in cache: $latest)"$'\n'
|
||||||
|
fi
|
||||||
|
done < <(go mod edit -json | awk -F'"' '/"Path":/ { path = $4 } /"Version":/ { print path, $4 }')
|
||||||
|
|
||||||
|
if [ -n "$stale" ]; then
|
||||||
|
echo 'These go.mod pins are not the newest cached versions:' >&2
|
||||||
|
printf '%s' "$stale" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$checked" -eq 0 ]; then
|
||||||
|
echo 'note: proxy disabled and module cache is empty; freshness not checked' >&2
|
||||||
|
else
|
||||||
|
echo "all $checked go.mod pins match the newest versions in the module cache"
|
||||||
|
fi
|
||||||
Executable
+29
@@ -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 ./...
|
||||||
Reference in new issue
Block a user