gitoria
All repositories: gitoria
9.8 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) => {let 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' } }let 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' } }let 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.' }let x = exchangeConnect(q.code)if (x.error != null) { return x.error }let 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) => {let 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) => {let c = connectionOf(slug)if (c == null) { return { error = 'not connected' } }let 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' } }let base = { project = c.project url = ticketsUrl + '/projects/' + c.project }let j = r.status == 200 ? r.json() : nullif (j == null || j.tickets == null) { return { error = 'tickets.worldapi.org answered ' + r.status } }let 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) => {let s = subject == null ? '' : ('' + subject).trim()if (s == '') { return { error = 'the ticket needs a subject' field = 'subject' } }let bad = plainError(s, 200, 'the subject')if (bad != null) { return { error = bad field = 'subject' } }let body = summary == null ? '' : ('' + summary).trim()if (body.length > 20000) { return { error = 'the text is too long (at most 20000 characters)' field = 'summary' } }let c = connectionOf(slug)if (c == null) { return { error = 'this repository is not connected to a tickets project (see Settings)' field = '' } }let 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 = '' } }let 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) => {let 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) => {let 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) => {let c = connectionOf(slug)let repo = repoBySlug(slug)if (c == null || repo == null) { return null }let owner = userRecord(repo.owner)if (owner == null || owner.identity == null) { return null }let 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) }}}}}}let 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
- mainmain branch
Latest commits
- 090a20c6mission 002 (code order) 2/4: one lib/ file per topic — git.hl split into git (calls, branches, init, temp folder) / homepage / code / pulls / releases (+ git-helpers: paths, ids, |||PR/|||RL markers); repos-helpers, tickets-helpers, transport-helpers; util.hl = localtime + env, storage dir, addresses, lists, text checks, one newest-first sort (was 3 copies); the function routes out of project.hl into lib/api.hl (thin; plumbing in api-helpers.hl), sshgate.hl folded into api.hl + sshkeys.hl keyLine + repos.hl mayPush; 'Make main' and the merge answer out of the faces (code.hl makeMain, pulls.hl pullsView), one login helper (users.hl userOfLoginCode); project.hl is the map. Session-writing routes get &req + &server.sessions. gates 200/0, 46/0, 44/0; real-data identical except /login/failed now shows the parked reason for a browser that already had a session (the old copy-of-req lost it)mre
- 110c2799mission 002 (code order) 1/4: .hl files out of the root — lib/ (api, git, localtime, markdown, repos, sshgate, sshkeys, tickets, tokens, transport, users), components/styles.hl; jsoncheck.hl removed (imported nowhere); import paths only. gates 200/0, 46/0, 44/0; real-data reads + writes identicalmre
- fdfb4b1bgitoria: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 200/0, 46/0, 44/0mre
- 5b46ac84antcolony#40: LOG.md — missions 069/072 are antcolony missions (report paths on Byrodin)mre
- 5602ff41gitoria: Hybriel master 190aa11d (fc838894 GC correctness, #127 mountKids by reference, #126, #48) — tracker README flat; gates 200/0, 46/0, 44/0mre
- e85eaf01gitoria: 069 round 2 — hybriel 1a096ad3 not adopted (Markdown SSR still grows); browser gate waits for the server-side logout before restartmre
- 09ce4f3fgitoria: mission 069 re-vendor hybriel 8efba065 stopped (big SSR pages grow + slow down); lambda audit clean; old vendor keptmre
- 3dc43108antcolony#40: mission references point to the moved missionsmre
- 8d9450fdantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
- 205d5fe4gitoria: Hybriel master ff51cf46; ssh keys/tokens no double rows (session sync); gates follow #20mre
- 9b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre
- 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
- 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
- fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
- 4a2d7125initial commitmre