gitoria
All repositories: gitoria
8.8 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> <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 (head sha, context); a newer PUT replaces it// requiredRows pk @id index !key, rid storage/mpackdb/statusreq.db// { rid, key "<rid> <context>", context } the contexts the owner requires before a pull request may be merged// 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 { 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 { 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).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 + ' ' + 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 = firstOf(statusRows.find('key', row.key))if (old != null) { statusRows.update(old.id, row) } else { statusRows.put(row) }saved = firstOf(statusRows.find('key', row.key))if (saved == null) { return { invalid = [{ field = 'status' reason = 'could_not_store' }] } }return { status = statusJson(saved) }}// ---- required before merge --------------------------------------------------------------------------------------static requiredOf = (repoId) => {all = requiredRows.find('rid', repoId)out = []if (countOfList(all) > 0) { for (r of all) { out.push(r.context) } }return out}// the owner's list for the Settings page: [{ id, name }]static requiredList = (repoId) => {all = requiredRows.find('rid', repoId)out = []if (countOfList(all) > 0) { for (r of all) { out.push({ id = r.id name = r.context }) } }return out}// the owner requires a context → { rows } or { error }static requireContext = (userId, session, slug, 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' } }bad = plainError(ctxName, 100, 'the context')if (bad != null) { return { error = bad } }ctx = ('' + ctxName).trim()if (ctx == '') { return { error = 'give the context an app sets, for example kunibert' } }if (requiredOf(repo.id).includes(ctx)) { return { rows = requiredList(repo.id) } }if (requiredOf(repo.id).length >= maxRequired) { return { error = 'at most ' + maxRequired + ' contexts can be required' } }id = requiredRows.put({ rid = repo.id key = repo.id + ' ' + ctx 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 }}// the contexts that are not success on `head`: ["ctx (pending)", "ctx (not set)"]static missingOf = (repoId, head) => {have = statusesOf(repoId, head)out = []for (c of requiredOf(repoId)) {let state = 'not set'for (s of have) { if (s.context == c && s.state == 'success') { state = 'success' } else if (s.context == c && state == 'not set') { state = s.state } }if (state != 'success') { out.push(c + ' (' + 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) + ': ' + 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: ' + 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
Latest commits
- 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
- 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
- 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
- afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
- 15e01e94gitoria#43: tests/events.mjs and its probemre
- 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
- b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
- 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre