Files
coder/scripts/release-action/github.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

110 lines
3.0 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"golang.org/x/xerrors"
)
// ghOutput runs a gh CLI command and returns trimmed stdout.
func ghOutput(exec CommandExecutor, args ...string) (string, error) {
return exec.RunOutput("gh", args...)
}
// pullRequest holds metadata about a GitHub pull request.
type pullRequest struct {
Number int
Title string
Labels []string
Author string
URL string
}
// pullRequestMap holds PR metadata indexed by PR number.
type pullRequestMap map[int]pullRequest
// ghBuildPullRequestMap builds a map of PR number to metadata by
// querying the GitHub API via the gh CLI for the given PR numbers.
func ghBuildPullRequestMap(exec CommandExecutor, prNumbers []int) pullRequestMap {
m := make(pullRequestMap)
for _, prNum := range prNumbers {
out, err := ghOutput(exec, "pr", "view", fmt.Sprintf("%d", prNum),
"--repo", fmt.Sprintf("%s/%s", owner, repo),
"--json", "number,labels,author")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "warning: failed to fetch PR #%d metadata: %v\n", prNum, err)
continue
}
var result struct {
Number int `json:"number"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
Author struct {
Login string `json:"login"`
} `json:"author"`
}
if err := json.Unmarshal([]byte(out), &result); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "warning: failed to parse PR #%d metadata: %v\n", prNum, err)
continue
}
var labels []string
for _, l := range result.Labels {
labels = append(labels, l.Name)
}
m[result.Number] = pullRequest{
Number: result.Number,
Labels: labels,
Author: result.Author.Login,
}
}
return m
}
// checkOpenPRs verifies that no pull requests are open against the
// given branch. If any are found, it returns an error listing them
// with instructions to merge or close before releasing.
func checkOpenPRs(exec CommandExecutor, branch string) error {
out, err := ghOutput(exec, "pr", "list",
"--repo", fmt.Sprintf("%s/%s", owner, repo),
"--base", branch,
"--state", "open",
"--json", "number,title,author,url",
"--limit", "100")
if err != nil {
return xerrors.Errorf("failed to list open PRs for branch %s: %w", branch, err)
}
var rawPRs []struct {
Number int `json:"number"`
Title string `json:"title"`
Author struct {
Login string `json:"login"`
} `json:"author"`
URL string `json:"url"`
}
if err := json.Unmarshal([]byte(out), &rawPRs); err != nil {
return xerrors.Errorf("failed to parse open PRs response: %w", err)
}
if len(rawPRs) == 0 {
return nil
}
var b strings.Builder
_, _ = fmt.Fprintf(&b, "found %d open pull request(s) targeting %s that must be merged or closed before releasing:\n\n", len(rawPRs), branch)
for _, pr := range rawPRs {
_, _ = fmt.Fprintf(&b, " - #%d: %s (by @%s)\n %s\n", pr.Number, pr.Title, pr.Author.Login, pr.URL)
}
_, _ = fmt.Fprintf(&b, "\nMerge or close these pull requests, then re-run the release workflow.")
return xerrors.New(b.String())
}