gunk-dev/the-valley

approve.go

source28abc5737165main's published tip history
4.5 KiB
1package cmd
2
3import (
4 "fmt"
5 "os"
6 "strings"
7 "time"
8
9 "the-valley/flow/internal/event"
10 "the-valley/flow/internal/run"
11 "github.com/spf13/cobra"
12)
13
14var approveCmd = &cobra.Command{
15 Use: "approve <pr-number> [<pr-number>...]",
16 Short: "Approve PRs for merging (operator task)",
17 Long: `Marks PRs as approved so they can be merged with 'valley flow merge'.
18Approval is a human task that exists only to ensure that all changes are gated by operator approval.
19
20Accepts PR numbers, or use --run to approve by run ID.
21Use --all to approve all merge-ready PRs.`,
22 RunE: func(cmd *cobra.Command, args []string) error {
23 runID, _ := cmd.Flags().GetString("run")
24 all, _ := cmd.Flags().GetBool("all")
25
26 if !all && runID == "" && len(args) == 0 {
27 return fmt.Errorf("specify PR numbers, --run <id>, or --all")
28 }
29
30 store, err := sessionStore()
31 if err != nil {
32 return err
33 }
34 states, err := store.List()
35 if err != nil {
36 return err
37 }
38
39 if all {
40 return approveAll(states, store)
41 }
42
43 if runID != "" {
44 return approveByRunID(runID, store)
45 }
46
47 return approveByPRNumbers(args, states, store)
48 },
49}
50
51func approveAll(states []*run.State, store run.StateStore) error {
52 count := 0
53 for _, s := range states {
54 if s.PRURL == nil || s.MergedAt != nil {
55 continue
56 }
57 if s.Approved != nil && *s.Approved {
58 continue
59 }
60 if err := markApproved(s, store); err != nil {
61 fmt.Fprintf(os.Stderr, "warning: failed to approve run %s: %v\n", s.ID, err)
62 continue
63 }
64 prNum := extractPRNumber(s)
65 fmt.Printf("Approved PR #%s (run %s)\n", prNum, shortID(s.ID))
66 count++
67 }
68 if count == 0 {
69 fmt.Println("No unapproved PRs found.")
70 }
71 return nil
72}
73
74func approveByRunID(id string, store run.StateStore) error {
75 state, err := store.Load(id)
76 if err != nil {
77 return fmt.Errorf("run %s not found: %w", id, err)
78 }
79 if err := markApproved(state, store); err != nil {
80 return err
81 }
82 prNum := extractPRNumber(state)
83 if prNum == "" {
84 prNum = "(no PR)"
85 }
86 fmt.Printf("Approved PR #%s (run %s)\n", prNum, shortID(state.ID))
87 return nil
88}
89
90func approveByPRNumbers(prNumbers []string, states []*run.State, store run.StateStore) error {
91 for _, prNum := range prNumbers {
92 state, st, err := findRunByPR(prNum, states, store)
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "warning: no run found for PR #%s: %v\n", prNum, err)
95 continue
96 }
97 if err := markApproved(state, st); err != nil {
98 fmt.Fprintf(os.Stderr, "warning: failed to approve PR #%s: %v\n", prNum, err)
99 continue
100 }
101 fmt.Printf("Approved PR #%s (run %s)\n", prNum, shortID(state.ID))
102 }
103 return nil
104}
105
106func markApproved(s *run.State, store run.StateStore) error {
107 approved := true
108 s.Approved = &approved
109 now := time.Now().UTC().Format(time.RFC3339)
110 s.ApprovedAt = &now
111 if store != nil {
112 if err := store.Save(s); err != nil {
113 return err
114 }
115 }
116 emitApprovalChanged(store, s)
117 return nil
118}
119
120// emitApprovalChanged writes a PRApprovalChanged event to the session event
121// log so the dashboard FSM wakes without waiting for an unrelated GitHub
122// webhook. The event is informational — failures are logged via stderr but
123// do not fail the approve command, since the state itself is already
124// persisted on disk and the dashboard's polling fallback (when enabled) will
125// pick up the change on the next tick.
126func emitApprovalChanged(store run.StateStore, s *run.State) {
127 hds, ok := store.(*run.HomeDirStore)
128 if !ok {
129 return
130 }
131 data := map[string]interface{}{}
132 if pr := extractPRNumber(s); pr != "" {
133 data["pr_number"] = pr
134 }
135 if s.PRURL != nil {
136 data["pr_url"] = *s.PRURL
137 }
138 data["approved"] = true
139 log := event.NewLog(hds.BaseDir())
140 if err := log.Emit(event.New(s.ID, event.PRApprovalChanged, withRunFields(s, data))); err != nil {
141 fmt.Fprintf(os.Stderr, "warning: failed to emit approval event: %v\n", err)
142 }
143}
144
145// findRunByPR finds a run state matching a PR number in the given store.
146func findRunByPR(prNumber string, states []*run.State, store run.StateStore) (*run.State, run.StateStore, error) {
147 prNumber = strings.TrimPrefix(prNumber, "#")
148 for _, s := range states {
149 if extractPRNumber(s) == prNumber {
150 return s, store, nil
151 }
152 }
153 return nil, nil, fmt.Errorf("no run with PR #%s", prNumber)
154}
155
156// shortID returns a truncated run ID for display (last 4 chars).
157func shortID(id string) string {
158 if len(id) <= 4 {
159 return id
160 }
161 parts := strings.Split(id, "-")
162 if len(parts) > 0 {
163 return parts[len(parts)-1]
164 }
165 return id
166}
167
168func init() {
169 approveCmd.Flags().String("run", "", "Approve by run ID instead of PR number")
170 approveCmd.Flags().Bool("all", false, "Approve all merge-ready PRs")
171 rootCmd.AddCommand(approveCmd)
172}