Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · server/web/channels.go

Line attribution on main · 381 line(s)

24019cb3
Keenan Nemetz
19d ago
1 package web
2
262e516e
Keenan Nemetz
19d ago
3 // Release channels. A channel is a mutable server-side pointer to one annotated
24019cb3
Keenan Nemetz
19d ago
4 // tag; the bytes it serves are the target release's attached files. Nothing here
5 // knows a package manager: a channel is a stable file tree, and apt/yum layouts
6 // are directory names in that tree.
7
8 import (
9 "fmt"
10 "html/template"
11 "net/http"
12 "path"
13 "strings"
14 "time"
15
16 "rodent.software/vole/core/git"
17 "rodent.software/vole/forge/access"
262e516e
Keenan Nemetz
19d ago
18 "rodent.software/vole/forge/channel"
24019cb3
Keenan Nemetz
19d ago
19 "rodent.software/vole/forge/repo"
262e516e
Keenan Nemetz
19d ago
20 "rodent.software/vole/internal/artifactpath"
24019cb3
Keenan Nemetz
19d ago
21 "rodent.software/vole/metadata"
22 )
23
262e516e
Keenan Nemetz
19d ago
24 // channels returns every channel for a repository. The store is authoritative and
25 // separate from the derived index, so a channel survives `vole admin index rebuild`.
26 func (w *Web) channels(m *repo.Meta) []channel.Channel {
27 if w.ChannelStore == nil {
28 return nil
24019cb3
Keenan Nemetz
19d ago
29 }
262e516e
Keenan Nemetz
19d ago
30 list, _ := w.ChannelStore.List(m.ID)
24019cb3
Keenan Nemetz
19d ago
31 return list
32 }
33
262e516e
Keenan Nemetz
19d ago
34 func (w *Web) channel(m *repo.Meta, name string) (channel.Channel, bool) {
35 if w.ChannelStore == nil {
36 return channel.Channel{}, false
24019cb3
Keenan Nemetz
19d ago
37 }
262e516e
Keenan Nemetz
19d ago
38 ch, ok, _ := w.ChannelStore.Get(m.ID, name)
39 return ch, ok
24019cb3
Keenan Nemetz
19d ago
40 }
41
de72893e
Keenan Nemetz
17d ago
42 // channelTargetFromTag reports whether t is the release ch was recorded against.
43 // An annotated tag is required because a channel is a release alias, and the tag
44 // object has to match the pointer: a rewritten tag is a different release, not a
45 // moved channel.
46 func channelTargetFromTag(t git.Tag, ch channel.Channel) bool {
47 return t.Annotated && ch.ReleaseObject != "" && strings.EqualFold(t.OID, ch.ReleaseObject)
48 }
49
50 // channelTarget resolves the release a channel names.
262e516e
Keenan Nemetz
19d ago
51 func (w *Web) channelTarget(g *git.Repo, ch channel.Channel) (git.Tag, bool) {
24019cb3
Keenan Nemetz
19d ago
52 t, err := g.Tag(ch.Release)
de72893e
Keenan Nemetz
17d ago
53 if err != nil || !channelTargetFromTag(t, ch) {
24019cb3
Keenan Nemetz
19d ago
54 return git.Tag{}, false
55 }
56 return t, true
57 }
58
59 type channelRow struct {
262e516e
Keenan Nemetz
19d ago
60 channel.Channel
24019cb3
Keenan Nemetz
19d ago
61 Target string
62 Dangling bool
63 Body template.HTML
64 }
65
ea6ce9fe
Keenan Nemetz
17d ago
66 // channelForm is the browser form for creating or moving one channel. Editing
67 // locks the name: the CLI has no rename either, and a channel's identity is its
68 // URL.
69 type channelForm struct {
70 Name string
71 Tag string
72 Description string
73 Editing bool
74 Error string
75 }
76
77 type channelFormView struct {
78 Page
79 Name string
80 Tags []git.Tag
81 Channel channelForm
82 MissingTarget bool
83 }
84
85 // canManageChannels is the page-and-handler check for the channel form. It is the
86 // release maintainer capability plus a store to write to.
87 func (w *Web) canManageChannels(r *http.Request, m *repo.Meta) bool {
88 return w.ChannelStore != nil && w.canManageReleases(r, m)
89 }
90
91 // channelFormView lists only annotated tags: a channel names a release, and a
92 // select of valid targets is more honest than a text box that can name anything.
93 func (w *Web) channelFormView(r *http.Request, m *repo.Meta, tags []git.Tag, form channelForm) channelFormView {
94 annotated := make([]git.Tag, 0, len(tags))
95 found := false
96 for _, t := range tags {
97 if !t.Annotated {
98 continue
99 }
100 annotated = append(annotated, t)
101 if t.Name == form.Tag {
102 found = true
103 }
104 }
105 title := m.Name + " · new channel"
106 if form.Editing {
107 title = m.Name + " · channel " + form.Name
108 }
109 return channelFormView{
110 Page: w.page(r, title),
111 Name: m.Name,
112 Tags: annotated,
113 Channel: form,
114 MissingTarget: form.Tag != "" && !found,
115 }
116 }
117
118 func (w *Web) ChannelNewPage(rw http.ResponseWriter, r *http.Request) {
119 m, ok := w.open(rw, r)
120 if !ok {
121 return
122 }
123 if _, ok := w.requireHTMLUser(rw, r); !ok {
124 return
125 }
126 if !w.canManageChannels(r, m) {
127 w.notFound(rw, r)
128 return
129 }
130 tags, err := m.Git().Tags()
131 if err != nil {
132 w.serverError(rw, r, "cannot list tags: "+err.Error())
133 return
134 }
135 w.render(rw, "channel_form", w.channelFormView(r, m, tags, channelForm{}), 0)
136 }
137
138 func (w *Web) ChannelEditPage(rw http.ResponseWriter, r *http.Request) {
139 m, ok := w.open(rw, r)
140 if !ok {
141 return
142 }
143 if _, ok := w.requireHTMLUser(rw, r); !ok {
144 return
145 }
146 if !w.canManageChannels(r, m) {
147 w.notFound(rw, r)
148 return
149 }
150 name := strings.TrimSpace(r.URL.Query().Get("channel"))
151 if err := channel.ValidName(name); err != nil {
152 w.notFound(rw, r)
153 return
154 }
155 ch, ok := w.channel(m, name)
156 if !ok {
157 w.notFound(rw, r)
158 return
159 }
160 tags, err := m.Git().Tags()
161 if err != nil {
162 w.serverError(rw, r, "cannot list tags: "+err.Error())
163 return
164 }
165 w.render(rw, "channel_form", w.channelFormView(r, m, tags, channelForm{
166 Name: ch.Name,
167 Tag: ch.Release,
168 Description: ch.Description,
169 Editing: true,
170 }), 0)
171 }
172
de72893e
Keenan Nemetz
17d ago
173 // SetChannel creates or moves a channel from the browser. It is the form of
174 // `vole release channel set`: the same maintain check, annotated-tag requirement,
175 // description bound, and audit record, with the actor taken from the session.
176 func (w *Web) SetChannel(rw http.ResponseWriter, r *http.Request) {
177 ctx, ok := w.beginWrite(rw, r, maxSmallForm)
24019cb3
Keenan Nemetz
19d ago
178 if !ok {
179 return
180 }
de72893e
Keenan Nemetz
17d ago
181 m := ctx.Repo
182 if !canMaintain(ctx.ACL, ctx.Name) {
183 w.notFound(rw, r)
184 return
185 }
186 if w.ChannelStore == nil {
187 w.serverError(rw, r, "the channel store is not available")
188 return
189 }
190
191 name := strings.TrimSpace(r.PostFormValue("channel"))
192 tag := strings.TrimSpace(r.PostFormValue("tag"))
193 description := r.PostFormValue("description")
194 editing := r.PostFormValue("editing") == "1"
195
196 tags, err := m.Git().Tags()
197 if err != nil {
198 w.serverError(rw, r, "cannot list tags: "+err.Error())
199 return
200 }
ea6ce9fe
Keenan Nemetz
17d ago
201 view := w.channelFormView(r, m, tags, channelForm{
202 Name: name, Tag: tag, Description: description, Editing: editing,
203 })
de72893e
Keenan Nemetz
17d ago
204 fail := func(msg string) {
ea6ce9fe
Keenan Nemetz
17d ago
205 view.Channel.Error = msg
206 w.render(rw, "channel_form", view, http.StatusBadRequest)
de72893e
Keenan Nemetz
17d ago
207 }
208
209 if err := channel.ValidName(name); err != nil {
210 fail(err.Error())
211 return
212 }
213 t, err := m.Git().Tag(tag)
214 if err != nil {
215 fail(fmt.Sprintf("No tag %q in this repository.", tag))
216 return
217 }
218 if !t.Annotated {
219 fail(fmt.Sprintf("%s is a lightweight tag; a channel names an annotated release.", tag))
220 return
221 }
222 if len(description) > channel.MaxDescription {
223 fail(fmt.Sprintf("The description is limited to %d bytes.", channel.MaxDescription))
224 return
225 }
226
227 if err := w.auditBefore("web.channel.set", ctx.Name, m.Name, name+" -> "+tag); err != nil {
228 w.serverError(rw, r, "cannot record the channel move: "+err.Error())
229 return
230 }
231 if err := w.ChannelStore.Set(m.ID, channel.Channel{
232 Name: name,
233 Release: tag,
234 ReleaseObject: t.OID,
235 UpdatedBy: ctx.Name,
236 UpdatedAt: time.Now().UTC(),
237 Description: description,
238 }); err != nil {
239 w.serverError(rw, r, "cannot save the channel: "+err.Error())
240 return
241 }
242 http.Redirect(rw, r, "/"+m.Name+"/releases#channels", http.StatusSeeOther)
243 }
244
245 // ClearChannel removes a channel from the browser. The release and its files are
246 // untouched; only the stable name stops resolving.
247 func (w *Web) ClearChannel(rw http.ResponseWriter, r *http.Request) {
248 ctx, ok := w.beginWrite(rw, r, maxTinyForm)
249 if !ok {
250 return
251 }
252 m := ctx.Repo
253 if !canMaintain(ctx.ACL, ctx.Name) {
254 w.notFound(rw, r)
255 return
256 }
257 if w.ChannelStore == nil {
258 w.serverError(rw, r, "the channel store is not available")
259 return
260 }
261 name := r.PathValue("channel")
262 if err := channel.ValidName(name); err != nil {
263 w.notFound(rw, r)
264 return
265 }
266 if _, found, err := w.ChannelStore.Get(m.ID, name); err != nil {
267 w.serverError(rw, r, "cannot read the channel: "+err.Error())
268 return
269 } else if !found {
270 w.notFound(rw, r)
271 return
272 }
273
274 if err := w.auditBefore("web.channel.clear", ctx.Name, m.Name, name); err != nil {
275 w.serverError(rw, r, "cannot record the channel change: "+err.Error())
276 return
277 }
278 if _, err := w.ChannelStore.Clear(m.ID, name); err != nil {
279 w.serverError(rw, r, "cannot clear the channel: "+err.Error())
280 return
281 }
282 http.Redirect(rw, r, "/"+m.Name+"/releases#channels", http.StatusSeeOther)
24019cb3
Keenan Nemetz
19d ago
283 }
284
285 // ChannelView sends a stable channel URL to the release it currently names. The
286 // release page is canonical; a channel is a pointer, not a second copy of the
287 // notes or artifacts.
288 func (w *Web) ChannelView(rw http.ResponseWriter, r *http.Request) {
289 m, ok := w.open(rw, r)
290 if !ok {
291 return
292 }
262e516e
Keenan Nemetz
19d ago
293 ch, ok := w.channel(m, r.PathValue("channel"))
24019cb3
Keenan Nemetz
19d ago
294 if !ok {
295 w.notFound(rw, r)
296 return
297 }
298 t, ok := w.channelTarget(m.Git(), ch)
299 if !ok {
300 w.notFound(rw, r)
301 return
302 }
303 http.Redirect(rw, r, "/"+m.Name+"/releases/"+t.Name, http.StatusFound)
304 }
305
306 // ChannelFile serves one file from the target release's tree. The channel name is
214e7ea6
Keenan Nemetz
17d ago
307 // the only mutable part of the URL: the bytes are content-addressed, so the ETag
308 // can be the object id while the channel itself revalidates. A re-pointed release
309 // therefore serves its new bytes under a new validator, never stale ones.
24019cb3
Keenan Nemetz
19d ago
310 func (w *Web) ChannelFile(rw http.ResponseWriter, r *http.Request) {
311 m, ok := w.open(rw, r)
312 if !ok {
313 return
314 }
262e516e
Keenan Nemetz
19d ago
315 ch, ok := w.channel(m, r.PathValue("channel"))
24019cb3
Keenan Nemetz
19d ago
316 if !ok {
317 w.notFound(rw, r)
318 return
319 }
320 t, ok := w.channelTarget(m.Git(), ch)
321 if !ok {
322 w.notFound(rw, r)
323 return
324 }
325 name := r.PathValue("path")
262e516e
Keenan Nemetz
19d ago
326 if err := artifactpath.Valid(name); err != nil {
24019cb3
Keenan Nemetz
19d ago
327 w.notFound(rw, r)
328 return
329 }
330 var art *metadata.Artifact
331 artifacts := w.releaseArtifacts(r, m, t.Name)
332 for i := range artifacts {
333 if artifacts[i].Name == name {
334 art = &artifacts[i]
335 break
336 }
337 }
338 if art == nil || w.LFS == nil {
339 w.notFound(rw, r)
340 return
341 }
342 f, _, err := w.LFS.Open(m.Name, art.OID)
343 if err != nil {
344 w.Logger("channel artifact %s/%s/%s: %v", m.Name, ch.Name, name, err)
345 w.notFound(rw, r)
346 return
347 }
348 defer f.Close()
349
350 base := path.Base(name)
351 rw.Header().Set("ETag", `"`+art.OID+`"`)
352 // A channel file is package bytes, never a page on the forge origin. Forcing
353 // octet-stream and an attachment keeps a release from injecting HTML or SVG.
354 rw.Header().Set("Content-Type", "application/octet-stream")
355 rw.Header().Set("X-Content-Type-Options", "nosniff")
356 rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", releaseSafe(base)))
357 // The channel moves without the bytes changing, so a cache must revalidate.
358 // The OID ETag makes that a 304 until the target release changes. A private
359 // repository never gets a shared-cache directive.
360 rw.Header().Set("Cache-Control", w.channelCacheControl(m))
361 http.ServeContent(rw, r, base, time.Time{}, f)
362 }
363
364 // channelCacheControl is downloadCacheControl with revalidation: a channel alias
365 // can move, so its response is never served from a shared cache without asking.
366 func (w *Web) channelCacheControl(m *repo.Meta) string {
367 acl, err := m.ACL()
368 if err == nil && acl.Can("", access.Read) {
369 return "public, max-age=0, must-revalidate"
370 }
371 return "private, no-store"
372 }
373
374 // channelsForTag is the release page's badge list.
262e516e
Keenan Nemetz
19d ago
375 func (w *Web) channelsForTag(m *repo.Meta, tag string) []string {
376 if w.ChannelStore == nil {
377 return nil
24019cb3
Keenan Nemetz
19d ago
378 }
262e516e
Keenan Nemetz
19d ago
379 names, _ := w.ChannelStore.ForTag(m.ID, tag)
24019cb3
Keenan Nemetz
19d ago
380 return names
381 }