gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit03b5bb0803b5bb08W39 report to reports/, drop scratch repro, mission + logmre03b5bb08/lib/repos.hl

13.8 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, 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 } 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, members = null) => {
  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' status = 403 } }
  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' status = 409 }
  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' status = 409 } }
  138. // Resolve the entire member list before writing a repo row or initializing git.
  139. memberUsers = []
  140. if (members != null) {
  141. if (hlTypeName(members) != 'Hybrid' || members.length == null) { return { error = 'members must be a list' field = 'members' } }
  142. for (name of members) {
  143. uid = userForMember(name)
  144. if (uid == null) { return { error = 'every member must name exactly one known user or identity short id' field = 'members' } }
  145. if (!memberUsers.includes(uid)) { memberUsers.push(uid) }
  146. }
  147. }
  148. id = reposTable.put({ slug = full description = d owner = u.id created = now() org = og priv = priv == true })
  149. if (id == null) { return { error = 'could not store the repository (is the address taken?): ' + reposTable.lastError() field = 'slug' } }
  150. initRepo(full)
  151. for (uid of memberUsers) {
  152. if (!addMember(refOfRepo(full), uid)) { return { error = 'could not add the member' field = 'members' status = 500 } }
  153. }
  154. r = reposTable.fetch(id)
  155. row = rowOf(r, isPrivateRepo(r))
  156. emit client repoCreated(row)
  157. return { repo = row }
  158. }
  159. // the old call: a public top-level repo
  160. static createRepo = (userId, slug, description) => { return createRepoIn(userId, '', slug, description, false) }
  161. // ---- visibility and members (the owner of the repo / the organization) ----------------------------------------------------
  162. static setRepoPriv = (userId, slug, priv) => {
  163. r = repoFor(slug, userId)
  164. if (r == null) { return { error = 'no such repository' } }
  165. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  166. reposTable.update(r.id, withChange(r, { priv = priv == true }))
  167. after = reposTable.fetch(r.id)
  168. if (after == null || (after.priv == true) != (priv == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  169. return { repo = rowOf(after, isPrivateRepo(after)) }
  170. }
  171. static setOrgPrivate = (userId, org, priv) => {
  172. o = orgFor(org, userId)
  173. if (o == null) { return { error = 'no such organization' } }
  174. if (userId == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  175. if (!setOrgPriv(o, priv == true)) { return { error = 'could not store the setting' } }
  176. return { ok = true }
  177. }
  178. // the member list of a repo ('r') or an organization ('o'): [{ id, name }] for its owner
  179. static membersOf = (kind, slug, userId) => {
  180. let ref = ''
  181. if (kind == 'r') {
  182. r = repoFor(slug, userId)
  183. if (r == null || r.owner != userId) { return { error = 'only the owner can see this' } }
  184. ref = refOfRepo(slug)
  185. } else {
  186. o = orgFor(slug, userId)
  187. if (o == null || o.owner != userId) { return { error = 'only the owner can see this' } }
  188. ref = refOfOrg(slug)
  189. }
  190. out = []
  191. for (uid of memberIds(ref)) { out.push({ id = uid name = nameOfUser(uid) }) }
  192. return { members = out }
  193. }
  194. // add (add = true) or remove a member by display name; the owner only
  195. static changeMember = (userId, kind, slug, name, add) => {
  196. let ref = ''
  197. if (kind == 'r') {
  198. r = repoFor(slug, userId)
  199. if (r == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  200. ref = refOfRepo(slug)
  201. } else {
  202. o = orgFor(slug, userId)
  203. if (o == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  204. ref = refOfOrg(slug)
  205. }
  206. n = name == null ? '' : ('' + name).trim()
  207. found = usersByName(n)
  208. if (found.length == 0) { return { error = "nobody is called '" + n + "' (people choose their display name at their first login)" } }
  209. if (found.length > 1) { return { error = "more than one person is called '" + n + "'" } }
  210. ok = add ? addMember(ref, found[0]) : removeMember(ref, found[0])
  211. if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }
  212. return membersOf(kind, slug, userId)
  213. }
  214. // does this (real) session's user own the repo?
  215. static ownsRepo = (slug, session) => {
  216. u = userOfSession(session)
  217. row = repoBySlug(slug)
  218. return u != null && row != null && row.owner == u.id
  219. }
  220. // MAY THIS USER PUSH? Only the repo's owner (a known user) — over HTTPS (transport.hl, the token's user) and over SSH
  221. // (lib/api.hl gitAccess, the key's user). Reading is everyone's.
  222. static mayPush = (userId, repo) => { return userRecord(userId) != null && isInside(userId, repo) }
  223. // the connection to a tickets project (gitoria#18); key '' = disconnected. The caller checked the owner.
  224. static setTicketsConnection = (slug, key, project, title, api) => {
  225. r = repoBySlug(slug)
  226. if (r == null) { return { error = 'no such repository' } }
  227. reposTable.update(r.id, withChange(r, { tktKey = key tktProject = project tktTitle = title tktApi = api }))
  228. after = reposTable.fetch(r.id)
  229. if (after == null || after.tktKey != key) { return { error = 'could not store the connection: ' + reposTable.lastError() } }
  230. return { repo = rowOf(after, isPrivateRepo(after)) }
  231. }
  232. // the owner's setting of the main branch (/code, the homepage). The caller checked the branch exists.
  233. static setMainBranch = (userId, slug, name) => {
  234. r = repoBySlug(slug)
  235. if (r == null) { return { error = 'no such repository' } }
  236. if (userId == null || r.owner != userId) { return { error = 'only the owner can change the main branch' } }
  237. reposTable.update(r.id, withChange(r, { branch = name }))
  238. after = reposTable.fetch(r.id)
  239. if (after == null || after.branch != name) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  240. return { repo = rowOf(after, isPrivateRepo(after)) }
  241. }
  242. // 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)
  243. static titleSlug = (host, orepo, session) => {
  244. s = slugOfRequest(host, orepo)
  245. if (repoAt(s, session) != null) { return s }
  246. return orepo != null && orepo != '' ? orepo : slugOfHost(host)
  247. }
  248. // The API input check; the creation rules and writes remain in createRepoIn.
  249. static createRepoFromInput = (userId, input) => {
  250. if (input == null || hlTypeName(input) != 'Hybrid' || hlTypeName(input.name) != 'String') { return { error = 'name must be a repository slug' field = 'name' } }
  251. if (input.org != null && hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }
  252. if (input.description != null && hlTypeName(input.description) != 'String') { return { error = 'description must be text' field = 'description' } }
  253. if (input.private != null && input.private != true && input.private != false) { return { error = 'private must be true or false' field = 'private' } }
  254. return createRepoIn(userId, input.org, input.name, input.description, input.private, input.members)
  255. }

Branches

Latest commits

  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre
  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre