gitoria
All repositories: gitoria
11.4 KB
// lib/statuses.hl — THE STATUS OF A COMMIT SET BY AN APP (gitoria#46, part of the push API gitoria#38; contract docs/apps-api.md §5 "Status").// Two tables, both keyed by the repo's ID (never the slug), every write to them is here:// statusRows pk @id index !key, shaKey storage/mpackdb/statuses.db// { rid, key "<rid> <sha> <appId> <context>", shaKey "<rid> <sha>", sha, context, state pending|success|failure|error, title, summary (≤ 65,536),// url (https or null), appId (apps @id), appName, updatedMs } one row per (repo, head sha, APP, context) (v1.4); a newer PUT of the same app replaces only its own.// A row of the old shape (key without the app id, written before v1.4) is found by its shaKey + appId + context and re-keyed by that app's next PUT: no migration.// requiredRows pk @id index !key, rid storage/mpackdb/statusreq.db// { rid, key "<rid> <appId> <context>", appId, context } the app + context the owner requires before a pull request may be merged (v1.4);// an entry whose app is gone stays, shows as "(removed app)" and is never met// listStatuses(repo, sha) the GET route's work (v1.3) → { statuses } or { unknown: true }// putStatus(repo, app, body) the PUT route's work → { status } or { invalid: [details] } or { unknown: true }// badgesOf / requirementOf what /pulls shows for a pull request's CURRENT head, and what blocks its Merge button// missingText(slug, head) the server-side check of the Merge (lib/pulls.hl mergePullNow): '' when every required context is success on that headimport { MPackDB } from 'hl:mpackdb'import { now, timestamp } from 'hl:time'import { storageDir, countOfList, firstOf, plainError } from './util.hl'import { repoBySlug, ownsRepo } from './repos.hl'import { appsTable } from './apps-table.hl'import { gitRaw } from './git.hl'import { isHexId } from './git-helpers.hl'static statusRows = new MPackDB(file = storageDir + '/statuses.db', primaryKey = '@id', indexes = ['!key', 'shaKey'])static requiredRows = new MPackDB(file = storageDir + '/statusreq.db', primaryKey = '@id', indexes = ['!key', 'rid'])static states = ['pending', 'success', 'failure', 'error']static maxSummary = 65536static maxRequired = 20// ---- the PUT --------------------------------------------------------------------------------------------------// is the sha a commit of this repo? (the object exists and is a commit)static isCommitOf = (slug, sha) => {if (sha == null || hlTypeName(sha) != 'String' || sha.length != 40 || !isHexId(sha)) { return false }r = gitRaw(slug, ['cat-file', '-e', sha + '^{commit}'])return r != null && r.exit == 0}static httpsUrl = (u) => {return hlTypeName(u) == 'String' && u.length <= 500 && u.startsWith('https://') && u.length > 10 && plainError(u, 500, 'the url') == null && !u.includes(' ')}// the body's complaints as contract `details` ([] = fine)static problems = (j) => {out = []if (hlTypeName(j.context) != 'String' || j.context.trim() == '') {out.push({ field = 'context' reason = 'required' })} else if (plainError(j.context, 100, 'the context') != null) {out.push({ field = 'context' reason = 'too_long_or_not_one_line' })}if (hlTypeName(j.state) != 'String' || !states.includes(j.state)) { out.push({ field = 'state' reason = 'one_of_pending_success_failure_error' }) }if (j.title != null && (hlTypeName(j.title) != 'String' || plainError(j.title, 200, 'the title') != null)) { out.push({ field = 'title' reason = 'one_line_at_most_200' }) }if (j.summary != null && (hlTypeName(j.summary) != 'String' || j.summary.length > maxSummary)) { out.push({ field = 'summary' reason = 'text_at_most_65536' }) }if (j.url != null && !httpsUrl(j.url)) { out.push({ field = 'url' reason = 'https_only' }) }return out}static statusJson = (row) => {return { head = row.sha sha = row.sha context = row.context state = row.state title = row.title summary = row.summary url = row.url app = { id = row.appId name = row.appName } updated = timestamp(row.updatedMs) }}// PUT {api}/statuses/<sha> for `app` on `repo` (both from appOfRequest). The sha is a path value: anything that is not a commit of the repo is `unknown` (422).// the row of THIS app for (sha, context): by the new key, else (old shape, key without the app id) by the sha's rowsstatic ownRow = (rid, sha, appId, ctx) => {f = firstOf(statusRows.find('key', rid + ' ' + sha + ' ' + appId + ' ' + ctx))if (f != null) { return f }for (r of statusesOf(rid, sha)) { if (r.appId == appId && r.context == ctx) { return r } }return null}static putStatus = (repo, app, sha, j) => {bad = problems(j)if (bad.length > 0) { return { invalid = bad } }if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }ctx = j.context.trim()row = { rid = repo.id key = repo.id + ' ' + sha + ' ' + app.id + ' ' + ctx shaKey = repo.id + ' ' + sha sha = sha context = ctx state = j.state title = j.title == null ? '' : j.title.trim() summary = j.summary == null ? '' : j.summary url = j.url appId = app.id appName = app.name updatedMs = now() }old = ownRow(repo.id, sha, app.id, ctx)if (old != null) { statusRows.update(old.id, row) } else { statusRows.put(row) }saved = ownRow(repo.id, sha, app.id, ctx)if (saved == null) { return { invalid = [{ field = 'status' reason = 'could_not_store' }] } }return { status = statusJson(saved) }}// GET {api}/statuses/<sha> (v1.3): every context on the sha, from every app → { statuses } or { unknown: true } when the repo has no such commitstatic listStatuses = (repo, sha) => {if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }out = []for (r of statusesOf(repo.id, sha)) { out.push(statusJson(r)) }return { statuses = out }}// ---- required before merge --------------------------------------------------------------------------------------static appNameOf = (appId) => {a = appId == null ? null : appsTable.fetch(appId)return a == null ? '(removed app)' : a.name}// the owner's entries: [{ appId, context }]static requiredEntries = (repoId) => {all = requiredRows.find('rid', repoId)out = []if (countOfList(all) > 0) { for (r of all) { out.push({ appId = r.appId context = r.context }) } }return out}// v1.4 pull.required: [{ app: { id, name }, context }]static requiredOf = (repoId) => {out = []for (e of requiredEntries(repoId)) { out.push({ app = { id = e.appId name = appNameOf(e.appId) } context = e.context }) }return out}// the owner's list for the Settings page: [{ id, name }] with name "App name · context"static requiredList = (repoId) => {all = requiredRows.find('rid', repoId)out = []if (countOfList(all) > 0) { for (r of all) { out.push({ id = r.id name = appNameOf(r.appId) + ' · ' + r.context }) } }return out}// the owner requires app + context → { rows } or { error }. The app is picked by its id (an app of THIS repo).static requireContext = (userId, session, slug, appId, ctxName) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }app = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)if (app == null || app.repo != repo.id) { return { error = 'pick one of the apps connected to this repository' } }bad = plainError(ctxName, 100, 'the context')if (bad != null) { return { error = bad } }ctx = ('' + ctxName).trim()if (ctx == '') { return { error = 'give the context the app sets, for example kunibert' } }key = repo.id + ' ' + app.id + ' ' + ctxif (firstOf(requiredRows.find('key', key)) != null) { return { rows = requiredList(repo.id) } }if (requiredEntries(repo.id).length >= maxRequired) { return { error = 'at most ' + maxRequired + ' contexts can be required' } }id = requiredRows.put({ rid = repo.id key = key appId = app.id context = ctx })if (id == null) { return { error = 'could not store the setting: ' + requiredRows.lastError() } }return { rows = requiredList(repo.id) }}static dropRequired = (userId, session, slug, id) => {repo = repoBySlug(slug)if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }row = id == null || hlTypeName(id) != 'String' ? null : requiredRows.fetch(id)if (row == null || row.rid != repo.id) { return { error = 'no such requirement' } }requiredRows.delete(row.id)return { rows = requiredList(repo.id) }}// ---- what a pull request shows and what blocks it ---------------------------------------------------------------static statusesOf = (repoId, head) => {all = statusRows.find('shaKey', repoId + ' ' + head)return countOfList(all) == 0 ? [] : all}// the badge of one status row: its state (class too), "<app>: <context>" and the title as textstatic badgeOf = (row) => {return { id = row.id state = row.state cls = 'badge ' + row.state label = row.appName + ': ' + row.context title = row.title hasTitle = row.title != '' url = row.url == null ? '' : row.url hasUrl = row.url != null }}// Required entries not successful on head, with app identity, context and machine-readable state.static missingOf = (repoId, head) => {have = statusesOf(repoId, head)out = []for (e of requiredEntries(repoId)) {let state = 'missing'if (e.appId != null && appsTable.fetch(e.appId) != null) {for (s of have) { if (s.appId == e.appId && s.context == e.context && s.state == 'success') { state = 'success' } else if (s.appId == e.appId && s.context == e.context && state == 'missing') { state = s.state } }}if (state != 'success') { out.push({ app = { id = e.appId name = appNameOf(e.appId) } context = e.context state = state }) }}return out}// Preserve the Merge button and error sentence, including the old labels for absent or removed apps.static missingLabels = (missing) => {out = []for (e of missing) {state = e.app.id == null || appsTable.fetch(e.app.id) == null ? 'app removed' : (e.state == 'missing' ? 'not set' : e.state)out.push(e.app.name + ' · ' + e.context + ' (' + state + ')')}return out}// the SERVER-SIDE merge check: '' when allowed, else the sentence that says which context is missingstatic missingText = (slug, head) => {repo = repoBySlug(slug)if (repo == null) { return '' }m = missingOf(repo.id, head)if (m.length == 0) { return '' }return 'Not merged: the required checks are not successful on the current head ' + head.slice(0, 8) + ': ' + missingLabels(m).join(', ') + '.'}// the branch's tip, '' when unknownstatic headOf = (slug, branch) => {if (branch == null || branch == '') { return '' }r = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + branch])return r != null && r.exit == 0 && r.lines.length > 0 && isHexId(r.lines[0]) ? r.lines[0] : ''}// the rows of /pulls with `statuses` (badges of the CURRENT head), and the Merge button off while a required context is not success therestatic withStatuses = (list, slug) => {out = []repo = repoBySlug(slug)for (p of list) {q = phead = repo != null ? headOf(slug, p.source) : ''badges = []let blocked = ''if (head != '') {for (s of statusesOf(repo.id, head)) { badges.push(badgeOf(s)) }m = missingOf(repo.id, head)if (m.length > 0) { blocked = 'Merge waits for: ' + missingLabels(m).join(', ') }}q.statuses = badgesq.hasStatuses = badges.length > 0q.blockText = blockedif (blocked != '') {q.canMerge = falseq.mergeClass = 'nomerge'}out.push(q)}return out}
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
- 2a4b3dc1|||PR Anonymous reads of public repos' pulls and statusesmre
- 21b2c141Merge branch 's25-no-session-on-login-error' into mainGitoria
- 698458e2|||PR No session for cookieless login errors: READMEmre
- cce7b785|||PR No session for cookieless login errorsmre
- 678d6303Merge branch 'v59' into mainGitoria
- 5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre
- 9eff6627Merge branch 'f2-api-bugs' into mainGitoria
- 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