gunk-dev/the-valley

model.go

source9b709a18cb1fmain's published tip history
9.8 KiB
1package knot
2
3import (
4 "slices"
5 "strings"
6)
7
8// Type is one of the four kinds of knot. Each fixes its ID prefix, its
9// directory, and the statuses it can be in, split into active and
10// terminal. A knot starts in its type's first active status.
11type Type struct {
12 Name string `json:"name"`
13 Prefix string `json:"prefix"`
14 Directory string `json:"directory"`
15 Active []string `json:"active"`
16 Terminal []string `json:"terminal"`
17}
18
19// Types are the four types, in the order the schema lists them.
20var Types = []Type{
21 {Name: "outcome", Prefix: "oc", Directory: "outcomes",
22 Active: []string{"open", "in-progress"}, Terminal: []string{"done", "abandoned"}},
23 {Name: "bug", Prefix: "bd", Directory: "bugs",
24 Active: []string{"open"}, Terminal: []string{"closed"}},
25 {Name: "idea", Prefix: "ida", Directory: "ideas",
26 Active: []string{"raw", "exploring", "adopted"}, Terminal: []string{"graduated", "superseded", "discarded"}},
27 {Name: "question", Prefix: "qn", Directory: "questions",
28 Active: []string{"open"}, Terminal: []string{"answered", "dropped"}},
29}
30
31// CommentsDirectory holds every knot's comments, one directory per knot.
32const CommentsDirectory = "comments"
33
34// TypeNamed is the type called name.
35func TypeNamed(name string) (Type, bool) {
36 for _, t := range Types {
37 if t.Name == name {
38 return t, true
39 }
40 }
41 return Type{}, false
42}
43
44// typeOfPrefix is the type whose IDs open with prefix.
45func typeOfPrefix(prefix string) (Type, bool) {
46 for _, t := range Types {
47 if t.Prefix == prefix {
48 return t, true
49 }
50 }
51 return Type{}, false
52}
53
54// typeOfDirectory is the type whose knots live in dir.
55func typeOfDirectory(dir string) (Type, bool) {
56 for _, t := range Types {
57 if t.Directory == dir {
58 return t, true
59 }
60 }
61 return Type{}, false
62}
63
64// TypeNames lists the four types' names.
65func TypeNames() []string {
66 var names []string
67 for _, t := range Types {
68 names = append(names, t.Name)
69 }
70 return names
71}
72
73// Statuses are every status of the type, active ones first.
74func (t Type) Statuses() []string {
75 return append(append([]string(nil), t.Active...), t.Terminal...)
76}
77
78// Initial is the status a new knot of the type starts in.
79func (t Type) Initial() string { return t.Active[0] }
80
81// IsTerminal reports whether status is one of the type's terminal ones.
82func (t Type) IsTerminal(status string) bool { return slices.Contains(t.Terminal, status) }
83
84// Field is one key a knot's frontmatter may hold.
85type Field struct {
86 Name string `json:"name"`
87 // On are the types that carry it.
88 On []string `json:"on"`
89 // Kind is how it is written: "token" bare, "string" as a JSON
90 // string, "list" as a flow list.
91 Kind string `json:"kind"`
92 // Required is "always", "never", or the condition under which it is
93 // required.
94 Required string `json:"required"`
95 // Set says how the valley command changes it: "set", "link",
96 // "claim", or "" for a field no command changes after `new`.
97 Set string `json:"set,omitempty"`
98 Meaning string `json:"meaning"`
99}
100
101var (
102 allTypes = []string{"outcome", "bug", "idea", "question"}
103 workTypes = []string{"outcome", "bug"}
104)
105
106// Fields are every key a knot may hold, in the order the canonical form
107// writes them.
108var Fields = []Field{
109 {Name: "type", On: allTypes, Kind: "token", Required: "always",
110 Meaning: "the knot's type: outcome, bug, idea or question"},
111 {Name: "id", On: allTypes, Kind: "token", Required: "always",
112 Meaning: "the type's prefix and the first 7 hex digits of the SHA-256 of the slug"},
113 {Name: "status", On: allTypes, Kind: "token", Required: "always", Set: "set",
114 Meaning: "the knot's state, from its type's statuses"},
115 {Name: "title", On: allTypes, Kind: "string", Required: "always", Set: "set",
116 Meaning: "one line naming the knot"},
117 {Name: "created", On: allTypes, Kind: "token", Required: "always",
118 Meaning: "the date the knot was created, YYYY-MM-DD"},
119 {Name: "created_by", On: allTypes, Kind: "string", Required: "always",
120 Meaning: "the principal that created the knot; the host checks it names the pusher"},
121 {Name: "source", On: allTypes, Kind: "string", Required: "never", Set: "set",
122 Meaning: "where the content came from: a date and a venue"},
123 {Name: "parent", On: allTypes, Kind: "token", Required: "never", Set: "link",
124 Meaning: "the outcome this belongs to: a milestone, a work item, or a residual's work item"},
125 {Name: "owner", On: allTypes, Kind: "string", Required: "never", Set: "set",
126 Meaning: "the principal it waits on: responsibility, not authority"},
127 {Name: "blocked_by", On: workTypes, Kind: "list", Required: "always", Set: "link",
128 Meaning: "the knots this waits on: local IDs, or <project>:<id> for another project's"},
129 {Name: "claimed_by", On: workTypes, Kind: "string", Required: "with claimed_at", Set: "claim",
130 Meaning: "the run working on it"},
131 {Name: "claimed_at", On: workTypes, Kind: "string", Required: "with claimed_by", Set: "claim",
132 Meaning: "when the run claimed it, as a UTC time"},
133 {Name: "changes", On: []string{"outcome", "bug", "question"}, Kind: "list", Required: "never", Set: "link",
134 Meaning: "the changes that do the work, or the change that records a question's answer in the source: valley:<project>/<change ID>, or a GitHub pull request URL"},
135 {Name: "target", On: workTypes, Kind: "string", Required: "never", Set: "set",
136 Meaning: "the project where the work lands, or <project>:<id>, when that is not this project"},
137 {Name: "graduated_into", On: []string{"idea"}, Kind: "token", Required: "when status is graduated", Set: "set",
138 Meaning: "the outcome or question a graduated idea moved to: work taken on, or a question to settle"},
139}
140
141// FieldNamed is the field called name.
142func FieldNamed(name string) (Field, bool) {
143 for _, f := range Fields {
144 if f.Name == name {
145 return f, true
146 }
147 }
148 return Field{}, false
149}
150
151// carries reports whether a knot of type t may hold field f.
152func (f Field) carries(t string) bool { return slices.Contains(f.On, t) }
153
154// FieldNames lists the names of the fields a knot of type t may hold, or
155// of every field when t is "".
156func FieldNames(t string) []string {
157 var names []string
158 for _, f := range Fields {
159 if t == "" || f.carries(t) {
160 names = append(names, f.Name)
161 }
162 }
163 return names
164}
165
166// Knot is one knot: its frontmatter, its body, and the slug its filename
167// carries.
168type Knot struct {
169 Type string
170 ID string
171 Status string
172 Title string
173 Created string
174 CreatedBy string
175 Source string
176 Parent string
177 Owner string
178 BlockedBy []string
179 ClaimedBy string
180 ClaimedAt string
181 Changes []string
182 Target string
183 GraduatedInto string
184
185 // Body is the markdown below the frontmatter, ending in a newline,
186 // or "" for none.
187 Body string
188 // Slug is the part of the filename after the ID.
189 Slug string
190}
191
192// Path is where the knot's file lives in the graph's tree.
193func (k *Knot) Path() string {
194 t, _ := TypeNamed(k.Type)
195 return t.Directory + "/" + k.ID + "-" + k.Slug + ".md"
196}
197
198// TypeOf is the knot's type.
199func (k *Knot) TypeOf() Type {
200 t, _ := TypeNamed(k.Type)
201 return t
202}
203
204// Active reports whether the knot's status is one of its type's active
205// ones.
206func (k *Knot) Active() bool { return !k.TypeOf().IsTerminal(k.Status) }
207
208// Get is the value of a field written the way `valley knot set` reads
209// it: a list joined with commas, and "" for a field the knot does not
210// hold.
211func (k *Knot) Get(field string) string {
212 switch field {
213 case "type":
214 return k.Type
215 case "id":
216 return k.ID
217 case "status":
218 return k.Status
219 case "title":
220 return k.Title
221 case "created":
222 return k.Created
223 case "created_by":
224 return k.CreatedBy
225 case "source":
226 return k.Source
227 case "parent":
228 return k.Parent
229 case "owner":
230 return k.Owner
231 case "blocked_by":
232 return strings.Join(k.BlockedBy, ",")
233 case "claimed_by":
234 return k.ClaimedBy
235 case "claimed_at":
236 return k.ClaimedAt
237 case "changes":
238 return strings.Join(k.Changes, ",")
239 case "target":
240 return k.Target
241 case "graduated_into":
242 return k.GraduatedInto
243 }
244 return ""
245}
246
247// Clone is a copy of the knot that shares nothing with it.
248func (k *Knot) Clone() *Knot {
249 c := *k
250 c.BlockedBy = slices.Clone(k.BlockedBy)
251 c.Changes = slices.Clone(k.Changes)
252 return &c
253}
254
255// Comment is one comment on a knot: who wrote it, and what it says. Its
256// filename says when, and which operation wrote it. A comment that has
257// been edited also says when it was last edited, and by whom; one that
258// has not leaves both empty.
259type Comment struct {
260 Author string
261 Body string
262 // Edited is when the comment was last edited, as a UTC time, and
263 // EditedBy is the principal that edited it.
264 Edited string
265 EditedBy string
266}
267
268// CommentPath is where the comment with commentID on the knot with knotID
269// lives in the graph's tree.
270func CommentPath(knotID, commentID string) string {
271 return CommentsDirectory + "/" + knotID + "/" + commentID + ".md"
272}
273
274// IsCommentID reports whether s is a comment's ID, <stamp>-<op>.
275func IsCommentID(s string) bool { return commentID.MatchString(s) }
276
277// IsLocalID reports whether s is a knot ID.
278func IsLocalID(s string) bool { return localID.MatchString(s) }
279
280// IsQualifiedID reports whether s names a knot in another project,
281// <project>:<id>.
282func IsQualifiedID(s string) bool { return len(s) <= MaxName+12 && qualifiedID.MatchString(s) }
283
284// IsName reports whether s is a principal's, a project's or a run's name.
285func IsName(s string) bool { return len(s) <= MaxName && name.MatchString(s) }
286
287// IsChangeLink reports whether s is an entry changes may hold.
288func IsChangeLink(s string) bool {
289 return len(s) <= MaxLink && (valleyChange.MatchString(s) || githubPR.MatchString(s))
290}
291
292// IsOpID reports whether s is an operation's ID.
293func IsOpID(s string) bool { return opID.MatchString(s) }
294
295// IsTimestamp reports whether s is a UTC time as claimed_at holds one.
296func IsTimestamp(s string) bool { return timestamp.MatchString(s) }