Files
coder/scripts/release-action/prepare.go
T
Garrett Delfosse ff7e0bc193 feat: add dry-run flag via CommandExecutor interface (#26422)
## Summary

Adds a `--dry-run` capability to the `release-action` Go tool and
exposes it through a **new** manual workflow, `tag-and-release.yaml`,
without disturbing the existing `release.yaml` pipeline.

PR #25162 had rewritten `release.yaml` in place to be driven by
`scripts/release-action`, which changed its `workflow_dispatch` inputs
from `release_channel`/`release_notes`/`dry_run` to
`release_type`/`commit_sha`. That broke `scripts/releaser`, which
dispatches `release.yaml` with the original inputs. This PR restores
`release.yaml` and moves the Go-driven pipeline to its own workflow.

## Workflow layout after this PR

| Workflow | Trigger | Driven by | Purpose |
|---|---|---|---|
| `release.yaml` | `scripts/releaser` (`gh workflow run`) | legacy
inline shell | Existing pipeline, restored to pre-#25162 state |
| `tag-and-release.yaml` | Manual (Actions UI) |
`scripts/release-action` Go tool | New pipeline with `prepare-release` +
`dry_run` |

`release.yaml` is restored byte-for-byte to its pre-#25162 version, so
its inputs match what `scripts/releaser` sends again.

## `release-action` design

### CommandExecutor interface

Abstracts CLI command execution behind read-only and mutating methods:

| Method | Purpose | Dry-run behavior |
|---|---|---|
| `RunOutput` | Read-only, capture stdout | Executes normally |
| `Run` | Read-only, exit code only | Executes normally |
| `RunMutation` | Changes remote state, no output | **Prints command,
skips execution** |
| `RunMutationStdout` | Changes remote state, streaming I/O | **Prints
command, skips execution** |

Two implementations: `realExecutor` (executes via `os/exec`) and
`dryRunExecutor` (delegates read-only calls, prints mutating calls).

### `prepare-release` subcommand

Composes `calculateNextVersion` with idempotent tag and branch
creation+push, emitting the same JSON as `calculate-version`. Matching
existing refs are skipped; mismatched refs error.

### `tag-and-release.yaml` `dry_run` input

When enabled: `prepare-release` runs with `--dry-run` (version
calculated, plan printed, nothing pushed), notes are generated for
inspection, and the build+publish job is skipped via an `if` guard
(cascading to homebrew/winget/docs).

## Mutating commands covered by `--dry-run`

| Command | Call site |
|---|---|
| `git tag -a <version> ...` | `createAndPushTag` |
| `git push origin refs/tags/...` | `createAndPushTag` |
| `git push origin <sha>:refs/heads/...` | `createAndPushBranch` |
| `gh release create ...` | `publishRelease` |

`git fetch --tags --force origin` is intentionally not a mutation; it
only updates local remote-tracking refs and must run for accurate
version calculation.

## Changes

- **New**: `scripts/release-action/cmdexec.go`, `prepare.go` (+ tests)
- **Refactored**: `git.go`, `github.go`, `calculate.go`, `notes.go`,
`commit.go`, `publish.go` to thread `CommandExecutor`; added `gitMutate`
- **Updated**: `main.go` adds `--dry-run` flag and `prepare-release`
subcommand
- **New**: `.github/workflows/tag-and-release.yaml` (manual, Go-driven,
with `dry_run`)
- **Reverted**: `.github/workflows/release.yaml` to its pre-#25162 state

> [!NOTE]
> Generated by Coder Agents on behalf of @f0ssel
2026-07-01 16:20:00 -04:00

95 lines
3.3 KiB
Go

package main
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
}