| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | |
| 9 | "the-valley/flow/internal/event" |
| 10 | "the-valley/flow/internal/git" |
| 11 | "the-valley/flow/internal/integration" |
| 12 | "the-valley/flow/internal/location" |
| 13 | "the-valley/flow/internal/run" |
| 14 | "the-valley/flow/internal/tmux" |
| 15 | "the-valley/flow/internal/valley" |
| 16 | "github.com/spf13/cobra" |
| 17 | ) |
| 18 | |
| 19 | var readyCmd = &cobra.Command{ |
| 20 | Use: "ready <run-id|handle> [--head <sha>]", |
| 21 | Short: "Mark a valley change ready for approval once its review passes", |
| 22 | Long: `Marks a valley change ready for approval: typically the draft that |
| 23 | finalize leaves awaiting review when "ready_at_finalize" is false in |
| 24 | ~/.klaus/config.json or the project's .klaus/config.json. Run it once the |
| 25 | change's review passes; until then approve refuses the change, and valley flow |
| 26 | status and the dashboard show it awaiting review. |
| 27 | |
| 28 | <run-id|handle> is a run ID, which names that run's change, or the change's |
| 29 | valley handle, its change ID or a prefix of it, as the runs of this session |
| 30 | record them. A ref that names several changes is an error listing them. |
| 31 | |
| 32 | It marks the change's current head ready: its active version's when the |
| 33 | valley lists the change, else the head the newest run on it, in any session, |
| 34 | pushed. With --head <sha>, the head you reviewed (its full ID or at least 7 |
| 35 | hex digits of it), it marks exactly that head ready, and refuses, naming the |
| 36 | current head, when the change's current head is another: a push after your |
| 37 | review has not been reviewed. With --head the change's current head is the |
| 38 | tip of its branch on origin when that differs from the one above, as when a |
| 39 | follow-up's push isn't a version yet; if origin can't be asked, it refuses. That is 'valley ready --change <change-id> |
| 40 | --head <head> <branch>', run in the run's clone, within 30 seconds. A draft |
| 41 | at its active version's head is ready again at that version. valley flow ready then |
| 42 | records the version on the run of this session that pushed the head, if any, |
| 43 | emits agent:change-ready, with "via": "klaus ready" ("valley flow ready" under |
| 44 | the bridge naming generation), as finalize does when it marks a change ready, |
| 45 | and prints the version and the head it marked ready, e.g. "widget nxvp v2 |
| 46 | ready at <head> (run <run-id>)". |
| 47 | |
| 48 | It refuses while an agent in any session is running on the change, as a |
| 49 | follow-up replacing its head is, and when the run's repo doesn't deliver to a |
| 50 | valley. It takes the lock that launches continuing a change in the same clone |
| 51 | take, so a follow-up launch can't start between that check and the ready.`, |
| 52 | Args: cobra.ExactArgs(1), |
| 53 | RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | ctx := cmd.Context() |
| 55 | reviewed, _ := cmd.Flags().GetString("head") |
| 56 | if reviewed != "" && !headArg.MatchString(reviewed) { |
| 57 | return fmt.Errorf("--head takes the commit you reviewed, as its full ID or at least 7 hex digits of it, and %q is not one", reviewed) |
| 58 | } |
| 59 | store, err := sessionStore() |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | states, err := store.List() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | s, err := readyTarget(states, args[0]) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if !run.ValidChangeID(s.ChangeID) { |
| 72 | return fmt.Errorf("run %s has no change ID (its state predates change IDs)", s.ID) |
| 73 | } |
| 74 | repo, dir := integration.RepoName(s), branchDir(s) |
| 75 | if repo == "" { |
| 76 | return fmt.Errorf("can't tell which repo run %s is in", s.ID) |
| 77 | } |
| 78 | // A follow-up launch in this clone takes the same lock from its |
| 79 | // check for a running holder through its run's registration, so it |
| 80 | // either returns the change to draft after this, or is seen here. |
| 81 | // The change's head is read under it, from the runs as they stand |
| 82 | // now: a follow-up may have pushed a newer head since. |
| 83 | unlock, err := lockContinuation(ctx, dir) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | defer unlock() |
| 88 | if states, err = store.List(); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | // Every session's runs, this one's first, as launch reads them. |
| 92 | runs := append([]*run.State(nil), states...) |
| 93 | mine := make(map[string]bool) |
| 94 | for _, c := range states { |
| 95 | mine[c.ID] = true |
| 96 | } |
| 97 | all, _ := run.ListAllSessions() |
| 98 | for _, c := range all { |
| 99 | if !mine[c.ID] { |
| 100 | runs = append(runs, c) |
| 101 | } |
| 102 | } |
| 103 | t, err := resolveChange(s.ID, changeSources{ |
| 104 | Runs: runs, |
| 105 | Repo: repo, |
| 106 | Valley: func() (*valley.Changes, error) { return valley.ListRepo(ctx, dir) }, |
| 107 | }) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | if t.Branch == "" || t.Head == "" { |
| 112 | return fmt.Errorf("change %s has no known head: no run on it recorded a pushed branch, and no valley lists one", t.Name) |
| 113 | } |
| 114 | deps := run.TmuxDepsFor(ctx, tmux.NewExecClient()) |
| 115 | running := func(r *run.State) bool { return r.IsAgentRunningWith(deps) } |
| 116 | if h := liveHolder(runs, running, "", t.ID, repo); h != nil { |
| 117 | return fmt.Errorf("run %s is still running on change %s; ready the change once it has pushed and its review has passed", h.ID, t.Name) |
| 118 | } |
| 119 | if reviewed != "" { |
| 120 | // A push the valley doesn't hold as a version yet, as when a |
| 121 | // follow-up's ready failed, moves the change on too. |
| 122 | head, where := t.Head, "" |
| 123 | tip, err := git.RemoteBranchSHA(ctx, dir, t.Branch) |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("couldn't check origin's %s for a push newer than %s: %w", t.Branch, shortSHA(t.Head), err) |
| 126 | } |
| 127 | if tip != "" && tip != head { |
| 128 | head, where = tip, fmt.Sprintf(" (origin's %s, a push the valley doesn't hold as a version yet)", t.Branch) |
| 129 | } |
| 130 | if !strings.HasPrefix(head, strings.ToLower(reviewed)) { |
| 131 | return fmt.Errorf("change %s's current head is %s%s, not %s: it has moved on since the head you reviewed; review %s, then valley flow ready %s --head %s", |
| 132 | t.Name, head, where, reviewed, shortSHA(head), args[0], head) |
| 133 | } |
| 134 | t.Head = head |
| 135 | } |
| 136 | if !valley.Detect(ctx, dir, t.Branch, t.Head) { |
| 137 | return fmt.Errorf("change %s isn't on a valley flow can mark changes ready on: %s's origin isn't one, its head carries no valley policy, or valley isn't on PATH", t.Name, dir) |
| 138 | } |
| 139 | r, err := valley.Ready(ctx, dir, t.ID, t.Head, t.Branch) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | // The run that pushed the head holds the version, when it is this |
| 145 | // session's; one marked ready elsewhere, or pushed by a run of |
| 146 | // another session, is recorded on none here. |
| 147 | var pusher *run.State |
| 148 | for _, c := range t.Runs { |
| 149 | if mine[c.ID] && c.PushedSHA != nil && *c.PushedSHA == t.Head { |
| 150 | pusher = c |
| 151 | break |
| 152 | } |
| 153 | } |
| 154 | title := "" |
| 155 | if pusher != nil { |
| 156 | rec := pusher.Valley |
| 157 | if rec == nil { |
| 158 | rec = &run.Valley{Title: commitSubject(ctx, dir, t.Head)} |
| 159 | pusher.Valley = rec |
| 160 | } |
| 161 | rec.Repo, rec.Handle, rec.Version, rec.Draft, rec.Error = r.Repo, r.Handle, r.Version, false, "" |
| 162 | title = rec.Title |
| 163 | if err := store.Save(pusher); err != nil { |
| 164 | return fmt.Errorf("%s %s v%d is ready, but recording it on run %s failed: %w", r.Repo, r.Handle, r.Version, pusher.ID, err) |
| 165 | } |
| 166 | } else { |
| 167 | title = commitSubject(ctx, dir, t.Head) |
| 168 | } |
| 169 | on := s |
| 170 | if pusher != nil { |
| 171 | on = pusher |
| 172 | } |
| 173 | if hds, ok := store.(*run.HomeDirStore); ok { |
| 174 | emitRunEvent(hds.BaseDir(), on, event.AgentChangeReady, map[string]interface{}{ |
| 175 | "id": on.ID, |
| 176 | "repo": r.Repo, |
| 177 | "change_id": t.ID, |
| 178 | "handle": r.Handle, |
| 179 | "version": r.Version, |
| 180 | "title": title, |
| 181 | "head": t.Head, |
| 182 | "via": location.Command("ready"), |
| 183 | }) |
| 184 | } |
| 185 | fmt.Printf("%s %s v%d ready at %s (run %s)\n", r.Repo, r.Handle, r.Version, t.Head, on.ID) |
| 186 | return nil |
| 187 | }, |
| 188 | } |
| 189 | |
| 190 | // headArg is what --head takes: a commit ID, full or at least 7 hex digits. |
| 191 | var headArg = regexp.MustCompile(`^[0-9a-fA-F]{7,40}$`) |
| 192 | |
| 193 | // readyTarget finds the run whose change ref names, among a session's runs: |
| 194 | // the run whose ID ref is, or else the newest run on the change whose |
| 195 | // recorded valley handle ref is, or whose change ID starts with ref. |
| 196 | func readyTarget(states []*run.State, ref string) (*run.State, error) { |
| 197 | if ref == "" { |
| 198 | return nil, errors.New("name a run ID, a valley handle or a change ID") |
| 199 | } |
| 200 | if s := findRun(states, ref); s != nil { |
| 201 | return s, nil |
| 202 | } |
| 203 | newest := make(map[integration.ChangeKey]*run.State) |
| 204 | for _, s := range states { |
| 205 | if s.Type == "session" || !run.ValidChangeID(s.ChangeID) || !refersTo(ref, s.ChangeID, recordedHandles(s)...) { |
| 206 | continue |
| 207 | } |
| 208 | k := integration.ChangeKey{Repo: integration.RepoName(s), ChangeID: s.ChangeID} |
| 209 | if prev, ok := newest[k]; !ok || createdBefore(prev, s) { |
| 210 | newest[k] = s |
| 211 | } |
| 212 | } |
| 213 | var lines []string |
| 214 | for k, s := range newest { |
| 215 | if len(newest) == 1 { |
| 216 | return s, nil |
| 217 | } |
| 218 | lines = append(lines, changeName(k.ChangeID, handleOf(s))+" in "+k.Repo+", latest run "+s.ID) |
| 219 | } |
| 220 | if len(lines) == 0 { |
| 221 | return nil, fmt.Errorf("no run in this session is on a change %q: name a run ID, a valley handle or a change ID", ref) |
| 222 | } |
| 223 | return nil, ambiguous(ref, "this session", lines) |
| 224 | } |
| 225 | |
| 226 | func init() { |
| 227 | readyCmd.Flags().String("head", "", "The head you reviewed: mark exactly it ready, and refuse when the change's current head is another") |
| 228 | rootCmd.AddCommand(readyCmd) |
| 229 | } |