Files
coder/scripts/release-action/cmdexec_test.go
T
Garrett Delfosse b1ead5f085 fix: set git identity for release tagging and surface git stderr (#26945)
## What happened

The [Tag and Release
run](https://github.com/coder/coder/actions/runs/28549109434/job/84641825784)
failed in the `prepare-release` job at the step "Prepare release
(calculate version, create tag and branch)" with:

```
error: create tag v2.35.0-rc.0: exit status 128
```

## Root cause

`prepare-release` creates an **annotated** tag via `git tag -a`
(`scripts/release-action/prepare.go`), which records a tagger and
therefore requires a git identity. The job never ran `git config
user.name/user.email`, and runners have none configured, so git aborts
with exit status 128. The real `fatal:` message was hidden because
`realExecutor.RunMutation` discarded the command's stderr.

## Changes

- **`.github/workflows/tag-and-release.yaml`**: add a "Configure git
identity" step (`ci@coder.com` / `Coder CI`) to the `prepare-release`
job, before the release tool runs. This matches the identity pattern
already used later in the same workflow.
- **`scripts/release-action/cmdexec.go`**: capture stderr in
`RunMutation` and include it in the returned error, so a failing
mutation surfaces the underlying command output (e.g. git's `fatal:`
line) instead of only `exit status N`.
- **`scripts/release-action/cmdexec_test.go`**: add a test asserting
stderr is surfaced on failure.

## Testing

- `go test ./scripts/release-action/...` passes.
- `go vet ./scripts/release-action/...` and `gofmt` clean.
- `actionlint .github/workflows/tag-and-release.yaml` clean.
- Reproduced the failure locally: `git tag -a` with no usable identity
exits 128 (`fatal: no email was given and auto-detection is disabled`);
with an identity configured it succeeds.

<details>
<summary>Root-cause analysis / decision log</summary>

**Failing step** runs `go run ./scripts/release-action prepare-release
--type create-release-branch --ref main --commit cb1a87b…`.

1. The tool computes the next version `v2.35.0-rc.0` and calls
`createAndPushTag`, which runs `git tag -a v2.35.0-rc.0 -m "Release
v2.35.0-rc.0" <targetRef>` (`prepare.go:56`).
2. That git command exits **128**, wrapped as `error: create tag
v2.35.0-rc.0: exit status 128`.

**Why it's the identity, and not something else:**

- No `git config user.name/user.email` step exists in the
`prepare-release` job; the `setup-mise` action does not set it; and the
tool itself never sets an identity. Annotated tags require a tagger, so
`git tag -a` fails on runners whose auto-detected identity is bogus
(`…@runner.(none)`), which is rejected under git's strict identity
check.
- Not a pre-existing tag collision: no `v2.35.0*` tag exists on the
remote, and the code pre-checks for an existing tag (and would emit a
different "already exists" error).
- Not an unresolved ref: `targetRef` resolves to the provided commit
SHA, checked out at `fetch-depth: 0`.
- The log was unhelpful because `RunMutation` used `cmd.Run()` without
wiring git's stderr (`cmdexec.go`), discarding the `fatal:` line and
leaving only `exit status 128`. This PR fixes that too.
- The sibling `release.yaml` explicitly sets `git config
user.email/user.name` before its git mutations; that step was simply
missing from the newer `tag-and-release.yaml` `prepare-release` job.

</details>

---

> Generated by Coder Agents on behalf of @f0ssel.
2026-07-01 15:10:45 -07:00

135 lines
3.5 KiB
Go

package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRealExecutor_RunOutput(t *testing.T) {
t.Parallel()
exec := realExecutor{}
out, err := exec.RunOutput("echo", "hello")
require.NoError(t, err)
assert.Equal(t, "hello", out)
}
func TestRealExecutor_Run(t *testing.T) {
t.Parallel()
exec := realExecutor{}
err := exec.Run("true")
require.NoError(t, err)
err = exec.Run("false")
require.Error(t, err)
}
func TestRealExecutor_RunMutation(t *testing.T) {
t.Parallel()
exec := realExecutor{}
err := exec.RunMutation("true")
require.NoError(t, err)
err = exec.RunMutation("false")
require.Error(t, err)
}
func TestRealExecutor_RunMutationSurfacesStderr(t *testing.T) {
t.Parallel()
exec := realExecutor{}
err := exec.RunMutation("sh", "-c", "echo 'fatal: boom' 1>&2; exit 1")
require.Error(t, err)
assert.Contains(t, err.Error(), "fatal: boom")
}
func TestRealExecutor_RunMutationStdout(t *testing.T) {
t.Parallel()
exec := realExecutor{}
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "echo", "output")
require.NoError(t, err)
assert.Equal(t, "output\n", stdout.String())
}
func TestDryRunExecutor_RunOutputDelegates(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
// RunOutput should still execute (read-only commands).
out, err := exec.RunOutput("echo", "real-output")
require.NoError(t, err)
assert.Equal(t, "real-output", out)
assert.Empty(t, buf.String(), "RunOutput should not produce dry-run output")
}
func TestDryRunExecutor_RunDelegates(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
err := exec.Run("true")
require.NoError(t, err)
assert.Empty(t, buf.String(), "Run should not produce dry-run output")
}
func TestDryRunExecutor_RunMutationPrints(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
err := exec.RunMutation("git", "push", "origin", "refs/tags/v2.21.0:refs/tags/v2.21.0")
require.NoError(t, err)
assert.Contains(t, buf.String(), "[dry-run] would run: git push origin refs/tags/v2.21.0:refs/tags/v2.21.0")
}
func TestDryRunExecutor_RunMutationStdoutPrints(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "gh", "release", "create", "--repo", "coder/coder", "--title", "v2.21.0")
require.NoError(t, err)
assert.Empty(t, stdout.String(), "RunMutationStdout should not produce real output in dry-run")
assert.Contains(t, buf.String(), "[dry-run] would run: gh release create --repo coder/coder --title v2.21.0")
}
func TestDryRunExecutor_RunMutationStdoutQuotesArgs(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "gh", "release", "create", "--title", "has space")
require.NoError(t, err)
assert.Contains(t, buf.String(), "'has space'")
}
func TestShelljoin(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
want string
}{
{"simple", []string{"a", "b"}, "a b"},
{"space", []string{"a", "has space"}, "a 'has space'"},
// shelljoin uses POSIX single-quote escaping, so an embedded
// single quote becomes '"'"'.
{"quote", []string{"it's"}, `'it'"'"'s'`},
{"empty", []string{}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := shelljoin(tt.args)
assert.Equal(t, tt.want, got)
})
}
}