Files
uptimesebi/tools/release/version.go
T
Sebastian UnterschützandClaude Opus 5.5 68a7827475
ci / image (pull_request) Successful in 3m39s
ci / build (pull_request) Successful in 5m34s
ci / lint (pull_request) Successful in 9m41s
ci / test (pull_request) Successful in 16m7s
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
2026-11-23 07:17:02 +01:00

155 lines
3.5 KiB
Go

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
}