| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "the-valley/flow/internal/git" |
| 8 | "the-valley/flow/internal/integration" |
| 9 | "the-valley/flow/internal/run" |
| 10 | ) |
| 11 | |
| 12 | // changeIDInput describes what a new run continues, for chooseChangeID. |
| 13 | type changeIDInput struct { |
| 14 | IsPRFix bool |
| 15 | PRURL string // the PR's URL, if known |
| 16 | PRBranch string // the PR's head branch |
| 17 | TargetRepo *string // the run's normalized target repo, as in run.State |
| 18 | ResumeFrom string // --resume-from run ID |
| 19 | Change *changeTarget // the change a --change run continues |
| 20 | Worktree string // the new run's worktree; on the PR branch for a --pr run |
| 21 | DefaultBranch string |
| 22 | } |
| 23 | |
| 24 | // changeChoice is the change ID a new run uses, and how it was found. |
| 25 | type changeChoice struct { |
| 26 | ID string |
| 27 | From string // for the launch output |
| 28 | Inherited bool // the run continues an existing change |
| 29 | Repo string // the change's repo as the run it was inherited from names it (integration.RepoName); "" when not inherited from a run |
| 30 | } |
| 31 | |
| 32 | // chooseChangeID picks the change ID for a new run. A run that continues |
| 33 | // another keeps its change. A --change run takes the change it names, and |
| 34 | // the repo the valley or its runs name it in, whatever --resume-from says. A |
| 35 | // --pr run, budget-paused replays included, takes the ID of the run that |
| 36 | // opened the PR: the earliest recorded run on the PR that has one, in any |
| 37 | // session, else the Change-Id on the PR's head commit when it is not on the |
| 38 | // default branch. A --resume-from run takes the ID of the run it resumes. A |
| 39 | // change inherited from a run keeps that run's repo too, so the new run |
| 40 | // shares its change whatever the valley or its target repo calls the repo. |
| 41 | // Anything else, or a run whose source has none (its state predates change |
| 42 | // IDs), gets a new ID, and From says which. |
| 43 | func chooseChangeID(ctx context.Context, store run.StateStore, in changeIDInput) (changeChoice, error) { |
| 44 | if c := in.Change; c != nil { |
| 45 | return changeChoice{ID: c.ID, From: "inherited from change " + c.Name, Inherited: true, Repo: c.Repo}, nil |
| 46 | } |
| 47 | var unknown string |
| 48 | if in.IsPRFix { |
| 49 | states, _ := run.ListAllSessions() |
| 50 | if s := prChangeSource(states, in.PRURL, in.PRBranch, in.TargetRepo); s != nil { |
| 51 | return changeChoice{ID: s.ChangeID, From: "inherited from run " + s.ID, Inherited: true, |
| 52 | Repo: integration.RepoName(s)}, nil |
| 53 | } |
| 54 | // The PR may have been opened on another machine. |
| 55 | if ahead, err := git.CommitsAhead(ctx, in.Worktree, "origin/"+in.DefaultBranch, "HEAD"); err == nil && ahead > 0 { |
| 56 | if id, err := git.CommitChangeID(ctx, in.Worktree, "HEAD"); err == nil && run.ValidChangeID(id) { |
| 57 | return changeChoice{ID: id, From: "inherited from the PR's head commit", Inherited: true}, nil |
| 58 | } |
| 59 | } |
| 60 | unknown = "the PR" |
| 61 | } |
| 62 | if in.ResumeFrom != "" { |
| 63 | if prior, err := store.Load(in.ResumeFrom); err == nil && run.ValidChangeID(prior.ChangeID) { |
| 64 | return changeChoice{ID: prior.ChangeID, From: "inherited from run " + prior.ID, Inherited: true, |
| 65 | Repo: integration.RepoName(prior)}, nil |
| 66 | } |
| 67 | if unknown == "" { |
| 68 | unknown = "run " + in.ResumeFrom |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | id, err := run.GenChangeID() |
| 73 | if err != nil { |
| 74 | return changeChoice{}, err |
| 75 | } |
| 76 | c := changeChoice{ID: id, From: "new"} |
| 77 | if unknown != "" { |
| 78 | c.From = "new; no change ID is recorded for " + unknown |
| 79 | } |
| 80 | return c, nil |
| 81 | } |
| 82 | |
| 83 | // prChangeSource returns the earliest-created run on the PR that has a change |
| 84 | // ID, normally the run that opened it, or nil. A run is on the PR when its PR |
| 85 | // URL is prURL, or, when either URL is unknown, its target repo is known and |
| 86 | // is targetRepo, and its branch is branch. |
| 87 | func prChangeSource(states []*run.State, prURL, branch string, targetRepo *string) *run.State { |
| 88 | var first *run.State |
| 89 | for _, s := range states { |
| 90 | if s.Type == "session" || !run.ValidChangeID(s.ChangeID) { |
| 91 | continue |
| 92 | } |
| 93 | if prURL != "" && s.PRURL != nil && *s.PRURL != "" { |
| 94 | if *s.PRURL != prURL { |
| 95 | continue |
| 96 | } |
| 97 | } else if branch == "" || s.Branch != branch || !sameTarget(s.TargetRepo, targetRepo) { |
| 98 | continue |
| 99 | } |
| 100 | if first == nil || createdBefore(s, first) { |
| 101 | first = s |
| 102 | } |
| 103 | } |
| 104 | return first |
| 105 | } |
| 106 | |
| 107 | // sameTarget reports whether two runs' target repos are known and the same. |
| 108 | func sameTarget(a, b *string) bool { |
| 109 | return a != nil && b != nil && *a != "" && *a == *b |
| 110 | } |
| 111 | |
| 112 | // createdBefore reports whether run a was created before run b. Unparseable |
| 113 | // times sort last. |
| 114 | func createdBefore(a, b *run.State) bool { |
| 115 | ta, errA := time.Parse(time.RFC3339, a.CreatedAt) |
| 116 | tb, errB := time.Parse(time.RFC3339, b.CreatedAt) |
| 117 | if errA != nil || errB != nil { |
| 118 | return errA == nil |
| 119 | } |
| 120 | return ta.Before(tb) |
| 121 | } |