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
Latest commits
- cd99d2feWIP gitoria#22: always render the repo level's slot (missing-repo boot hang), pages render nothing without a repo; hydration diagnosis in reports/G2-hydrate.mdmre
- e5404b9breport: commit idsmre
- 63cfa21ereports/G2-repotabs.mdmre
- 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
- 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
- 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
- 48a6154bre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
- e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
- f06f5349re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
- c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
- f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
- fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
- 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
- 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
- 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
- 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
- 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
- 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
- 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
- e6dff0cbgitoria#23: report 005, STATUS, LOGmre