gitoria
All repositories: gitoria
11.3 KB
// lib/tickets.hl — THE TICKETS OF A REPO (gitoria#10, gitoria#18; CONCEPT "/tickets"). The tickets are NOT stored in// gitoria: they live in a tickets.worldapi.org project the repo's OWNER connected in the repo's settings// (docs: antcolony-docs/docs/tickets-connect-apps.md). The connect flow:// 1. /settings/connect (lib/api.hl connectStart) sends the owner's browser to <tickets>/connect?app=gitoria&label=<slug>&return=…&state=…// 2. tickets brings them back to /settings/connected?code=…&state=… (the state is the nonce of step 1, kept in the session)// 3. exchangeConnect(code): POST <tickets>/api/connect/exchange { code } → { key, project, title, api }, from THIS server.// The key is stored per repo (repos.hl, mpackdb) and never sent to a page.// Every write then carries `Authorization: Bearer <key>` and `X-Tickets-Identity: <ident id of the person>`: the person// (not gitoria) is the author, under the project's roles. Reads need no key.// Links both ways: "#12" in a commit subject or a pull request title links ticket 12 (refParts); a push posts// "mentioned in commit …" on that ticket, and a merged `|||PR` whose title says "fixes #12" sets ticket 12 to review (notifyPush).// Config: GITORIA_TICKETS_URL (tickets-helpers.hl). The shapes, the `#12` references: tickets-helpers.hl.import { randomBytes } from 'hl:crypto'import { fetch } from 'hl:fetch'import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { NL, storageDir, countOfList, plainError, repoOrigin } from './util.hl'import { userRecord, userIdOfSession } from './users.hl'import { repoBySlug, setTicketsConnection, ownsRepo } from './repos.hl'import { gitRaw } from './git.hl'import { tabs, isHexId } from './git-helpers.hl'import { pullsNow } from './pulls.hl'import { ticketsUrl, maxRows, agent, isCode, viewOf, writeHeaders, whyOf, refsOf, fixedBy } from './tickets-helpers.hl'// what was already told to tickets (a push arrives again with the same commits): one row per `<slug> <what> <sha> <n>`static seenTable = new MPackDB(file = storageDir + '/ticketlinks.db', primaryKey = '@id', indexes = ['!key'])// ---- the connect flow ------------------------------------------------------------------------------static connectHref = (slug, state) => {back = repoOrigin(slug) + '/settings/connected'return ticketsUrl + '/connect?app=gitoria&label=' + encodeURIComponent(slug) + '&return=' + encodeURIComponent(back) + '&state=' + encodeURIComponent(state)}// the code tickets sent back → { key, project, title, api } | { error }static exchangeConnect = (code) => {if (!isCode(code)) { return { error = 'that is not a code from tickets' } }r = fetch(ticketsUrl + '/api/connect/exchange', { method = 'POST' json = { code = code } headers = { 'user-agent' = agent } timeoutMs = 10000 })if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }j = r.json()if (r.status != 200 || j == null || j.key == null || j.project == null || j.api == null) {return { error = 'tickets.worldapi.org refused the connection (' + r.status + (j != null && j.error != null ? ': ' + j.error : '') + ')' }}if (hlTypeName(j.key) != 'String' || !j.key.startsWith('tktc_') || hlTypeName(j.api) != 'String' || !j.api.startsWith(ticketsUrl + '/api/projects/')) { return { error = 'tickets.worldapi.org sent an answer gitoria does not understand' } }return { key = j.key project = '' + j.project title = j.title != null ? '' + j.title : '' + j.project api = j.api }}// THE END OF THE CONNECT FLOW (/settings/connected, lib/api.hl connectBack): tickets' answer `q` (?code&state, or ?error),// `want` = the nonce parked in the session at the start → '' (connected: the key is stored per repo, the history so far// is marked as told) or the note for the settings page. The caller checked that the person owns the repo.static finishConnect = (slug, want, q) => {if (q.error != null && q.code == null) { return 'Tickets did not connect: ' + ('' + q.error).slice(0, 100) }if (want == null || want != slug + '.' + q.state) { return 'That connection was not started here (or was used already) — click "Tickets: connect" again.' }x = exchangeConnect(q.code)if (x.error != null) { return x.error }r = setTicketsConnection(slug, x.key, x.project, x.title, x.api)if (r.error != null) { return r.error }notifyPush(slug, true)return ''}// ---- reading ---------------------------------------------------------------------------------------// the project of a connected repo (the server-side record has the key): { key, api, project } or nullstatic connectionOf = (slug) => {r = repoBySlug(slug)if (r == null || r.tktKey == null || r.tktKey == '' || r.tktApi == null || r.tktApi == '') { return null }return { key = r.tktKey api = r.tktApi project = r.tktProject title = r.tktTitle }}// the repo's tickets, newest update first (tickets' order): { tickets, count, truncated, project, url } or { error }static loadTickets = (slug) => {c = connectionOf(slug)if (c == null) { return { error = 'not connected' } }r = fetch(c.api + '/tickets', { method = 'GET' headers = { 'user-agent' = agent } timeoutMs = 10000 })if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }base = { project = c.project url = ticketsUrl + '/projects/' + c.project }j = r.status == 200 ? r.json() : nullif (j == null || j.tickets == null) { return { error = 'tickets.worldapi.org answered ' + r.status } }out = []for (t of j.tickets) { if (out.length < maxRows) { out.push(viewOf(t)) } }base.tickets = outbase.count = j.tickets.lengthbase.truncated = j.tickets.length > out.lengthreturn base}// ---- writing (as a person) ---------------------------------------------------------------------------// open a ticket for a repo as the person with this ident id: { ticket } or { error, field }static openTicket = (slug, identity, subject, summary) => {s = subject == null ? '' : ('' + subject).trim()if (s == '') { return { error = 'the ticket needs a subject' field = 'subject' } }bad = plainError(s, 200, 'the subject')if (bad != null) { return { error = bad field = 'subject' } }body = summary == null ? '' : ('' + summary).trim()if (body.length > 20000) { return { error = 'the text is too long (at most 20000 characters)' field = 'summary' } }c = connectionOf(slug)if (c == null) { return { error = 'this repository is not connected to a tickets project (see Settings)' field = '' } }r = fetch(c.api + '/tickets', { method = 'POST' json = { subject = s summary = body } headers = writeHeaders(c, identity) timeoutMs = 10000 })if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' field = '' } }j = r.json()if ((r.status != 201 && r.status != 200) || j == null || j.ticket == null) {if (r.status == 403) { return { error = 'tickets refused: log in to tickets.worldapi.org once, choose a display name there, and check you may edit this project' + whyOf(r) field = '' } }return { error = 'tickets.worldapi.org refused the ticket (' + r.status + whyOf(r).slice(0, 200) + ')' field = '' }}return { ticket = viewOf(j.ticket) }}// a comment on ticket n as a person → true when tickets took it, or answered it with a refusal (no retry then)static commentTicket = (c, identity, n, text) => {r = fetch(c.api + '/tickets/' + n + '/comments', { method = 'POST' json = { text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })return r != null && r.status != null && r.status > 0 && r.status < 500}static stateTicket = (c, identity, n, state, text) => {r = fetch(c.api + '/tickets/' + n + '/state', { method = 'POST' json = { state = state text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })return r != null && r.status != null && r.status > 0 && r.status < 500}// ---- after a push or a merge: tell tickets ---------------------------------------------------------------static seen = (key) => { return countOfList(seenTable.find('key', key)) > 0 }static markSeen = (key) => { seenTable.put({ key = key created = now() }) }// The recent commits of every branch that name a ticket: "mentioned in commit …" as a comment on it, once per// commit and ticket. `quiet` only marks them (the moment of connecting: old history is not announced).// Then every MERGED pull request whose title says "fixes #N" sets ticket N to review, once. The person is the repo's// owner (pushes and merges are the owner's). Failures are silent (the push has succeeded); a refusal by tickets is not retried.static notifyPush = (slug, quiet) => {c = connectionOf(slug)repo = repoBySlug(slug)if (c == null || repo == null) { return null }owner = userRecord(repo.owner)if (owner == null || owner.identity == null) { return null }lg = gitRaw(slug, ['log', '-100', '--branches', '--format=%H%x09%s'])if (lg != null && lg.exit == 0) {for (l of lg.lines) {f = tabs(l)if (f.length >= 2 && isHexId(f[0])) {sub = f.slice(1).join(' ')for (x of refsOf(sub)) {key = slug + ' commit ' + f[0] + ' ' + x.nif (!seen(key)) {if (quiet || commentTicket(c, owner.identity, x.n, 'Mentioned in commit [' + f[0].slice(0, 8) + '](' + repoOrigin(slug) + '/commit/' + f[0] + ') of ' + slug + ':' + NL + NL + '> ' + sub)) { markSeen(key) }}}}}}got = pullsNow(slug, repo.branch != null ? repo.branch : '')if (got != null && !got.isMessage) {for (p of got.pulls) {if (p.isMerged) {for (n of fixedBy(p.title)) {key = slug + ' pr ' + p.sha + ' ' + nif (!seen(key)) {if (quiet || stateTicket(c, owner.identity, n, 'review', 'Fixed by the merged pull request [' + p.title + '](' + repoOrigin(slug) + '/commit/' + p.sha + ') of ' + slug + '.')) { markSeen(key) }}}}}}return null}// Session state belongs to the tickets connect flow, shared by its thin function routes.static startConnect = (slug, session, &sessions) => {nonce = randomBytes(16)session.data.connectState = slug + '.' + noncesession.data.connectNote = nullsessions.save(session)return connectHref(slug, nonce)}static connectLog = (slug, outcome, hasState) => {console.log('connect-back ' + slug + ' ' + outcome + ' state=' + (hasState ? 'present' : 'absent'))}static completeConnect = (slug, session, q, &sessions) => {hasState = q.state != nulllet s = sessionfresh = s == nullif (s == null || userIdOfSession(s) == null || !ownsRepo(slug, s)) {why = s == null ? 'no-session' : (userIdOfSession(s) == null ? 'session-not-signed-in' : 'not-owner')connectLog(slug, 'refused reason=' + why, hasState)if (fresh) { s = sessions.mint() }s.data.connectNote = 'The connection to tickets did not finish: this browser is not signed in to gitoria as the repo owner. Sign in and press Connect again.'sessions.save(s)return { session = s fresh = fresh }}want = s.data.connectStates.data.connectState = nullnote = finishConnect(slug, want, q)s.data.connectNote = note == '' ? null : notesessions.save(s)if (note == '') {connectLog(slug, 'connected', hasState)} else if (q.error != null && q.code == null) {connectLog(slug, 'tickets-error', hasState)} else if (want == null || want != slug + '.' + q.state) {connectLog(slug, 'state-mismatch', hasState)} else {connectLog(slug, 'exchange-failed', hasState)}return { session = s fresh = false }}
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
- eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
- 25309981log: STOP and paused workers 2026-11-08mre
- 5286803alog: worker briefs and reports of 2026-11-08mre
- 85c86605log: 2026-11-08 Arachne on list shapemre
- cb634512log: 2026-11-08 keyed lists livemre
- 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
- 0db0d81elog: 2026-11-08 day52 live, W30 startedmre
- b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
- 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
- f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre
- dc77fb95main: offline note from __hlOnline, tick removed (#29)mre
- 37a86d93tests/reviews: seeded fake hook's DNS failures are not server errors (flaky gate)mre
- 07ab845bvendor: Hybriel set-day50 (run() leak fix #335)mre
- 71b2fa4blog: hybriel#335mre
- bd94cc7elog: #25 cause found (run() leak)mre
- 565cb508log: H25 round 1mre
- 90a0af84log: H25 startedmre
- 87bd53b8mission H25mre
- 71a746cflog: day48 livemre
- bf568e14log: V48 mergedmre