| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | |
| 6 | "the-valley/flow/internal/event" |
| 7 | "the-valley/flow/internal/integration" |
| 8 | "the-valley/flow/internal/pipeline" |
| 9 | "the-valley/flow/internal/run" |
| 10 | "the-valley/flow/internal/valley" |
| 11 | ) |
| 12 | |
| 13 | // reportChangeTransitions emits the change:* events for the valley changes |
| 14 | // of the runs and tracked changes an integration check followed: for each |
| 15 | // change the valley's listing holds in a repo it could read, the |
| 16 | // transitions its record shows that the ledger didn't hold |
| 17 | // (pipeline.ChangeLedger.Observe). It reads the change's own record, not a |
| 18 | // run's version, so a change restacked under its run reports its landing |
| 19 | // once, as the change's. |
| 20 | // |
| 21 | // A version is this session's own when one of its runs pushed its head or |
| 22 | // records it (run.State.Valley), as finalize and klaus ready do. The runs |
| 23 | // are read after the listing, and finalize saves a run's pushed head before |
| 24 | // it marks the head ready, so no version of the session's is taken for |
| 25 | // another's. If they can't be read, nothing is reported until the next |
| 26 | // check. |
| 27 | func reportChangeTransitions(store *run.HomeDirStore, ledger *pipeline.ChangeLedger, followed []*run.State, listing *valley.Changes) { |
| 28 | keys := followedChanges(followed) |
| 29 | if len(keys) == 0 { |
| 30 | return |
| 31 | } |
| 32 | mine, err := store.List() |
| 33 | if err != nil { |
| 34 | slog.Warn("reading the session's runs to report change transitions", "err", err) |
| 35 | return |
| 36 | } |
| 37 | listed := integration.ListedChanges(listing) |
| 38 | log := event.NewLog(store.BaseDir()) |
| 39 | for _, k := range keys { |
| 40 | ch, ok := listed[k] |
| 41 | if !ok { |
| 42 | continue |
| 43 | } |
| 44 | ts, err := ledger.Observe(k.Repo, ch, func(v valley.Version) bool { return recordsVersion(mine, k, v) }) |
| 45 | if err != nil { |
| 46 | slog.Warn("recording a change's transitions", "repo", k.Repo, "change_id", k.ChangeID, "err", err) |
| 47 | } |
| 48 | id := ch.Handle |
| 49 | if id == "" { |
| 50 | id = ch.ChangeID |
| 51 | } |
| 52 | for _, t := range ts { |
| 53 | if err := log.Emit(event.New(id, t.Type, changeTransitionData(k.Repo, ch, t))); err != nil { |
| 54 | slog.Warn("emitting a change event", "type", t.Type, "change_id", k.ChangeID, "err", err) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // followedChanges names the changes of states, once each, in order. |
| 61 | func followedChanges(states []*run.State) []integration.ChangeKey { |
| 62 | seen := make(map[integration.ChangeKey]bool) |
| 63 | var keys []integration.ChangeKey |
| 64 | for _, s := range states { |
| 65 | k := integration.ChangeKey{Repo: integration.RepoName(s), ChangeID: s.ChangeID} |
| 66 | if k.Repo == "" || k.ChangeID == "" || seen[k] { |
| 67 | continue |
| 68 | } |
| 69 | seen[k] = true |
| 70 | keys = append(keys, k) |
| 71 | } |
| 72 | return keys |
| 73 | } |
| 74 | |
| 75 | // recordsVersion reports whether a run among states pushed version v of |
| 76 | // change k, or records it as the version its pushed head became. A run |
| 77 | // whose recorded valley repo is another pushed another repo's change; one |
| 78 | // finalize hasn't recorded a version for yet counts by its pushed head. A |
| 79 | // tracked change's record names the version klaus track found, which it |
| 80 | // didn't record. |
| 81 | func recordsVersion(states []*run.State, k integration.ChangeKey, v valley.Version) bool { |
| 82 | for _, s := range states { |
| 83 | if s.ChangeID != k.ChangeID || s.Type == "track" || s.Valley != nil && s.Valley.Repo != "" && s.Valley.Repo != k.Repo { |
| 84 | continue |
| 85 | } |
| 86 | if s.PushedSHA != nil && *s.PushedSHA == v.Head || |
| 87 | s.Valley != nil && s.Valley.Repo == k.Repo && s.Valley.Version == v.N { |
| 88 | return true |
| 89 | } |
| 90 | } |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | // changeTransitionData is what a change:* event carries: the change, and |
| 95 | // the version the transition concerns, with repo_key and key as run events |
| 96 | // name a valley change. |
| 97 | func changeTransitionData(repo string, ch valley.Change, t pipeline.ChangeTransition) map[string]interface{} { |
| 98 | d := map[string]interface{}{ |
| 99 | "repo": repo, |
| 100 | "handle": ch.Handle, |
| 101 | "change_id": ch.ChangeID, |
| 102 | "version": t.Version.N, |
| 103 | "head": t.Version.Head, |
| 104 | "title": t.Version.Title, |
| 105 | "repo_key": "valley:" + repo, |
| 106 | "key": changeKey("valley:"+repo, "", ch.ChangeID), |
| 107 | } |
| 108 | switch t.Type { |
| 109 | case event.ChangeIntegrated: |
| 110 | d["landed_commit"] = t.Commit |
| 111 | case event.ChangeNewVersion: |
| 112 | d["uploader"] = t.Version.Uploader |
| 113 | case event.ChangeChecksFailed, event.ChangeChecksPassed: |
| 114 | failed := t.Failed |
| 115 | if failed == nil { |
| 116 | failed = []string{} |
| 117 | } |
| 118 | d["failed"] = failed |
| 119 | } |
| 120 | return d |
| 121 | } |