Files
coder/scripts/releaser/v1/github.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

196 lines
4.7 KiB
Go

package v1
import (
"errors"
"os/exec"
"slices"
"strconv"
"strings"
"time"
)
// ghOutput runs a gh CLI command and returns trimmed stdout.
func ghOutput(args ...string) (string, error) {
cmd := exec.Command("gh", args...)
out, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return "", exitErr
}
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// checkGHAuth verifies that the gh CLI is installed and
// authenticated. Returns true if gh is available.
func checkGHAuth() bool {
cmd := exec.Command("gh", "auth", "status")
cmd.Stdout = nil
cmd.Stderr = nil
return cmd.Run() == nil
}
// ghPR is a minimal pull request representation parsed from gh CLI
// JSON output.
type ghPR struct {
Number int `json:"number"`
Title string `json:"title"`
Author string `json:"author"`
Labels []string
}
// ghListOpenPRs returns open PRs targeting the given branch via
// the gh CLI.
func ghListOpenPRs(branch string) ([]ghPR, error) {
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", branch,
"--state", "open",
"--json", "number,title,author",
"--jq", `.[] | "\(.number)\t\(.title)\t\(.author.login)"`,
)
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var prs []ghPR
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 3)
if len(parts) < 3 {
continue
}
num, _ := strconv.Atoi(parts[0])
prs = append(prs, ghPR{
Number: num,
Title: parts[1],
Author: parts[2],
})
}
return prs, nil
}
// ghListPRsWithLabel returns merged PRs targeting the given branch
// that have a specific label.
func ghListPRsWithLabel(branch, label string) ([]ghPR, error) {
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", branch,
"--state", "merged",
"--label", label,
"--json", "number,title",
"--jq", `.[] | "\(.number)\t\(.title)"`,
)
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var prs []ghPR
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 2)
if len(parts) < 2 {
continue
}
num, _ := strconv.Atoi(parts[0])
prs = append(prs, ghPR{Number: num, Title: parts[1]})
}
return prs, nil
}
// prMetadata holds labels and author for a merged PR.
type prMetadata struct {
Labels []string
Author string
}
// prMetadataMaps holds PR metadata indexed by both merge-commit SHA
// and PR number. On release branches, commits are cherry-picked so
// their SHA differs from the original merge commit on main. The PR
// number (preserved in the commit title) provides a fallback lookup.
type prMetadataMaps struct {
bySHA map[string]prMetadata
byNumber map[int]prMetadata
}
// lookupCommit returns PR metadata for a commit, trying the full SHA
// first and falling back to PR number for cherry-picked commits.
func (m *prMetadataMaps) lookupCommit(fullSHA string, prNumber int) prMetadata {
if meta, ok := m.bySHA[fullSHA]; ok {
return meta
}
if prNumber > 0 {
return m.byNumber[prNumber]
}
return prMetadata{}
}
// ghBuildPRMetadataMap returns PR metadata indexed by both
// merge-commit SHA and PR number for merged PRs targeting main.
// This matches the bash script's approach of querying --base main
// with a date filter based on the oldest commit in the range.
func ghBuildPRMetadataMap(commits []commitEntry) (*prMetadataMaps, error) {
empty := &prMetadataMaps{
bySHA: make(map[string]prMetadata),
byNumber: make(map[int]prMetadata),
}
if len(commits) == 0 {
return empty, nil
}
// Find the earliest commit timestamp to scope the PR query.
earliest := commits[0].Timestamp
for _, c := range commits[1:] {
if c.Timestamp < earliest {
earliest = c.Timestamp
}
}
lookbackDate := time.Unix(earliest, 0).Format("2006-01-02")
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", "main",
"--state", "merged",
"--limit", "10000",
"--search", "merged:>="+lookbackDate,
"--json", "number,mergeCommit,labels,author",
"--jq", `.[] | "\(.number)\t\(.mergeCommit.oid)\t\(.author.login)\t\([.labels[].name] | join(","))"`,
)
if err != nil {
return nil, err
}
if out == "" {
return empty, nil
}
result := &prMetadataMaps{
bySHA: make(map[string]prMetadata),
byNumber: make(map[int]prMetadata),
}
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 4)
if len(parts) < 4 {
continue
}
num, _ := strconv.Atoi(parts[0])
sha := parts[1]
author := parts[2]
var labels []string
if parts[3] != "" {
labels = strings.Split(parts[3], ",")
slices.Sort(labels)
}
meta := prMetadata{
Labels: labels,
Author: author,
}
result.bySHA[sha] = meta
if num > 0 {
result.byNumber[num] = meta
}
}
return result, nil
}