gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6e03be0a6e03be0apull state: keep the resync marker until the sync ran (#56)mre6e03be0a/lib/repos.hl

29.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, title (optional display name), 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 { resumePullResync } from './pullsync.hl'
  9. import { MPackDB } from 'hl:mpackdb'
  10. import { run } from 'hl:proc'
  11. import { now } from 'hl:time'
  12. import { NL, storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'
  13. import { userOfSession, userRecord, userIdOfSession, nameOfUser, resolveDisplayName, userForMember } from './users.hl'
  14. import { repoOrigin, slugOfHost } from './util.hl'
  15. import { slugOfRequest } from './orgs.hl'
  16. import { slugError, fullNameError, rowOf, withChange, addressFromName, orgRowOf, orgChoice } from './repos-helpers.hl'
  17. import { initRepo, repoDir, gitRoot } from './git.hl'
  18. import { writeFile, remove, exists, mkDir } from 'hl:fs'
  19. import { orgBySlug, putOrg, setOrgPriv, allOrgs, saveOrgTitle, saveOrgOwner, orgsOwnedBy } from './orgs.hl'
  20. import { refOfRepo, refOfOrg, isMember, memberIds, addMember, removeMember, moveMembers, organizationRefsOfUser } from './members.hl'
  21. import { randomBytes } from 'hl:crypto'
  22. import { forwardOf, movedForwards, namesForwardingTo } from './forwards.hl'
  23. import { moveTicketLinks } from './tickets.hl'
  24. static reposTable = new MPackDB(file = storageDir + '/repos.db', primaryKey = '@id', indexes = ['!slug', 'owner'])
  25. // ---- 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
  26. // members (of the repo or of the organization) see and push; everybody else gets the answer for a name that does not exist.
  27. static isPrivateRepo = (r) => {
  28. if (r.priv == true) { return true }
  29. if (r.org != null && r.org != '') {
  30. o = orgBySlug(r.org)
  31. return o != null && o.priv == true
  32. }
  33. return false
  34. }
  35. static mayRead = (userId, r) => {
  36. if (!isPrivateRepo(r)) { return true }
  37. return isInside(userId, r)
  38. }
  39. // owner / organization owner / member of the repo or of its organization
  40. static isInside = (userId, r) => {
  41. if (userId == null) { return false }
  42. if (r.owner == userId) { return true }
  43. if (isMember(refOfRepo(r.slug), userId)) { return true }
  44. if (r.org != null && r.org != '') {
  45. o = orgBySlug(r.org)
  46. return o != null && (o.owner == userId || isMember(refOfOrg(r.org), userId))
  47. }
  48. return false
  49. }
  50. // WHO HEARS THAT A REPO LEFT ITS ORGANIZATION: its readers as they were BEFORE that move, kept server-side under a random id PER MOVE and never changed
  51. // afterwards (a replayed event always meets the readers of ITS move). The event carries only (org, id, moveId); the audience decides with this record.
  52. // The newest maxLeaving moves are kept.
  53. static leaving = {}
  54. static leavingOrder = []
  55. static maxLeaving = 500
  56. static readersBefore = (Hybrid r) => {
  57. ids = [r.owner]
  58. for (u of memberIds(refOfRepo(r.slug))) { ids.push(u) }
  59. if (r.org != null && r.org != '') {
  60. o = orgBySlug(r.org)
  61. if (o != null) {
  62. ids.push(o.owner)
  63. for (u of memberIds(refOfOrg(r.org))) { ids.push(u) }
  64. }
  65. }
  66. return { open = !isPrivateRepo(r) ids = ids }
  67. }
  68. static keepLeaving = (Hybrid before) => {
  69. moveId = randomBytes(12, 'hex')
  70. let order = leavingOrder.slice(0)
  71. if (order.length >= maxLeaving) {
  72. delete leaving[order[0]]
  73. order = order.slice(1)
  74. }
  75. leaving[moveId] = before
  76. order.push(moveId)
  77. leavingOrder = order
  78. return moveId
  79. }
  80. static hearsRepoLeft = (String moveId, String userId) => {
  81. s = leaving[moveId]
  82. if (s == null) { return false }
  83. return s.open == true || (userId != null && s.ids.includes(userId))
  84. }
  85. static repoBySlug = (slug) => {
  86. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  87. return firstOf(reposTable.find('slug', slug))
  88. }
  89. // 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
  90. static repoFor = (slug, userId) => {
  91. r = repoBySlug(slug)
  92. return r != null && mayRead(userId, r) ? r : null
  93. }
  94. // every repo the viewer may see, newest first
  95. static repoRows = (userId) => {
  96. out = []
  97. all = reposTable.find(null, null)
  98. if (countOfList(all) == 0) { return out }
  99. for (r of all) { if (mayRead(userId, r)) { out.push(rowOf(r, isPrivateRepo(r))) } }
  100. return newestFirst(out)
  101. }
  102. static repoRow = (slug, userId) => {
  103. r = repoFor(slug, userId)
  104. return r == null ? null : rowOf(r, isPrivateRepo(r))
  105. }
  106. // the repos of one organization the viewer may see, newest first
  107. static orgRepoRows = (org, userId) => {
  108. out = []
  109. for (x of repoRows(userId)) { if (x.org == org) { out.push(x) } }
  110. return out
  111. }
  112. // 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
  113. static repoAt = (slug, session) => {
  114. if (slug == null || slug == '' || fullNameError(slug) != null) { return null }
  115. row = repoRow(slug, userIdOfSession(session))
  116. if (row != null) { resumePullResync(slug) }
  117. return row
  118. }
  119. // 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)
  120. static orgRows = (userId) => {
  121. out = []
  122. for (o of allOrgs()) {
  123. if (o.priv != true || o.owner == userId || isMember(refOfOrg(o.slug), userId)) { out.push(orgRowOf(o)) }
  124. }
  125. return newestFirst(out)
  126. }
  127. // an organization for a viewer: the record or null (a private one is invisible to others)
  128. static orgFor = (slug, userId) => {
  129. o = orgBySlug(slug)
  130. if (o == null) { return null }
  131. if (o.priv != true || (userId != null && (o.owner == userId || isMember(refOfOrg(slug), userId)))) { return o }
  132. // 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)
  133. if (userId != null) {
  134. all = reposTable.find(null, null)
  135. if (countOfList(all) > 0) { for (r of all) { if (r.org == slug && mayRead(userId, r)) { return o } } }
  136. }
  137. return null
  138. }
  139. // does this name already stand for an organization or a top-level repo? (one namespace: both are subdomains)
  140. static nameTaken = (String name) => { return orgBySlug(name) != null || repoBySlug(name) != null || forwardOf(name) != null }
  141. // a new organization for a user: { org } or { error }
  142. static createOrg = (String userId, String slug, Boolean priv, String title = '') => {
  143. u = userRecord(userId)
  144. if (u == null) { return { error = 'log in with ident (top right) first' } }
  145. if (u.name == '') { return { error = 'choose a display name first (top of the page)' } }
  146. t = title == null ? '' : ('' + title).trim()
  147. tbad = plainError(t, 80, 'the name')
  148. if (tbad != null) { return { error = tbad } }
  149. address = slug == null ? '' : ('' + slug).trim()
  150. s = address == '' ? addressFromName(t) : address
  151. bad = slugError(s)
  152. if (bad != null) { return { error = bad } }
  153. if (nameTaken(s)) { return { error = "the address '" + s + "' is taken — organizations and repositories share their addresses" status = 409 } }
  154. o = putOrg(s, u.id, priv == true, t)
  155. if (o == null) { return { error = 'could not store the organization' } }
  156. row = orgRowOf(o)
  157. emit client orgChanged(row)
  158. return { org = row }
  159. }
  160. // may this user create a repo in the organization / push to its repos?
  161. static mayUseOrg = (userId, o) => {
  162. return userId != null && (o.owner == userId || isMember(refOfOrg(o.slug), userId))
  163. }
  164. // a new repo for a user: { repo } (the row) or { error, field }. `org` = '' or an organization the user belongs to; `priv` = private
  165. static createRepoIn = (String userId, String org, String slug, String description, Boolean priv, Hybrid members = null, String title = '') => {
  166. u = userRecord(userId)
  167. if (u == null) { return { error = 'log in with ident (top right) to create a repository' field = '' } }
  168. if (u.name == '') { return { error = 'choose a display name first (top of the page)' field = '' } }
  169. t = title == null ? '' : ('' + title).trim()
  170. tbad = plainError(t, 80, 'the name')
  171. if (tbad != null) { return { error = tbad field = 'title' } }
  172. address = slug == null ? '' : ('' + slug).trim()
  173. s = address == '' ? addressFromName(t) : address
  174. og = org == null ? '' : ('' + org).trim()
  175. bad = slugError(s)
  176. if (bad != null) { return { error = bad field = 'slug' } }
  177. d = description == null ? '' : ('' + description).trim()
  178. dbad = plainError(d, 200, 'the description')
  179. if (dbad != null) { return { error = dbad field = 'description' } }
  180. let full = s
  181. if (og != '') {
  182. o = orgBySlug(og)
  183. if (o == null || !mayUseOrg(u.id, o)) { return { error = 'no such organization (or you are not in it)' field = 'org' status = 403 } }
  184. full = fullNameError(og + '/' + s) == null ? og + '/' + s : ''
  185. if (full == '') { return { error = fullNameError(og + '/' + s) field = 'slug' } }
  186. } else if (nameTaken(s)) {
  187. return { error = "the address '" + s + "' is taken — every repository address is unique in the whole system" field = 'slug' status = 409 }
  188. }
  189. 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 } }
  190. // Resolve the entire member list before writing a repo row or initializing git.
  191. memberUsers = []
  192. if (members != null) {
  193. if (hlTypeName(members) != 'Hybrid' || members.length == null) { return { error = 'members must be a list' field = 'members' } }
  194. for (name of members) {
  195. uid = userForMember(name)
  196. if (uid == null) { return { error = 'every member must name exactly one known user or identity short id' field = 'members' } }
  197. if (!memberUsers.includes(uid)) { memberUsers.push(uid) }
  198. }
  199. }
  200. id = reposTable.put({ slug = full title = t description = d owner = u.id created = now() org = og priv = priv == true })
  201. if (id == null) { return { error = 'could not store the repository (is the address taken?): ' + reposTable.lastError() field = 'slug' } }
  202. initRepo(full)
  203. for (uid of memberUsers) {
  204. if (!addMember(refOfRepo(full), uid)) { return { error = 'could not add the member' field = 'members' status = 500 } }
  205. }
  206. r = reposTable.fetch(id)
  207. row = rowOf(r, isPrivateRepo(r))
  208. emit client repoCreated(row)
  209. return { repo = row }
  210. }
  211. // the old call: a public top-level repo
  212. static createRepo = (userId, slug, description) => { return createRepoIn(userId, '', slug, description, false) }
  213. // ---- visibility and members (the owner of the repo / the organization) ----------------------------------------------------
  214. static setRepoPriv = (userId, slug, priv) => {
  215. r = repoFor(slug, userId)
  216. if (r == null) { return { error = 'no such repository' } }
  217. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  218. reposTable.update(r.id, withChange(r, { priv = priv == true }))
  219. after = reposTable.fetch(r.id)
  220. if (after == null || (after.priv == true) != (priv == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  221. return { repo = rowOf(after, isPrivateRepo(after)) }
  222. }
  223. static setOrgPrivate = (userId, org, priv) => {
  224. o = orgFor(org, userId)
  225. if (o == null) { return { error = 'no such organization' } }
  226. if (userId == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  227. if (!setOrgPriv(o, priv == true)) { return { error = 'could not store the setting' } }
  228. return { ok = true }
  229. }
  230. // the member list of a repo ('r') or an organization ('o'): [{ id, name }] for its owner
  231. static membersOf = (kind, slug, userId) => {
  232. let ref = ''
  233. if (kind == 'r') {
  234. r = repoFor(slug, userId)
  235. if (r == null || r.owner != userId) { return { error = 'only the owner can see this' } }
  236. ref = refOfRepo(slug)
  237. } else {
  238. o = orgFor(slug, userId)
  239. if (o == null || o.owner != userId) { return { error = 'only the owner can see this' } }
  240. ref = refOfOrg(slug)
  241. }
  242. out = []
  243. for (uid of memberIds(ref)) { out.push({ id = uid name = nameOfUser(uid) }) }
  244. return { members = out }
  245. }
  246. // add (add = true) or remove a member by display name; the owner only
  247. static changeMember = (userId, kind, slug, name, add) => {
  248. let ref = ''
  249. if (kind == 'r') {
  250. r = repoFor(slug, userId)
  251. if (r == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  252. ref = refOfRepo(slug)
  253. } else {
  254. o = orgFor(slug, userId)
  255. if (o == null || o.owner != userId) { return { error = 'only the owner can change this' } }
  256. ref = refOfOrg(slug)
  257. }
  258. found = resolveDisplayName(name)
  259. if (found.error != null) { return found }
  260. ok = add ? addMember(ref, found.userId) : removeMember(ref, found.userId)
  261. if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }
  262. emit client membersChanged(kind, slug, found.userId)
  263. return membersOf(kind, slug, userId)
  264. }
  265. // Ownership changes leave the address, organization and attached records intact.
  266. static transferRepo = (String userId, String slug, String name) => {
  267. r = repoFor(slug, userId)
  268. if (r == null) { return { error = 'no such repository' } }
  269. if (userId == null || r.owner != userId) { return { error = 'only the owner can transfer this repository' } }
  270. found = resolveDisplayName(name)
  271. if (found.error != null) { return found }
  272. if (found.userId == userId) { return { error = 'you already own this repository' } }
  273. reposTable.update(r.id, withChange(r, { owner = found.userId }))
  274. after = reposTable.fetch(r.id)
  275. if (after == null || after.owner != found.userId) { return { error = 'could not transfer the repository' } }
  276. if (isMember(refOfRepo(slug), found.userId)) { removeMember(refOfRepo(slug), found.userId) }
  277. row = rowOf(after, isPrivateRepo(after))
  278. emit client repoChanged(row)
  279. emit client membersChanged('r', slug, userId)
  280. emit client membersChanged('r', slug, found.userId)
  281. return { repo = row }
  282. }
  283. static confirmedTransfer = (String userId, String slug, String name, String confirm) => {
  284. r = repoFor(slug, userId)
  285. if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can transfer this repository' } }
  286. if (confirm != slug) { return { error = 'type the repository name to confirm' field = 'confirm' } }
  287. return transferRepo(userId, slug, name)
  288. }
  289. // The members API lists names for a reader with push rights, including organization membership.
  290. static repoMemberNames = (String userId, String slug) => {
  291. r = repoFor(slug, userId)
  292. if (r == null || !isInside(userId, r)) { return { error = 'only the owner or a member can see this' } }
  293. names = []
  294. for (uid of memberIds(refOfRepo(slug))) { names.push(nameOfUser(uid)) }
  295. return { members = names }
  296. }
  297. // does this (real) session's user own the repo?
  298. static ownsRepo = (slug, session) => {
  299. u = userOfSession(session)
  300. row = repoBySlug(slug)
  301. return u != null && row != null && row.owner == u.id
  302. }
  303. // MAY THIS USER PUSH? Only the repo's owner (a known user) — over HTTPS (transport.hl, the token's user) and over SSH
  304. // (lib/api.hl gitAccess, the key's user). Reading is everyone's.
  305. static mayPush = (userId, repo) => { return userRecord(userId) != null && isInside(userId, repo) }
  306. // the connection to a tickets project (gitoria#18); key '' = disconnected. The caller checked the owner.
  307. static setTicketsConnection = (slug, key, project, title, api) => {
  308. r = repoBySlug(slug)
  309. if (r == null) { return { error = 'no such repository' } }
  310. reposTable.update(r.id, withChange(r, { tktKey = key tktProject = project tktTitle = title tktApi = api }))
  311. after = reposTable.fetch(r.id)
  312. if (after == null || after.tktKey != key) { return { error = 'could not store the connection: ' + reposTable.lastError() } }
  313. return { repo = rowOf(after, isPrivateRepo(after)) }
  314. }
  315. // the owner's setting of the main branch (/code, the homepage). The caller checked the branch exists.
  316. static setMainBranch = (userId, slug, name) => {
  317. r = repoBySlug(slug)
  318. if (r == null) { return { error = 'no such repository' } }
  319. if (userId == null || r.owner != userId) { return { error = 'only the owner can change the main branch' } }
  320. reposTable.update(r.id, withChange(r, { branch = name }))
  321. after = reposTable.fetch(r.id)
  322. if (after == null || after.branch != name) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  323. syncProtect(after)
  324. return { repo = rowOf(after, isPrivateRepo(after)) }
  325. }
  326. // 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
  327. // (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`).
  328. // No file = off. Merges done by gitoria itself (update-ref) are not pushes. Written here when the switch or the main branch changes.
  329. static syncProtect = (r) => {
  330. f = repoDir(r.slug) + '/gitoria-protect'
  331. if (r.protect == true) { writeFile(f, (r.branch != null && r.branch != '' ? r.branch : 'main') + NL, 420) }
  332. else if (exists(f)) { remove(f) }
  333. return null
  334. }
  335. static isProtectedMain = (slug) => {
  336. r = repoBySlug(slug)
  337. return r != null && r.protect == true
  338. }
  339. static setProtectMain = (userId, slug, wanted) => {
  340. r = repoBySlug(slug)
  341. if (r == null) { return { error = 'no such repository' } }
  342. if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }
  343. reposTable.update(r.id, withChange(r, { protect = wanted == true }))
  344. after = reposTable.fetch(r.id)
  345. if (after == null || (after.protect == true) != (wanted == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }
  346. syncProtect(after)
  347. return { protect = after.protect == true }
  348. }
  349. // 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)
  350. static titleSlug = (host, orepo, session) => {
  351. s = slugOfRequest(host, orepo)
  352. r = repoAt(s, session)
  353. if (r != null) { return r.heading }
  354. o = orgFor(s, userIdOfSession(session))
  355. if ((orepo == null || orepo == '') && o != null && o.title != null && o.title != '') { return o.title }
  356. return orepo != null && orepo != '' ? orepo : slugOfHost(host)
  357. }
  358. // The API input check; the creation rules and writes remain in createRepoIn.
  359. static createRepoFromInput = (userId, input) => {
  360. if (input == null || hlTypeName(input) != 'Hybrid' || (hlTypeName(input.name) != 'String' && !(input.name == null && hlTypeName(input.title) == 'String'))) { return { error = 'name must be a repository slug' field = 'name' } }
  361. if (input.title != null && hlTypeName(input.title) != 'String') { return { error = 'title must be text' field = 'title' } }
  362. if (input.org != null && hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }
  363. if (input.description != null && hlTypeName(input.description) != 'String') { return { error = 'description must be text' field = 'description' } }
  364. if (input.private != null && input.private != true && input.private != false) { return { error = 'private must be true or false' field = 'private' } }
  365. if (input.members != null && hlTypeName(input.members) != 'Hybrid') { return { error = 'members must be a list' field = 'members' } }
  366. return createRepoIn(userId, input.org, input.name, input.description, input.private, input.members, input.title)
  367. }
  368. // One move implementation for the stopped-app transfer tool and live Settings.
  369. // Git locks are refused; relative aliases preserve paths held by in-flight Git processes.
  370. static moveRepo = (String oldName, String to, String vis) => {
  371. r = repoBySlug(oldName)
  372. if (r == null) { return { error = 'no such repository' } }
  373. bad = fullNameError(to)
  374. if (bad != null) { return { error = bad } }
  375. before = readersBefore(r)
  376. if (to == oldName) { return { error = 'the new name is the old name' } }
  377. if (vis != 'public' && vis != 'private') { return { error = 'visibility must be public or private' } }
  378. if (repoBySlug(to) != null || (forwardOf(to) != null && forwardOf(to) != oldName) || (!to.includes('/') && orgBySlug(to) != null)) { return { error = 'the address is taken' } }
  379. if (!exists(repoDir(oldName))) { return { error = 'the bare repository does not exist' } }
  380. aliases = namesForwardingTo(oldName)
  381. destinationLink = run(['test', '-L', repoDir(to)], { timeout = 15 })
  382. backToAlias = aliases.includes(to) && destinationLink != null && destinationLink.exit == 0
  383. if ((exists(repoDir(to)) || (destinationLink != null && destinationLink.exit == 0)) && !backToAlias) { return { error = 'the destination folder already exists' } }
  384. if (backToAlias) {
  385. resolved = run(['readlink', '-f', repoDir(to)], { timeout = 15 })
  386. current = run(['readlink', '-f', repoDir(oldName)], { timeout = 15 })
  387. if (resolved == null || current == null || resolved.exit != 0 || current.exit != 0 || resolved.lines.join('') != current.lines.join('')) { return { error = 'the destination alias does not point to this repository' } }
  388. }
  389. locks = run(['find', repoDir(oldName), '-name', '*.lock', '-o', '-name', 'incoming-*'], { timeout = 15 })
  390. if (locks == null || locks.exit != 0 || locks.lines.length > 0) { return { error = 'repository is busy; try again after the push finishes' } }
  391. i = to.indexOf('/')
  392. org = i < 0 ? '' : to.slice(0, i)
  393. priv = vis == 'private'
  394. if (org != '' && orgBySlug(org) == null) {
  395. if (putOrg(org, r.owner, priv) == null) { return { error = 'could not create the organization' } }
  396. }
  397. if (org != '' && !exists(gitRoot + '/' + org)) { mkDir(gitRoot + '/' + org, 493) }
  398. if (backToAlias) { run(['rm', '--', repoDir(to)], { timeout = 15 }) }
  399. m = run(['mv', '-T', '-n', repoDir(oldName), repoDir(to)], { timeout = 60 })
  400. if (m == null || m.exit != 0 || exists(repoDir(oldName)) || !exists(repoDir(to))) { return { error = 'could not move the folder (the destination may have been taken)' } }
  401. linked = run(['ln', '-sT', (oldName.includes('/') ? '../' : '') + to + '.git', repoDir(oldName)], { timeout = 15 })
  402. if (linked == null || linked.exit != 0) {
  403. run(['mv', '-T', '-n', repoDir(to), repoDir(oldName)], { timeout = 60 })
  404. if (backToAlias) { run(['ln', '-sT', (to.includes('/') ? '../' : '') + oldName + '.git', repoDir(to)], { timeout = 15 }) }
  405. return { error = 'could not preserve the old Git path; the folder move was rolled back' }
  406. }
  407. reposTable.update(r.id, withChange(r, { slug = to org = org priv = priv }))
  408. after = reposTable.fetch(r.id)
  409. if (after == null || after.slug != to) {
  410. run(['rm', '--', repoDir(oldName)], { timeout = 15 })
  411. back = run(['mv', '-T', '-n', repoDir(to), repoDir(oldName)], { timeout = 60 })
  412. if (backToAlias) { run(['ln', '-sT', (to.includes('/') ? '../' : '') + oldName + '.git', repoDir(to)], { timeout = 15 }) }
  413. return { error = back != null && back.exit == 0 && exists(repoDir(oldName)) && (!exists(repoDir(to)) || backToAlias) ? 'could not store the new name; the folder was restored' : 'could not store the new name or restore the folder; contact the administrator' }
  414. }
  415. for (alias of aliases) {
  416. if (alias != to) {
  417. link = run(['ln', '-sfnT', (alias.includes('/') ? '../' : '') + to + '.git', repoDir(alias)], { timeout = 15 })
  418. if (link == null || link.exit != 0) { return { error = 'repository moved, but an old Git path could not be updated' } }
  419. }
  420. }
  421. if (!moveTicketLinks(oldName, to)) { return { error = 'repository moved, but ticket notification history could not be updated' } }
  422. moveMembers(refOfRepo(oldName), refOfRepo(to))
  423. if (!movedForwards(oldName, to)) { return { error = 'repository moved, but the old address could not be stored' } }
  424. row = rowOf(after, isPrivateRepo(after))
  425. emit client repoChanged(row)
  426. // open lists of the organization it left drop it by id
  427. leftOrg = r.org != null ? r.org : ''
  428. if (leftOrg != '' && leftOrg != org) {
  429. emit client repoLeftOrg(leftOrg, r.id, keepLeaving(before))
  430. }
  431. return { repo = row }
  432. }
  433. // Settings → Rename: the address label and the organization (none, or one the user belongs to). Never creates an organization.
  434. static renameRepo = (String userId, String slug, String name, String org) => {
  435. r = repoBySlug(slug)
  436. if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can rename this repository' } }
  437. bad = slugError(name)
  438. if (bad != null) { return { error = bad } }
  439. if (org != '' && org != (r.org != null ? r.org : '')) {
  440. o = orgBySlug(org)
  441. if (o == null || !mayUseOrg(userId, o)) { return { error = 'you can only move a repository into an organization you belong to' status = 403 field = 'org' } }
  442. }
  443. to = org != '' ? org + '/' + name : name
  444. if (to == slug) { return { error = 'the address is the same; change the name or the organization' } }
  445. oldOrg = r.org != null ? r.org : ''
  446. // A repository that was effectively private (own flag or private organization) never becomes public by leaving or changing its organization.
  447. vis = r.priv == true || (oldOrg != '' && oldOrg != org && isPrivateRepo(r)) ? 'private' : 'public'
  448. return moveRepo(slug, to, vis)
  449. }
  450. static setRepoTitle = (String userId, String slug, String title) => {
  451. r = repoBySlug(slug)
  452. if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can change the display name' } }
  453. t = title == null ? '' : title.trim()
  454. bad = plainError(t, 80, 'the display name')
  455. if (bad != null) { return { error = bad } }
  456. reposTable.update(r.id, withChange(r, { title = t }))
  457. after = reposTable.fetch(r.id)
  458. if (after == null || after.title != t) { return { error = 'could not store the display name' } }
  459. row = rowOf(after, isPrivateRepo(after))
  460. emit client repoChanged(row)
  461. return { repo = row }
  462. }
  463. static setOrgTitle = (String userId, String slug, String title) => {
  464. o = orgBySlug(slug)
  465. if (o == null || userId == null || o.owner != userId) { return { error = 'only the owner can change the organization name' } }
  466. t = title == null ? '' : title.trim()
  467. bad = plainError(t, 80, 'the name')
  468. if (bad != null) { return { error = bad } }
  469. after = saveOrgTitle(o, t)
  470. if (after == null) { return { error = 'could not store the name' } }
  471. row = orgRowOf(after)
  472. emit client orgChanged(row)
  473. return { org = row }
  474. }
  475. // Available destinations use the same membership rule as repository creation.
  476. static orgChoices = (String userId) => {
  477. out = orgsOwnedBy(userId)
  478. seen = []
  479. for (o of out) { seen.push(o.slug) }
  480. for (ref of organizationRefsOfUser(userId)) {
  481. o = orgBySlug(ref.slice(2))
  482. if (o != null && !seen.includes(o.slug)) { out.push(orgChoice(o)) seen.push(o.slug) }
  483. }
  484. return out
  485. }
  486. // Only the affected user and the current list owner receive membership metadata.
  487. static hearsMembers = (String kind, String slug, String changedUser, String viewer) => {
  488. if (viewer == null) { return false }
  489. if (viewer == changedUser) { return true }
  490. if (kind == 'r') { r = repoBySlug(slug) return r != null && r.owner == viewer }
  491. o = orgBySlug(slug)
  492. return o != null && o.owner == viewer
  493. }
  494. static createOrgFromInput = (String userId, Hybrid input) => {
  495. if (hlTypeName(input.name) != 'String') { return { error = 'name must be an organization slug' field = 'name' } }
  496. if (input.title != null && hlTypeName(input.title) != 'String') { return { error = 'title must be text' field = 'title' } }
  497. if (input.private != null && hlTypeName(input.private) != 'Boolean') { return { error = 'private must be true or false' field = 'private' } }
  498. return createOrg(userId, input.name, input.private == true, input.title)
  499. }
  500. static orgMemberNames = (String userId, String org) => {
  501. r = membersOf('o', org, userId)
  502. if (r.error != null) { return r }
  503. names = []
  504. for (m of r.members) { names.push(m.name) }
  505. return { members = names }
  506. }
  507. static transferOrg = (String userId, String slug, String name, String confirm, Boolean stayMember = false) => {
  508. o = orgFor(slug, userId)
  509. if (o == null || userId == null || o.owner != userId) { return { error = 'only the owner can transfer this organization' } }
  510. if (confirm != slug) { return { error = 'type the organization name to confirm' field = 'confirm' } }
  511. found = resolveDisplayName(name)
  512. if (found.error != null) { return found }
  513. if (found.userId == userId) { return { error = 'you already own this organization' field = 'name' } }
  514. ref = refOfOrg(slug)
  515. wasMember = isMember(ref, userId)
  516. if (stayMember && !addMember(ref, userId)) { return { error = 'could not retain your membership' } }
  517. after = saveOrgOwner(o, found.userId)
  518. if (after == null) {
  519. if (stayMember && !wasMember) { removeMember(ref, userId) }
  520. return { error = 'could not transfer the organization' }
  521. }
  522. if (isMember(ref, found.userId)) { removeMember(ref, found.userId) }
  523. row = orgRowOf(after)
  524. emit client orgChanged(row)
  525. emit client membersChanged('o', slug, userId)
  526. emit client membersChanged('o', slug, found.userId)
  527. return { org = row }
  528. }
  529. static moveRepoFromInput = (String userId, String slug, Hybrid input) => {
  530. if (hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }
  531. if (input.name != null && hlTypeName(input.name) != 'String') { return { error = 'name must be a repository slug' field = 'name' } }
  532. o = orgBySlug(input.org)
  533. if (o == null || !mayUseOrg(userId, o)) { return { error = 'you must belong to the destination organization' field = 'org' status = 403 } }
  534. i = slug.indexOf('/')
  535. name = input.name == null ? (i < 0 ? slug : slug.slice(i + 1)) : input.name
  536. return renameRepo(userId, slug, name, input.org)
  537. }

Branches

Latest commits

  • 6e03be0apull state: keep the resync marker until the sync ran (#56)mre
  • 93bdd26cpull state: wait for a claimed row, resync when it stays busy (#56)mre
  • e53d284bpull state: stop on a failed branch scan, claim the row on writes (#56)mre
  • d8608224close and reopen: keep a manual close against sync (#56)mre
  • ffb60746|||PR close and reopen pull requests (#56)mre
  • fe209ffeMerge branch 'w56-owner-members' into mainGitoria
  • fa0754c8|||PR repository owner transfer and members API (#55)mre
  • 8862e537Merge branch 'w55-rename-org' into mainGitoria
  • 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
  • 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
  • 499f25f2Review fixes for the Settings organization move (#53)mre
  • ed3b922b|||PR move a repository into an organization in Settings (#53)mre
  • 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
  • 9f7ce8d8Preserve rename compatibility and live page titlesmre
  • c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
  • ce08920dMerge branch 'w52c-header-phone' into mainGitoria
  • 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria