gitoria
All repositories: gitoria
12.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, 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 { storageDir, countOfList, firstOf, plainError, newestFirst } from './util.hl'import { userOfSession, userRecord, userIdOfSession, nameOfUser, usersByName } 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 } from './git.hl'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), by namestatic orgRows = (userId) => {out = []for (o of allOrgs()) {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) + '/' }) }}return 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 = { slug = o.slug isPrivate = o.priv == true } }}// 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) => {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' } }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' }}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' } }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)r = reposTable.fetch(id)return { repo = rowOf(r, isPrivateRepo(r)) }}// 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() } }return { repo = rowOf(after, isPrivateRepo(after)) }}// 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)}
Branches
Latest commits
- a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre
- 17f031f0gitoria#34: private repos, organizations, members, move tool, gatemre
- 71b69343gitoria#23: the sidebar's Repositories link was a 404 after any in-page navigation (static menu re-evaluated publicUrl in the browser); the menu is an instance property now; gate taps it after a tab, folder, filemre
- 90b4f728gitoria#25: restore the homepage cache and the parsed-blocks cache (they store data under immutable keys, no HTML); the page key, the framework readme key and the textHash key stay removedmre
- a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
- 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
- c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre
- e307d69agitoria: say what is per process during the hand-overmre
- f0cbe6a6gitoria: standby service on port 45014 and a deploy that hands over through it without a gapmre
- 727cd083gitoria#25: README: vendor notes for Hybriel c1b1010f and the page cachemre
- 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre
- 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
- bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
- 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
- 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
- 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
- 2a2fdd77gs: C2 check before cookie experimentsmre
- 76e0587aWIP gs 2mre
- a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
- 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre