gunk-dev/the-valley

untrack.go

source6a6791929c36main's published tip history
3.0 KiB
1package cmd
2
3import (
4 "fmt"
5 "os"
6 "strings"
7
8 "the-valley/flow/internal/run"
9 "github.com/spf13/cobra"
10)
11
12var untrackCmd = &cobra.Command{
13 Use: "untrack <ref> [<ref>...]",
14 Short: "Stop tracking PRs or valley changes on the dashboard",
15 Long: `Removes tracked PRs or valley changes from the flow dashboard and valley flow
16status. Only removes what was added via 'valley flow track' — agent-created runs are
17never removed.
18
19A PR is named by its number. A valley change is named by its handle, its
20full change ID, or <project>:<handle>; --repo names the project when the ref
21doesn't. A change ref that names tracked changes in more than one project
22removes none; name the project.
23
24Examples:
25 valley flow untrack 405
26 valley flow untrack vpkz
27 valley flow untrack the-valley:vpkz
28 valley flow untrack --repo the-valley vpkz`,
29 Args: cobra.MinimumNArgs(1),
30 RunE: func(cmd *cobra.Command, args []string) error {
31 repoFlag, _ := cmd.Flags().GetString("repo")
32 store, err := sessionStore()
33 if err != nil {
34 return err
35 }
36
37 states, err := store.List()
38 if err != nil {
39 return err
40 }
41
42 for _, ref := range args {
43 if project, id, ok := parseChangeRef(ref); ok {
44 if project == "" {
45 project = repoFlag
46 }
47 if err := untrackChange(store, states, ref, project, id); err != nil {
48 return err
49 }
50 continue
51 }
52 prNum := strings.TrimPrefix(ref, "#")
53 found := false
54 for _, s := range states {
55 if s.Type != "track" {
56 continue
57 }
58 if extractPRNumber(s) == prNum {
59 if err := store.Delete(s.ID); err != nil {
60 fmt.Fprintf(os.Stderr, "warning: failed to remove PR #%s: %v\n", prNum, err)
61 } else {
62 fmt.Printf("Stopped tracking PR #%s\n", prNum)
63 }
64 found = true
65 break
66 }
67 }
68 if !found {
69 fmt.Fprintf(os.Stderr, "warning: no tracked PR #%s found\n", prNum)
70 }
71 }
72 return nil
73 },
74}
75
76// untrackChange removes the track record of the valley change id names, in
77// project when it isn't "", saying what it did, or why it removed none.
78func untrackChange(store run.StateStore, states []*run.State, ref, project, id string) error {
79 matches, err := trackedChanges(states, project, id)
80 if err != nil {
81 return err
82 }
83 switch len(matches) {
84 case 0:
85 fmt.Fprintf(os.Stderr, "warning: no tracked change %s found\n", ref)
86 return nil
87 case 1:
88 default:
89 var names []string
90 for _, s := range matches {
91 names = append(names, s.Valley.Repo+":"+s.Valley.Handle)
92 }
93 fmt.Fprintf(os.Stderr, "warning: %s names tracked changes in more than one project (%s); name the project\n", ref, strings.Join(names, ", "))
94 return nil
95 }
96 s := matches[0]
97 if err := store.Delete(s.ID); err != nil {
98 fmt.Fprintf(os.Stderr, "warning: failed to stop tracking change %s: %v\n", ref, err)
99 return nil
100 }
101 fmt.Printf("Stopped tracking change %s (%s)\n", s.Valley.Handle, s.Valley.Repo)
102 return nil
103}
104
105func init() {
106 untrackCmd.Flags().String("repo", "", "Project of a valley change named by its handle or change ID")
107 rootCmd.AddCommand(untrackCmd)
108}