tools/bump: hand publishing off to a separate step

Lets the gate run without a write token, and the token that
publishes be minted after a gate that can outlive it.
This commit is contained in:
Kristoffer Dalby authored and Juan Font committed 2026-11-11 15:19:25 +01:00
1 parent d2dc13696a
commit 727024ec52
2 files changed
+98 -19

No files matched your search

+47 -12
View File
@@ -14,6 +14,7 @@
//
// bump plan resolve every source of truth and print what would change
// bump run apply, gate, and open or update the pull request
// bump publish open or update the pull request a run handed off
// bump verify assert the pins are mutually consistent
package main
@@ -30,20 +31,27 @@ import (
)
type runFlags struct {
DryRun bool `flag:"dry-run,default=false,Rebuild the branch locally and report, but run no final gate and touch no remote"`
NoPR bool `flag:"no-pr,default=false,Push nothing and open no pull request"`
Areas string `flag:"areas,Comma-separated areas to run (default: all)"`
Skip string `flag:"skip,Comma-separated areas to skip"`
Force bool `flag:"force,default=false,Open a pull request even if an identical one was closed unmerged"`
Branch string `flag:"branch,default=automation/version-bump,Branch to push"`
Base string `flag:"base,default=main,Base branch"`
Remote string `flag:"remote,default=origin,Git remote"`
Repo string `flag:"repo,GitHub repository (default: the one the job runs in)"`
Gate string `flag:"gate,default=full,Final gate level: none, quick or full"`
DryRun bool `flag:"dry-run,default=false,Rebuild the branch locally and report, but run no final gate and touch no remote"`
NoPR bool `flag:"no-pr,default=false,Push nothing and open no pull request"`
Areas string `flag:"areas,Comma-separated areas to run (default: all)"`
Skip string `flag:"skip,Comma-separated areas to skip"`
Force bool `flag:"force,default=false,Open a pull request even if an identical one was closed unmerged"`
Branch string `flag:"branch,default=automation/version-bump,Branch to push"`
Base string `flag:"base,default=main,Base branch"`
Remote string `flag:"remote,default=origin,Git remote"`
Repo string `flag:"repo,GitHub repository (default: the one the job runs in)"`
Gate string `flag:"gate,default=full,Final gate level: none, quick or full"`
Handoff string `flag:"handoff,Write the pull request to this file for bump publish instead of opening it"`
}
var runCfg runFlags
type publishFlags struct {
Handoff string `flag:"handoff,File written by bump run --handoff"`
}
var publishCfg publishFlags
func main() {
log.SetFlags(0)
log.SetPrefix("bump: ")
@@ -63,6 +71,12 @@ func main() {
SetFlags: command.Flags(flax.MustBind, &runCfg),
Run: func(env *command.Env) error { return cmdRun(env.Context()) },
},
{
Name: "publish",
Help: "Open or update the pull request a run handed off",
SetFlags: command.Flags(flax.MustBind, &publishCfg),
Run: func(env *command.Env) error { return cmdPublish(env.Context()) },
},
{
Name: "verify",
Help: "Assert the pins are mutually consistent",
@@ -190,7 +204,7 @@ func cmdRun(ctx context.Context) error {
lease = open.HeadRefOid
}
return publish(ctx, r, publishOptions{
opt := publishOptions{
Slug: slug,
Remote: runCfg.Remote,
Branch: runCfg.Branch,
@@ -198,9 +212,30 @@ func cmdRun(ctx context.Context) error {
Title: "all: bump pinned versions",
Gate: gate,
Force: runCfg.Force,
Head: head,
Lease: lease,
Carried: carried,
}, results)
}
if runCfg.Handoff != "" {
return writeHandoff(runCfg.Handoff, handoff{Options: opt, Body: body})
}
return publish(ctx, r, opt, body)
}
func cmdPublish(ctx context.Context) error {
h, err := readHandoff(publishCfg.Handoff)
if err != nil {
return err
}
r, err := openRepo(ctx)
if err != nil {
return err
}
return publish(ctx, r, h.Options, h.Body)
}
func anyApplied(results []result) bool {
+51 -7
View File
@@ -3,8 +3,10 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
@@ -17,6 +19,8 @@ const skipLabel = "no-autoupdate"
// same diff the next morning is how an automation earns itself an ignore rule.
const closedCooldown = 7 * 24 * time.Hour
var errHeadMoved = errors.New("HEAD moved after the gate")
type prState struct {
Number int `json:"number"`
State string `json:"state"`
@@ -80,27 +84,67 @@ type publishOptions struct {
Title string
Gate string
Force bool
// Head is the commit the gate passed; publish pushes nothing else.
Head string
// Lease is the branch tip the run started from, when it updates an open
// pull request; Carried lists hand-pushed commits it built on.
Lease string
Carried []string
}
// handoff is a gated run waiting to be published. Publishing in a later step
// keeps the write token away from the gate, which builds freshly bumped
// dependencies, and mints it after the gate, which can outlive it.
type handoff struct {
Options publishOptions
Body string
}
func writeHandoff(path string, h handoff) error {
data, err := json.Marshal(h)
if err != nil {
return fmt.Errorf("encoding handoff: %w", err)
}
if err := os.WriteFile(path, data, 0o600); err != nil { //nolint:noinlineerr
return fmt.Errorf("writing handoff: %w", err)
}
return nil
}
func readHandoff(path string) (handoff, error) {
var h handoff
data, err := os.ReadFile(path)
if err != nil {
return h, fmt.Errorf("reading handoff: %w", err)
}
if err := json.Unmarshal(data, &h); err != nil { //nolint:noinlineerr
return h, fmt.Errorf("decoding handoff: %w", err)
}
return h, nil
}
// publish force-pushes the rebuilt branch and puts it in front of a human:
// reusing the open pull request if there is one, opening a new one once the
// last was merged, and staying out of the way when the last was rejected.
func publish(ctx context.Context, r *repo, opt publishOptions, results []result) error {
tree, err := treeSHA(ctx, r)
if err != nil {
return err
}
func publish(ctx context.Context, r *repo, opt publishOptions, body string) error {
head, err := headSHA(ctx, r)
if err != nil {
return err
}
body := renderBody(results, markerOf(results, tree, head), opt.Gate, opt.Carried)
if head != opt.Head {
return fmt.Errorf("%w: gated %s, HEAD is %s", errHeadMoved, opt.Head, head)
}
tree, err := treeSHA(ctx, r)
if err != nil {
return err
}
pr, err := lookupPR(ctx, r, opt.Slug, opt.Branch)
if err != nil {