gunk-dev/the-valley

knot.go

source9ee95920e399main's published tip history
6.0 KiB
1package valley
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "regexp"
8 "time"
9)
10
11// KnotTimeout bounds each `valley knot` call.
12const KnotTimeout = 30 * time.Second
13
14// KnotSchemaV1 is the schema of what `valley knot show --json` prints.
15const KnotSchemaV1 = "valley-knot/v1"
16
17// knotRef is a knot ID, outcome, bug, idea or question, optionally behind
18// the name of the project whose plan graph holds it: oc-897917f or
19// the-valley:oc-897917f.
20var knotRef = regexp.MustCompile(`^(?:([A-Za-z0-9][A-Za-z0-9._-]*):)?((?:oc|bd|ida|qn)-[0-9a-f]{7})$`)
21
22// KnotRef names a knot in a valley project's plan graph. Project is "" for
23// the graph of the project valley runs in.
24type KnotRef struct {
25 Project string
26 ID string
27}
28
29// ParseKnotRef reads an --issue value as a knot: a knot ID (oc-, bd-, ida- or
30// qn- and 7 hex digits), optionally as <project>:<id>. ok is false for
31// anything else, such as a GitHub issue number.
32func ParseKnotRef(s string) (KnotRef, bool) {
33 m := knotRef.FindStringSubmatch(s)
34 if m == nil {
35 return KnotRef{}, false
36 }
37 return KnotRef{Project: m[1], ID: m[2]}, true
38}
39
40func (r KnotRef) String() string {
41 if r.Project == "" {
42 return r.ID
43 }
44 return r.Project + ":" + r.ID
45}
46
47// Knot is a knot as `valley knot show --json` prints it.
48type Knot struct {
49 ID string `json:"id"`
50 Type string `json:"type"`
51 Status string `json:"status"`
52 Active bool `json:"active"`
53 Title string `json:"title"`
54 Created string `json:"created"`
55 CreatedBy string `json:"created_by"`
56 Source string `json:"source"`
57 Parent string `json:"parent"`
58 Owner string `json:"owner"`
59 Target string `json:"target"`
60 ClaimedBy string `json:"claimed_by"`
61 ClaimedAt string `json:"claimed_at"`
62 Changes []string `json:"changes"`
63 Body string `json:"body"`
64 Comments []KnotComment `json:"comments"`
65 Children []string `json:"children"`
66}
67
68// KnotComment is a comment on a knot.
69type KnotComment struct {
70 Author string `json:"author"`
71 At string `json:"at"`
72 Body string `json:"body"`
73}
74
75// KnotView is what `valley knot show --json` prints: the knot and the
76// project whose graph holds it.
77type KnotView struct {
78 Schema string `json:"schema"`
79 Project string `json:"project"`
80 Knot Knot `json:"knot"`
81}
82
83// ShowKnot runs `valley knot show <id> --json` in dir, in project's graph
84// ("" for the graph of the project valley runs in).
85func ShowKnot(ctx context.Context, dir, project, id string) (*KnotView, error) {
86 out, err := command(ctx, dir, KnotTimeout, knotArgs(project, "show", id, "--json")...)
87 if err != nil {
88 return nil, err
89 }
90 var v KnotView
91 if err := json.Unmarshal(out, &v); err != nil {
92 return nil, &Error{Verb: "knot show", Reason: "unexpected output: " + shortReason(out)}
93 }
94 if v.Schema != KnotSchemaV1 {
95 return nil, &Error{Verb: "knot show", Reason: fmt.Sprintf("schema %q, want %q", v.Schema, KnotSchemaV1)}
96 }
97 return &v, nil
98}
99
100// ClaimKnot runs `valley knot claim <id> --run <runID>`. A knot claimed
101// already is a conflict (Error.Conflict).
102func ClaimKnot(ctx context.Context, dir, project, id, runID string) error {
103 _, err := command(ctx, dir, KnotTimeout, knotArgs(project, "claim", id, "--run", runID)...)
104 return err
105}
106
107// UnclaimKnot runs `valley knot unclaim <id> --if claimed_by=<runID>`,
108// releasing runID's claim. A claim that isn't runID's is a conflict
109// (Error.Conflict), and is left alone.
110func UnclaimKnot(ctx context.Context, dir, project, id, runID string) error {
111 _, err := command(ctx, dir, KnotTimeout, knotArgs(project, "unclaim", id, "--if", "claimed_by="+runID)...)
112 return err
113}
114
115// LinkKnot runs `valley knot link <id> --change <change>`. Linking a change
116// the knot holds already changes nothing.
117func LinkKnot(ctx context.Context, dir, project, id, change string) error {
118 _, err := command(ctx, dir, KnotTimeout, knotArgs(project, "link", id, "--change", change)...)
119 return err
120}
121
122// CommentKnot runs `valley knot comment <id> --body -` with body on stdin.
123func CommentKnot(ctx context.Context, dir, project, id, body string) error {
124 _, err := commandInput(ctx, dir, KnotTimeout, []byte(body), knotArgs(project, "comment", id, "--body", "-")...)
125 return err
126}
127
128// SetKnotStatus runs `valley knot set <id> status=<status>` with an --if for
129// each of conditions, <field>=<value>: valley checks them against the knot
130// it writes, and a field that holds another value is a conflict. A list
131// field's value is its entries joined by commas, as show lists them.
132func SetKnotStatus(ctx context.Context, dir, project, id, status string, conditions ...string) error {
133 args := []string{id, "status=" + status}
134 for _, c := range conditions {
135 args = append(args, "--if", c)
136 }
137 _, err := command(ctx, dir, KnotTimeout, knotArgs(project, "set", args...)...)
138 return err
139}
140
141// ChangeLink is how a knot links a valley change: valley:<repo>/<change ID>.
142func ChangeLink(repo, changeID string) string {
143 return "valley:" + repo + "/" + changeID
144}
145
146// changeLink matches a valley change link.
147var changeLink = regexp.MustCompile(`^valley:([^/\s]+)/(\S+)$`)
148
149// ParseChangeLink reads a knot's link to a valley change; ok is false for
150// any other link, such as a GitHub pull request URL.
151func ParseChangeLink(link string) (repo, changeID string, ok bool) {
152 m := changeLink.FindStringSubmatch(link)
153 if m == nil {
154 return "", "", false
155 }
156 return m[1], m[2], true
157}
158
159// knotArgs is `knot <verb> <args>`, with --project when project is set.
160func knotArgs(project, verb string, args ...string) []string {
161 out := append([]string{"knot", verb}, args...)
162 if project != "" {
163 out = append(out, "--project", project)
164 }
165 return out
166}
167
168// DetectKnots reports whether the repo in dir is a valley project whose
169// plan graph klaus can work: Detect's checks against head, the commit a
170// launch starts from, and a valley on PATH that also lists knot.
171func DetectKnots(ctx context.Context, dir, head string) bool {
172 return detect(ctx, dir, "", head, append(changeVerbs, "knot")...)
173}