| 1 | // Package integration observes whether the work of a run that finished by |
| 2 | // pushing its branch (no PR) has landed on its repo's default branch, and, |
| 3 | // for a repo that delivers to a valley, where the run's change stands there. |
| 4 | // |
| 5 | // It is a display-time observation: nothing is persisted and no event is |
| 6 | // emitted. If an observation is wrong, the next check corrects it. |
| 7 | package integration |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "the-valley/flow/internal/run" |
| 13 | "the-valley/flow/internal/valley" |
| 14 | ) |
| 15 | |
| 16 | // Verdict is what a check observed about one pushed run. |
| 17 | type Verdict int |
| 18 | |
| 19 | const ( |
| 20 | // Unknown means the check could not complete (no clone, origin |
| 21 | // unreachable, fetch failed), or the branch is gone but the clone lacks |
| 22 | // the history to rule out that it landed. Callers show the run as |
| 23 | // awaiting. |
| 24 | Unknown Verdict = iota |
| 25 | // Awaiting means the branch is on origin and its work has not landed. |
| 26 | Awaiting |
| 27 | // Integrated means the pushed SHA, or the branch's current tip, is an |
| 28 | // ancestor of the default branch. |
| 29 | Integrated |
| 30 | // IntegratedContent means a commit on the default branch carries the |
| 31 | // same change as the branch (equal patch-ids). It is evidence, not proof. |
| 32 | IntegratedContent |
| 33 | // Gone means the branch was deleted from origin without landing, as far |
| 34 | // as a complete check of the clone's history can tell. |
| 35 | Gone |
| 36 | |
| 37 | // The verdicts below come from a valley's record of the run's version |
| 38 | // (ValleyChecker). A version the valley says landed is Integrated. |
| 39 | |
| 40 | // Ready means the version is its change's active one, ready and |
| 41 | // awaiting the operator's approval. |
| 42 | Ready |
| 43 | // Approved means an integration request is filed for the version and the |
| 44 | // integrator hasn't decided it yet, or it judged the request stale only |
| 45 | // because the host's checks are pending (see StaleReason). |
| 46 | Approved |
| 47 | // Uncertain means a request for the version was consumed or failed with |
| 48 | // no landing evidence, or the valley's record says something klaus |
| 49 | // doesn't know. The operator must look. |
| 50 | Uncertain |
| 51 | // Stale, Rejected and Failed are the integrator's decision on the |
| 52 | // version's request. |
| 53 | Stale |
| 54 | Rejected |
| 55 | Failed |
| 56 | // ChecksFailed is a stale decision on a version whose checks failed. |
| 57 | ChecksFailed |
| 58 | // Conflicting is a stale decision on a change whose current restack |
| 59 | // report says a rebase conflicted. |
| 60 | Conflicting |
| 61 | // Withdrawn means the change was withdrawn. |
| 62 | Withdrawn |
| 63 | // Superseded means another version of the change is the active one, and |
| 64 | // the change hasn't landed. |
| 65 | Superseded |
| 66 | // Draft means the change is back with its submitter (`valley unready`), |
| 67 | // as when a follow-up run continues it: approve refuses it until a head |
| 68 | // is marked ready again. |
| 69 | Draft |
| 70 | ) |
| 71 | |
| 72 | // Landed reports whether the verdict says the run's work is on the default |
| 73 | // branch. |
| 74 | func (v Verdict) Landed() bool { |
| 75 | return v == Integrated || v == IntegratedContent |
| 76 | } |
| 77 | |
| 78 | // Settled reports whether nothing is left to do for the run: its work |
| 79 | // landed, its branch is gone, or its change was withdrawn or superseded. |
| 80 | func (v Verdict) Settled() bool { |
| 81 | return v.Landed() || v == Gone || v == Withdrawn || v == Superseded |
| 82 | } |
| 83 | |
| 84 | func (v Verdict) String() string { |
| 85 | switch v { |
| 86 | case Awaiting: |
| 87 | return "awaiting" |
| 88 | case Integrated: |
| 89 | return "integrated" |
| 90 | case IntegratedContent: |
| 91 | return "integrated-content" |
| 92 | case Gone: |
| 93 | return "gone" |
| 94 | case Ready: |
| 95 | return "ready" |
| 96 | case Approved: |
| 97 | return "approved" |
| 98 | case Uncertain: |
| 99 | return "uncertain" |
| 100 | case Stale: |
| 101 | return "stale" |
| 102 | case ChecksFailed: |
| 103 | return "checks-failed" |
| 104 | case Conflicting: |
| 105 | return "conflicting" |
| 106 | case Rejected: |
| 107 | return "rejected" |
| 108 | case Failed: |
| 109 | return "failed" |
| 110 | case Withdrawn: |
| 111 | return "withdrawn" |
| 112 | case Superseded: |
| 113 | return "superseded" |
| 114 | case Draft: |
| 115 | return "draft" |
| 116 | default: |
| 117 | return "unknown" |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Result is what a check observed about one pushed run. |
| 122 | type Result struct { |
| 123 | Verdict Verdict |
| 124 | // Change is the valley's record of the run's change when a valley placed |
| 125 | // the run; nil when git judged it. |
| 126 | Change *Change |
| 127 | // Outdated marks a valley's view from an earlier check, kept by a caller |
| 128 | // because the latest check couldn't place the run. Checkers never set it. |
| 129 | Outdated bool |
| 130 | } |
| 131 | |
| 132 | // Change is how a valley knows a run: the version of a change that the |
| 133 | // run's pushed head is. |
| 134 | type Change struct { |
| 135 | Repo string // the valley's name for the repo |
| 136 | ChangeID string |
| 137 | Handle string |
| 138 | Title string |
| 139 | Version int // the run's version |
| 140 | Head string // the version's head, which is the run's pushed SHA |
| 141 | Branch string // the branch the version's head was pushed to, when known |
| 142 | // Active is set when the run's version is its change's active one. Every |
| 143 | // version of a change that landed reads Integrated, whichever is active. |
| 144 | Active bool |
| 145 | // LandedAs and LandedCommit are the version the change landed as and the |
| 146 | // commit it landed as (its landed_commit, else its head), when the valley |
| 147 | // says it landed (LandedAs). |
| 148 | LandedAs int |
| 149 | LandedCommit string |
| 150 | // Knots are the IDs of the knots in the repo's plan graph that link the |
| 151 | // change, as the valley lists them. |
| 152 | Knots []string |
| 153 | // Checks is the host's check worker's view of the version's required |
| 154 | // checks, which the valley lists on a ready change's active version; |
| 155 | // nil when unknown. |
| 156 | Checks *valley.Checks |
| 157 | } |
| 158 | |
| 159 | // ChangeKey identifies a change across runs and in a valley's listing: the |
| 160 | // valley's name for its repo (RepoName) and its change ID. |
| 161 | type ChangeKey struct{ Repo, ChangeID string } |
| 162 | |
| 163 | // Report is what a check observed. |
| 164 | type Report struct { |
| 165 | // Runs holds a result per pushed run, by run ID. Runs a check can't |
| 166 | // judge are Unknown or missing; callers treat both as awaiting. |
| 167 | Runs map[string]Result |
| 168 | // Changes holds a valley's view of every change it lists in a repo it |
| 169 | // could read and a state klaus knows: the change's active version, with |
| 170 | // Active set, whichever machine marked it ready. It decides where a |
| 171 | // change stands; a run's own result says only where its version does. |
| 172 | // nil when no valley was asked, or it couldn't be. |
| 173 | Changes map[ChangeKey]Result |
| 174 | // Listing is the valley's listing the check read, as it read it; nil |
| 175 | // when no valley was asked, or it couldn't be. |
| 176 | Listing *valley.Changes |
| 177 | // Asked is set when the check asked a valley, whether or not it could |
| 178 | // be asked. |
| 179 | Asked bool |
| 180 | // ValleyErr is why the valley couldn't be asked, in one line; "" when |
| 181 | // it was asked and answered, or not asked. The runs were judged by git. |
| 182 | ValleyErr string |
| 183 | } |
| 184 | |
| 185 | // ValleyNote is the one-line note for a failed valley check, "" when err is |
| 186 | // empty. |
| 187 | func ValleyNote(err string) string { |
| 188 | if err == "" { |
| 189 | return "" |
| 190 | } |
| 191 | return "valley unreachable: " + err + "; showing git's view" |
| 192 | } |
| 193 | |
| 194 | // Checker observes integration for a set of pushed runs. |
| 195 | // |
| 196 | // GitChecker judges runs with git alone. ValleyChecker asks a valley about |
| 197 | // the runs it serves and defers to a GitChecker for the rest. |
| 198 | type Checker interface { |
| 199 | Check(ctx context.Context, runs []*run.State) Report |
| 200 | } |
| 201 | |
| 202 | // Applies reports whether a run carries what a check needs: the branch it |
| 203 | // pushed and the SHA it pushed. |
| 204 | func Applies(s *run.State) bool { |
| 205 | return s.PushedBranch != nil && *s.PushedBranch != "" && s.PushedSHA != nil && *s.PushedSHA != "" |
| 206 | } |
| 207 | |
| 208 | // RecordedChange is the change version finalize recorded for a run when it |
| 209 | // marked the run's pushed head ready on a valley, or nil when it marked none. |
| 210 | // It stands in for the valley's view when a check couldn't ask the valley. |
| 211 | func RecordedChange(s *run.State) *Change { |
| 212 | v := s.Valley |
| 213 | if v == nil || v.Handle == "" || s.PushedSHA == nil { |
| 214 | return nil |
| 215 | } |
| 216 | c := &Change{Repo: v.Repo, ChangeID: s.ChangeID, Handle: v.Handle, Title: v.Title, Version: v.Version, Head: *s.PushedSHA} |
| 217 | if s.PushedBranch != nil { |
| 218 | c.Branch = *s.PushedBranch |
| 219 | } |
| 220 | return c |
| 221 | } |
| 222 | |
| 223 | // TracksChange reports whether s is the record `klaus track` keeps of a |
| 224 | // valley change: a track record naming the change and the valley's name for |
| 225 | // its repo. It has no push of its own; where the change stands comes from |
| 226 | // the valley's listing (Report.Changes). |
| 227 | func TracksChange(s *run.State) bool { |
| 228 | return s.Type == "track" && s.ChangeID != "" && s.Valley != nil && s.Valley.Repo != "" |
| 229 | } |
| 230 | |
| 231 | // TrackedChange is the change a tracked valley change's record names, as |
| 232 | // klaus track found its active version, or nil when s isn't one. It stands |
| 233 | // in for the valley's view until a check gets one. |
| 234 | func TrackedChange(s *run.State) *Change { |
| 235 | if !TracksChange(s) { |
| 236 | return nil |
| 237 | } |
| 238 | v := s.Valley |
| 239 | return &Change{Repo: v.Repo, ChangeID: s.ChangeID, Handle: v.Handle, Title: v.Title, |
| 240 | Version: v.Version, Head: s.HeadSHA, Branch: s.Branch, Active: true} |
| 241 | } |