Files
tailscale/misc/bumpdeps/resolve.go
Brad Fitzpatrick 63f625397b misc/bumpdeps, .github/workflows: unify the bumpdeps tool and bumpdep workflow
The bumpdep workflow grew 170 lines of inline shell that duplicated
much of what misc/bumpdeps already did in Go (resolving versions,
special branches, running go get), and disagreed with it in small ways
(the workflow fetched wireguard-go with GOPROXY=direct; the tool asked
the proxy for the branch head). It was also untestable except by
extracting the run block and running it by hand.

Move all of that logic into misc/bumpdeps and make the workflow a thin
wrapper that runs it with -github. The tool now:

  - accepts the workflow's argument forms: "go" for the toolchain (via
    ./pull-toolchain.sh), the "wireguard-go" and "gvisor" aliases,
    exact module paths, "path@version", and modules not yet in go.mod,
    alongside its existing substring filters; comma-separated arguments
    are split, so the workflow input passes straight through;
  - finds branch heads with git ls-remote and then asks the proxy for
    the pseudo-version of that commit, so a just-pushed commit is seen
    without ever cloning (GOPROXY=direct on github.com/gokrazy/kernel.*
    hung the workflow for hours);
  - refuses downgrades by diffing go.mod before and after go get,
    rather than grepping go get's output;
  - runs "make tidy" and "make updatedeps" itself (-tidy=false to skip);
  - renders the PR title, body (GitHub compare links derived from the
    module path, including subdirectory-tagged modules), and commit
    message, printing a suggested commit message locally and, with
    -github, writing step outputs and the step summary. -issue accepts
    the issue URL or "#N" form and becomes the "Updates" line.

The branch name changes from a slug of every module path, which
produced names like actions/bumpdep/github.com-gokrazy-kernel.amd64-
main-github.com-gokrazy-kernel.arm64-main-github, to
actions/<workflow>/<actor>/<yyyymmdd-hhmmss> (no actor for scheduled
runs). Each run is a fresh PR; delete-branch cleans up after merge or
close.

The bumpdep workflow gains an optional exclude-newer-than-days input for
the cooldown. All of the formerly-inline logic now has unit tests,
including a fake module proxy.

Also add a gokrazy-bump workflow that runs the same tool every Sunday
night on the direct github.com/gokrazy/* modules (kernels, firmware,
gokrazy itself) and opens a PR labeled run-natlab-tests, so the natlab
VM tests boot the new kernels before merge. Substring filters now skip
indirect dependencies unless -indirect is set, so that "github.com/
gokrazy/" doesn't drag in gokapi; naming an indirect module exactly
still selects it.

Updates tailscale/corp#48312
Updates #8043
Updates #1866

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I7c3e91a4d2f58b0e6a1c9d47f3b2e8a5c6d0f1e2
2026-09-23 06:42:52 -07:00

289 lines
9.1 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"slices"
"strings"
"time"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
// resolver looks up versions on the module proxy.
type resolver struct {
client *http.Client
base string // proxy URL without a trailing slash
cutoff time.Time // versions committed after this are too new; zero means no cooldown
// lsRemote returns the commit hash at the head of branch in the git
// repo at repoURL. It's a field so tests can fake it.
lsRemote func(ctx context.Context, repoURL, branch string) (string, error)
}
// heldError says a newer version exists but is younger than the cooldown.
type heldError struct {
version string
t time.Time
}
func (e *heldError) Error() string {
return fmt.Sprintf("%s is only %.1f days old", e.version, time.Since(e.t).Hours()/24)
}
// versionInfo is the proxy's @latest and @v/<version>.info response.
type versionInfo struct {
Version string
Time time.Time
}
// tooNew reports whether a version committed at t is younger than the
// cooldown.
func (r *resolver) tooNew(t time.Time) bool {
return !r.cutoff.IsZero() && t.After(r.cutoff)
}
// lookupNewer asks the module proxy for the newest version of mod, or for
// the head of its branch if it's in specialBranches. It returns "" if there
// is nothing newer than the version already in go.mod or if the newer
// version isn't usable under mod's path, and a *heldError if the only
// newer versions are younger than the cooldown.
//
// mod.Version may be empty for a module that's not yet in go.mod, in
// which case anything the proxy offers is newer.
func (r *resolver) lookupNewer(ctx context.Context, mod module.Version) (string, error) {
escaped, err := module.EscapePath(mod.Path)
if err != nil {
return "", err
}
base := r.base + "/" + escaped
var info versionInfo
if bi, ok := specialBranches[mod.Path]; ok {
info, err = r.branchHead(ctx, base, bi)
if err != nil {
return "", err
}
} else {
if err := fetchJSON(ctx, r.client, base+"/@latest", &info); err != nil {
return "", err
}
if isNewer(info.Version, mod.Version) && r.tooNew(info.Time) {
// Fall back to the newest release that has aged enough, if any.
// Only tagged versions are listed, so branch heads (above)
// have nothing to fall back to and get held instead.
aged, err := r.newestAged(ctx, base, mod.Version)
if err != nil {
return "", err
}
if aged.Version == "" {
return "", &heldError{info.Version, info.Time}
}
info = aged
}
}
if !semver.IsValid(info.Version) {
return "", fmt.Errorf("proxy returned invalid version %q", info.Version)
}
// Only ever move forward. The proxy's @latest is the newest tagged
// release, which can be older than a pseudo-version already in go.mod.
if !isNewer(info.Version, mod.Version) {
return "", nil
}
if r.tooNew(info.Time) {
return "", &heldError{info.Version, info.Time}
}
// Also never move backward in time. Forks sometimes carry stray tags
// that sort above their real development branch (github.com/tailscale/
// golang-x-crypto has a v0.91.0 from 2024, say), and the proxy happily
// reports those as @latest.
if mod.Version != "" {
curTime, err := r.versionTime(ctx, base, mod.Version)
if err != nil {
return "", err
}
if !info.Time.After(curTime) {
return "", fmt.Errorf("%s (%s) is older than current %s (%s); probably a stray tag, so add it to specialBranches or bump by hand",
info.Version, info.Time.Format(time.DateOnly), mod.Version, curTime.Format(time.DateOnly))
}
}
// Modules sometimes move (github.com/imdario/mergo became
// dario.cat/mergo, say) and keep tagging releases under the new path.
// The proxy still reports those as @latest for the old path, but go
// get rejects them, so check the go.mod of the candidate version.
escapedVer, err := module.EscapeVersion(info.Version)
if err != nil {
return "", err
}
gomod, err := fetch(ctx, r.client, base+"/@v/"+escapedVer+".mod")
if err != nil {
return "", err
}
if got := modfile.ModulePath(gomod); got != mod.Path {
return "", fmt.Errorf("%s declares module path %s; update the import path by hand", info.Version, got)
}
return info.Version, nil
}
// isNewer reports whether candidate is newer than cur, where an empty cur
// (module not yet in go.mod) is older than everything.
func isNewer(candidate, cur string) bool {
return cur == "" || semver.Compare(candidate, cur) > 0
}
// branchHead returns the version at the head of the branch that bi
// describes, for the module served at base. It asks the git repo directly
// for the head commit, so a just-pushed commit is seen even if the proxy
// hasn't noticed it yet, and then asks the proxy for the pseudo-version of
// that commit. If git isn't available it falls back to the proxy's idea of
// the branch head.
func (r *resolver) branchHead(ctx context.Context, base string, bi branchInfo) (versionInfo, error) {
var info versionInfo
rev, err := r.lsRemote(ctx, bi.repo, bi.branch)
if err != nil {
log.Printf("git ls-remote %s %s failed (%v); asking the proxy instead", bi.repo, bi.branch, err)
rev = bi.branch
}
if err := fetchJSON(ctx, r.client, base+"/@v/"+rev+".info", &info); err != nil {
return versionInfo{}, err
}
return info, nil
}
// gitLsRemote returns the commit hash at the head of branch in repoURL.
func gitLsRemote(ctx context.Context, repoURL, branch string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
out, err := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", repoURL, "refs/heads/"+branch).Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
return "", fmt.Errorf("%w: %s", err, bytes.TrimSpace(ee.Stderr))
}
return "", err
}
hash, _, _ := strings.Cut(strings.TrimSpace(string(out)), "\t")
if len(hash) != 40 && len(hash) != 64 {
return "", fmt.Errorf("unexpected ls-remote output %q", out)
}
return hash, nil
}
// newestAged returns the newest tagged release of the module served at base
// that is both newer than cur and older than the cooldown, or a zero
// versionInfo if there is none. Prereleases are skipped, matching what
// @latest would pick.
func (r *resolver) newestAged(ctx context.Context, base, cur string) (versionInfo, error) {
list, err := fetch(ctx, r.client, base+"/@v/list")
if err != nil {
return versionInfo{}, err
}
var candidates []string
for _, v := range strings.Fields(string(list)) {
if semver.IsValid(v) && semver.Prerelease(v) == "" && isNewer(v, cur) {
candidates = append(candidates, v)
}
}
semver.Sort(candidates)
slices.Reverse(candidates)
for _, v := range candidates {
escapedVer, err := module.EscapeVersion(v)
if err != nil {
return versionInfo{}, err
}
var info versionInfo
if err := fetchJSON(ctx, r.client, base+"/@v/"+escapedVer+".info", &info); err != nil {
return versionInfo{}, err
}
if !r.tooNew(info.Time) {
return info, nil
}
}
return versionInfo{}, nil
}
// versionTime returns the commit time of version ver of the module served
// at base. Pseudo-versions carry it in their name, so only tagged versions
// need a proxy round trip.
func (r *resolver) versionTime(ctx context.Context, base, ver string) (time.Time, error) {
if module.IsPseudoVersion(ver) {
return module.PseudoVersionTime(ver)
}
escapedVer, err := module.EscapeVersion(ver)
if err != nil {
return time.Time{}, err
}
var info versionInfo
if err := fetchJSON(ctx, r.client, base+"/@v/"+escapedVer+".info", &info); err != nil {
return time.Time{}, err
}
return info.Time, nil
}
// statusError is a non-2xx response from the proxy.
type statusError struct {
code int
body string
}
func (e *statusError) Error() string {
return fmt.Sprintf("proxy returned %d: %s", e.code, strings.TrimSpace(e.body))
}
// fetchJSON fetches url from the proxy and decodes its JSON body into dst.
func fetchJSON(ctx context.Context, client *http.Client, url string, dst any) error {
body, err := fetch(ctx, client, url)
if err != nil {
return err
}
return json.Unmarshal(body, dst)
}
// fetch fetches url from the proxy and returns its body. It retries once
// on transient errors, since the proxy occasionally returns those under
// load. Client errors are final.
func fetch(ctx context.Context, client *http.Client, url string) ([]byte, error) {
for attempt := 0; ; attempt++ {
body, err := fetchOnce(ctx, client, url)
if err == nil {
return body, nil
}
var se *statusError
if attempt > 0 || (errors.As(err, &se) && se.code < 500) {
return nil, err
}
}
}
func fetchOnce(ctx context.Context, client *http.Client, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(res.Body, 4<<10))
return nil, &statusError{res.StatusCode, string(body)}
}
return io.ReadAll(res.Body)
}