gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5803cf4f5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre5803cf4f/lib/repos.hl

15.3 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 { NL, storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'
  11. import { userOfSession, userRecord, userIdOfSession, nameOfUser, usersByName, userForMember } 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, repoDir } from './git.hl'
  16. import { writeFile, remove, exists } from 'hl:fs'
  17. import { orgBySlug, putOrg, setOrgPriv, allOrgs } from './orgs.hl'
  18. import { refOfRepo, refOfOrg, isMember, memberIds, addMember, removeMember } from './members.hl'
  19. import { forwardOf } from './forwards.hl'
  20. static reposTable = new MPackDB(file = storageDir + '/repos.db', primaryKey = '@id', indexes = ['!slug', 'owner'])
  21. // ---- 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
  22. // members (of the repo or of the organization) see and push; everybody else gets the answer for a name that does not exist.
  23. static isPrivateRepo = (r) => {
  24. if (r.priv == true) { return true }
  25. if (r.org != null && r.org != '') {
  26. o = orgBySlug(r.org)
  27. return o != null && o.priv == true
  28. }
  29. return false
  30. }
  31. static mayRead = (userId, r) => {
  32. if (!isPrivateRepo(r)) { return true }
  33. return isInside(userId, r)
  34. }
  35. // owner / organization owner / member of the repo or of its organization
  36. static isInside = (userId, r) => {
  37. if (userId == null) { return false }
  38. if (r.owner == userId) { return true }
  39. if (isMember(refOfRepo(r.slug), userId)) { return true }
  40. if (r.org != null && r.org != '') {
  41. o = orgBySlug(r.org)
  42. return o != null && (o.owner == userId || isMember(refOfOrg(r.org), userId))
  43. }
  44. return false
  45. }
  46. static repoBySlug = (slug) => {
  47. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  48. return firstOf(reposTable.find('slug', slug))
  49. }
  50. // 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
  51. static repoFor = (slug, userId) => {
  52. r = repoBySlug(slug)
  53. return r != null && mayRead(userId, r) ? r : null
  54. }
  55. // every repo the viewer may see, newest first
  56. static repoRows = (userId) => {
  57. out = []
  58. all = reposTable.find(null, null)
  59. if (countOfList(all) == 0) { return out }
  60. for (r of all) { if (mayRead(userId, r)) { out.push(rowOf(r, isPrivateRepo(r))) } }
  61. return newestFirst(out)
  62. }
  63. static repoRow = (slug, userId) => {
  64. r = repoFor(slug, userId)
  65. return r == null ? null : rowOf(r, isPrivateRepo(r))
  66. }
  67. // the repos of one organization the viewer may see, newest first
  68. static orgRepoRows = (org, userId) => {
  69. out = []
  70. for (x of repoRows(userId)) { if (x.org == org) { out.push(x) } }
  71. return out
  72. }
  73. // 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
  74. static repoAt = (slug, session) => {
  75. if (slug == null || slug == '' || fullNameError(slug) != null) { return null }
  76. return repoRow(slug, userIdOfSession(session))
  77. }
  78. // the organizations the viewer may see (a private one: its owner and members), newest first; each row is keyed by @id (the orgs table's id)
  79. static orgRows = (userId) => {
  80. out = []
  81. for (o of allOrgs()) {
  82. if (o.priv != true || o.owner == userId || isMember(refOfOrg(o.slug), userId)) { out.push({ @id = o.id slug = o.slug isPrivate = o.priv == true isPublic = o.priv != true createdMs = o.created address = repoOrigin(o.slug) + '/' }) }
  83. }
  84. return newestFirst(out)
  85. }
  86. // an organization for a viewer: the record or null (a private one is invisible to others)
  87. static orgFor = (slug, userId) => {
  88. o = orgBySlug(slug)
  89. if (o == null) { return null }
  90. if (o.priv != true || (userId != null && (o.owner == userId || isMember(refOfOrg(slug), userId)))) { return o }
  91. // 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)
  92. if (userId != null) {
  93. all = reposTable.find(null, null)
  94. if (countOfList(all) > 0) { for (r of all) { if (r.org == slug && mayRead(userId, r)) { return o } } }
  95. }
  96. return null
  97. }
  98. // does this name already stand for an organization or a top-level repo? (one namespace: both are subdomains)
  99. static nameTaken = (name) => { return orgBySlug(name) != null || repoBySlug(name) != null }
  100. // a new organization for a user: { org } or { error }
  101. static createOrg = (userId, slug, priv) => {
  102. u = userRecord(userId)
  103. if (u == null) { return { error = 'log in with ident (top right) first' } }
  104. if (u.name == '') { return { error = 'choose a display name first (top of the page)' } }
  105. s = slug == null ? '' : ('' + slug).trim()
  106. bad = slugError(s)
  107. if (bad != null) { return { error = bad } }
  108. if (nameTaken(s)) { return { error = "the address '" + s + "' is taken — organizations and repositories share their addresses" } }
  109. o = putOrg(s, u.id, priv == true)
  110. if (o == null) { return { error = 'could not store the organization' } }
  111. return { org = { id = o.id slug = o.slug isPrivate = o.priv == true createdMs = o.created } }
  112. }
  113. // may this user create a repo in the organization / push to its repos?
  114. static mayUseOrg = (userId, o) => {
  115. return userId != null && (o.owner == userId || isMember(refOfOrg(o.slug), userId))
  116. }
  117. // a new repo for a user: { repo } (the row) or { error, field }. `org` = '' or an organization the user belongs to; `priv` = private
  118. static createRepoIn = (userId, org, slug, description, priv, members = null) => {
  119. u = userRecord(userId)
  120. if (u == null) { return { error = 'log in with ident (top right) to create a repository' field = '' } }
  121. if (u.name == '') { return { error = 'choose a display name first (top of the page)' field = '' } }
  122. s = slug == null ? '' : ('' + slug).trim()
  123. og = org == null ? '' : ('' + org).trim()
  124. bad = slugError(s)
  125. if (bad != null) { return { error = bad field = 'slug' } }
  126. d = description == null ? '' : ('' + description).trim()
  127. dbad = plainError(d, 200, 'the description')
  128. if (dbad != null) { return { error = dbad field = 'description' } }
  129. let full = s
  130. if (og != '') {
  131. o = orgBySlug(og)
  132. if (o == null || !mayUseOrg(u.id, o)) { return { error = 'no such organization (or you are not in it)' field = 'org' status = 403 } }
  133. full = fullNameError(og + '/' + s) == null ? og + '/' + s : ''
  134. if (full == '') { return { error = fullNameError(og + '/' + s) field = 'slug' } }
  135. } else if (nameTaken(s)) {
  136. return { error = "the address '" + s + "' is taken — every repository address is unique in the whole system" field = 'slug' status = 409 }
  137. }
  138. 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' status = 409 } }
  139. // Resolve the entire member list before writing a repo row or initializing git.
  140. memberUsers = []
  141. if (members != null) {
  142. if (hlTypeName(members) != 'Hybrid' || members.length == null) { return { error = 'members must be a list' field = 'members' } }
  143. for (name of members) {
  144. uid = userForMember(name)
  145. if (uid == null) { return { error = 'every member must name exactly one known user or identity short id' field = 'members' } }
  146. if (!memberUsers.includes(uid)) { memberUsers.push(uid) }
  147. }
  148. }
  149. id = reposTable.put({ slug = full description = d owner = u.id created = now() org = og priv = priv == true })
  150. if (id == null) { return { error = 'could not store the repository (is the address taken?): ' + reposTable.lastError() field = 'slug' } }
  151. initRepo(full)
  152. for (uid of memberUsers) {
  153. if (!addMember(refOfRepo(full), uid)) { return { error = 'could not add the member' field = 'members' status = 500 } }
  154. }
  155. r = reposTable.fetch(id)
  156. row = rowOf(r, isPrivateRepo(r))
  157. emit client repoCreated(row)
  158. return { repo = row }
  159. }
  160. // the old call: a public top-level repo
  161. static createRepo = (userId, slug, description) => { return createRepoIn(userId, '', slug, description, false) }
  162. // ---- visibility and members (the owner of the repo / the organization) ----------------------------------------------------
  163. static setRepoPriv = (userId, slug, priv) => {
  164. r = repoFor(slug, userId)
  165. if (r == null) { return { error = 'no such repository' } }
  166. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  167. reposTable.update(r.id, withChange(r, { priv = priv == true }))
  168. after = reposTable.fetch(r.id)
  169. if (after == null || (after.priv == true) != (priv == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  170. return { repo = rowOf(after, isPrivateRepo(after)) }
  171. }
  172. static setOrgPrivate = (userId, org, priv) => {
  173. o = orgFor(org, userId)
  174. if (o == null) { return { error = 'no such organization' } }
  175. if (userId == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  176. if (!setOrgPriv(o, priv == true)) { return { error = 'could not store the setting' } }
  177. return { ok = true }
  178. }
  179. // the member list of a repo ('r') or an organization ('o'): [{ id, name }] for its owner
  180. static membersOf = (kind, slug, userId) => {
  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 see 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 see this' } }
  189. ref = refOfOrg(slug)
  190. }
  191. out = []
  192. for (uid of memberIds(ref)) { out.push({ id = uid name = nameOfUser(uid) }) }
  193. return { members = out }
  194. }
  195. // add (add = true) or remove a member by display name; the owner only
  196. static changeMember = (userId, kind, slug, name, add) => {
  197. let ref = ''
  198. if (kind == 'r') {
  199. r = repoFor(slug, userId)
  200. if (r == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  201. ref = refOfRepo(slug)
  202. } else {
  203. o = orgFor(slug, userId)
  204. if (o == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  205. ref = refOfOrg(slug)
  206. }
  207. n = name == null ? '' : ('' + name).trim()
  208. found = usersByName(n)
  209. if (found.length == 0) { return { error = "nobody is called '" + n + "' (people choose their display name at their first login)" } }
  210. if (found.length > 1) { return { error = "more than one person is called '" + n + "'" } }
  211. ok = add ? addMember(ref, found[0]) : removeMember(ref, found[0])
  212. if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }
  213. return membersOf(kind, slug, userId)
  214. }
  215. // does this (real) session's user own the repo?
  216. static ownsRepo = (slug, session) => {
  217. u = userOfSession(session)
  218. row = repoBySlug(slug)
  219. return u != null && row != null && row.owner == u.id
  220. }
  221. // MAY THIS USER PUSH? Only the repo's owner (a known user) — over HTTPS (transport.hl, the token's user) and over SSH
  222. // (lib/api.hl gitAccess, the key's user). Reading is everyone's.
  223. static mayPush = (userId, repo) => { return userRecord(userId) != null && isInside(userId, repo) }
  224. // the connection to a tickets project (gitoria#18); key '' = disconnected. The caller checked the owner.
  225. static setTicketsConnection = (slug, key, project, title, api) => {
  226. r = repoBySlug(slug)
  227. if (r == null) { return { error = 'no such repository' } }
  228. reposTable.update(r.id, withChange(r, { tktKey = key tktProject = project tktTitle = title tktApi = api }))
  229. after = reposTable.fetch(r.id)
  230. if (after == null || after.tktKey != key) { return { error = 'could not store the connection: ' + reposTable.lastError() } }
  231. return { repo = rowOf(after, isPrivateRepo(after)) }
  232. }
  233. // the owner's setting of the main branch (/code, the homepage). The caller checked the branch exists.
  234. static setMainBranch = (userId, slug, name) => {
  235. r = repoBySlug(slug)
  236. if (r == null) { return { error = 'no such repository' } }
  237. if (userId == null || r.owner != userId) { return { error = 'only the owner can change the main branch' } }
  238. reposTable.update(r.id, withChange(r, { branch = name }))
  239. after = reposTable.fetch(r.id)
  240. if (after == null || after.branch != name) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  241. syncProtect(after)
  242. return { repo = rowOf(after, isPrivateRepo(after)) }
  243. }
  244. // REFUSE DIRECT PUSHES TO THE MAIN BRANCH (gitoria#50): the owner's switch. The server enforces it where a push is accepted: docker/sshd/hooks/pre-receive
  245. // (core.hooksPath for HTTPS in transport.hl and for ssh in gitoria-shell) reads <repo>.git/gitoria-protect = the branch name (the PR target setting, else `main`).
  246. // No file = off. Merges done by gitoria itself (update-ref) are not pushes. Written here when the switch or the main branch changes.
  247. static syncProtect = (r) => {
  248. f = repoDir(r.slug) + '/gitoria-protect'
  249. if (r.protect == true) { writeFile(f, (r.branch != null && r.branch != '' ? r.branch : 'main') + NL, 420) }
  250. else if (exists(f)) { remove(f) }
  251. return null
  252. }
  253. static isProtectedMain = (slug) => {
  254. r = repoBySlug(slug)
  255. return r != null && r.protect == true
  256. }
  257. static setProtectMain = (userId, slug, wanted) => {
  258. r = repoBySlug(slug)
  259. if (r == null) { return { error = 'no such repository' } }
  260. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  261. reposTable.update(r.id, withChange(r, { protect = wanted == true }))
  262. after = reposTable.fetch(r.id)
  263. if (after == null || (after.protect == true) != (wanted == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  264. syncProtect(after)
  265. return { protect = after.protect == true }
  266. }
  267. // 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)
  268. static titleSlug = (host, orepo, session) => {
  269. s = slugOfRequest(host, orepo)
  270. if (repoAt(s, session) != null) { return s }
  271. return orepo != null && orepo != '' ? orepo : slugOfHost(host)
  272. }
  273. // The API input check; the creation rules and writes remain in createRepoIn.
  274. static createRepoFromInput = (userId, input) => {
  275. if (input == null || hlTypeName(input) != 'Hybrid' || hlTypeName(input.name) != 'String') { return { error = 'name must be a repository slug' field = 'name' } }
  276. if (input.org != null && hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }
  277. if (input.description != null && hlTypeName(input.description) != 'String') { return { error = 'description must be text' field = 'description' } }
  278. if (input.private != null && input.private != true && input.private != false) { return { error = 'private must be true or false' field = 'private' } }
  279. return createRepoIn(userId, input.org, input.name, input.description, input.private, input.members)
  280. }

Branches

Latest commits

  • 5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre
  • 9eff6627Merge branch 'f2-api-bugs' into mainGitoria
  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre