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.

Add named release channels and a generic channel file route

24019cb30892b9fc0118697e8b89cd61e9fab576 · Keenan Nemetz · 19d ago · parent 4f8a3da8

Add named release channels and a generic channel file route

A channel is a mutable pointer, channels/<name>.md, to one immutable annotated
release. The target tag object is recorded so a rewritten tag is detected rather
than silently followed.

- metadata: channel parse/render/validate, closed-layout and size checks,
  set/clear writers
- CLI: vole release channel set|list|clear; a dangling target is reported
- server: maintain/admin path gate and audit for channel.set/clear
- index: release_channels table, incremental refresh, per-tag badge query
- web: /channels list, /channels/<name> redirect, and
  /channels/<name>/<path...> serving the target release's tree with OID ETags,
  Range, revalidation, and octet-stream responses
- tests: metadata round trips, index moves, hook role/audit, page/tree/dangling/
  private, and the CLI end to end

No apt, yum, or other package-manager code is involved: a package layout is a
directory in the release tree.

Refs: 01M7BPHANWMSW5HK2GT0PY74QZ

server/web/channels.go +197

@@ -0,0 +1,197 @@
1 package web
2
3 // Release channels. A channel is a mutable metadata pointer to one annotated
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"
18 "rodent.software/vole/forge/repo"
19 "rodent.software/vole/metadata"
20 )
21
22 // channelRows returns every channel, or an empty list when the metadata branch
23 // has none. The index answers from the snapshot it was built from; a server
24 // without one reads the branch directly.
25 func (w *Web) channels(r *http.Request, m *repo.Meta) []metadata.Channel {
26 if w.Index != nil {
27 if err := w.ensureIndex(memoOf(r), m); err == nil {
28 if list, err := w.Index.Channels(m.ID); err == nil {
29 return list
30 }
31 }
32 }
33 list, _ := metadata.LoadChannelsAt(m.Git(), metadata.Ref)
34 return list
35 }
36
37 func (w *Web) channel(r *http.Request, m *repo.Meta, name string) (metadata.Channel, bool) {
38 for _, ch := range w.channels(r, m) {
39 if ch.Name == name {
40 return ch, true
41 }
42 }
43 return metadata.Channel{}, false
44 }
45
46 // channelTarget resolves the release a channel names. An annotated tag is
47 // required because a channel is a release alias, and the tag object has to match
48 // the pointer: a rewritten tag is a different release, not a moved channel.
49 func (w *Web) channelTarget(g *git.Repo, ch metadata.Channel) (git.Tag, bool) {
50 t, err := g.Tag(ch.Release)
51 if err != nil || !t.Annotated || ch.ReleaseObject == "" {
52 return git.Tag{}, false
53 }
54 if !strings.EqualFold(t.OID, ch.ReleaseObject) {
55 return git.Tag{}, false
56 }
57 return t, true
58 }
59
60 type channelRow struct {
61 metadata.Channel
62 Target string
63 Dangling bool
64 Body template.HTML
65 }
66
67 // Channels lists the repository's stable names. The list is the place to see
68 // what a channel points at without following it.
69 func (w *Web) Channels(rw http.ResponseWriter, r *http.Request) {
70 m, ok := w.open(rw, r)
71 if !ok {
72 return
73 }
74 list := w.channels(r, m)
75 rows := make([]channelRow, 0, len(list))
76 for _, ch := range list {
77 _, ok := w.channelTarget(m.Git(), ch)
78 rows = append(rows, channelRow{
79 Channel: ch,
80 Target: ch.Release,
81 Dangling: !ok,
82 Body: w.renderMarkdown(m.Name, ch.Description),
83 })
84 }
85 w.render(rw, "channels", struct {
86 Page
87 Name string
88 Channels []channelRow
89 }{w.page(r, m.Name+" · channels"), m.Name, rows}, 0)
90 }
91
92 // ChannelView sends a stable channel URL to the release it currently names. The
93 // release page is canonical; a channel is a pointer, not a second copy of the
94 // notes or artifacts.
95 func (w *Web) ChannelView(rw http.ResponseWriter, r *http.Request) {
96 m, ok := w.open(rw, r)
97 if !ok {
98 return
99 }
100 ch, ok := w.channel(r, m, r.PathValue("channel"))
101 if !ok {
102 w.notFound(rw, r)
103 return
104 }
105 t, ok := w.channelTarget(m.Git(), ch)
106 if !ok {
107 w.notFound(rw, r)
108 return
109 }
110 http.Redirect(rw, r, "/"+m.Name+"/releases/"+t.Name, http.StatusFound)
111 }
112
113 // ChannelFile serves one file from the target release's tree. The channel name is
114 // the only mutable part: the bytes are an immutable release artifact, so the ETag
115 // can be the object id while the channel itself revalidates.
116 func (w *Web) ChannelFile(rw http.ResponseWriter, r *http.Request) {
117 m, ok := w.open(rw, r)
118 if !ok {
119 return
120 }
121 ch, ok := w.channel(r, m, r.PathValue("channel"))
122 if !ok {
123 w.notFound(rw, r)
124 return
125 }
126 t, ok := w.channelTarget(m.Git(), ch)
127 if !ok {
128 w.notFound(rw, r)
129 return
130 }
131 name := r.PathValue("path")
132 if err := metadata.ValidArtifactPath(name); err != nil {
133 w.notFound(rw, r)
134 return
135 }
136 var art *metadata.Artifact
137 artifacts := w.releaseArtifacts(r, m, t.Name)
138 for i := range artifacts {
139 if artifacts[i].Name == name {
140 art = &artifacts[i]
141 break
142 }
143 }
144 if art == nil || w.LFS == nil {
145 w.notFound(rw, r)
146 return
147 }
148 f, _, err := w.LFS.Open(m.Name, art.OID)
149 if err != nil {
150 w.Logger("channel artifact %s/%s/%s: %v", m.Name, ch.Name, name, err)
151 w.notFound(rw, r)
152 return
153 }
154 defer f.Close()
155
156 base := path.Base(name)
157 rw.Header().Set("ETag", `"`+art.OID+`"`)
158 // A channel file is package bytes, never a page on the forge origin. Forcing
159 // octet-stream and an attachment keeps a release from injecting HTML or SVG.
160 rw.Header().Set("Content-Type", "application/octet-stream")
161 rw.Header().Set("X-Content-Type-Options", "nosniff")
162 rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", releaseSafe(base)))
163 // The channel moves without the bytes changing, so a cache must revalidate.
164 // The OID ETag makes that a 304 until the target release changes. A private
165 // repository never gets a shared-cache directive.
166 rw.Header().Set("Cache-Control", w.channelCacheControl(m))
167 http.ServeContent(rw, r, base, time.Time{}, f)
168 }
169
170 // channelCacheControl is downloadCacheControl with revalidation: a channel alias
171 // can move, so its response is never served from a shared cache without asking.
172 func (w *Web) channelCacheControl(m *repo.Meta) string {
173 acl, err := m.ACL()
174 if err == nil && acl.Can("", access.Read) {
175 return "public, max-age=0, must-revalidate"
176 }
177 return "private, no-store"
178 }
179
180 // channelsForTag is the release page's badge list.
181 func (w *Web) channelsForTag(r *http.Request, m *repo.Meta, tag string) []string {
182 if w.Index != nil {
183 if err := w.ensureIndex(memoOf(r), m); err == nil {
184 if names, err := w.Index.ChannelsForTag(m.ID, tag); err == nil {
185 return names
186 }
187 }
188 }
189 list, _ := metadata.LoadChannelsAt(m.Git(), metadata.Ref)
190 var names []string
191 for _, ch := range list {
192 if ch.Release == tag {
193 names = append(names, ch.Name)
194 }
195 }
196 return names
197 }