gitoria
All repositories: gitoria
15.3 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, 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 { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { NL, storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'import { userOfSession, userRecord, userIdOfSession, nameOfUser, usersByName, userForMember } from './users.hl'import { repoOrigin, slugOfHost } from './util.hl'import { slugOfRequest } from './orgs.hl'import { slugError, fullNameError, rowOf, withChange } from './repos-helpers.hl'import { initRepo, repoDir } from './git.hl'import { writeFile, remove, exists } from 'hl:fs'import { orgBySlug, putOrg, setOrgPriv, allOrgs } from './orgs.hl'import { refOfRepo, refOfOrg, isMember, memberIds, addMember, removeMember } from './members.hl'import { forwardOf } from './forwards.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}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 }return repoRow(slug, userIdOfSession(session))}// 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({ @id = o.id slug = o.slug isPrivate = o.priv == true isPublic = o.priv != true createdMs = o.created address = repoOrigin(o.slug) + '/' }) }}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 = (name) => { return orgBySlug(name) != null || repoBySlug(name) != null }// a new organization for a user: { org } or { error }static createOrg = (userId, slug, priv) => {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)' } }s = slug == null ? '' : ('' + slug).trim()bad = slugError(s)if (bad != null) { return { error = bad } }if (nameTaken(s)) { return { error = "the address '" + s + "' is taken — organizations and repositories share their addresses" } }o = putOrg(s, u.id, priv == true)if (o == null) { return { error = 'could not store the organization' } }return { org = { id = o.id slug = o.slug isPrivate = o.priv == true createdMs = o.created } }}// 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 = (userId, org, slug, description, priv, members = null) => {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 = '' } }s = slug == null ? '' : ('' + slug).trim()og = 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 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)}n = name == null ? '' : ('' + name).trim()found = usersByName(n)if (found.length == 0) { return { error = "nobody is called '" + n + "' (people choose their display name at their first login)" } }if (found.length > 1) { return { error = "more than one person is called '" + n + "'" } }ok = add ? addMember(ref, found[0]) : removeMember(ref, found[0])if (!ok) { return { error = add ? 'could not add the member' : 'that person is not on the list' } }return membersOf(kind, slug, userId)}// 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)if (repoAt(s, session) != null) { return s }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') { return { error = 'name must be a repository slug' field = 'name' } }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' } }return createRepoIn(userId, input.org, input.name, input.description, input.private, input.members)}
Branches
Latest commits
- 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
- b88e39f1log: privacy cleanup donemre
- bfd7826ddocs: neutral example organisation namemre