gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita81abdc9a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmrea81abdc9/lib/repos.hl

12.2 KB

  1. // lib/repos.hl — THE REPOS (ticket gitoria#6). A repo is a SLUG that is unique in the whole system;
  2. // it lives at <slug>.<domain> (util.hl repoOrigin). One hl:mpackdb table, storage/mpackdb/repos.db:
  3. // pk @id index !slug, owner { slug, description, owner (users @id), created, branch (the main branch; '' / absent = default),
  4. // tktKey, tktProject, tktTitle, tktApi (the connected tickets project, gitoria#18; the key is a secret and never leaves the server) }
  5. // The unique index makes the slug unique for good (a second put of a taken slug fails); slugError (repos-helpers.hl)
  6. // answers it in words first. A new repo also gets an empty bare git repository (git.hl initRepo).
  7. // Every write to the table is here. The slug's rules, the row a page shows, the Markdown view: repos-helpers.hl.
  8. import { MPackDB } from 'hl:mpackdb'
  9. import { now } from 'hl:time'
  10. import { storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'
  11. import { userOfSession, userRecord, userIdOfSession, nameOfUser, usersByName } from './users.hl'
  12. import { repoOrigin, slugOfHost } from './util.hl'
  13. import { slugOfRequest } from './orgs.hl'
  14. import { slugError, fullNameError, rowOf, withChange } from './repos-helpers.hl'
  15. import { initRepo } from './git.hl'
  16. import { orgBySlug, putOrg, setOrgPriv, allOrgs } from './orgs.hl'
  17. import { refOfRepo, refOfOrg, isMember, memberIds, addMember, removeMember } from './members.hl'
  18. import { forwardOf } from './forwards.hl'
  19. static reposTable = new MPackDB(file = storageDir + '/repos.db', primaryKey = '@id', indexes = ['!slug', 'owner'])
  20. // ---- WHO SEES WHAT (GO34): a repo is private by its own flag or because its organization is; the owner, the organization's owner and the
  21. // members (of the repo or of the organization) see and push; everybody else gets the answer for a name that does not exist.
  22. static isPrivateRepo = (r) => {
  23. if (r.priv == true) { return true }
  24. if (r.org != null && r.org != '') {
  25. o = orgBySlug(r.org)
  26. return o != null && o.priv == true
  27. }
  28. return false
  29. }
  30. static mayRead = (userId, r) => {
  31. if (!isPrivateRepo(r)) { return true }
  32. return isInside(userId, r)
  33. }
  34. // owner / organization owner / member of the repo or of its organization
  35. static isInside = (userId, r) => {
  36. if (userId == null) { return false }
  37. if (r.owner == userId) { return true }
  38. if (isMember(refOfRepo(r.slug), userId)) { return true }
  39. if (r.org != null && r.org != '') {
  40. o = orgBySlug(r.org)
  41. return o != null && (o.owner == userId || isMember(refOfOrg(r.org), userId))
  42. }
  43. return false
  44. }
  45. static repoBySlug = (slug) => {
  46. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  47. return firstOf(reposTable.find('slug', slug))
  48. }
  49. // the repo for a viewer (a users @id or null): the record, or null when it does not exist or the viewer may not see it
  50. static repoFor = (slug, userId) => {
  51. r = repoBySlug(slug)
  52. return r != null && mayRead(userId, r) ? r : null
  53. }
  54. // every repo the viewer may see, newest first
  55. static repoRows = (userId) => {
  56. out = []
  57. all = reposTable.find(null, null)
  58. if (countOfList(all) == 0) { return out }
  59. for (r of all) { if (mayRead(userId, r)) { out.push(rowOf(r, isPrivateRepo(r))) } }
  60. return newestFirst(out)
  61. }
  62. static repoRow = (slug, userId) => {
  63. r = repoFor(slug, userId)
  64. return r == null ? null : rowOf(r, isPrivateRepo(r))
  65. }
  66. // the repos of one organization the viewer may see, newest first
  67. static orgRepoRows = (org, userId) => {
  68. out = []
  69. for (x of repoRows(userId)) { if (x.org == org) { out.push(x) } }
  70. return out
  71. }
  72. // the repo a page stands on (orgs.hl slugOfRequest): its row for this session's viewer, or null for the main address / an unknown or invisible address
  73. static repoAt = (slug, session) => {
  74. if (slug == null || slug == '' || fullNameError(slug) != null) { return null }
  75. return repoRow(slug, userIdOfSession(session))
  76. }
  77. // the organizations the viewer may see (a private one: its owner and members), by name
  78. static orgRows = (userId) => {
  79. out = []
  80. for (o of allOrgs()) {
  81. if (o.priv != true || o.owner == userId || isMember(refOfOrg(o.slug), userId)) { out.push({ slug = o.slug isPrivate = o.priv == true isPublic = o.priv != true address = repoOrigin(o.slug) + '/' }) }
  82. }
  83. return out
  84. }
  85. // an organization for a viewer: the record or null (a private one is invisible to others)
  86. static orgFor = (slug, userId) => {
  87. o = orgBySlug(slug)
  88. if (o == null) { return null }
  89. if (o.priv != true || (userId != null && (o.owner == userId || isMember(refOfOrg(slug), userId)))) { return o }
  90. // somebody who was added to ONE repo of a private organization reaches that repo through the organization's address (the organization's page then lists only what they may see)
  91. if (userId != null) {
  92. all = reposTable.find(null, null)
  93. if (countOfList(all) > 0) { for (r of all) { if (r.org == slug && mayRead(userId, r)) { return o } } }
  94. }
  95. return null
  96. }
  97. // does this name already stand for an organization or a top-level repo? (one namespace: both are subdomains)
  98. static nameTaken = (name) => { return orgBySlug(name) != null || repoBySlug(name) != null }
  99. // a new organization for a user: { org } or { error }
  100. static createOrg = (userId, slug, priv) => {
  101. u = userRecord(userId)
  102. if (u == null) { return { error = 'log in with ident (top right) first' } }
  103. if (u.name == '') { return { error = 'choose a display name first (top of the page)' } }
  104. s = slug == null ? '' : ('' + slug).trim()
  105. bad = slugError(s)
  106. if (bad != null) { return { error = bad } }
  107. if (nameTaken(s)) { return { error = "the address '" + s + "' is taken — organizations and repositories share their addresses" } }
  108. o = putOrg(s, u.id, priv == true)
  109. if (o == null) { return { error = 'could not store the organization' } }
  110. return { org = { slug = o.slug isPrivate = o.priv == true } }
  111. }
  112. // may this user create a repo in the organization / push to its repos?
  113. static mayUseOrg = (userId, o) => {
  114. return userId != null && (o.owner == userId || isMember(refOfOrg(o.slug), userId))
  115. }
  116. // a new repo for a user: { repo } (the row) or { error, field }. `org` = '' or an organization the user belongs to; `priv` = private
  117. static createRepoIn = (userId, org, slug, description, priv) => {
  118. u = userRecord(userId)
  119. if (u == null) { return { error = 'log in with ident (top right) to create a repository' field = '' } }
  120. if (u.name == '') { return { error = 'choose a display name first (top of the page)' field = '' } }
  121. s = slug == null ? '' : ('' + slug).trim()
  122. og = org == null ? '' : ('' + org).trim()
  123. bad = slugError(s)
  124. if (bad != null) { return { error = bad field = 'slug' } }
  125. d = description == null ? '' : ('' + description).trim()
  126. dbad = plainError(d, 200, 'the description')
  127. if (dbad != null) { return { error = dbad field = 'description' } }
  128. let full = s
  129. if (og != '') {
  130. o = orgBySlug(og)
  131. if (o == null || !mayUseOrg(u.id, o)) { return { error = 'no such organization (or you are not in it)' field = 'org' } }
  132. full = fullNameError(og + '/' + s) == null ? og + '/' + s : ''
  133. if (full == '') { return { error = fullNameError(og + '/' + s) field = 'slug' } }
  134. } else if (nameTaken(s)) {
  135. return { error = "the address '" + s + "' is taken — every repository address is unique in the whole system" field = 'slug' }
  136. }
  137. if (repoBySlug(full) != null || forwardOf(full) != null) { return { error = "the address '" + full + "' is taken — every repository address is unique in the whole system" field = 'slug' } }
  138. id = reposTable.put({ slug = full description = d owner = u.id created = now() org = og priv = priv == true })
  139. if (id == null) { return { error = 'could not store the repository (is the address taken?): ' + reposTable.lastError() field = 'slug' } }
  140. initRepo(full)
  141. r = reposTable.fetch(id)
  142. return { repo = rowOf(r, isPrivateRepo(r)) }
  143. }
  144. // the old call: a public top-level repo
  145. static createRepo = (userId, slug, description) => { return createRepoIn(userId, '', slug, description, false) }
  146. // ---- visibility and members (the owner of the repo / the organization) ----------------------------------------------------
  147. static setRepoPriv = (userId, slug, priv) => {
  148. r = repoFor(slug, userId)
  149. if (r == null) { return { error = 'no such repository' } }
  150. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  151. reposTable.update(r.id, withChange(r, { priv = priv == true }))
  152. after = reposTable.fetch(r.id)
  153. if (after == null || (after.priv == true) != (priv == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  154. return { repo = rowOf(after, isPrivateRepo(after)) }
  155. }
  156. static setOrgPrivate = (userId, org, priv) => {
  157. o = orgFor(org, userId)
  158. if (o == null) { return { error = 'no such organization' } }
  159. if (userId == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  160. if (!setOrgPriv(o, priv == true)) { return { error = 'could not store the setting' } }
  161. return { ok = true }
  162. }
  163. // the member list of a repo ('r') or an organization ('o'): [{ id, name }] for its owner
  164. static membersOf = (kind, slug, userId) => {
  165. let ref = ''
  166. if (kind == 'r') {
  167. r = repoFor(slug, userId)
  168. if (r == null || r.owner != userId) { return { error = 'only the owner can see this' } }
  169. ref = refOfRepo(slug)
  170. } else {
  171. o = orgFor(slug, userId)
  172. if (o == null || o.owner != userId) { return { error = 'only the owner can see this' } }
  173. ref = refOfOrg(slug)
  174. }
  175. out = []
  176. for (uid of memberIds(ref)) { out.push({ id = uid name = nameOfUser(uid) }) }
  177. return { members = out }
  178. }
  179. // add (add = true) or remove a member by display name; the owner only
  180. static changeMember = (userId, kind, slug, name, add) => {
  181. let ref = ''
  182. if (kind == 'r') {
  183. r = repoFor(slug, userId)
  184. if (r == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  185. ref = refOfRepo(slug)
  186. } else {
  187. o = orgFor(slug, userId)
  188. if (o == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  189. ref = refOfOrg(slug)
  190. }
  191. n = name == null ? '' : ('' + name).trim()
  192. found = usersByName(n)
  193. if (found.length == 0) { return { error = "nobody is called '" + n + "' (people choose their display name at their first login)" } }
  194. if (found.length > 1) { return { error = "more than one person is called '" + n + "'" } }
  195. ok = add ? addMember(ref, found[0]) : removeMember(ref, found[0])
  196. if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }
  197. return membersOf(kind, slug, userId)
  198. }
  199. // does this (real) session's user own the repo?
  200. static ownsRepo = (slug, session) => {
  201. u = userOfSession(session)
  202. row = repoBySlug(slug)
  203. return u != null && row != null && row.owner == u.id
  204. }
  205. // MAY THIS USER PUSH? Only the repo's owner (a known user) — over HTTPS (transport.hl, the token's user) and over SSH
  206. // (lib/api.hl gitAccess, the key's user). Reading is everyone's.
  207. static mayPush = (userId, repo) => { return userRecord(userId) != null && isInside(userId, repo) }
  208. // the connection to a tickets project (gitoria#18); key '' = disconnected. The caller checked the owner.
  209. static setTicketsConnection = (slug, key, project, title, api) => {
  210. r = repoBySlug(slug)
  211. if (r == null) { return { error = 'no such repository' } }
  212. reposTable.update(r.id, withChange(r, { tktKey = key tktProject = project tktTitle = title tktApi = api }))
  213. after = reposTable.fetch(r.id)
  214. if (after == null || after.tktKey != key) { return { error = 'could not store the connection: ' + reposTable.lastError() } }
  215. return { repo = rowOf(after, isPrivateRepo(after)) }
  216. }
  217. // the owner's setting of the main branch (/code, the homepage). The caller checked the branch exists.
  218. static setMainBranch = (userId, slug, name) => {
  219. r = repoBySlug(slug)
  220. if (r == null) { return { error = 'no such repository' } }
  221. if (userId == null || r.owner != userId) { return { error = 'only the owner can change the main branch' } }
  222. reposTable.update(r.id, withChange(r, { branch = name }))
  223. after = reposTable.fetch(r.id)
  224. if (after == null || after.branch != name) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  225. return { repo = rowOf(after, isPrivateRepo(after)) }
  226. }
  227. // the name a page's tab title shows: the repo's name when the viewer may see it, else only what was typed in the address (an invisible repo and one nobody made get the SAME title)
  228. static titleSlug = (host, orepo, session) => {
  229. s = slugOfRequest(host, orepo)
  230. if (repoAt(s, session) != null) { return s }
  231. return orepo != null && orepo != '' ? orepo : slugOfHost(host)
  232. }

Branches

Latest commits

  • a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre
  • 17f031f0gitoria#34: private repos, organizations, members, move tool, gatemre
  • 71b69343gitoria#23: the sidebar's Repositories link was a 404 after any in-page navigation (static menu re-evaluated publicUrl in the browser); the menu is an instance property now; gate taps it after a tab, folder, filemre
  • 90b4f728gitoria#25: restore the homepage cache and the parsed-blocks cache (they store data under immutable keys, no HTML); the page key, the framework readme key and the textHash key stay removedmre
  • a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
  • 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
  • c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre
  • e307d69agitoria: say what is per process during the hand-overmre
  • f0cbe6a6gitoria: standby service on port 45014 and a deploy that hands over through it without a gapmre
  • 727cd083gitoria#25: README: vendor notes for Hybriel c1b1010f and the page cachemre
  • 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre
  • 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
  • bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
  • 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
  • 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
  • 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
  • 2a2fdd77gs: C2 check before cookie experimentsmre
  • 76e0587aWIP gs 2mre
  • a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
  • 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre