| 1 | package main |
| 2 | |
| 3 | // The refs transport: a reconciliation loop over the integration requests |
| 4 | // durable in the served repository. |
| 5 | // |
| 6 | // It polls, deliberately. The bus is unauthenticated (bd-d853d9c), which |
| 7 | // gates automated consumers, and the requests are durable in git with the |
| 8 | // bus as a projection over them (architecture.md). So the controller |
| 9 | // level-triggers over the request refs themselves and converges: each pass |
| 10 | // recomputes the verdict for every request against the current tip, acts |
| 11 | // only where the answer has changed since the last pass, and needs no |
| 12 | // subscription at all. It publishes outcomes to the bus exactly as the |
| 13 | // post-receive hook publishes ref updates. Consuming events arrives in a |
| 14 | // later phase. |
| 15 | // |
| 16 | // # The request ref |
| 17 | // |
| 18 | // A request is a ref: |
| 19 | // |
| 20 | // refs/the-valley/integration-requests/<target>/<change> -> commit |
| 21 | // |
| 22 | // The target segment names the protected branch, so the change's target |
| 23 | // stream is refs/heads/<target>; the last segment names the change. The |
| 24 | // commit is the change's head, its tree is what the attestations are over, |
| 25 | // and the delta is the merge base with the target's tip to that head. |
| 26 | // Nothing else is encoded: a change is a diff targeting a stream |
| 27 | // (ida-93e4f91), and every part of that is already in git. A request |
| 28 | // whose last segment is c-<change ID> is the request of the change with a |
| 29 | // record under that ID, and the records decide whether it may land at all |
| 30 | // (records.go). |
| 31 | |
| 32 | import ( |
| 33 | "the-valley/change" |
| 34 | "the-valley/integrator/verdict" |
| 35 | |
| 36 | "fmt" |
| 37 | "sort" |
| 38 | "strings" |
| 39 | "time" |
| 40 | ) |
| 41 | |
| 42 | const ( |
| 43 | requestPrefix = "refs/the-valley/integration-requests" |
| 44 | outcomePrefix = "refs/the-valley/integration-outcomes" |
| 45 | ) |
| 46 | |
| 47 | // request is one integration request as the refs transport found it. |
| 48 | type request struct { |
| 49 | ref string |
| 50 | change string |
| 51 | target string |
| 52 | head string |
| 53 | } |
| 54 | |
| 55 | // requests lists the pending requests, in ref order so a pass is |
| 56 | // deterministic, along with the refs under the namespace that are not |
| 57 | // requests at all. |
| 58 | // |
| 59 | // Any holder of the approve grant can write this namespace, and anyone at all |
| 60 | // where a repository is served without the pre-receive hook, so a name that |
| 61 | // does not parse is an ordinary thing to find there and not a reason to stop: one |
| 62 | // unreadable ref must not hold up every readable one. It is reported and |
| 63 | // skipped. |
| 64 | func (in *integrator) requests() ([]request, []string, error) { |
| 65 | out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", requestPrefix) |
| 66 | if err != nil { |
| 67 | return nil, nil, err |
| 68 | } |
| 69 | var found []request |
| 70 | var malformed []string |
| 71 | for _, line := range strings.Split(strings.TrimSpace(out), "\n") { |
| 72 | if line == "" { |
| 73 | continue |
| 74 | } |
| 75 | ref, head, ok := strings.Cut(line, " ") |
| 76 | if !ok { |
| 77 | continue |
| 78 | } |
| 79 | rest := strings.TrimPrefix(ref, requestPrefix+"/") |
| 80 | target, change, ok := strings.Cut(rest, "/") |
| 81 | if !ok || strings.Contains(change, "/") || target == "" || change == "" { |
| 82 | malformed = append(malformed, ref) |
| 83 | continue |
| 84 | } |
| 85 | found = append(found, request{ref: ref, change: change, target: "refs/heads/" + target, head: head}) |
| 86 | } |
| 87 | sort.Slice(found, func(i, j int) bool { return found[i].ref < found[j].ref }) |
| 88 | sort.Strings(malformed) |
| 89 | return found, malformed, nil |
| 90 | } |
| 91 | |
| 92 | // reconcile is one pass: judge every request against the current tip of its |
| 93 | // target, and act where the answer has changed. |
| 94 | // |
| 95 | // The change records are read once, before any request is judged. A pass |
| 96 | // that cannot read them judges nothing: which heads are versions of |
| 97 | // changes that are not ready is what keeps those heads from landing. The |
| 98 | // requests a withdrawn change voids are deleted before the rest are |
| 99 | // judged. |
| 100 | func (in *integrator) reconcile() error { |
| 101 | requests, malformed, err := in.requests() |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | for _, ref := range malformed { |
| 106 | fmt.Fprintf(in.out, "%s: not a request ref; one is %s/<target>/<change>. Skipped.\n", ref, requestPrefix) |
| 107 | } |
| 108 | if len(requests) == 0 { |
| 109 | return nil |
| 110 | } |
| 111 | idx, err := in.readRecords() |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | for _, u := range idx.unreadable { |
| 116 | fmt.Fprintf(in.out, "WARNING: %s is a change record that does not read: %s. Requests are judged as though it did not exist, so a head it names is not held back by it. The hook refuses such a record, so the repository's storage is corrupt; restore the record or delete it.\n", u.ref, u.why) |
| 117 | } |
| 118 | requests = in.voidWithdrawn(requests, idx) |
| 119 | for _, r := range requests { |
| 120 | if err := in.handle(r, idx); err != nil { |
| 121 | // One bad request must not stop the pass: the integrator is a |
| 122 | // controller, and a request it cannot judge is one request's |
| 123 | // problem. |
| 124 | fmt.Fprintf(in.out, "%s: %v\n", r.change, err) |
| 125 | } |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // handle judges one request and acts on the verdict. |
| 131 | func (in *integrator) handle(r request, idx *recordIndex) error { |
| 132 | tip, err := gitLine(in.repo, "rev-parse", "--verify", "--quiet", r.target) |
| 133 | if err != nil { |
| 134 | return fmt.Errorf("no target %s to integrate onto", r.target) |
| 135 | } |
| 136 | // Level-triggering: the verdict is a function of the request's head, |
| 137 | // the target's tip, and the evidence stored for the change, so an |
| 138 | // unchanged answer is one where none of the three has moved. |
| 139 | // Re-announcing an unchanged answer would be a retry storm; missing a |
| 140 | // moved one would strand a contributor who pushed the attestation a |
| 141 | // previous pass said was absent. |
| 142 | evidence, err := in.evidenceState() |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | memo := fmt.Sprintf("%s %s %s", r.head, tip, evidence) |
| 147 | last, err := in.readMemo(r) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | if in.answered(last, memo) { |
| 152 | return nil |
| 153 | } |
| 154 | // From here on the request has been taken up, so whatever happens is |
| 155 | // this pass's answer to it — including a failure. Recording that is |
| 156 | // what keeps the next tick from asking the identical question. |
| 157 | outcome, err := in.refuse(r, idx, tip, last, memo) |
| 158 | if err == nil && outcome == "" { |
| 159 | outcome, err = in.act(r, tip) |
| 160 | } |
| 161 | if err != nil { |
| 162 | if memoErr := in.writeMemo(r, memo+" "+failedMemo+" "+in.now().Format(time.RFC3339)); memoErr != nil { |
| 163 | return fmt.Errorf("%v (and the failure could not be recorded: %v)", err, memoErr) |
| 164 | } |
| 165 | return err |
| 166 | } |
| 167 | if last == memo+" "+string(outcome) { |
| 168 | return nil |
| 169 | } |
| 170 | return in.writeMemo(r, memo+" "+string(outcome)) |
| 171 | } |
| 172 | |
| 173 | // refuse applies the rules that refuse a request before it is judged |
| 174 | // (records.go), and reports a refusal. It reports one only when it is new: |
| 175 | // a refusal is decided again on every pass, and saying the same one every |
| 176 | // tick would bury it. |
| 177 | func (in *integrator) refuse(r request, idx *recordIndex, tip, last, memo string) (verdict.Outcome, error) { |
| 178 | outcome, why, err := in.recordRefusal(r, idx, tip) |
| 179 | if err == nil && outcome == "" { |
| 180 | outcome, why, err = in.mergeRefusal(r, tip) |
| 181 | } |
| 182 | if err != nil { |
| 183 | return "", err |
| 184 | } |
| 185 | if outcome != "" && last != memo+" "+string(outcome) { |
| 186 | in.reportRefusal(r, tip, outcome, why) |
| 187 | } |
| 188 | // A record that does not read may name a commit this request brings, |
| 189 | // and whether it holds that commit back cannot be told. That is said |
| 190 | // with the request, whatever is decided about it. |
| 191 | if outcome == "" || last != memo+" "+string(outcome) { |
| 192 | for _, u := range in.uncheckable(r, idx, tip) { |
| 193 | fmt.Fprintf(in.out, "WARNING: %s may name a commit %s brings, and does not read, so whether it holds that commit back cannot be checked\n", u.ref, r.change) |
| 194 | } |
| 195 | } |
| 196 | return outcome, nil |
| 197 | } |
| 198 | |
| 199 | // reportRefusal prints a refusal the way report prints a verdict. |
| 200 | func (in *integrator) reportRefusal(r request, tip string, outcome verdict.Outcome, why string) { |
| 201 | fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), outcome) |
| 202 | fmt.Fprintf(in.out, " %s\n", why) |
| 203 | fmt.Fprintf(in.out, " no event: a refusal is not staleness, and the request stays where it is\n") |
| 204 | } |
| 205 | |
| 206 | // failedMemo marks a memo written for a pass that could not act on its own |
| 207 | // verdict, and retryAfter is how long that answer stands. |
| 208 | // |
| 209 | // A failure here is a function of the same three things the verdict is, so |
| 210 | // the identical attempt fails identically until one of them moves — and |
| 211 | // moving any of them already re-judges the request, whatever the memo says. |
| 212 | // The window exists for the other kind of failure: a bus, a disk, a lock, |
| 213 | // something outside the three that will be different later. Minutes rather |
| 214 | // than ticks, because a controller that reports the same refusal every |
| 215 | // fifteen seconds has buried the report it was making. |
| 216 | const ( |
| 217 | failedMemo = "failed" |
| 218 | retryAfter = 10 * time.Minute |
| 219 | ) |
| 220 | |
| 221 | // answered says whether this pass already has this request's answer, given |
| 222 | // the last memo written for it. An outcome stands until the request, the |
| 223 | // tip, or the evidence moves; a recorded failure stands the same way, and |
| 224 | // additionally expires, so a transient one is retried without being |
| 225 | // retried at tick rate. A refusal never stands: what refused it can change |
| 226 | // while the three stay where they are, as when a draft is readied again, |
| 227 | // so it is decided again on every pass. Nor does a voiding: a request |
| 228 | // filed again at the same head is a fresh approval. |
| 229 | func (in *integrator) answered(last, memo string) bool { |
| 230 | rest, ok := strings.CutPrefix(last, memo+" ") |
| 231 | if !ok { |
| 232 | return false |
| 233 | } |
| 234 | switch verdict.Outcome(rest) { |
| 235 | case refusedNotReady, refusedMerge, voidedWithdrawn: |
| 236 | return false |
| 237 | } |
| 238 | when, failed := strings.CutPrefix(rest, failedMemo+" ") |
| 239 | if !failed { |
| 240 | return true |
| 241 | } |
| 242 | at, err := time.Parse(time.RFC3339, when) |
| 243 | if err != nil { |
| 244 | return false |
| 245 | } |
| 246 | return in.now().Sub(at) < retryAfter |
| 247 | } |
| 248 | |
| 249 | // act judges one request and does what the verdict says, returning the |
| 250 | // outcome so the memo can record it. |
| 251 | func (in *integrator) act(r request, tip string) (verdict.Outcome, error) { |
| 252 | ch, snap, landed, err := in.materialize(r, tip) |
| 253 | if err != nil { |
| 254 | return "", err |
| 255 | } |
| 256 | v := verdict.Decide(ch, snap) |
| 257 | in.report(r, tip, v) |
| 258 | if landed.composition != "" { |
| 259 | fmt.Fprintf(in.out, " policy %s\n", landed.composition) |
| 260 | } |
| 261 | |
| 262 | switch v.Outcome { |
| 263 | case verdict.Land: |
| 264 | outcome, err := in.land(ch, v, r, tip, landed) |
| 265 | if outcome != "" || err != nil { |
| 266 | if outcome == "" { |
| 267 | outcome = v.Outcome |
| 268 | } |
| 269 | return outcome, err |
| 270 | } |
| 271 | case verdict.Stale: |
| 272 | in.publishStale(ch, v, tip) |
| 273 | case verdict.Reject: |
| 274 | // There is no rejection event. The failure model is unified on |
| 275 | // staleness (architecture.md), and a change whose evidence is |
| 276 | // forged is not stale — so rather than stretch either event to |
| 277 | // cover it, the vocabulary stays as it is and the refusal is the |
| 278 | // integrator's own output. The request stays put; nothing about |
| 279 | // it will change on its own. |
| 280 | fmt.Fprintf(in.out, " no event: a forged attestation is not staleness, and the vocabulary has no other word\n") |
| 281 | } |
| 282 | return v.Outcome, nil |
| 283 | } |
| 284 | |
| 285 | // landing is the commit a verdict would move the target to, and what the |
| 286 | // verdict was computed under. |
| 287 | type landing struct { |
| 288 | commit string // the commit that would be the target's new tip |
| 289 | digest string // the valley-tree-v1 digest of the resulting tree |
| 290 | policy string // a digest of the composed policy the verdict used |
| 291 | |
| 292 | // composition is what the pass has to say about how the policy layers |
| 293 | // resolved, or empty when both were where they are configured to be. |
| 294 | composition string |
| 295 | |
| 296 | // notes are the contributor's attestations as stored, by check name. |
| 297 | // The verdict does not read them; what lands countersigns them. |
| 298 | notes map[string][]byte |
| 299 | } |
| 300 | |
| 301 | // materialize gathers everything the verdict is a function of: the applied |
| 302 | // delta, the required checks, the recomputed closures, and what intervening |
| 303 | // landings touched. |
| 304 | func (in *integrator) materialize(r request, tip string) (verdict.Change, verdict.Snapshot, landing, error) { |
| 305 | var land landing |
| 306 | wt, err := addWorktree(in.repo, tip) |
| 307 | if err != nil { |
| 308 | return verdict.Change{}, verdict.Snapshot{}, land, err |
| 309 | } |
| 310 | defer wt.remove() |
| 311 | |
| 312 | base, err := gitLine(in.repo, "merge-base", tip, r.head) |
| 313 | if err != nil { |
| 314 | return verdict.Change{}, verdict.Snapshot{}, land, fmt.Errorf("%s and %s share no history", verdict.Short(r.head), r.target) |
| 315 | } |
| 316 | attested, err := in.treeDigest(wt, r.head) |
| 317 | if err != nil { |
| 318 | return verdict.Change{}, verdict.Snapshot{}, land, err |
| 319 | } |
| 320 | ch := verdict.Change{ID: r.change, Target: r.target, Base: base, Head: r.head, Attested: attested} |
| 321 | if ch.Evidence, land.notes, err = in.collectEvidence(wt, r.head, attested); err != nil { |
| 322 | return ch, verdict.Snapshot{}, land, err |
| 323 | } |
| 324 | |
| 325 | // The policy is read from the integrator's own side and never from the |
| 326 | // tree being gated: the project layer from this checkout of the target |
| 327 | // tip, the floor from the instance repository's integrated tip. Both |
| 328 | // are resolved once and held for the whole pass. |
| 329 | pol, closePolicy, err := in.policy.open(wt) |
| 330 | if err != nil { |
| 331 | return ch, verdict.Snapshot{}, land, err |
| 332 | } |
| 333 | defer closePolicy() |
| 334 | land.composition = pol.note |
| 335 | cat, policyDigest, err := pol.catalogue() |
| 336 | if err != nil { |
| 337 | return ch, verdict.Snapshot{}, land, err |
| 338 | } |
| 339 | land.policy = policyDigest |
| 340 | derived, err := pol.derive(wt, base, r.head) |
| 341 | if err != nil { |
| 342 | return ch, verdict.Snapshot{}, land, err |
| 343 | } |
| 344 | required, err := pol.required(derived, cat) |
| 345 | if err != nil { |
| 346 | return ch, verdict.Snapshot{}, land, err |
| 347 | } |
| 348 | |
| 349 | snap := verdict.Snapshot{Required: required, Touched: map[string]bool{}, Now: in.now()} |
| 350 | |
| 351 | // Apply the delta. A base that is already the tip is a fast-forward: |
| 352 | // the head lands as it stands and its tree is the tree that was |
| 353 | // attested. |
| 354 | if base == tip { |
| 355 | land.commit = r.head |
| 356 | land.digest = attested |
| 357 | snap.Apply = verdict.Applied{Clean: true, Tree: attested} |
| 358 | } else { |
| 359 | tree, conflict, err := mergeDelta(in.repo, base, tip, r.head) |
| 360 | if err != nil { |
| 361 | return ch, snap, land, err |
| 362 | } |
| 363 | if conflict != "" { |
| 364 | snap.Apply = verdict.Applied{Clean: false, Conflict: conflict} |
| 365 | return ch, snap, land, nil |
| 366 | } |
| 367 | if land.commit, err = in.commitTree(tree, tip, r); err != nil { |
| 368 | return ch, snap, land, err |
| 369 | } |
| 370 | if land.digest, err = in.treeDigest(wt, tree); err != nil { |
| 371 | return ch, snap, land, err |
| 372 | } |
| 373 | snap.Apply = verdict.Applied{Clean: true, Tree: land.digest} |
| 374 | |
| 375 | // What intervening landings touched, in the policy's own terms: |
| 376 | // the classes the deriver matches over base..tip. Asking the |
| 377 | // deriver rather than matching globs here keeps one implementation |
| 378 | // of what a class covers. |
| 379 | intervening, err := pol.derive(wt, base, tip) |
| 380 | if err != nil { |
| 381 | return ch, snap, land, err |
| 382 | } |
| 383 | snap.Touched = intervening.classes |
| 384 | } |
| 385 | |
| 386 | // The closures only have to be recomputed when the tree moved: an |
| 387 | // unmoved tree presents the identical computation by construction. |
| 388 | if land.digest != attested { |
| 389 | snap.Closures, snap.ClosureErrors = in.closures(wt, land.commit, required) |
| 390 | } |
| 391 | return ch, snap, land, nil |
| 392 | } |
| 393 | |
| 394 | // committerEmail is the address on the commits the integrator makes. git |
| 395 | // wants an address and there is none to give: the integrator is a service, |
| 396 | // not a correspondent. .invalid never resolves, which says so in the address |
| 397 | // itself. Which integrator committed is carried by the name beside it. |
| 398 | const committerEmail = "integrator@the-valley.invalid" |
| 399 | |
| 400 | // commitTree turns the merged tree into the commit that would become the |
| 401 | // target's new tip. The tip is its only parent, so the ref write is a |
| 402 | // fast-forward; the change's authorship is carried over from its head. |
| 403 | // |
| 404 | // Both identities are stated here rather than left to git. The author is the |
| 405 | // asker's, because re-parenting a delta is not authoring it. The committer is |
| 406 | // the integrator, under the name it signs its transfer statement with: the |
| 407 | // commit and the statement about it then name the same actor. Leaving either |
| 408 | // to git would ask git to guess an identity from the environment, and under |
| 409 | // the unit there is nothing to guess from — which is a landing that fails for |
| 410 | // a reason that has nothing to do with the change. |
| 411 | func (in *integrator) commitTree(tree, tip string, r request) (string, error) { |
| 412 | message, err := git(in.repo, "log", "-1", "--format=%B", r.head) |
| 413 | if err != nil { |
| 414 | return "", err |
| 415 | } |
| 416 | author, err := git(in.repo, "log", "-1", "--format=%an%n%ae%n%aI", r.head) |
| 417 | if err != nil { |
| 418 | return "", err |
| 419 | } |
| 420 | fields := strings.Split(strings.TrimRight(author, "\n"), "\n") |
| 421 | if len(fields) != 3 { |
| 422 | return "", fmt.Errorf("cannot read the authorship of %s", verdict.Short(r.head)) |
| 423 | } |
| 424 | cmd := []string{"commit-tree", tree, "-p", tip, "-m", strings.TrimRight(message, "\n")} |
| 425 | // The committer date is the author's, so that the commit id is a function |
| 426 | // of what the pass decided from — the tree, the tip, the message and the |
| 427 | // authorship — and of nothing else. A pass that dies between here and the |
| 428 | // ref write recomputes the identical commit next time round instead of |
| 429 | // leaving another unreferenced one behind. Nothing reads this date: when |
| 430 | // the landing happened is recorded, signed, in the transfer statement. |
| 431 | out, err := in.gitEnv(map[string]string{ |
| 432 | "GIT_AUTHOR_NAME": fields[0], |
| 433 | "GIT_AUTHOR_EMAIL": fields[1], |
| 434 | "GIT_AUTHOR_DATE": fields[2], |
| 435 | "GIT_COMMITTER_NAME": in.signerName, |
| 436 | "GIT_COMMITTER_EMAIL": committerEmail, |
| 437 | "GIT_COMMITTER_DATE": fields[2], |
| 438 | }, cmd...) |
| 439 | return strings.TrimSpace(out), err |
| 440 | } |
| 441 | |
| 442 | // land moves the protected ref, stores the evidence, and says so on the |
| 443 | // bus. The ref write is a compare-and-swap against the tip the verdict was |
| 444 | // computed over, so a request that lost a race to another lands nothing and |
| 445 | // is judged again next pass against the tip that won. |
| 446 | // |
| 447 | // The order is the commit point's. Everything that can fail while the |
| 448 | // integrator is still deciding happens first, and the ref write is the |
| 449 | // commit. Composing the transfer statement — vetting it, writing it down, |
| 450 | // signing it — is part of deciding: a claim the integrator cannot write |
| 451 | // down is a claim it has not made, and a stream moved on a decision that |
| 452 | // cannot be stated is a landing nothing records. A failure before the |
| 453 | // commit point leaves the stream where it was and the request where it was, |
| 454 | // which is a transaction abandoned before it committed and costs one pass |
| 455 | // to redo. |
| 456 | // |
| 457 | // Past the commit point the landing is a fact in the stream, and what is |
| 458 | // left is recording it: handing the ref move to the host to publish, |
| 459 | // storing the evidence, consuming the request, and saying so on the bus. |
| 460 | // None of those may be skipped because an earlier one failed, and |
| 461 | // consuming the request in particular may not be reachable only through |
| 462 | // something that can fail — a request left pending for a change |
| 463 | // already in the stream is re-judged against an empty delta on every pass, |
| 464 | // forever. So each step runs, each failure is reported, and the landing is |
| 465 | // reported as having happened, because it did. |
| 466 | func (in *integrator) land(ch verdict.Change, v verdict.Verdict, r request, tip string, l landing) (verdict.Outcome, error) { |
| 467 | transfer, err := in.composeTransfer(ch, v, l.digest, l.commit, tip, l.policy) |
| 468 | if err != nil { |
| 469 | return "", err |
| 470 | } |
| 471 | evidence, err := in.countersign(ch, v, l, transfer) |
| 472 | if err != nil { |
| 473 | return "", err |
| 474 | } |
| 475 | |
| 476 | // Judging and signing can take minutes, and a change can be unreadied |
| 477 | // or withdrawn meanwhile, or a record can come to name a commit this |
| 478 | // request brings. So every record is read again here, as the last |
| 479 | // thing before the commit point, and a refusal lands nothing. A record |
| 480 | // written between this read and the ref write is still missed: refs |
| 481 | // offer no way to make the two one step. What lands then is still the |
| 482 | // head the operator approved. |
| 483 | if outcome, why, err := in.finalRefusal(r, tip); err != nil || outcome != "" { |
| 484 | if outcome != "" { |
| 485 | in.reportRefusal(r, tip, outcome, why) |
| 486 | } |
| 487 | return outcome, err |
| 488 | } |
| 489 | |
| 490 | // The commit point. |
| 491 | if _, err := git(in.repo, "update-ref", ch.Target, l.commit, tip); err != nil { |
| 492 | return "", fmt.Errorf("%s moved under the verdict; nothing landed: %w", ch.Target, err) |
| 493 | } |
| 494 | fmt.Fprintf(in.out, " landed %s %s -> %s\n", ch.Target, verdict.Short(tip), verdict.Short(l.commit)) |
| 495 | in.queueRefUpdate(ch.Target, tip, l.commit) |
| 496 | |
| 497 | var unrecorded []string |
| 498 | refs, err := in.store(evidence) |
| 499 | if err != nil { |
| 500 | unrecorded = append(unrecorded, fmt.Sprintf("the evidence was not stored (%v)", err)) |
| 501 | } |
| 502 | for _, ref := range refs { |
| 503 | fmt.Fprintf(in.out, " evidence %s\n", ref) |
| 504 | } |
| 505 | // The request is consumed only if it still names the head that landed. |
| 506 | // A second approval can replace it while this pass works — the asker's |
| 507 | // write is leased against what it read, and nothing stops it reading |
| 508 | // the request this pass is about to land — and a delete that did not |
| 509 | // compare would take the new request with the old one. A request that |
| 510 | // moved is a new request, and it stands for the next pass. |
| 511 | if _, err := git(in.repo, "update-ref", "-d", r.ref, r.head); err != nil { |
| 512 | if now, found := gitTry(in.repo, "rev-parse", "--verify", "--quiet", r.ref); found && now != r.head { |
| 513 | fmt.Fprintf(in.out, " request %s moved to %s while %s landed; it stands for the next pass\n", |
| 514 | r.ref, verdict.Short(now), verdict.Short(r.head)) |
| 515 | } else { |
| 516 | unrecorded = append(unrecorded, fmt.Sprintf("the request ref %s was not consumed (%v)", r.ref, err)) |
| 517 | } |
| 518 | } |
| 519 | // The bus comes last. Everything above is the landing's own record; an |
| 520 | // event is a best-effort notice of it, and a bus that fails or hangs |
| 521 | // must not leave any of that record undone. |
| 522 | in.publishRefUpdated(ch.Target, tip, l.commit) |
| 523 | in.publishLanded(ch, v, tip, l.commit) |
| 524 | if len(unrecorded) > 0 { |
| 525 | return "", fmt.Errorf("%s landed as %s and the landing is not fully recorded: %s", |
| 526 | ch.ID, verdict.Short(l.commit), strings.Join(unrecorded, "; ")) |
| 527 | } |
| 528 | return "", nil |
| 529 | } |
| 530 | |
| 531 | // report prints the verdict, per check, in the order it was judged. |
| 532 | func (in *integrator) report(r request, tip string, v verdict.Verdict) { |
| 533 | fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), v.Outcome) |
| 534 | fmt.Fprintf(in.out, " %s\n", v.Reason) |
| 535 | for _, cv := range v.Checks { |
| 536 | if cv.Transferred { |
| 537 | fmt.Fprintf(in.out, " check %-24s transferred (%s)\n", cv.Name, cv.Rule) |
| 538 | continue |
| 539 | } |
| 540 | fmt.Fprintf(in.out, " check %-24s %s\n", cv.Name, cv.Reason) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // The memo is the level-trigger's memory: what this integrator last decided |
| 545 | // about a request, keyed by the request. It is durable in git like |
| 546 | // everything else the transport reads, so a restarted controller does not |
| 547 | // re-announce a judgement it has already made. |
| 548 | func (in *integrator) memoRef(r request) string { |
| 549 | return outcomePrefix + "/" + strings.TrimPrefix(r.ref, requestPrefix+"/") |
| 550 | } |
| 551 | |
| 552 | func (in *integrator) readMemo(r request) (string, error) { |
| 553 | out, ok := gitTry(in.repo, "cat-file", "blob", in.memoRef(r)) |
| 554 | if !ok { |
| 555 | return "", nil |
| 556 | } |
| 557 | return strings.TrimSpace(out), nil |
| 558 | } |
| 559 | |
| 560 | func (in *integrator) writeMemo(r request, value string) error { |
| 561 | blob, err := in.writeBlob([]byte(value + "\n")) |
| 562 | if err != nil { |
| 563 | return err |
| 564 | } |
| 565 | _, err = git(in.repo, "update-ref", in.memoRef(r), blob) |
| 566 | return err |
| 567 | } |
| 568 | |
| 569 | // evidenceState digests the attestation namespace: every ref under it and |
| 570 | // the object it points at, as change.EvidenceFingerprint reads them. |
| 571 | // Attestation refs are create-only, so this moves exactly when evidence |
| 572 | // arrives. |
| 573 | // |
| 574 | // It is deliberately the whole namespace rather than the refs for one |
| 575 | // change's subject digest, which would take a checkout to compute and would |
| 576 | // have to be recomputed before the memo could rule the work out. So an |
| 577 | // attestation for one change re-judges every pending request. That is the |
| 578 | // conservative direction — a pass that runs and changes nothing costs a |
| 579 | // pass; a verdict never revisited strands a change — and it is bounded by |
| 580 | // the number of requests pending. |
| 581 | func (in *integrator) evidenceState() (string, error) { |
| 582 | out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", attestationPrefix) |
| 583 | if err != nil { |
| 584 | return "", err |
| 585 | } |
| 586 | var lines []string |
| 587 | if out = strings.TrimSuffix(out, "\n"); out != "" { |
| 588 | lines = strings.Split(out, "\n") |
| 589 | } |
| 590 | return change.EvidenceFingerprint(lines), nil |
| 591 | } |
| 592 | |
| 593 | func (in *integrator) now() time.Time { |
| 594 | if !in.clock.IsZero() { |
| 595 | return in.clock |
| 596 | } |
| 597 | return time.Now().UTC() |
| 598 | } |