gitoria
All repositories: gitoria
29.2 KB
// lib/repos.hl — THE REPOS (ticket gitoria#6). A repo is a SLUG that is unique in the whole system;// it lives at <slug>.<domain> (util.hl repoOrigin). One hl:mpackdb table, storage/mpackdb/repos.db:// pk @id index !slug, owner { slug, title (optional display name), description, owner (users @id), created, branch (the main branch; '' / absent = default),// tktKey, tktProject, tktTitle, tktApi (the connected tickets project, gitoria#18; the key is a secret and never leaves the server) }// The unique index makes the slug unique for good (a second put of a taken slug fails); slugError (repos-helpers.hl)// answers it in words first. A new repo also gets an empty bare git repository (git.hl initRepo).// Every write to the table is here. The slug's rules, the row a page shows, the Markdown view: repos-helpers.hl.import { resumePullResync } from './pullsync.hl'import { MPackDB } from 'hl:mpackdb'import { run } from 'hl:proc'import { now } from 'hl:time'import { NL, storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'import { userOfSession, userRecord, userIdOfSession, nameOfUser, resolveDisplayName, userForMember } from './users.hl'import { repoOrigin, slugOfHost } from './util.hl'import { slugOfRequest } from './orgs.hl'import { slugError, fullNameError, rowOf, withChange, addressFromName, orgRowOf, orgChoice } from './repos-helpers.hl'import { initRepo, repoDir, gitRoot } from './git.hl'import { writeFile, remove, exists, mkDir } from 'hl:fs'import { orgBySlug, putOrg, setOrgPriv, allOrgs, saveOrgTitle, saveOrgOwner, orgsOwnedBy } from './orgs.hl'import { refOfRepo, refOfOrg, isMember, memberIds, addMember, removeMember, moveMembers, organizationRefsOfUser } from './members.hl'import { randomBytes } from 'hl:crypto'import { forwardOf, movedForwards, namesForwardingTo } from './forwards.hl'import { moveTicketLinks } from './tickets.hl'static reposTable = new MPackDB(file = storageDir + '/repos.db', primaryKey = '@id', indexes = ['!slug', 'owner'])// ---- 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// members (of the repo or of the organization) see and push; everybody else gets the answer for a name that does not exist.static isPrivateRepo = (r) => {if (r.priv == true) { return true }if (r.org != null && r.org != '') {o = orgBySlug(r.org)return o != null && o.priv == true}return false}static mayRead = (userId, r) => {if (!isPrivateRepo(r)) { return true }return isInside(userId, r)}// owner / organization owner / member of the repo or of its organizationstatic isInside = (userId, r) => {if (userId == null) { return false }if (r.owner == userId) { return true }if (isMember(refOfRepo(r.slug), userId)) { return true }if (r.org != null && r.org != '') {o = orgBySlug(r.org)return o != null && (o.owner == userId || isMember(refOfOrg(r.org), userId))}return false}// 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// afterwards (a replayed event always meets the readers of ITS move). The event carries only (org, id, moveId); the audience decides with this record.// The newest maxLeaving moves are kept.static leaving = {}static leavingOrder = []static maxLeaving = 500static readersBefore = (Hybrid r) => {ids = [r.owner]for (u of memberIds(refOfRepo(r.slug))) { ids.push(u) }if (r.org != null && r.org != '') {o = orgBySlug(r.org)if (o != null) {ids.push(o.owner)for (u of memberIds(refOfOrg(r.org))) { ids.push(u) }}}return { open = !isPrivateRepo(r) ids = ids }}static keepLeaving = (Hybrid before) => {moveId = randomBytes(12, 'hex')let order = leavingOrder.slice(0)if (order.length >= maxLeaving) {delete leaving[order[0]]order = order.slice(1)}leaving[moveId] = beforeorder.push(moveId)leavingOrder = orderreturn moveId}static hearsRepoLeft = (String moveId, String userId) => {s = leaving[moveId]if (s == null) { return false }return s.open == true || (userId != null && s.ids.includes(userId))}static repoBySlug = (slug) => {if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }return firstOf(reposTable.find('slug', slug))}// 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 itstatic repoFor = (slug, userId) => {r = repoBySlug(slug)return r != null && mayRead(userId, r) ? r : null}// every repo the viewer may see, newest firststatic repoRows = (userId) => {out = []all = reposTable.find(null, null)if (countOfList(all) == 0) { return out }for (r of all) { if (mayRead(userId, r)) { out.push(rowOf(r, isPrivateRepo(r))) } }return newestFirst(out)}static repoRow = (slug, userId) => {r = repoFor(slug, userId)return r == null ? null : rowOf(r, isPrivateRepo(r))}// the repos of one organization the viewer may see, newest firststatic orgRepoRows = (org, userId) => {out = []for (x of repoRows(userId)) { if (x.org == org) { out.push(x) } }return out}// 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 addressstatic repoAt = (slug, session) => {if (slug == null || slug == '' || fullNameError(slug) != null) { return null }row = repoRow(slug, userIdOfSession(session))if (row != null) { resumePullResync(slug) }return row}// 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)static orgRows = (userId) => {out = []for (o of allOrgs()) {if (o.priv != true || o.owner == userId || isMember(refOfOrg(o.slug), userId)) { out.push(orgRowOf(o)) }}return newestFirst(out)}// an organization for a viewer: the record or null (a private one is invisible to others)static orgFor = (slug, userId) => {o = orgBySlug(slug)if (o == null) { return null }if (o.priv != true || (userId != null && (o.owner == userId || isMember(refOfOrg(slug), userId)))) { return o }// 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)if (userId != null) {all = reposTable.find(null, null)if (countOfList(all) > 0) { for (r of all) { if (r.org == slug && mayRead(userId, r)) { return o } } }}return null}// does this name already stand for an organization or a top-level repo? (one namespace: both are subdomains)static nameTaken = (String name) => { return orgBySlug(name) != null || repoBySlug(name) != null || forwardOf(name) != null }// a new organization for a user: { org } or { error }static createOrg = (String userId, String slug, Boolean priv, String title = '') => {u = userRecord(userId)if (u == null) { return { error = 'log in with ident (top right) first' } }if (u.name == '') { return { error = 'choose a display name first (top of the page)' } }t = title == null ? '' : ('' + title).trim()tbad = plainError(t, 80, 'the name')if (tbad != null) { return { error = tbad } }address = slug == null ? '' : ('' + slug).trim()s = address == '' ? addressFromName(t) : addressbad = slugError(s)if (bad != null) { return { error = bad } }if (nameTaken(s)) { return { error = "the address '" + s + "' is taken — organizations and repositories share their addresses" status = 409 } }o = putOrg(s, u.id, priv == true, t)if (o == null) { return { error = 'could not store the organization' } }row = orgRowOf(o)emit client orgChanged(row)return { org = row }}// may this user create a repo in the organization / push to its repos?static mayUseOrg = (userId, o) => {return userId != null && (o.owner == userId || isMember(refOfOrg(o.slug), userId))}// a new repo for a user: { repo } (the row) or { error, field }. `org` = '' or an organization the user belongs to; `priv` = privatestatic createRepoIn = (String userId, String org, String slug, String description, Boolean priv, Hybrid members = null, String title = '') => {u = userRecord(userId)if (u == null) { return { error = 'log in with ident (top right) to create a repository' field = '' } }if (u.name == '') { return { error = 'choose a display name first (top of the page)' field = '' } }t = title == null ? '' : ('' + title).trim()tbad = plainError(t, 80, 'the name')if (tbad != null) { return { error = tbad field = 'title' } }address = slug == null ? '' : ('' + slug).trim()s = address == '' ? addressFromName(t) : addressog = org == null ? '' : ('' + org).trim()bad = slugError(s)if (bad != null) { return { error = bad field = 'slug' } }d = description == null ? '' : ('' + description).trim()dbad = plainError(d, 200, 'the description')if (dbad != null) { return { error = dbad field = 'description' } }let full = sif (og != '') {o = orgBySlug(og)if (o == null || !mayUseOrg(u.id, o)) { return { error = 'no such organization (or you are not in it)' field = 'org' status = 403 } }full = fullNameError(og + '/' + s) == null ? og + '/' + s : ''if (full == '') { return { error = fullNameError(og + '/' + s) field = 'slug' } }} else if (nameTaken(s)) {return { error = "the address '" + s + "' is taken — every repository address is unique in the whole system" field = 'slug' status = 409 }}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 } }// Resolve the entire member list before writing a repo row or initializing git.memberUsers = []if (members != null) {if (hlTypeName(members) != 'Hybrid' || members.length == null) { return { error = 'members must be a list' field = 'members' } }for (name of members) {uid = userForMember(name)if (uid == null) { return { error = 'every member must name exactly one known user or identity short id' field = 'members' } }if (!memberUsers.includes(uid)) { memberUsers.push(uid) }}}id = reposTable.put({ slug = full title = t description = d owner = u.id created = now() org = og priv = priv == true })if (id == null) { return { error = 'could not store the repository (is the address taken?): ' + reposTable.lastError() field = 'slug' } }initRepo(full)for (uid of memberUsers) {if (!addMember(refOfRepo(full), uid)) { return { error = 'could not add the member' field = 'members' status = 500 } }}r = reposTable.fetch(id)row = rowOf(r, isPrivateRepo(r))emit client repoCreated(row)return { repo = row }}// the old call: a public top-level repostatic createRepo = (userId, slug, description) => { return createRepoIn(userId, '', slug, description, false) }// ---- visibility and members (the owner of the repo / the organization) ----------------------------------------------------static setRepoPriv = (userId, slug, priv) => {r = repoFor(slug, userId)if (r == null) { return { error = 'no such repository' } }if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }reposTable.update(r.id, withChange(r, { priv = priv == true }))after = reposTable.fetch(r.id)if (after == null || (after.priv == true) != (priv == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }return { repo = rowOf(after, isPrivateRepo(after)) }}static setOrgPrivate = (userId, org, priv) => {o = orgFor(org, userId)if (o == null) { return { error = 'no such organization' } }if (userId == null || o.owner != userId) { return { error = 'only the owner can change this' } }if (!setOrgPriv(o, priv == true)) { return { error = 'could not store the setting' } }return { ok = true }}// the member list of a repo ('r') or an organization ('o'): [{ id, name }] for its ownerstatic membersOf = (kind, slug, userId) => {let ref = ''if (kind == 'r') {r = repoFor(slug, userId)if (r == null || r.owner != userId) { return { error = 'only the owner can see this' } }ref = refOfRepo(slug)} else {o = orgFor(slug, userId)if (o == null || o.owner != userId) { return { error = 'only the owner can see this' } }ref = refOfOrg(slug)}out = []for (uid of memberIds(ref)) { out.push({ id = uid name = nameOfUser(uid) }) }return { members = out }}// add (add = true) or remove a member by display name; the owner onlystatic changeMember = (userId, kind, slug, name, add) => {let ref = ''if (kind == 'r') {r = repoFor(slug, userId)if (r == null || r.owner != userId) { return { error = 'only the owner can change this' } }ref = refOfRepo(slug)} else {o = orgFor(slug, userId)if (o == null || o.owner != userId) { return { error = 'only the owner can change this' } }ref = refOfOrg(slug)}found = resolveDisplayName(name)if (found.error != null) { return found }ok = add ? addMember(ref, found.userId) : removeMember(ref, found.userId)if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }emit client membersChanged(kind, slug, found.userId)return membersOf(kind, slug, userId)}// Ownership changes leave the address, organization and attached records intact.static transferRepo = (String userId, String slug, String name) => {r = repoFor(slug, userId)if (r == null) { return { error = 'no such repository' } }if (userId == null || r.owner != userId) { return { error = 'only the owner can transfer this repository' } }found = resolveDisplayName(name)if (found.error != null) { return found }if (found.userId == userId) { return { error = 'you already own this repository' } }reposTable.update(r.id, withChange(r, { owner = found.userId }))after = reposTable.fetch(r.id)if (after == null || after.owner != found.userId) { return { error = 'could not transfer the repository' } }if (isMember(refOfRepo(slug), found.userId)) { removeMember(refOfRepo(slug), found.userId) }row = rowOf(after, isPrivateRepo(after))emit client repoChanged(row)emit client membersChanged('r', slug, userId)emit client membersChanged('r', slug, found.userId)return { repo = row }}static confirmedTransfer = (String userId, String slug, String name, String confirm) => {r = repoFor(slug, userId)if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can transfer this repository' } }if (confirm != slug) { return { error = 'type the repository name to confirm' field = 'confirm' } }return transferRepo(userId, slug, name)}// The members API lists names for a reader with push rights, including organization membership.static repoMemberNames = (String userId, String slug) => {r = repoFor(slug, userId)if (r == null || !isInside(userId, r)) { return { error = 'only the owner or a member can see this' } }names = []for (uid of memberIds(refOfRepo(slug))) { names.push(nameOfUser(uid)) }return { members = names }}// does this (real) session's user own the repo?static ownsRepo = (slug, session) => {u = userOfSession(session)row = repoBySlug(slug)return u != null && row != null && row.owner == u.id}// MAY THIS USER PUSH? Only the repo's owner (a known user) — over HTTPS (transport.hl, the token's user) and over SSH// (lib/api.hl gitAccess, the key's user). Reading is everyone's.static mayPush = (userId, repo) => { return userRecord(userId) != null && isInside(userId, repo) }// the connection to a tickets project (gitoria#18); key '' = disconnected. The caller checked the owner.static setTicketsConnection = (slug, key, project, title, api) => {r = repoBySlug(slug)if (r == null) { return { error = 'no such repository' } }reposTable.update(r.id, withChange(r, { tktKey = key tktProject = project tktTitle = title tktApi = api }))after = reposTable.fetch(r.id)if (after == null || after.tktKey != key) { return { error = 'could not store the connection: ' + reposTable.lastError() } }return { repo = rowOf(after, isPrivateRepo(after)) }}// the owner's setting of the main branch (/code, the homepage). The caller checked the branch exists.static setMainBranch = (userId, slug, name) => {r = repoBySlug(slug)if (r == null) { return { error = 'no such repository' } }if (userId == null || r.owner != userId) { return { error = 'only the owner can change the main branch' } }reposTable.update(r.id, withChange(r, { branch = name }))after = reposTable.fetch(r.id)if (after == null || after.branch != name) { return { error = 'could not store the setting: ' + reposTable.lastError() } }syncProtect(after)return { repo = rowOf(after, isPrivateRepo(after)) }}// 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// (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`).// No file = off. Merges done by gitoria itself (update-ref) are not pushes. Written here when the switch or the main branch changes.static syncProtect = (r) => {f = repoDir(r.slug) + '/gitoria-protect'if (r.protect == true) { writeFile(f, (r.branch != null && r.branch != '' ? r.branch : 'main') + NL, 420) }else if (exists(f)) { remove(f) }return null}static isProtectedMain = (slug) => {r = repoBySlug(slug)return r != null && r.protect == true}static setProtectMain = (userId, slug, wanted) => {r = repoBySlug(slug)if (r == null) { return { error = 'no such repository' } }if (userId == null || r.owner != userId) { return { error = 'only the owner can change this' } }reposTable.update(r.id, withChange(r, { protect = wanted == true }))after = reposTable.fetch(r.id)if (after == null || (after.protect == true) != (wanted == true)) { return { error = 'could not store the setting: ' + reposTable.lastError() } }syncProtect(after)return { protect = after.protect == true }}// 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)static titleSlug = (host, orepo, session) => {s = slugOfRequest(host, orepo)r = repoAt(s, session)if (r != null) { return r.heading }o = orgFor(s, userIdOfSession(session))if ((orepo == null || orepo == '') && o != null && o.title != null && o.title != '') { return o.title }return orepo != null && orepo != '' ? orepo : slugOfHost(host)}// The API input check; the creation rules and writes remain in createRepoIn.static createRepoFromInput = (userId, input) => {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' } }if (input.title != null && hlTypeName(input.title) != 'String') { return { error = 'title must be text' field = 'title' } }if (input.org != null && hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }if (input.description != null && hlTypeName(input.description) != 'String') { return { error = 'description must be text' field = 'description' } }if (input.private != null && input.private != true && input.private != false) { return { error = 'private must be true or false' field = 'private' } }if (input.members != null && hlTypeName(input.members) != 'Hybrid') { return { error = 'members must be a list' field = 'members' } }return createRepoIn(userId, input.org, input.name, input.description, input.private, input.members, input.title)}// One move implementation for the stopped-app transfer tool and live Settings.// Git locks are refused; relative aliases preserve paths held by in-flight Git processes.static moveRepo = (String oldName, String to, String vis) => {r = repoBySlug(oldName)if (r == null) { return { error = 'no such repository' } }bad = fullNameError(to)if (bad != null) { return { error = bad } }before = readersBefore(r)if (to == oldName) { return { error = 'the new name is the old name' } }if (vis != 'public' && vis != 'private') { return { error = 'visibility must be public or private' } }if (repoBySlug(to) != null || (forwardOf(to) != null && forwardOf(to) != oldName) || (!to.includes('/') && orgBySlug(to) != null)) { return { error = 'the address is taken' } }if (!exists(repoDir(oldName))) { return { error = 'the bare repository does not exist' } }aliases = namesForwardingTo(oldName)destinationLink = run(['test', '-L', repoDir(to)], { timeout = 15 })backToAlias = aliases.includes(to) && destinationLink != null && destinationLink.exit == 0if ((exists(repoDir(to)) || (destinationLink != null && destinationLink.exit == 0)) && !backToAlias) { return { error = 'the destination folder already exists' } }if (backToAlias) {resolved = run(['readlink', '-f', repoDir(to)], { timeout = 15 })current = run(['readlink', '-f', repoDir(oldName)], { timeout = 15 })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' } }}locks = run(['find', repoDir(oldName), '-name', '*.lock', '-o', '-name', 'incoming-*'], { timeout = 15 })if (locks == null || locks.exit != 0 || locks.lines.length > 0) { return { error = 'repository is busy; try again after the push finishes' } }i = to.indexOf('/')org = i < 0 ? '' : to.slice(0, i)priv = vis == 'private'if (org != '' && orgBySlug(org) == null) {if (putOrg(org, r.owner, priv) == null) { return { error = 'could not create the organization' } }}if (org != '' && !exists(gitRoot + '/' + org)) { mkDir(gitRoot + '/' + org, 493) }if (backToAlias) { run(['rm', '--', repoDir(to)], { timeout = 15 }) }m = run(['mv', '-T', '-n', repoDir(oldName), repoDir(to)], { timeout = 60 })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)' } }linked = run(['ln', '-sT', (oldName.includes('/') ? '../' : '') + to + '.git', repoDir(oldName)], { timeout = 15 })if (linked == null || linked.exit != 0) {run(['mv', '-T', '-n', repoDir(to), repoDir(oldName)], { timeout = 60 })if (backToAlias) { run(['ln', '-sT', (to.includes('/') ? '../' : '') + oldName + '.git', repoDir(to)], { timeout = 15 }) }return { error = 'could not preserve the old Git path; the folder move was rolled back' }}reposTable.update(r.id, withChange(r, { slug = to org = org priv = priv }))after = reposTable.fetch(r.id)if (after == null || after.slug != to) {run(['rm', '--', repoDir(oldName)], { timeout = 15 })back = run(['mv', '-T', '-n', repoDir(to), repoDir(oldName)], { timeout = 60 })if (backToAlias) { run(['ln', '-sT', (to.includes('/') ? '../' : '') + oldName + '.git', repoDir(to)], { timeout = 15 }) }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' }}for (alias of aliases) {if (alias != to) {link = run(['ln', '-sfnT', (alias.includes('/') ? '../' : '') + to + '.git', repoDir(alias)], { timeout = 15 })if (link == null || link.exit != 0) { return { error = 'repository moved, but an old Git path could not be updated' } }}}if (!moveTicketLinks(oldName, to)) { return { error = 'repository moved, but ticket notification history could not be updated' } }moveMembers(refOfRepo(oldName), refOfRepo(to))if (!movedForwards(oldName, to)) { return { error = 'repository moved, but the old address could not be stored' } }row = rowOf(after, isPrivateRepo(after))emit client repoChanged(row)// open lists of the organization it left drop it by idleftOrg = r.org != null ? r.org : ''if (leftOrg != '' && leftOrg != org) {emit client repoLeftOrg(leftOrg, r.id, keepLeaving(before))}return { repo = row }}// Settings → Rename: the address label and the organization (none, or one the user belongs to). Never creates an organization.static renameRepo = (String userId, String slug, String name, String org) => {r = repoBySlug(slug)if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can rename this repository' } }bad = slugError(name)if (bad != null) { return { error = bad } }if (org != '' && org != (r.org != null ? r.org : '')) {o = orgBySlug(org)if (o == null || !mayUseOrg(userId, o)) { return { error = 'you can only move a repository into an organization you belong to' status = 403 field = 'org' } }}to = org != '' ? org + '/' + name : nameif (to == slug) { return { error = 'the address is the same; change the name or the organization' } }oldOrg = r.org != null ? r.org : ''// A repository that was effectively private (own flag or private organization) never becomes public by leaving or changing its organization.vis = r.priv == true || (oldOrg != '' && oldOrg != org && isPrivateRepo(r)) ? 'private' : 'public'return moveRepo(slug, to, vis)}static setRepoTitle = (String userId, String slug, String title) => {r = repoBySlug(slug)if (r == null || userId == null || r.owner != userId) { return { error = 'only the owner can change the display name' } }t = title == null ? '' : title.trim()bad = plainError(t, 80, 'the display name')if (bad != null) { return { error = bad } }reposTable.update(r.id, withChange(r, { title = t }))after = reposTable.fetch(r.id)if (after == null || after.title != t) { return { error = 'could not store the display name' } }row = rowOf(after, isPrivateRepo(after))emit client repoChanged(row)return { repo = row }}static setOrgTitle = (String userId, String slug, String title) => {o = orgBySlug(slug)if (o == null || userId == null || o.owner != userId) { return { error = 'only the owner can change the organization name' } }t = title == null ? '' : title.trim()bad = plainError(t, 80, 'the name')if (bad != null) { return { error = bad } }after = saveOrgTitle(o, t)if (after == null) { return { error = 'could not store the name' } }row = orgRowOf(after)emit client orgChanged(row)return { org = row }}// Available destinations use the same membership rule as repository creation.static orgChoices = (String userId) => {out = orgsOwnedBy(userId)seen = []for (o of out) { seen.push(o.slug) }for (ref of organizationRefsOfUser(userId)) {o = orgBySlug(ref.slice(2))if (o != null && !seen.includes(o.slug)) { out.push(orgChoice(o)) seen.push(o.slug) }}return out}// Only the affected user and the current list owner receive membership metadata.static hearsMembers = (String kind, String slug, String changedUser, String viewer) => {if (viewer == null) { return false }if (viewer == changedUser) { return true }if (kind == 'r') { r = repoBySlug(slug) return r != null && r.owner == viewer }o = orgBySlug(slug)return o != null && o.owner == viewer}static createOrgFromInput = (String userId, Hybrid input) => {if (hlTypeName(input.name) != 'String') { return { error = 'name must be an organization slug' field = 'name' } }if (input.title != null && hlTypeName(input.title) != 'String') { return { error = 'title must be text' field = 'title' } }if (input.private != null && hlTypeName(input.private) != 'Boolean') { return { error = 'private must be true or false' field = 'private' } }return createOrg(userId, input.name, input.private == true, input.title)}static orgMemberNames = (String userId, String org) => {r = membersOf('o', org, userId)if (r.error != null) { return r }names = []for (m of r.members) { names.push(m.name) }return { members = names }}static transferOrg = (String userId, String slug, String name, String confirm, Boolean stayMember = false) => {o = orgFor(slug, userId)if (o == null || userId == null || o.owner != userId) { return { error = 'only the owner can transfer this organization' } }if (confirm != slug) { return { error = 'type the organization name to confirm' field = 'confirm' } }found = resolveDisplayName(name)if (found.error != null) { return found }if (found.userId == userId) { return { error = 'you already own this organization' field = 'name' } }ref = refOfOrg(slug)wasMember = isMember(ref, userId)if (stayMember && !addMember(ref, userId)) { return { error = 'could not retain your membership' } }after = saveOrgOwner(o, found.userId)if (after == null) {if (stayMember && !wasMember) { removeMember(ref, userId) }return { error = 'could not transfer the organization' }}if (isMember(ref, found.userId)) { removeMember(ref, found.userId) }row = orgRowOf(after)emit client orgChanged(row)emit client membersChanged('o', slug, userId)emit client membersChanged('o', slug, found.userId)return { org = row }}static moveRepoFromInput = (String userId, String slug, Hybrid input) => {if (hlTypeName(input.org) != 'String') { return { error = 'org must be an organization slug' field = 'org' } }if (input.name != null && hlTypeName(input.name) != 'String') { return { error = 'name must be a repository slug' field = 'name' } }o = orgBySlug(input.org)if (o == null || !mayUseOrg(userId, o)) { return { error = 'you must belong to the destination organization' field = 'org' status = 403 } }i = slug.indexOf('/')name = input.name == null ? (i < 0 ? slug : slug.slice(i + 1)) : input.namereturn renameRepo(userId, slug, name, input.org)}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
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