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
- 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
- fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
- f7198131gitoria#45: tests/pr-page.mjs gatemre
- 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
- e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
- 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
- 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
- 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
- 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
- 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
- 187e1871gitoria#46: badges without decorative bordersmre
- 6b022798gitoria#46: tests/status.mjs in GATESmre
- c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
- e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
- 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
- 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
- ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
- 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
- 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
- cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
- 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre