gitoria
All repositories: gitoria
9.7 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 { 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 } from './users.hl'import { repoBySlug, setTicketsConnection } 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) {let f = tabs(l)if (f.length >= 2 && isHexId(f[0])) {let sub = f.slice(1).join(' ')for (x of refsOf(sub)) {let 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)) {let 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}
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
- 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
- a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
- a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
- 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre
- 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
- ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
- 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre