gunk-dev/the-valley

knot.go

source937924e2f63emain's published tip history
8.8 KiB
1package server
2
3import (
4 "net/http"
5 "slices"
6 "strconv"
7 "strings"
8
9 "the-valley/knot"
10 "the-valley/web/export"
11)
12
13// knotPage is one knot: its project header, where it sits, from its
14// project's knots page down, its fields, the knots it links, its body and
15// its comments.
16type knotPage struct {
17 Head projectHead
18 Plan string
19 Knot knotLink
20 Type string
21 Status string
22 Trail []knotLink
23 TrailCut bool
24 Facts []fact
25 BlockedBy []reference
26 Blocks []knotLink
27 Children []knotLink
28 Changes []string
29 Body string
30 Comments []comment
31}
32
33// fact is one of a knot's fields. A fact with a URL links to what it
34// names.
35type fact struct {
36 Label string
37 Text string
38 URL string
39}
40
41// reference is one entry of blocked_by: a knot the page links to, a
42// reference it can only name, with what is known of it, or a knot of a
43// project the reader may not see, which it does not name at all.
44type reference struct {
45 Ref string
46 Knot *knotLink
47 Note string
48}
49
50// comment is one comment as the page shows it. An edited comment says so.
51type comment struct {
52 Author string
53 At string
54 Edited bool
55 EditedAt string
56 EditedBy string
57 Body string
58}
59
60func (q *request) knot(project, id string) {
61 p, ok := q.project(project)
62 if !ok {
63 q.notFound()
64 return
65 }
66 knots := q.idx.knots[p.Name]
67 k, ok := knots[id]
68 if !ok {
69 q.notFound()
70 return
71 }
72 page := knotPage{Head: q.idx.head(p.Name, "knots"), Plan: projectKnotsURL(q.idx.bases[p.Name]),
73 Knot: q.idx.linkOf(p.Name, k), Type: k.Type, Status: k.Status, Body: trimEnd(k.Body)}
74
75 // The trail runs from the milestone down to the knot's parent. It
76 // names the nearest MaxPlanDepth parents, and marks any above them.
77 seen := map[string]bool{k.ID: true}
78 for at := k.Parent; at != "" && !seen[at]; {
79 parent, ok := knots[at]
80 if !ok {
81 break
82 }
83 if len(page.Trail) == MaxPlanDepth {
84 page.TrailCut = true
85 break
86 }
87 seen[at] = true
88 page.Trail = append(page.Trail, q.idx.linkOf(p.Name, parent))
89 at = parent.Parent
90 }
91 slices.Reverse(page.Trail)
92
93 add := func(label, text, url string) {
94 if text != "" {
95 page.Facts = append(page.Facts, fact{label, text, url})
96 }
97 }
98 add("State", stateLabel(k.State), "")
99 add("Created", k.Created+" by "+k.CreatedBy, "")
100 add("Owner", k.Owner, "")
101 add("Source", k.Source, "")
102 if c := k.Claim; c != nil {
103 add("Claimed", "by "+c.By+", "+stampOf(c.At), "")
104 }
105 if pr := k.Progress; pr != nil {
106 add("Progress", strconv.Itoa(pr.Done)+" of "+strconv.Itoa(pr.Of)+" done", "")
107 }
108 if k.Target != "" {
109 text, url := q.idx.target(k.Target)
110 add("Lands in", text, url)
111 }
112 if k.GraduatedInto != "" {
113 text, url := q.idx.graduated(p.Name, k.GraduatedInto)
114 add("Graduated into", text, url)
115 }
116 for _, c := range k.Changes {
117 page.Changes = append(page.Changes, q.idx.change(c))
118 }
119
120 for _, ref := range k.BlockedBy {
121 page.BlockedBy = append(page.BlockedBy, q.reference(p.Name, k, ref))
122 }
123 for _, id := range k.Blocks {
124 if b, ok := knots[id]; ok {
125 page.Blocks = append(page.Blocks, q.idx.linkOf(p.Name, b))
126 }
127 }
128 for _, id := range k.Children {
129 if c, ok := knots[id]; ok {
130 page.Children = append(page.Children, q.idx.linkOf(p.Name, c))
131 }
132 }
133 for _, c := range k.Comments {
134 page.Comments = append(page.Comments, comment{Author: c.Author, At: stampOf(c.At),
135 Edited: c.Edited != "", EditedAt: stampOf(c.Edited), EditedBy: c.EditedBy, Body: trimEnd(c.Body)})
136 }
137 q.page(http.StatusOK, "knot", k.ID+" · "+k.Title, page)
138}
139
140// The words a page shows for a reference into a project the reader may
141// not see, whether the export holds it for another reader or not at all.
142// They name nothing of what is referred to: not the project, not the knot,
143// and not the reference's text. A page shows them too for a value that has
144// none of the shapes its field allows, since such a value names nothing
145// the reader may see.
146const (
147 unpublishedKnot = "a knot in an unpublished project"
148 unpublishedProject = "an unpublished project"
149 unpublishedChange = "a change in an unpublished project"
150)
151
152// knotRef reads ref as a reference to a knot, from a knot of project's: a
153// knot's ID in project's own graph, or <project>:<id> for a knot of
154// another project's, each whole (knot/validate.go). other and id are the
155// project and the ID it names. ok is false for any other text, which names
156// no knot.
157func knotRef(project, ref string) (other, id string, ok bool) {
158 if knot.IsLocalID(ref) {
159 return project, ref, true
160 }
161 if !knot.IsQualifiedID(ref) {
162 return "", "", false
163 }
164 other, id, _ = strings.Cut(ref, ":")
165 return other, id, true
166}
167
168// reference resolves one blocked_by entry of k's, read by knotRef. A knot
169// of this project or of another the reader may see is linked. A reference
170// into a project the index does not hold is shown as unpublished, with
171// none of its text, and so is an entry of neither shape, which names no
172// knot. Any other reference is named, and said to be missing.
173func (q *request) reference(project string, k *export.Knot, ref string) reference {
174 other, id, ok := knotRef(project, ref)
175 if !ok {
176 return reference{Note: unpublishedKnot}
177 }
178 if _, ok := q.project(other); !ok {
179 return reference{Note: unpublishedKnot}
180 }
181 if b, ok := q.idx.knots[other][id]; ok {
182 link := q.idx.linkOf(other, b)
183 return reference{Ref: ref, Knot: &link}
184 }
185 for _, e := range k.Edges {
186 if e.Field == "blocked_by" && e.Ref == ref && e.State == export.EdgeDangling {
187 return reference{Ref: ref, Note: "not found in its graph"}
188 }
189 }
190 return reference{Ref: ref, Note: "not found"}
191}
192
193// target is how a page names what a knot's target holds, and the page it
194// links to. A target is a project's name, or <project>:<id> for a knot of
195// one, each whole (knot/validate.go). It is never a knot of the knot's own
196// graph, so a project named as a knot's ID is still a project. A project
197// the reader may see is linked to the tree of its published tip. A knot of
198// one is linked to its page, or named and not linked when the index does
199// not hold it. A target into a project the index does not hold is named
200// only as unpublished, and not linked, and so is a target of neither
201// shape.
202func (idx *index) target(ref string) (text, url string) {
203 if knot.IsName(ref) {
204 if p, ok := idx.project(ref); ok {
205 return ref, treeURL(idx.bases[p.Name], p.SourceTip, nil)
206 }
207 return unpublishedProject, ""
208 }
209 if !knot.IsQualifiedID(ref) {
210 return unpublishedProject, ""
211 }
212 other, id, _ := strings.Cut(ref, ":")
213 if _, ok := idx.project(other); !ok {
214 return unpublishedKnot, ""
215 }
216 if _, ok := idx.knots[other][id]; ok {
217 return ref, knotURL(idx.bases[other], id)
218 }
219 return ref, ""
220}
221
222// graduated is how a page names what a knot's graduated_into holds, and
223// the page it links to. graduated_into is the ID of an outcome or a
224// question in the knot's own graph (knot/validate.go), which the reader
225// sees, since they see the knot. It is linked when the index holds it, and
226// named and not linked when it does not. Any other value names no knot the
227// reader may see, and is named only as unpublished.
228func (idx *index) graduated(project, ref string) (text, url string) {
229 if !knot.IsLocalID(ref) || !strings.HasPrefix(ref, "oc-") && !strings.HasPrefix(ref, "qn-") {
230 return unpublishedKnot, ""
231 }
232 if _, ok := idx.knots[project][ref]; ok {
233 return ref, knotURL(idx.bases[project], ref)
234 }
235 return ref, ""
236}
237
238// change is how a page names one of a knot's change links. A link is
239// valley:<project>/<change ID>, or a GitHub pull request's URL, each whole
240// (knot/validate.go). A valley link is named only as unpublished unless
241// the index holds its project, and so is a link of neither shape.
242func (idx *index) change(link string) string {
243 if !knot.IsChangeLink(link) {
244 return unpublishedChange
245 }
246 if rest, ok := strings.CutPrefix(link, "valley:"); ok {
247 project, _, _ := strings.Cut(rest, "/")
248 if _, public := idx.project(project); !public {
249 return unpublishedChange
250 }
251 }
252 return link
253}
254
255// waitsOn is the knots a blocked knot of project's waits on, as a tree row
256// names them: refs, and, when hidden is set, the knots of projects the
257// reader may not see that the export or the projection left out of refs.
258// Those, any of refs into a project the index does not hold, and any that
259// knotRef does not read as a knot's, are named once, as unpublished, after
260// the others. A blocked knot that waits on nothing refs names waits on
261// something unpublished, so it is named so too, as it is from an export
262// that does not carry waiting_on_hidden.
263func (idx *index) waitsOn(project string, refs []string, hidden bool) string {
264 var out []string
265 named := false
266 for _, ref := range refs {
267 idx.checkBuild()
268 other, _, ok := knotRef(project, ref)
269 if !ok {
270 hidden = true
271 continue
272 }
273 if _, public := idx.project(other); !public {
274 hidden = true
275 continue
276 }
277 out = append(out, ref)
278 named = true
279 }
280 if hidden || !named {
281 out = append(out, unpublishedKnot)
282 }
283 return strings.Join(out, ", ")
284}