Files
coder/scripts/releaser/github.go
T
Garrett Delfosse 86a050de43 fix(scripts/releaser): scope breaking-change check to commits since previous release (#29487)
The patch-release breaking-change check in `./scripts/release.sh`
queried every merged PR labeled `release/breaking` against the release
branch with no range filter, so breaking changes shipped in earlier
patches were reported again on every subsequent patch release.

Both the `!` title marker and the `release/breaking` label are now
evaluated only over `prevVersion..HEAD`, using the same PR metadata
lookup the release notes already use (`ghBuildPRMetadataMap` +
`categorizeCommit`). The now-unused `ghListPRsWithLabel` is removed and
the metadata fetch-with-fallback is extracted into `loadPRMetadata` so
both call sites share it.

The GPG signing prompt was already removed on `main` in #27422, so this
PR only covers the breaking-change check. Backporting to `release/2.34`
and `release/2.35` needs both #27422 and this PR; `release/2.36` only
needs this PR.

Supersedes #27065.

---
Generated by Coder Agents on behalf of @f0ssel.
2026-09-17 15:10:26 -04:00

187 lines
4.7 KiB
Go

package main
import (
"errors"
"io"
"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
}
// 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{}
}
// loadPRMetadata fetches PR metadata for the given commits via the gh
// CLI. It never fails: when gh is unavailable or the query errors, a
// warning is written and empty maps are returned so callers degrade to
// title-only categorization.
func loadPRMetadata(w io.Writer, ghAvailable bool, commits []commitEntry) *prMetadataMaps {
if ghAvailable {
prMeta, err := ghBuildPRMetadataMap(commits)
if err != nil {
warnf(w, "Failed to fetch PR metadata: %v", err)
} else if prMeta != nil {
return prMeta
}
}
return &prMetadataMaps{
bySHA: make(map[string]prMetadata),
byNumber: make(map[int]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
}