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
22 changes: 15 additions & 7 deletions go/test/site/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ func (r *runner) prepare() error {
// tick runs everything that is due, one job at a time.
func (r *runner) tick() {
stateFile := filepath.Join(r.work, "last-random")
last, _ := os.ReadFile(stateFile)
jobs, err := plan(r.gh, r.cfg, time.Now(), strings.TrimSpace(string(last)))
if err != nil {
log.Printf("plan: %v", err)
return
}
for _, j := range jobs {
// Plan again before each job: a run takes about 20 minutes, in which a
// pull request can move on or a second runner can take the same commit.
ran := map[string]bool{}
for {
last, _ := os.ReadFile(stateFile)
jobs, err := plan(r.gh, r.cfg, time.Now(), strings.TrimSpace(string(last)))
if err != nil {
log.Printf("plan: %v", err)
break
}
j, ok := next(jobs, ran)
if !ok {
break
}
ran[j.Context+" "+j.SHA] = true
log.Printf("start %s", j)
state, desc := r.runJob(j)
log.Printf("done %s: %s, %s", j, state, desc)
Expand Down
22 changes: 19 additions & 3 deletions go/test/site/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
)
Expand Down Expand Up @@ -63,9 +64,10 @@ func short(sha string) string {

// pull is the part of a pull request the runner needs.
type pull struct {
Number int `json:"number"`
Draft bool `json:"draft"`
Head struct {
Number int `json:"number"`
Draft bool `json:"draft"`
UpdatedAt string `json:"updated_at"` // RFC 3339, so it sorts as text
Head struct {
SHA string `json:"sha"`
Repo struct {
FullName string `json:"full_name"`
Expand Down Expand Up @@ -111,6 +113,9 @@ func plan(gh github, cfg config, now time.Time, lastRandom string) ([]job, error
if err != nil {
return nil, err
}
// Most recently updated first: that is the work in review, and a pull
// request untouched for weeks can wait.
sort.SliceStable(pulls, func(i, j int) bool { return pulls[i].UpdatedAt > pulls[j].UpdatedAt })
for _, p := range pulls {
if p.Draft && !cfg.Drafts {
continue
Expand Down Expand Up @@ -142,6 +147,17 @@ func plan(gh github, cfg config, now time.Time, lastRandom string) ([]job, error
return jobs, nil
}

// next is the first job not yet run in this tick. A job whose status failed
// to post is planned again; this keeps it from running twice.
func next(jobs []job, ran map[string]bool) (job, bool) {
for _, j := range jobs {
if !ran[j.Context+" "+j.SHA] {
return j, true
}
}
return job{}, false
}

func touchesWatched(files []string) bool {
for _, f := range files {
for _, p := range watchedPaths {
Expand Down
32 changes: 32 additions & 0 deletions go/test/site/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,35 @@ func TestDescribe(t *testing.T) {
t.Fatalf("%d characters", len(desc))
}
}

func TestPlanTakesRecentPullFirstAndNextSkipsWhatRan(t *testing.T) {
watched := []string{"go/internal/control/dispatch.go"}
gh := fakeGitHub{
master: "m1",
pulls: []pull{pr(3, "c3", "srcfl/ftw", false), pr(9, "c9", "srcfl/ftw", false)},
files: map[int][]string{9: watched, 3: watched},
contexts: map[string]map[string]bool{"m1": {ctxFull: true}},
}
gh.pulls[0].UpdatedAt, gh.pulls[1].UpdatedAt = "2026-10-01T08:00:00Z", "2026-10-27T21:00:00Z"
jobs, err := plan(gh, config{Repo: "srcfl/ftw", Drafts: true, RandomAt: -1}, time.Now(), "")
if err != nil {
t.Fatal(err)
}
if len(jobs) != 2 || jobs[0].PR != 9 || jobs[1].PR != 3 {
t.Fatalf("jobs %v, want #9 (updated last) then #3", jobs)
}
ran := map[string]bool{}
j, ok := next(jobs, ran)
if !ok || j.PR != 9 {
t.Fatalf("first: %v %v", j, ok)
}
// #9's status failed to post, so the next plan lists it again.
ran[j.Context+" "+j.SHA] = true
if j, ok = next(jobs, ran); !ok || j.PR != 3 {
t.Fatalf("second: %v %v", j, ok)
}
ran[j.Context+" "+j.SHA] = true
if _, ok = next(jobs, ran); ok {
t.Fatal("a third job after both ran")
}
}
Loading