gunk-dev/the-valley

knot.go

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