releases.gno
6.68 Kb · 221 lines
1// Package releases is the registry every version of a project asks "am I the
2// latest, and if not, what am I allowed to do?".
3//
4// It stores one p/moul/releases Project per name, each owned by the address
5// that opened it, and serves each one's stage homepage at its render path:
6// /r/moul/releases/v0:<project>. The library is gno.land/p/moul/releases/v0;
7// this realm is its state and its demo.
8//
9// # The integration, in a version
10//
11// import (
12// "chain/runtime/unsafe"
13// reg "gno.land/r/moul/releases/v0"
14// )
15//
16// var self = unsafe.CurrentRealm().PkgPath() // computed once, at init
17//
18// func Render(path string) string { return reg.Banner("myproject", self) + body(path) }
19// func Post(cur realm, s string) { reg.AssertWritable("myproject", self); … }
20//
21// The getters take no realm, so a version calls them without crossing. Every
22// lookup is scoped by project name, which the version hard-codes: a path is
23// only ever judged by the project that names it, so nobody can pause someone
24// else's realm by listing its path as yanked in a project of their own.
25package releases
26
27import (
28 "chain/runtime"
29 "chain/runtime/unsafe"
30 "strings"
31
32 "gno.land/p/moul/pausable/v0"
33 rel "gno.land/p/moul/releases/v0"
34 "gno.land/p/nt/bptree/v0"
35)
36
37// entry is one project and the one address allowed to change it.
38type entry struct {
39 project *rel.Project
40 owner address
41}
42
43// projects maps a name to its *entry. Projects are never removed, so the
44// fanout that wins for a tree that only grows.
45var projects = bptree.NewBPTreeN(128)
46
47// stageBase is this realm's own render path, "/r/moul/releases/v0:", computed
48// in init. A getter cannot compute it later: called by another realm, a
49// borrowed function sees the CALLER as the current realm (AGENTS.md).
50var stageBase string
51
52func init() {
53 stageBase = strings.TrimPrefix(unsafe.CurrentRealm().PkgPath(), runtime.ChainDomain()) + ":"
54 seed()
55}
56
57func lookup(name string) *entry {
58 e, _ := projects.Get(name).(*entry)
59 return e
60}
61
62// StageURL is the link to a project's stage homepage.
63func StageURL(project string) string { return stageBase + project }
64
65// ---- reads: no realm argument, callable from any version without crossing
66
67// Owner is the address that may change project, or "" when it does not exist.
68func Owner(project string) address {
69 if e := lookup(project); e != nil {
70 return e.owner
71 }
72 return ""
73}
74
75// Status is what project says about the release at pkgPath, with Latest
76// computed. ok is false when the project or the path is not listed.
77func Status(project, pkgPath string) (rel.Status, bool) {
78 e := lookup(project)
79 if e == nil {
80 return rel.Draft, false
81 }
82 r, ok := e.project.ByPath(pkgPath)
83 if !ok {
84 return rel.Draft, false
85 }
86 return e.project.StatusOf(r), true
87}
88
89// IsLatest reports whether pkgPath is project's latest supported release.
90func IsLatest(project, pkgPath string) bool {
91 e := lookup(project)
92 return e != nil && e.project.IsLatest(pkgPath)
93}
94
95// Latest returns project's latest supported release.
96func Latest(project string) (version, pkgPath string, ok bool) {
97 e := lookup(project)
98 if e == nil {
99 return "", "", false
100 }
101 r, ok := e.project.Latest()
102 return r.Version, r.PkgPath, ok
103}
104
105// State is the pause state the release at pkgPath is in at the current block,
106// the zero (running) State when it is not listed.
107func State(project, pkgPath string) pausable.State {
108 e := lookup(project)
109 if e == nil {
110 return pausable.State{}
111 }
112 return e.project.State(pkgPath, runtime.ChainHeight())
113}
114
115// AssertWritable aborts unless the release at pkgPath still accepts writes:
116// it is not yanked, not vulnerable, and not past a deprecation sunset.
117func AssertWritable(project, pkgPath string) {
118 State(project, pkgPath).AssertWritable()
119}
120
121// Banner is the markdown the release at pkgPath prepends to its Render: ""
122// for the latest or an unlisted path, a one-paragraph notice otherwise.
123func Banner(project, pkgPath string) string {
124 e := lookup(project)
125 if e == nil {
126 return ""
127 }
128 return e.project.Banner(pkgPath, runtime.ChainHeight(), StageURL(project))
129}
130
131// ---- writes: crossing, the project owner only
132
133func caller(cur realm) address {
134 if !cur.IsCurrent() {
135 panic("releases: spoofed realm")
136 }
137 return cur.Previous().Address()
138}
139
140// owned returns the project the caller owns, or aborts.
141func owned(cur realm, name string) *rel.Project {
142 who := caller(cur)
143 e := lookup(name)
144 if e == nil {
145 panic("releases: no such project")
146 }
147 if e.owner != who {
148 panic("releases: not the project owner")
149 }
150 return e.project
151}
152
153func check(err error) {
154 if err != nil {
155 panic(err.Error())
156 }
157}
158
159// NewProject opens a project owned by the caller. First come, first served:
160// a name is taken for good, which is why a version names its project in code
161// before it ships.
162func NewProject(cur realm, name, description string) {
163 who := caller(cur)
164 if projects.Has(name) {
165 panic("releases: project name taken")
166 }
167 p, err := rel.NewProject(name, description)
168 check(err)
169 projects.Set(name, &entry{project: p, owner: who})
170}
171
172// SetDescription replaces a project's one-line description.
173func SetDescription(cur realm, project, description string) {
174 check(owned(cur, project).SetDescription(description))
175}
176
177// AddRelease lists a version. status is a word: draft, prerelease,
178// supported, deprecated, yanked or vulnerable.
179func AddRelease(cur realm, project, version, pkgPath, url, notes, status string) {
180 p := owned(cur, project)
181 st, ok := rel.ParseStatus(status)
182 if !ok {
183 panic("releases: unknown status " + status)
184 }
185 check(p.Add(version, pkgPath, url, notes, st, runtime.ChainHeight()))
186}
187
188// SetStatus moves a version to a status word, with the reason readers see.
189func SetStatus(cur realm, project, version, status, reason string) {
190 p := owned(cur, project)
191 st, ok := rel.ParseStatus(status)
192 if !ok {
193 panic("releases: unknown status " + status)
194 }
195 check(p.SetStatus(version, st, reason))
196}
197
198// Deprecate marks a version deprecated: it keeps working, with a banner,
199// until block height sunset, and is read-only from then on. 0 is never.
200func Deprecate(cur realm, project, version, reason string, sunset int64) {
201 check(owned(cur, project).Deprecate(version, reason, sunset))
202}
203
204// Yank withdraws a version for good. It stays listed and accepts nothing.
205func Yank(cur realm, project, version, reason string) {
206 check(owned(cur, project).Yank(version, reason))
207}
208
209// SetSuccessor sends readers of version to successor instead of to Latest.
210func SetSuccessor(cur realm, project, version, successor string) {
211 check(owned(cur, project).SetSuccessor(version, successor))
212}
213
214// TransferProject hands a project to another address.
215func TransferProject(cur realm, project string, to address) {
216 owned(cur, project)
217 if !to.IsValid() {
218 panic("releases: invalid address")
219 }
220 lookup(project).owner = to
221}