ci: release automation from Conventional Commits
tools/release derives the next version from the commits since the last stable release and renders the changelog. `make release` tags it on main, CHANNEL=beta a numbered prerelease of the coming version; pushing the tag runs the release workflow, which publishes the release with its notes, Linux binaries and checksums. Images get channel tags: edge for main, beta for prereleases, latest, stable and X.Y for releases. Closes #33 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
1ad68e2e87
commit
68a7827475
8 files changed
+644
-5
No files matched your search
@@ -165,12 +165,21 @@ jobs:
|
||||
*) TAG="$(date -u +%Y%m%d%H%M%S)-$SHORT" ;;
|
||||
esac
|
||||
PUSH="--no-push"
|
||||
DEST="--destination ${IMAGE}:${TAG}"
|
||||
DEST="--destination ${IMAGE}:${TAG#v}"
|
||||
# Channels: edge follows main, beta the newest prerelease, latest
|
||||
# and stable the newest release, X.Y its newest patch.
|
||||
case "$GITHUB_REF" in
|
||||
refs/heads/main) CHANNELS="edge" ;;
|
||||
refs/tags/v*-beta.*) CHANNELS="beta" ;;
|
||||
refs/tags/v*)
|
||||
MINOR="$(printf '%s' "${TAG#v}" | cut -d. -f1-2)"
|
||||
CHANNELS="latest stable $MINOR" ;;
|
||||
esac
|
||||
case "$GITHUB_REF" in
|
||||
refs/heads/main|refs/tags/v*)
|
||||
if [ -n "${PACKAGE_TOKEN:-}" ]; then
|
||||
PUSH=""
|
||||
[ "$GITHUB_REF" = refs/heads/main ] && DEST="$DEST --destination ${IMAGE}:latest"
|
||||
for c in $CHANNELS; do DEST="$DEST --destination ${IMAGE}:$c"; done
|
||||
else
|
||||
echo "PACKAGE_TOKEN is not set; building without pushing."
|
||||
fi ;;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
name: release
|
||||
|
||||
# Runs for release tags, which `make release` creates. The image of a tag
|
||||
# is built by the ci workflow.
|
||||
on:
|
||||
push:
|
||||
tags: ["v*"]
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
with:
|
||||
# The notes need the history back to the previous release.
|
||||
fetch-depth: 0
|
||||
- name: Install Go
|
||||
run: |
|
||||
v="$(awk '/^go /{print $2}' go.mod)"
|
||||
curl -fsSL "https://go.dev/dl/go${v}.linux-amd64.tar.gz" | tar -C /usr/local -xz
|
||||
echo /usr/local/go/bin >> "$GITHUB_PATH"
|
||||
- name: Check tag
|
||||
run: go run ./tools/release check "$GITHUB_REF_NAME"
|
||||
- name: Build frontend
|
||||
run: npm ci --no-audit --no-fund && npm run build
|
||||
- name: Build binaries
|
||||
run: |
|
||||
set -eu
|
||||
pkg=git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo
|
||||
commit="$(git rev-parse --short=12 HEAD)"
|
||||
for platform in linux/amd64 linux/arm64; do
|
||||
os=${platform%/*} arch=${platform#*/}
|
||||
dir="uptimesebi_${GITHUB_REF_NAME#v}_${os}_${arch}"
|
||||
CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath \
|
||||
-ldflags "-s -w -X $pkg.Version=$GITHUB_REF_NAME -X $pkg.Commit=$commit" \
|
||||
-o "dist/$dir/" ./cmd/...
|
||||
cp LICENSE README.md .env.example "dist/$dir/"
|
||||
tar -C dist -czf "dist/$dir.tar.gz" "$dir"
|
||||
done
|
||||
(cd dist && sha256sum ./*.tar.gz | sed 's| \./| |' > SHA256SUMS)
|
||||
cat dist/SHA256SUMS
|
||||
- name: Publish release
|
||||
env:
|
||||
TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: |
|
||||
set -eu
|
||||
api="$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY"
|
||||
if curl -fsS -o /dev/null -H "Authorization: token $TOKEN" "$api/releases/tags/$GITHUB_REF_NAME"; then
|
||||
echo "Release $GITHUB_REF_NAME exists already; delete it to publish again." >&2
|
||||
exit 1
|
||||
fi
|
||||
go run ./tools/release notes -json "$GITHUB_REF_NAME" > release.json
|
||||
id="$(curl -fsS -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
|
||||
--data-binary @release.json "$api/releases" | grep -o '"id":[0-9]*' | head -n1 | cut -d: -f2)"
|
||||
for f in dist/*.tar.gz dist/SHA256SUMS; do
|
||||
curl -fsS -o /dev/null -X POST -H "Authorization: token $TOKEN" \
|
||||
-F "attachment=@$f" "$api/releases/$id/assets?name=$(basename "$f")"
|
||||
done
|
||||
@@ -3,7 +3,7 @@ COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
|
||||
PKG := git.zb-server.de/ZB-Server/uptimesebi
|
||||
LDFLAGS := -s -w -X $(PKG)/internal/buildinfo.Version=$(VERSION) -X $(PKG)/internal/buildinfo.Commit=$(COMMIT)
|
||||
|
||||
.PHONY: generate web build test lint dev-up dev-up-sentinel dev-down migrate image
|
||||
.PHONY: generate web build test lint release dev-up dev-up-sentinel dev-down migrate image
|
||||
|
||||
generate:
|
||||
sqlc generate
|
||||
@@ -22,6 +22,19 @@ test:
|
||||
lint:
|
||||
golangci-lint run
|
||||
|
||||
# Tags the next release from the Conventional Commits since the last one;
|
||||
# pushing the tag publishes it. CHANNEL=beta tags a prerelease.
|
||||
CHANNEL ?= stable
|
||||
release:
|
||||
@test "$$(git rev-parse --abbrev-ref HEAD)" = main || { echo "release from main" >&2; exit 1; }
|
||||
@git diff --quiet HEAD || { echo "commit or stash your changes first" >&2; exit 1; }
|
||||
@git fetch -q --tags origin main
|
||||
@test "$$(git rev-parse HEAD)" = "$$(git rev-parse origin/main)" || { echo "main is not origin/main" >&2; exit 1; }
|
||||
@tag="$$(go run ./tools/release next $(CHANNEL))" && \
|
||||
git tag -a "$$tag" -m "$$tag" && \
|
||||
go run ./tools/release notes "$$tag" && \
|
||||
echo "Tagged $$tag. Publish it with: git push origin $$tag"
|
||||
|
||||
dev-up:
|
||||
docker compose up -d --wait
|
||||
|
||||
|
||||
@@ -67,7 +67,17 @@ UPTIME_STATUSPAGE_CACHE_DIR=./.statuspage-cache go run ./cmd/statuspage
|
||||
|
||||
### Run in production
|
||||
|
||||
One image contains all three commands; `api` is the default entrypoint.
|
||||
One image contains all three commands; `api` is the default entrypoint. Images are published as `git.zb-server.de/zb-server/uptimesebi:<tag>`; pick a channel:
|
||||
|
||||
| Tag | Follows |
|
||||
| --- | --- |
|
||||
| `latest`, `stable` | the newest release |
|
||||
| `1.4` | the newest patch release of 1.4 |
|
||||
| `1.4.2` | exactly that release |
|
||||
| `beta` | the newest prerelease (`1.5.0-beta.N`) |
|
||||
| `edge` | every commit on `main` |
|
||||
|
||||
Each [release](https://git.zb-server.de/ZB-Server/uptimesebi/releases) also carries Linux binaries (amd64, arm64) with checksums and the changelog. Or build the image yourself:
|
||||
|
||||
```sh
|
||||
docker build -t uptimesebi .
|
||||
@@ -156,7 +166,7 @@ make lint
|
||||
|
||||
The tests run against the `uptime_test` database and Redis database 15 from [`.env.example`](.env.example). CI runs lint, tests, builds and the image for every pull request ([`.gitea/workflows/ci.yaml`](.gitea/workflows/ci.yaml)).
|
||||
|
||||
Commits follow [Conventional Commits](https://www.conventionalcommits.org/). Code comments, commits and issues are in English; the interface is translated (English and German, `internal/i18n/locales`).
|
||||
Commits follow [Conventional Commits](https://www.conventionalcommits.org/), which decide the version: `make release` tags the next one on `main` (breaking changes move the major version, before 1.0 the minor one; `feat` the minor, `fix` and `perf` the patch version), `make release CHANNEL=beta` the next prerelease. Pushing the tag publishes the release with its changelog ([`.gitea/workflows/release.yaml`](.gitea/workflows/release.yaml)). Code comments, commits and issues are in English; the interface is translated (English and German, `internal/i18n/locales`).
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
// Command release derives versions and release notes from Conventional
|
||||
// Commits.
|
||||
//
|
||||
// release next [stable|beta] print the tag the next release gets
|
||||
// release notes [-json] <tag> print the release notes of a tag, with
|
||||
// -json as the body of a Gitea release
|
||||
// release check <tag> fail unless tag is a valid release tag
|
||||
//
|
||||
// Stable releases follow semantic versioning from the commits since the
|
||||
// last stable release: breaking changes move the major version (the minor
|
||||
// one before 1.0), features the minor and fixes the patch version. Betas
|
||||
// are numbered prereleases of the coming stable version.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const usage = `usage: release next [stable|beta] | notes [-json] <tag> | check <tag>`
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return usageError{}
|
||||
}
|
||||
switch args[0] {
|
||||
case "next":
|
||||
channel := "stable"
|
||||
if len(args) > 1 {
|
||||
channel = args[1]
|
||||
}
|
||||
tags, err := gitTags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
from := ""
|
||||
if base := lastStable(tags); base != (version{}) {
|
||||
from = base.String()
|
||||
}
|
||||
commits, err := gitCommits(from, "HEAD")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err := next(tags, commits, channel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(v)
|
||||
case "notes":
|
||||
asJSON := len(args) == 3 && args[1] == "-json"
|
||||
if asJSON {
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
v, err := parseVersion(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tags, err := gitTags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
prev, ok := previous(tags, v)
|
||||
from := ""
|
||||
var pp *version
|
||||
if ok {
|
||||
from, pp = prev.String(), &prev
|
||||
}
|
||||
commits, err := gitCommits(from, v.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text := notes(v, pp, commits, repoURL())
|
||||
if !asJSON {
|
||||
fmt.Print(text)
|
||||
return nil
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{
|
||||
"tag_name": v.String(), "name": v.String(), "body": text, "prerelease": v.Beta > 0,
|
||||
})
|
||||
case "check":
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
_, err := parseVersion(args[1])
|
||||
return err
|
||||
default:
|
||||
return usageError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type usageError struct{}
|
||||
|
||||
func (usageError) Error() string { return usage }
|
||||
|
||||
func git(args ...string) (string, error) {
|
||||
out, err := exec.Command("git", args...).Output()
|
||||
var exit *exec.ExitError
|
||||
if errors.As(err, &exit) {
|
||||
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), strings.TrimSpace(string(exit.Stderr)))
|
||||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
func gitTags() ([]version, error) {
|
||||
out, err := git("tag", "--list", "v*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseTags(strings.Split(out, "\n")), nil
|
||||
}
|
||||
|
||||
// gitCommits lists the commits after from up to to, oldest first, without
|
||||
// merge commits. An empty from means the whole history.
|
||||
func gitCommits(from, to string) ([]commit, error) {
|
||||
rng := to
|
||||
if from != "" {
|
||||
rng = from + ".." + to
|
||||
}
|
||||
out, err := git("log", "--reverse", "--no-merges", "--format=%H%x1f%s%x1f%b%x1e", rng, "--")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var commits []commit
|
||||
for rec := range strings.SplitSeq(out, "\x1e") {
|
||||
f := strings.SplitN(strings.TrimLeft(rec, "\n"), "\x1f", 3)
|
||||
if len(f) != 3 {
|
||||
continue
|
||||
}
|
||||
commits = append(commits, commit{Hash: f[0], Subject: f[1], Body: f[2]})
|
||||
}
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
// repoURL is the repository's web address in CI, for links in the notes.
|
||||
func repoURL() string {
|
||||
server, repo := os.Getenv("GITHUB_SERVER_URL"), os.Getenv("GITHUB_REPOSITORY")
|
||||
if server == "" || repo == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(server, "/") + "/" + repo
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// commit is one Conventional Commit.
|
||||
type commit struct {
|
||||
Hash, Subject, Body string
|
||||
}
|
||||
|
||||
var subjectPattern = regexp.MustCompile(`^([a-z]+)(?:\(([^)]+)\))?(!)?: (.+)$`)
|
||||
|
||||
// parsed splits the subject into type, scope and description.
|
||||
func (c commit) parsed() (typ, scope, desc string, breaking bool) {
|
||||
m := subjectPattern.FindStringSubmatch(c.Subject)
|
||||
if m == nil {
|
||||
return "", "", c.Subject, false
|
||||
}
|
||||
return m[1], m[2], m[4], m[3] == "!" || c.breakingNote() != ""
|
||||
}
|
||||
|
||||
// breakingNote returns the BREAKING CHANGE footer.
|
||||
func (c commit) breakingNote() string {
|
||||
for _, prefix := range []string{"BREAKING CHANGE: ", "BREAKING-CHANGE: "} {
|
||||
if _, after, ok := strings.Cut(c.Body, prefix); ok {
|
||||
note, _, _ := strings.Cut(after, "\n\n")
|
||||
return strings.Join(strings.Fields(note), " ")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c commit) bump() bump {
|
||||
typ, _, _, breaking := c.parsed()
|
||||
switch {
|
||||
case breaking:
|
||||
return bumpMajor
|
||||
case typ == "feat":
|
||||
return bumpMinor
|
||||
case typ == "fix" || typ == "perf":
|
||||
return bumpPatch
|
||||
}
|
||||
return bumpNone
|
||||
}
|
||||
|
||||
var sections = []struct{ typ, title string }{
|
||||
{"feat", "Features"},
|
||||
{"fix", "Fixes"},
|
||||
{"perf", "Performance"},
|
||||
}
|
||||
|
||||
// notes renders the changelog of a release in Markdown. Changes outside
|
||||
// features, fixes and performance stay out, except breaking ones.
|
||||
func notes(v version, prev *version, commits []commit, repoURL string) string {
|
||||
var b strings.Builder
|
||||
line := func(c commit, desc, scope string) {
|
||||
if scope != "" {
|
||||
desc = "**" + scope + ":** " + desc
|
||||
}
|
||||
short := c.Hash[:min(len(c.Hash), 7)]
|
||||
if repoURL != "" {
|
||||
fmt.Fprintf(&b, "- %s ([%s](%s/commit/%s))\n", desc, short, repoURL, c.Hash)
|
||||
} else {
|
||||
fmt.Fprintf(&b, "- %s (%s)\n", desc, short)
|
||||
}
|
||||
}
|
||||
|
||||
var breaking []commit
|
||||
for _, c := range commits {
|
||||
if _, _, _, br := c.parsed(); br {
|
||||
breaking = append(breaking, c)
|
||||
}
|
||||
}
|
||||
if len(breaking) > 0 {
|
||||
b.WriteString("### Breaking changes\n\n")
|
||||
for _, c := range breaking {
|
||||
_, scope, desc, _ := c.parsed()
|
||||
if n := c.breakingNote(); n != "" {
|
||||
desc += ": " + n
|
||||
}
|
||||
line(c, desc, scope)
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
for _, s := range sections {
|
||||
head := false
|
||||
for _, c := range commits {
|
||||
typ, scope, desc, _ := c.parsed()
|
||||
if typ != s.typ {
|
||||
continue
|
||||
}
|
||||
if !head {
|
||||
fmt.Fprintf(&b, "### %s\n\n", s.title)
|
||||
head = true
|
||||
}
|
||||
line(c, desc, scope)
|
||||
}
|
||||
if head {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
if b.Len() == 0 {
|
||||
b.WriteString("No user-facing changes.\n\n")
|
||||
}
|
||||
if prev != nil && repoURL != "" {
|
||||
fmt.Fprintf(&b, "**Full changes:** [%s...%s](%s/compare/%s...%s)\n", prev, v, repoURL, prev, v)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func tags(t *testing.T, names ...string) []version {
|
||||
t.Helper()
|
||||
out := parseTags(names)
|
||||
if len(out) != len(names) {
|
||||
t.Fatalf("invalid tags in %v", names)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func commits(subjects ...string) []commit {
|
||||
var out []commit
|
||||
for i, s := range subjects {
|
||||
subject, body, _ := strings.Cut(s, "\n\n")
|
||||
out = append(out, commit{Hash: strings.Repeat(string(rune('a'+i)), 40), Subject: subject, Body: body})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestNext(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
tags []string
|
||||
commits []string
|
||||
channel string
|
||||
want string
|
||||
}{
|
||||
{"first release", nil, []string{"feat: monitors"}, "stable", "v0.1.0"},
|
||||
{"fix", []string{"v0.3.1"}, []string{"fix: retry", "chore: deps"}, "stable", "v0.3.2"},
|
||||
{"feature", []string{"v0.3.1"}, []string{"fix: retry", "feat(api): paging"}, "stable", "v0.4.0"},
|
||||
{"breaking before 1.0", []string{"v0.3.1"}, []string{"feat!: new config"}, "stable", "v0.4.0"},
|
||||
{"breaking footer", []string{"v1.3.1"}, []string{"refactor: config\n\nBREAKING CHANGE: UPTIME_X is gone"}, "stable", "v2.0.0"},
|
||||
{"breaking after 1.0", []string{"v1.3.1"}, []string{"feat(api)!: drop v0"}, "stable", "v2.0.0"},
|
||||
{"first beta", []string{"v1.3.1"}, []string{"feat: x"}, "beta", "v1.4.0-beta.1"},
|
||||
{"next beta", []string{"v1.3.1", "v1.4.0-beta.1", "v1.4.0-beta.2"}, []string{"feat: x", "fix: y"}, "beta", "v1.4.0-beta.3"},
|
||||
{"stable after betas", []string{"v1.3.1", "v1.4.0-beta.2"}, []string{"feat: x"}, "stable", "v1.4.0"},
|
||||
{"betas of another version", []string{"v1.3.1", "v1.3.2-beta.4"}, []string{"feat: x"}, "beta", "v1.4.0-beta.1"},
|
||||
{"perf", []string{"v1.0.0"}, []string{"perf: cache"}, "stable", "v1.0.1"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got, err := next(tags(t, c.tags...), commits(c.commits...), c.channel)
|
||||
if err != nil || got.String() != c.want {
|
||||
t.Errorf("next = %v, %v; want %s", got, err, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := next(nil, commits("chore: x", "docs: y", "Merge branch"), "stable"); !errors.Is(err, errNothingToRelease) {
|
||||
t.Errorf("nothing to release: err = %v", err)
|
||||
}
|
||||
if _, err := next(nil, commits("feat: x"), "nightly"); err == nil {
|
||||
t.Error("unknown channel accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
for _, bad := range []string{"1.0.0", "v1.0", "v01.0.0", "v1.0.0-rc.1", "v1.0.0-beta.0", "v1.0.0-beta", "v1.0.0\n"} {
|
||||
if _, err := parseVersion(bad); err == nil {
|
||||
t.Errorf("%q accepted", bad)
|
||||
}
|
||||
}
|
||||
got := tags(t, "v1.0.0", "v0.9.0", "v1.0.0-beta.10", "v1.0.0-beta.2", "v0.10.0")
|
||||
var names []string
|
||||
for _, v := range got {
|
||||
names = append(names, v.String())
|
||||
}
|
||||
if want := "v0.9.0 v0.10.0 v1.0.0-beta.2 v1.0.0-beta.10 v1.0.0"; strings.Join(names, " ") != want {
|
||||
t.Errorf("order = %v, want %s", names, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrevious(t *testing.T) {
|
||||
all := tags(t, "v0.1.0", "v0.2.0-beta.1", "v0.2.0-beta.2", "v0.2.0", "v0.3.0-beta.1")
|
||||
for tag, want := range map[string]string{
|
||||
"v0.2.0": "v0.1.0",
|
||||
"v0.2.0-beta.2": "v0.2.0-beta.1",
|
||||
"v0.3.0-beta.1": "v0.2.0",
|
||||
"v0.3.0": "v0.2.0",
|
||||
"v0.2.0-beta.1": "v0.1.0",
|
||||
} {
|
||||
v, _ := parseVersion(tag)
|
||||
if got, ok := previous(all, v); !ok || got.String() != want {
|
||||
t.Errorf("previous(%s) = %v, want %s", tag, got, want)
|
||||
}
|
||||
}
|
||||
first, _ := parseVersion("v0.1.0")
|
||||
if _, ok := previous(all, first); ok {
|
||||
t.Error("the first release has a predecessor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotes(t *testing.T) {
|
||||
v, _ := parseVersion("v0.4.0")
|
||||
prev, _ := parseVersion("v0.3.1")
|
||||
got := notes(v, &prev, commits(
|
||||
"feat(api): keyset paging",
|
||||
"fix: retry DNS",
|
||||
"chore: bump deps",
|
||||
"feat!: config in one file\n\nBREAKING CHANGE: UPTIME_FOO is\nnow UPTIME_BAR.\n\nCo-Authored-By: x",
|
||||
"perf(check): reuse connections",
|
||||
), "https://git.example/o/r")
|
||||
for _, want := range []string{
|
||||
"### Breaking changes\n\n- config in one file: UPTIME_FOO is now UPTIME_BAR. ([ddddddd](https://git.example/o/r/commit/" + strings.Repeat("d", 40) + "))",
|
||||
"### Features\n\n- **api:** keyset paging ([aaaaaaa]",
|
||||
"- config in one file ([ddddddd]",
|
||||
"### Fixes\n\n- retry DNS ([bbbbbbb]",
|
||||
"### Performance\n\n- **check:** reuse connections",
|
||||
"[v0.3.1...v0.4.0](https://git.example/o/r/compare/v0.3.1...v0.4.0)",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("notes lack %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
if strings.Contains(got, "bump deps") || strings.Contains(got, "Co-Authored") {
|
||||
t.Errorf("notes contain internal changes:\n%s", got)
|
||||
}
|
||||
if got := notes(v, nil, commits("ci: x"), ""); got != "No user-facing changes.\n\n" {
|
||||
t.Errorf("empty notes = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// version is a release tag: vMAJOR.MINOR.PATCH, or a beta of the next
|
||||
// stable release with -beta.N.
|
||||
type version struct {
|
||||
Major, Minor, Patch int
|
||||
// Beta is 0 for stable releases.
|
||||
Beta int
|
||||
}
|
||||
|
||||
var tagPattern = regexp.MustCompile(`^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-beta\.([1-9]\d*))?$`)
|
||||
|
||||
func parseVersion(tag string) (version, error) {
|
||||
m := tagPattern.FindStringSubmatch(tag)
|
||||
if m == nil {
|
||||
return version{}, fmt.Errorf("release: %q is not vX.Y.Z or vX.Y.Z-beta.N", tag)
|
||||
}
|
||||
var v version
|
||||
for i, p := range []*int{&v.Major, &v.Minor, &v.Patch, &v.Beta} {
|
||||
if m[i+1] != "" {
|
||||
*p, _ = strconv.Atoi(m[i+1])
|
||||
}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (v version) String() string {
|
||||
s := fmt.Sprintf("v%d.%d.%d", v.Major, v.Minor, v.Patch)
|
||||
if v.Beta > 0 {
|
||||
s += fmt.Sprintf("-beta.%d", v.Beta)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (v version) stable() version { return version{v.Major, v.Minor, v.Patch, 0} }
|
||||
|
||||
// compare orders versions; a beta comes before its stable release.
|
||||
func (v version) compare(o version) int {
|
||||
for _, d := range []int{v.Major - o.Major, v.Minor - o.Minor, v.Patch - o.Patch} {
|
||||
if d != 0 {
|
||||
return d
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case v.Beta == o.Beta:
|
||||
return 0
|
||||
case v.Beta == 0:
|
||||
return 1
|
||||
case o.Beta == 0:
|
||||
return -1
|
||||
}
|
||||
return v.Beta - o.Beta
|
||||
}
|
||||
|
||||
// bump is how much a set of changes moves the version.
|
||||
type bump int
|
||||
|
||||
const (
|
||||
bumpNone bump = iota
|
||||
bumpPatch
|
||||
bumpMinor
|
||||
bumpMajor
|
||||
)
|
||||
|
||||
func (v version) bumped(b bump) version {
|
||||
// Before 1.0, breaking changes move the minor version.
|
||||
if b == bumpMajor && v.Major == 0 {
|
||||
b = bumpMinor
|
||||
}
|
||||
switch b {
|
||||
case bumpMajor:
|
||||
return version{Major: v.Major + 1}
|
||||
case bumpMinor:
|
||||
return version{Major: v.Major, Minor: v.Minor + 1}
|
||||
}
|
||||
return version{Major: v.Major, Minor: v.Minor, Patch: v.Patch + 1}
|
||||
}
|
||||
|
||||
var errNothingToRelease = errors.New("release: no feat, fix or perf commits since the last release")
|
||||
|
||||
// next returns the tag to release on channel from the existing tags and the
|
||||
// commits since the last stable release.
|
||||
func next(tags []version, commits []commit, channel string) (version, error) {
|
||||
if channel != "stable" && channel != "beta" {
|
||||
return version{}, fmt.Errorf("release: channel %q is not stable or beta", channel)
|
||||
}
|
||||
base := lastStable(tags)
|
||||
b := bumpNone
|
||||
for _, c := range commits {
|
||||
b = max(b, c.bump())
|
||||
}
|
||||
if b == bumpNone {
|
||||
return version{}, errNothingToRelease
|
||||
}
|
||||
v := base.bumped(b)
|
||||
if channel == "stable" {
|
||||
return v, nil
|
||||
}
|
||||
beta := v
|
||||
for _, t := range tags {
|
||||
if t.stable() == v && t.Beta > beta.Beta {
|
||||
beta.Beta = t.Beta
|
||||
}
|
||||
}
|
||||
beta.Beta++
|
||||
return beta, nil
|
||||
}
|
||||
|
||||
// lastStable returns the highest stable tag, v0.0.0 without one.
|
||||
func lastStable(tags []version) version {
|
||||
var out version
|
||||
for _, t := range tags {
|
||||
if t.Beta == 0 && t.compare(out) > 0 {
|
||||
out = t
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// previous returns the release the notes of v start after: the last stable
|
||||
// release for a stable one, the last release of any kind for a beta.
|
||||
func previous(tags []version, v version) (version, bool) {
|
||||
var out version
|
||||
found := false
|
||||
for _, t := range tags {
|
||||
if t.compare(v) >= 0 || (v.Beta == 0 && t.Beta > 0) {
|
||||
continue
|
||||
}
|
||||
if !found || t.compare(out) > 0 {
|
||||
out, found = t, true
|
||||
}
|
||||
}
|
||||
return out, found
|
||||
}
|
||||
|
||||
func parseTags(lines []string) []version {
|
||||
var out []version
|
||||
for _, l := range lines {
|
||||
if v, err := parseVersion(strings.TrimSpace(l)); err == nil {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
slices.SortFunc(out, version.compare)
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user