mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
## 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.
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package v2
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// prepareRelease computes the next release version, then creates and
|
|
// pushes the annotated tag and (optionally) the release branch.
|
|
// It emits the same JSON as calculateNextVersion so the workflow
|
|
// can consume it identically.
|
|
func prepareRelease(exec CommandExecutor, releaseType, ref, commitSHA string) (calculateResult, error) {
|
|
result, err := calculateNextVersion(exec, releaseType, ref, commitSHA)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch v := result.(type) {
|
|
case CreateBranchRequest:
|
|
if err := createAndPushTag(exec, v.Version, v.TargetRef); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := createAndPushBranch(exec, v.BranchName, v.TargetRef); err != nil {
|
|
return nil, err
|
|
}
|
|
case ReleaseRequest:
|
|
if err := createAndPushTag(exec, v.Version, v.TargetRef); err != nil {
|
|
return nil, err
|
|
}
|
|
default:
|
|
return nil, xerrors.Errorf("unexpected result type %T", result)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// createAndPushTag creates an annotated tag at targetRef and pushes
|
|
// it. If the tag already exists at the correct commit, it is a
|
|
// no-op. If it exists at a different commit, it returns an error.
|
|
func createAndPushTag(exec CommandExecutor, versionTag, targetRef string) error {
|
|
// Check if the tag already exists locally. Dereference the tag
|
|
// object to the underlying commit with ^{}.
|
|
existing, err := gitOutput(exec, "rev-parse", "--verify", fmt.Sprintf("refs/tags/%s^{}", versionTag))
|
|
if err == nil {
|
|
if existing == targetRef {
|
|
_, _ = fmt.Fprintf(os.Stderr, "tag %s already exists at %s, skipping\n", versionTag, targetRef)
|
|
return nil
|
|
}
|
|
return xerrors.Errorf("tag %s already exists at %s, expected %s", versionTag, existing, targetRef)
|
|
}
|
|
|
|
// Create annotated tag.
|
|
if err := gitMutate(exec, "tag", "-a", versionTag, "-m", fmt.Sprintf("Release %s", versionTag), targetRef); err != nil {
|
|
return xerrors.Errorf("create tag %s: %w", versionTag, err)
|
|
}
|
|
|
|
// Push tag using explicit refspec.
|
|
refspec := fmt.Sprintf("refs/tags/%s:refs/tags/%s", versionTag, versionTag)
|
|
if err := gitMutate(exec, "push", "origin", refspec); err != nil {
|
|
return xerrors.Errorf("push tag %s: %w", versionTag, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// createAndPushBranch creates a branch at targetRef and pushes it.
|
|
// If the branch already exists at the correct commit on the remote,
|
|
// it is a no-op. If it exists at a different commit, it returns an
|
|
// error.
|
|
func createAndPushBranch(exec CommandExecutor, branchName, targetRef string) error {
|
|
// Check if the branch already exists on the remote.
|
|
existing, err := gitOutput(exec, "ls-remote", "--exit-code", "origin", fmt.Sprintf("refs/heads/%s", branchName))
|
|
if err == nil && existing != "" {
|
|
// ls-remote output format: "<sha>\trefs/heads/<branch>"
|
|
remoteSHA, _, _ := strings.Cut(existing, "\t")
|
|
if remoteSHA == targetRef {
|
|
_, _ = fmt.Fprintf(os.Stderr, "branch %s already exists at %s, skipping\n", branchName, targetRef)
|
|
return nil
|
|
}
|
|
return xerrors.Errorf("branch %s already exists at %s, expected %s", branchName, remoteSHA, targetRef)
|
|
}
|
|
|
|
// Push the commit directly to create the remote branch, without
|
|
// needing a local branch.
|
|
refspec := fmt.Sprintf("%s:refs/heads/%s", targetRef, branchName)
|
|
if err := gitMutate(exec, "push", "origin", refspec); err != nil {
|
|
return xerrors.Errorf("push branch %s: %w", branchName, err)
|
|
}
|
|
|
|
return nil
|
|
}
|