Files
coder/scripts/releaser/v1/version.go
T
Garrett Delfosse bfbacd64f4 refactor: consolidate release tooling into a single releaser command (#27034)
## What

Consolidates the two separate release programs into a single command at
`scripts/releaser`:

- `scripts/releaser/v1/` — the former interactive releaser (package
`v1`).
- `scripts/releaser/v2/` — the former `scripts/release-action` CI tool
(package `v2`).
- `scripts/releaser/main.go` — new entrypoint. Runs the **v2** tooling
by
  default and the **v1** interactive wizard with `--legacy`.

## CLI shape

Three documented subcommands, each backed by v2 `prepare-release` with
the
release type baked in:

- `releaser rc` — tag a release candidate
- `releaser branch` — cut a new release branch and tag its first RC
- `releaser release` — tag a stable release or patch

The former release-action verbs (`calculate-version`, `prepare-release`,
`generate-notes`, `publish`) are retained as **hidden** top-level
commands with
identical flags and stdout, so `tag-and-release.yaml` migrates with a
path-only
change (`scripts/release-action` -> `scripts/releaser`). `--legacy` runs
the v1
wizard and is mutually exclusive with the subcommands.
`scripts/release.sh` now
launches `releaser --legacy`.

All file moves are rename-detected by git, so the per-file diff is just
the
package declaration.

## Testing

- `go build ./scripts/...`, `go vet ./scripts/releaser/...`, `go test
./scripts/releaser/...`
- `golangci-lint run ./scripts/releaser/...`, `make lint/emdash`,
`shellcheck`, `actionlint`
- Smoke: `releaser --help` shows only rc/branch/release; hidden verbs
still run;
`releaser rc --ref main --dry-run` emits the same JSON contract;
`--legacy rc`
  errors cleanly.

<details>
<summary>Implementation plan</summary>

# Plan: Consolidate release tooling into a single `scripts/releaser`
command

## Goal

Merge the two separate release programs into one binary at
`scripts/releaser`:

- `scripts/releaser/v1/` — the current interactive releaser (package
`v1`).
- `scripts/releaser/v2/` — the current CI `scripts/release-action`
(package `v2`).
- `scripts/releaser/main.go` — new entrypoint (package `main`).
  - Uses v2 by default, v1 with `--legacy`.
- Exposes 3 subcommands: `rc`, `branch` (cut release branch), `release`.

## Design decision (Option A, chosen)

The workflow needs `prepare-release`, `generate-notes`, and `publish`
invokable
separately (a build happens between prepare and publish). The latter two
are
version-driven and type-agnostic, so they do not map cleanly onto
`rc`/`branch`/`release`.

- Visible subcommands `rc`, `branch`, `release` run v2 `prepare-release`
with the
  type baked in and print the same JSON.
- Hidden verbs `calculate-version`, `prepare-release`, `generate-notes`,
  `publish` keep byte-identical flags/stdout, so the workflow change is
  path-only. Lowest risk; honors "3 subcommands" from a UX perspective.

## `--legacy` semantics

- `releaser --legacy` runs the v1 interactive wizard (preserves today's
  behavior; the wizard auto-detects RC vs release from the branch).
- `--legacy` is mutually exclusive with the subcommands (clear error if
  combined), because v1 auto-detects type and cannot cut a branch.

## Work items

1. Create `v1` and `v2` packages via `git mv`, renaming `package main`.
Move the `owner`/`repo` consts into each package. Add `v1.Run(inv,
dryRun)`
   (old wizard `main()` body) and v2 command builders (`CICommands`,
   `TypeCommand`) so internals stay unexported.
2. New `scripts/releaser/main.go`: top-level `releaser` with `--legacy`,
the 3
subcommands, and the hidden compat verbs; delegates to `v1.Run` for
legacy.
3. Update references: `tag-and-release.yaml` (3 command paths + header
comment)
   and `scripts/release.sh` (`--legacy`).
4. Verify: build, vet, test, `go run` smoke tests, fmt, lint.
5. Open a single PR from a feature branch.

## Risks / notes

- stdout contract for rc/branch/release and the hidden verbs must stay
identical
  (workflow parses stdout); logs go to stderr.
- Patch releases from pre-existing `release/X.Y` branches run those
branches'
own (old) workflow + `scripts/release-action`, so they stay
self-consistent.
New releases cut from branches containing this change get the new
workflow +
`scripts/releaser`. No forwarding stub needed since code and workflow
ship
  together.

</details>

---

This PR was created by Coder Agents on behalf of @f0ssel.
2026-07-07 11:13:50 -04:00

138 lines
3.3 KiB
Go

package v1
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
)
// version holds a parsed semver version with optional prerelease
// suffix (e.g. "rc.0").
type version struct {
Major int
Minor int
Patch int
Pre string // e.g. "rc.0", "" for stable releases.
}
var semverRe = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)(-(.+))?$`)
func parseVersion(s string) (version, bool) {
m := semverRe.FindStringSubmatch(s)
if m == nil {
return version{}, false
}
maj, _ := strconv.Atoi(m[1])
mnr, _ := strconv.Atoi(m[2])
pat, _ := strconv.Atoi(m[3])
return version{Major: maj, Minor: mnr, Patch: pat, Pre: m[5]}, true
}
func (v version) String() string {
if v.Pre != "" {
return fmt.Sprintf("v%d.%d.%d-%s", v.Major, v.Minor, v.Patch, v.Pre)
}
return fmt.Sprintf("v%d.%d.%d", v.Major, v.Minor, v.Patch)
}
// IsRC returns true when the version has a prerelease suffix starting
// with "rc." (e.g. "rc.0", "rc.1").
func (v version) IsRC() bool {
return strings.HasPrefix(v.Pre, "rc.")
}
// rcNumber returns the numeric RC identifier (e.g. 0 for "rc.0").
// It returns -1 when the version is not an RC.
func (v version) rcNumber() int {
if !v.IsRC() {
return -1
}
n, err := strconv.Atoi(strings.TrimPrefix(v.Pre, "rc."))
if err != nil {
return -1
}
return n
}
func (v version) GreaterThan(b version) bool {
if v.Major != b.Major {
return v.Major > b.Major
}
if v.Minor != b.Minor {
return v.Minor > b.Minor
}
if v.Patch != b.Patch {
return v.Patch > b.Patch
}
// A release without prerelease suffix is greater than one
// with a prerelease suffix (v2.32.0 > v2.32.0-rc.0).
if v.Pre == "" && b.Pre != "" {
return true
}
if v.Pre != "" && b.Pre == "" {
return false
}
// Both have prerelease: compare numerically for RC versions.
if v.IsRC() && b.IsRC() {
return v.rcNumber() > b.rcNumber()
}
// Fallback for non-RC prerelease strings.
return v.Pre > b.Pre
}
func (v version) Equal(b version) bool {
return v.Major == b.Major && v.Minor == b.Minor && v.Patch == b.Patch && v.Pre == b.Pre
}
// sortVersionsDesc sorts a slice of versions in descending order
// using semver-correct comparison. This is necessary because git's
// --sort=-v:refname treats pre-release suffixes (e.g. -rc.0) as
// greater than the release version, which is the opposite of semver
// where v2.32.0 > v2.32.0-rc.0.
func sortVersionsDesc(tags []version) {
sort.Slice(tags, func(i, j int) bool {
return tags[i].GreaterThan(tags[j])
})
}
// allSemverTags returns all semver tags sorted descending.
func allSemverTags() ([]version, error) {
out, err := gitOutput("tag", "--sort=-v:refname")
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var tags []version
for _, line := range strings.Split(out, "\n") {
if v, ok := parseVersion(strings.TrimSpace(line)); ok {
tags = append(tags, v)
}
}
sortVersionsDesc(tags)
return tags, nil
}
// mergedSemverTags returns semver tags reachable from HEAD, sorted
// descending.
func mergedSemverTags() ([]version, error) {
out, err := gitOutput("tag", "--merged", "HEAD", "--sort=-v:refname")
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var tags []version
for _, line := range strings.Split(out, "\n") {
if v, ok := parseVersion(strings.TrimSpace(line)); ok {
tags = append(tags, v)
}
}
sortVersionsDesc(tags)
return tags, nil
}