Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/versions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Usage:
every entry tracked with it at the same
versions refresh <name>... bump each to its newest, a commit each, and say what moved;
-propose pushes them as refresh/<label>-<sum> and opens
the pull request (GITHUB_TOKEN or GH_TOKEN), once per set,
the pull request (GH_TOKEN or GITHUB_TOKEN), once per set,
closing the open one it supersedes (-label, -base)

Every command takes -file, versions.yaml by default. Nothing is built by a bump: each entry's
Expand Down
35 changes: 25 additions & 10 deletions cmd/versions/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (r refresher) pending() string { return "refresh/" + r.label + "-pending" }
func (r refresher) propose(ctx context.Context, start string, all []moved, failed []error) error {
if len(all) == 0 {
_, err := fmt.Fprintln(r.out, "refresh: every entry is at its newest")
return errors.Join(err, r.back(start))
return errors.Join(err, r.back(start, r.pending()))
}
raw, err := os.ReadFile(r.file)
if err != nil {
Expand All @@ -144,7 +144,7 @@ func (r refresher) propose(ctx context.Context, start string, all []moved, faile
if err == nil {
_, err = fmt.Fprintf(r.out, "refresh: %s was proposed already: %s\n", branch, was)
}
return errors.Join(err, r.back(start))
return errors.Join(err, r.back(start, branch))
}
if _, err := r.git("push", "-q", "-f", "origin", branch); err != nil {
return err
Expand All @@ -153,22 +153,36 @@ func (r refresher) propose(ctx context.Context, start string, all []moved, faile
if err != nil {
return err
}
if _, err := fmt.Fprintln(r.out, pr.URL); err != nil {
if err := r.say(pr.URL); err != nil {
return err
}
return r.gh.supersede(ctx, "refresh/"+r.label+"-", branch, pr)
}

// back returns to the branch the refresh started on and drops the one it made for nothing.
func (r refresher) back(start string) error {
cur, err := r.git("rev-parse", "--abbrev-ref", "HEAD")
// say prints line, and in a workflow adds it to the run's summary, where the pull request is
// found without opening the step's log.
func (r refresher) say(line string) error {
if _, err := fmt.Fprintln(r.out, line); err != nil {
return err
}
path := os.Getenv("GITHUB_STEP_SUMMARY")
if path == "" {
return nil
}
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) //nolint:gosec // the path the runner gives
if err != nil {
return err
}
_, err = fmt.Fprintln(f, line)
return errors.Join(err, f.Close())
}

// back returns to the branch the refresh started on and drops made, the one it made for nothing.
func (r refresher) back(start, made string) error {
if _, err := r.git("switch", "-q", start); err != nil {
return err
}
_, err = r.git("branch", "-q", "-D", cur)
_, err := r.git("branch", "-q", "-D", made)
return err
}

Expand Down Expand Up @@ -235,11 +249,12 @@ type pull struct {
var remote = regexp.MustCompile(`github\.com[:/]([^/]+/[^/]+?)(?:\.git)?$`)

// newGitHub is the repository GITHUB_REPOSITORY names in a workflow, or origin's elsewhere, as
// GITHUB_TOKEN or GH_TOKEN.
// GH_TOKEN or GITHUB_TOKEN, in gh's order: a workflow sets GITHUB_TOKEN to the run's own token,
// which may only read, and hands an app's token as GH_TOKEN for what is to start the checks.
func newGitHub(git func(...string) (string, error)) (*github, error) {
token := cmp.Or(os.Getenv("GITHUB_TOKEN"), os.Getenv("GH_TOKEN"))
token := cmp.Or(os.Getenv("GH_TOKEN"), os.Getenv("GITHUB_TOKEN"))
if token == "" {
return nil, errors.New("refresh: -propose opens a pull request as GITHUB_TOKEN or GH_TOKEN, and neither is set")
return nil, errors.New("refresh: -propose opens a pull request as GH_TOKEN or GITHUB_TOKEN, and neither is set")
}
repo := os.Getenv("GITHUB_REPOSITORY")
if repo == "" {
Expand Down
12 changes: 11 additions & 1 deletion cmd/versions/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func TestARefreshProposesWhatMoved(t *testing.T) {
origin := inRepository(t, refreshFile())
f := &fakeGitHub{open: []pull{openPull(3, "refresh/versions-old"), openPull(4, "feature"), openPull(5, "refresh/kernel-1")}}
var out bytes.Buffer
summaryFile := filepath.Join(t.TempDir(), "summary.md")
t.Setenv("GITHUB_STEP_SUMMARY", summaryFile)
now := time.Date(2026, 11, 18, 23, 0, 0, 0, time.FixedZone("ART", -3*3600))
r := refresher{file: "versions.yaml", git: runGit, gh: f.serve(t), label: "versions", base: "main", out: &out, now: now}
require.NoError(t, r.refresh(t.Context(), []string{"snapshot"}))
Expand All @@ -172,6 +174,9 @@ func TestARefreshProposesWhatMoved(t *testing.T) {
assert.Regexp(t, `^refresh/versions-[0-9a-f]{12}$`, branch)
assert.Equal(t, git(t, "rev-parse", "HEAD"), gittest.Run(t, origin, "rev-parse", branch), "pushed")
assert.Equal(t, "https://github.com/o/r/pull/9\n", out.String())
written, err := os.ReadFile(summaryFile)
require.NoError(t, err)
assert.Equal(t, "https://github.com/o/r/pull/9\n", string(written), "the run's summary has it too")
require.Len(t, f.asked, 6)
assert.Equal(t, "GET /repos/o/r/pulls?head=o%3A"+strings.ReplaceAll(branch, "/", "%2F")+"&state=all", f.asked[0])
var created map[string]string
Expand Down Expand Up @@ -236,11 +241,16 @@ func TestTheRepositoryIsTheWorkflowsOrOrigins(t *testing.T) {
_, err = newGitHub(origin("https://gitlab.com/o/r"))
assert.ErrorContains(t, err, "is not a GitHub repository")

// GH_TOKEN first, as gh reads them: a workflow's GITHUB_TOKEN is its run's, which may only read.
t.Setenv("GITHUB_TOKEN", "workflow")
t.Setenv("GITHUB_REPOSITORY", "w/x")
g, err := newGitHub(origin("unused"))
require.NoError(t, err)
assert.Equal(t, &github{api: "https://api.github.com", repo: "w/x", token: "workflow"}, g)
assert.Equal(t, &github{api: "https://api.github.com", repo: "w/x", token: "gh"}, g)
t.Setenv("GH_TOKEN", "")
g, err = newGitHub(origin("unused"))
require.NoError(t, err)
assert.Equal(t, "workflow", g.token)
}

// GitHub refusing a request is an error that says what it was asked and what it answered.
Expand Down
9 changes: 8 additions & 1 deletion site/src/content/docs/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ jobs:
- uses: actions/setup-go@<commit> # v7.0.0
with:
go-version-file: go.mod
# A runner has no git identity, and refresh commits each entry it moves.
- env:
GITHUB_TOKEN: ${{ github.token }}
GIT_AUTHOR_NAME: github-actions[bot]
GIT_AUTHOR_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
GIT_COMMITTER_NAME: github-actions[bot]
GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
run: go tool task refresh
```

Expand All @@ -164,7 +169,9 @@ cannot be moved is listed in the body and fails the run, after the others are pr
entry's `note` is in the body, as what to check before merging.

A pull request opened with a workflow's own `GITHUB_TOKEN` starts no workflow: a repository whose
checks must run on it opens it as a GitHub App, and commits as one.
checks must run on it opens it as a GitHub App, and commits as one. The token is `GH_TOKEN`'s when
it is set and `GITHUB_TOKEN`'s otherwise, as `gh` reads them, so the app's goes in `GH_TOKEN`. The
pull request's link is printed, and added to the run's summary.

## The gate

Expand Down
8 changes: 3 additions & 5 deletions versions/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,9 @@ func run(ctx context.Context, name string, args ...string) (string, error) {
// newestWith is what an entry tracked with another is at: that one's, in v or in its repository's
// file as it is now.
func newestWith(ctx context.Context, v *versions.Versions, e versions.Entry) (string, error) {
a, _, err := e.With()
switch {
case err != nil: // mutate-exempt: Parse refused a with track of no form, and v is parsed
return "", err
case a.Repo == "":
// A with track of no form has no Repo, and Expected says what is wrong with it.
a, _, _ := e.With()
if a.Repo == "" {
return v.Expected(e)
}
other, err := repositoryFile(ctx, a.Repo)
Expand Down
8 changes: 1 addition & 7 deletions versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,7 @@ func (v *Versions) validateWith(e Entry) error {

// Expected is the version an entry tracked with another in this file is at: see ExpectedIn.
func (v *Versions) Expected(e Entry) (string, error) {
a, ok, err := e.With()
switch {
case err != nil:
return "", err
case !ok:
return "", fmt.Errorf("versions: %s is not tracked with another entry", e.Name)
case a.Repo != "":
if a, _, _ := e.With(); a.Repo != "" {
return "", fmt.Errorf("versions: %s is with %s's %s, which this file does not have", e.Name, a.Repo, a.Name)
}
return ExpectedIn(e, v)
Expand Down
Loading