Files
coder/scripts/releaser/v2/commands.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

265 lines
6.8 KiB
Go

package v2
import (
"fmt"
"os"
"golang.org/x/xerrors"
"github.com/coder/serpent"
)
const (
owner = "coder"
repo = "coder"
)
// newExecutor returns the appropriate CommandExecutor based on the
// dry-run setting.
//
//nolint:revive // dryRun selects the dry-run executor.
func newExecutor(dryRun bool) CommandExecutor {
if dryRun {
return newDryRunExecutor(os.Stderr)
}
return realExecutor{}
}
// dryRunOption returns the shared --dry-run option bound to dryRun.
func dryRunOption(dryRun *bool) serpent.Option {
return serpent.Option{
Name: "dry-run",
Flag: "dry-run",
Description: "Print mutating commands instead of executing them.",
Value: serpent.BoolOf(dryRun),
}
}
// CICommands returns the low-level, CI-oriented release subcommands
// (calculate-version, prepare-release, generate-notes, publish). Their
// names, flags, and stdout output match the former scripts/release-action
// tool so GitHub Actions workflows can invoke them unchanged.
func CICommands() []*serpent.Command {
return []*serpent.Command{
calculateVersionCommand(),
prepareReleaseCommand(),
generateNotesCommand(),
publishCommand(),
}
}
// TypeCommand returns a command that runs prepare-release for a fixed
// release type. It backs the top-level rc, branch, and release
// subcommands, printing the same JSON as prepare-release.
func TypeCommand(use, short, releaseType string) *serpent.Command {
var (
ref string
commitSHA string
dryRun bool
)
return &serpent.Command{
Use: use,
Short: short,
Options: serpent.OptionSet{
{
Name: "ref",
Flag: "ref",
Description: "Git ref (branch name) to release from.",
Value: serpent.StringOf(&ref),
Required: true,
},
{
Name: "commit",
Flag: "commit",
Description: "Commit SHA to tag (defaults to HEAD of --ref if empty).",
Value: serpent.StringOf(&commitSHA),
},
dryRunOption(&dryRun),
},
Handler: func(inv *serpent.Invocation) error {
result, err := prepareRelease(newExecutor(dryRun), releaseType, ref, commitSHA)
if err != nil {
return err
}
_, _ = fmt.Fprintln(inv.Stdout, result.String())
return nil
},
}
}
func calculateVersionCommand() *serpent.Command {
var (
releaseType string
ref string
commitSHA string
dryRun bool
)
return &serpent.Command{
Use: "calculate-version",
Short: "Calculate the next release version from git state.",
Options: serpent.OptionSet{
{
Name: "type",
Flag: "type",
Description: "Release type: rc, release, or create-release-branch.",
Value: serpent.StringOf(&releaseType),
Required: true,
},
{
Name: "ref",
Flag: "ref",
Description: "Git ref (branch name) the workflow is running on.",
Value: serpent.StringOf(&ref),
Required: true,
},
{
Name: "commit",
Flag: "commit",
Description: "Commit SHA to tag (defaults to HEAD of --ref if empty).",
Value: serpent.StringOf(&commitSHA),
},
dryRunOption(&dryRun),
},
Handler: func(inv *serpent.Invocation) error {
result, err := calculateNextVersion(newExecutor(dryRun), releaseType, ref, commitSHA)
if err != nil {
return err
}
_, _ = fmt.Fprintln(inv.Stdout, result.String())
return nil
},
}
}
func prepareReleaseCommand() *serpent.Command {
var (
releaseType string
ref string
commitSHA string
dryRun bool
)
return &serpent.Command{
Use: "prepare-release",
Short: "Calculate version, create and push tag (and optionally release branch).",
Options: serpent.OptionSet{
{
Name: "type",
Flag: "type",
Description: "Release type: rc, release, or create-release-branch.",
Value: serpent.StringOf(&releaseType),
Required: true,
},
{
Name: "ref",
Flag: "ref",
Description: "Git ref (branch name) the workflow is running on.",
Value: serpent.StringOf(&ref),
Required: true,
},
{
Name: "commit",
Flag: "commit",
Description: "Commit SHA to tag (defaults to HEAD of --ref if empty).",
Value: serpent.StringOf(&commitSHA),
},
dryRunOption(&dryRun),
},
Handler: func(inv *serpent.Invocation) error {
result, err := prepareRelease(newExecutor(dryRun), releaseType, ref, commitSHA)
if err != nil {
return err
}
_, _ = fmt.Fprintln(inv.Stdout, result.String())
return nil
},
}
}
func generateNotesCommand() *serpent.Command {
var (
versionStr string
prevVersionStr string
dryRun bool
)
return &serpent.Command{
Use: "generate-notes",
Short: "Generate release notes from commit log and PR metadata.",
Options: serpent.OptionSet{
{
Name: "version",
Flag: "version",
Description: "New release version (e.g. v2.21.0).",
Value: serpent.StringOf(&versionStr),
Required: true,
},
{
Name: "previous-version",
Flag: "previous-version",
Description: "Previous release version (e.g. v2.20.0).",
Value: serpent.StringOf(&prevVersionStr),
Required: true,
},
dryRunOption(&dryRun),
},
Handler: func(inv *serpent.Invocation) error {
newVer, err := parseVersion(versionStr)
if err != nil {
return xerrors.Errorf("parse --version: %w", err)
}
prevVer, err := parseVersion(prevVersionStr)
if err != nil {
return xerrors.Errorf("parse --previous-version: %w", err)
}
notes, err := generateReleaseNotes(newExecutor(dryRun), newVer, prevVer)
if err != nil {
return err
}
_, _ = fmt.Fprint(inv.Stdout, notes)
return nil
},
}
}
func publishCommand() *serpent.Command {
var (
versionStr string
stable bool
notesFile string
dryRun bool
)
return &serpent.Command{
Use: "publish",
Short: "Publish a GitHub release with assets and checksums.",
Options: serpent.OptionSet{
{
Name: "version",
Flag: "version",
Description: "Release version tag (e.g. v2.21.0).",
Value: serpent.StringOf(&versionStr),
Required: true,
},
{
Name: "stable",
Flag: "stable",
Description: "Mark this release as the latest stable release.",
Value: serpent.BoolOf(&stable),
},
{
Name: "release-notes-file",
Flag: "release-notes-file",
Description: "Path to release notes markdown file.",
Value: serpent.StringOf(&notesFile),
Required: true,
},
dryRunOption(&dryRun),
},
Handler: func(inv *serpent.Invocation) error {
assets := inv.Args
if len(assets) == 0 {
return xerrors.New("no asset files provided as arguments")
}
return publishRelease(newExecutor(dryRun), versionStr, stable, notesFile, assets)
},
}
}