| 1 | package knot |
| 2 | |
| 3 | import ( |
| 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. |
| 11 | type 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. |
| 20 | var 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: "decision", Prefix: "dcr", Directory: "decisions", |
| 28 | Active: []string{"proposed"}, Terminal: []string{"decided", "superseded"}}, |
| 29 | } |
| 30 | |
| 31 | // CommentsDirectory holds every knot's comments, one directory per knot. |
| 32 | const CommentsDirectory = "comments" |
| 33 | |
| 34 | // TypeNamed is the type called name. |
| 35 | func 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. |
| 45 | func 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. |
| 55 | func 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. |
| 65 | func 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. |
| 74 | func (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. |
| 79 | func (t Type) Initial() string { return t.Active[0] } |
| 80 | |
| 81 | // IsTerminal reports whether status is one of the type's terminal ones. |
| 82 | func (t Type) IsTerminal(status string) bool { return slices.Contains(t.Terminal, status) } |
| 83 | |
| 84 | // Field is one key a knot's frontmatter may hold. |
| 85 | type 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 | |
| 101 | var ( |
| 102 | allTypes = []string{"outcome", "bug", "idea", "decision"} |
| 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. |
| 108 | var Fields = []Field{ |
| 109 | {Name: "type", On: allTypes, Kind: "token", Required: "always", |
| 110 | Meaning: "the knot's type: outcome, bug, idea or decision"}, |
| 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: workTypes, Kind: "list", Required: "never", Set: "link", |
| 134 | Meaning: "the changes that do the work: 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 decision a graduated idea's thinking moved to"}, |
| 139 | } |
| 140 | |
| 141 | // FieldNamed is the field called name. |
| 142 | func 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. |
| 152 | func (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 "". |
| 156 | func 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. |
| 168 | type 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. |
| 193 | func (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. |
| 199 | func (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. |
| 206 | func (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. |
| 211 | func (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. |
| 248 | func (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. |
| 257 | type Comment struct { |
| 258 | Author string |
| 259 | Body string |
| 260 | } |
| 261 | |
| 262 | // IsLocalID reports whether s is a knot ID. |
| 263 | func IsLocalID(s string) bool { return localID.MatchString(s) } |
| 264 | |
| 265 | // IsQualifiedID reports whether s names a knot in another project, |
| 266 | // <project>:<id>. |
| 267 | func IsQualifiedID(s string) bool { return len(s) <= MaxName+12 && qualifiedID.MatchString(s) } |
| 268 | |
| 269 | // IsName reports whether s is a principal's, a project's or a run's name. |
| 270 | func IsName(s string) bool { return len(s) <= MaxName && name.MatchString(s) } |
| 271 | |
| 272 | // IsChangeLink reports whether s is an entry changes may hold. |
| 273 | func IsChangeLink(s string) bool { |
| 274 | return len(s) <= MaxLink && (valleyChange.MatchString(s) || githubPR.MatchString(s)) |
| 275 | } |
| 276 | |
| 277 | // IsOpID reports whether s is an operation's ID. |
| 278 | func IsOpID(s string) bool { return opID.MatchString(s) } |
| 279 | |
| 280 | // IsTimestamp reports whether s is a UTC time as claimed_at holds one. |
| 281 | func IsTimestamp(s string) bool { return timestamp.MatchString(s) } |