gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mremain/lib/tickets.hl

9.7 KB

  1. // lib/tickets.hl — THE TICKETS OF A REPO (gitoria#10, gitoria#18; CONCEPT "/tickets"). The tickets are NOT stored in
  2. // gitoria: they live in a tickets.worldapi.org project the repo's OWNER connected in the repo's settings
  3. // (docs: antcolony-docs/docs/tickets-connect-apps.md). The connect flow:
  4. // 1. /settings/connect (lib/api.hl connectStart) sends the owner's browser to <tickets>/connect?app=gitoria&label=<slug>&return=…&state=…
  5. // 2. tickets brings them back to /settings/connected?code=…&state=… (the state is the nonce of step 1, kept in the session)
  6. // 3. exchangeConnect(code): POST <tickets>/api/connect/exchange { code } → { key, project, title, api }, from THIS server.
  7. // The key is stored per repo (repos.hl, mpackdb) and never sent to a page.
  8. // Every write then carries `Authorization: Bearer <key>` and `X-Tickets-Identity: <ident id of the person>`: the person
  9. // (not gitoria) is the author, under the project's roles. Reads need no key.
  10. // Links both ways: "#12" in a commit subject or a pull request title links ticket 12 (refParts); a push posts
  11. // "mentioned in commit …" on that ticket, and a merged `|||PR` whose title says "fixes #12" sets ticket 12 to review (notifyPush).
  12. // Config: GITORIA_TICKETS_URL (tickets-helpers.hl). The shapes, the `#12` references: tickets-helpers.hl.
  13. import { fetch } from 'hl:fetch'
  14. import { MPackDB } from 'hl:mpackdb'
  15. import { now } from 'hl:time'
  16. import { NL, storageDir, countOfList, plainError, repoOrigin } from './util.hl'
  17. import { userRecord } from './users.hl'
  18. import { repoBySlug, setTicketsConnection } from './repos.hl'
  19. import { gitRaw } from './git.hl'
  20. import { tabs, isHexId } from './git-helpers.hl'
  21. import { pullsNow } from './pulls.hl'
  22. import { ticketsUrl, maxRows, agent, isCode, viewOf, writeHeaders, whyOf, refsOf, fixedBy } from './tickets-helpers.hl'
  23. // what was already told to tickets (a push arrives again with the same commits): one row per `<slug> <what> <sha> <n>`
  24. static seenTable = new MPackDB(file = storageDir + '/ticketlinks.db', primaryKey = '@id', indexes = ['!key'])
  25. // ---- the connect flow ------------------------------------------------------------------------------
  26. static connectHref = (slug, state) => {
  27. back = repoOrigin(slug) + '/settings/connected'
  28. return ticketsUrl + '/connect?app=gitoria&label=' + encodeURIComponent(slug) + '&return=' + encodeURIComponent(back) + '&state=' + encodeURIComponent(state)
  29. }
  30. // the code tickets sent back → { key, project, title, api } | { error }
  31. static exchangeConnect = (code) => {
  32. if (!isCode(code)) { return { error = 'that is not a code from tickets' } }
  33. r = fetch(ticketsUrl + '/api/connect/exchange', { method = 'POST' json = { code = code } headers = { 'user-agent' = agent } timeoutMs = 10000 })
  34. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }
  35. j = r.json()
  36. if (r.status != 200 || j == null || j.key == null || j.project == null || j.api == null) {
  37. return { error = 'tickets.worldapi.org refused the connection (' + r.status + (j != null && j.error != null ? ': ' + j.error : '') + ')' }
  38. }
  39. 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' } }
  40. return { key = j.key project = '' + j.project title = j.title != null ? '' + j.title : '' + j.project api = j.api }
  41. }
  42. // THE END OF THE CONNECT FLOW (/settings/connected, lib/api.hl connectBack): tickets' answer `q` (?code&state, or ?error),
  43. // `want` = the nonce parked in the session at the start → '' (connected: the key is stored per repo, the history so far
  44. // is marked as told) or the note for the settings page. The caller checked that the person owns the repo.
  45. static finishConnect = (slug, want, q) => {
  46. if (q.error != null && q.code == null) { return 'Tickets did not connect: ' + ('' + q.error).slice(0, 100) }
  47. if (want == null || want != slug + '.' + q.state) { return 'That connection was not started here (or was used already) — click "Tickets: connect" again.' }
  48. x = exchangeConnect(q.code)
  49. if (x.error != null) { return x.error }
  50. r = setTicketsConnection(slug, x.key, x.project, x.title, x.api)
  51. if (r.error != null) { return r.error }
  52. notifyPush(slug, true)
  53. return ''
  54. }
  55. // ---- reading ---------------------------------------------------------------------------------------
  56. // the project of a connected repo (the server-side record has the key): { key, api, project } or null
  57. static connectionOf = (slug) => {
  58. r = repoBySlug(slug)
  59. if (r == null || r.tktKey == null || r.tktKey == '' || r.tktApi == null || r.tktApi == '') { return null }
  60. return { key = r.tktKey api = r.tktApi project = r.tktProject title = r.tktTitle }
  61. }
  62. // the repo's tickets, newest update first (tickets' order): { tickets, count, truncated, project, url } or { error }
  63. static loadTickets = (slug) => {
  64. c = connectionOf(slug)
  65. if (c == null) { return { error = 'not connected' } }
  66. r = fetch(c.api + '/tickets', { method = 'GET' headers = { 'user-agent' = agent } timeoutMs = 10000 })
  67. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }
  68. base = { project = c.project url = ticketsUrl + '/projects/' + c.project }
  69. j = r.status == 200 ? r.json() : null
  70. if (j == null || j.tickets == null) { return { error = 'tickets.worldapi.org answered ' + r.status } }
  71. out = []
  72. for (t of j.tickets) { if (out.length < maxRows) { out.push(viewOf(t)) } }
  73. base.tickets = out
  74. base.count = j.tickets.length
  75. base.truncated = j.tickets.length > out.length
  76. return base
  77. }
  78. // ---- writing (as a person) ---------------------------------------------------------------------------
  79. // open a ticket for a repo as the person with this ident id: { ticket } or { error, field }
  80. static openTicket = (slug, identity, subject, summary) => {
  81. s = subject == null ? '' : ('' + subject).trim()
  82. if (s == '') { return { error = 'the ticket needs a subject' field = 'subject' } }
  83. bad = plainError(s, 200, 'the subject')
  84. if (bad != null) { return { error = bad field = 'subject' } }
  85. body = summary == null ? '' : ('' + summary).trim()
  86. if (body.length > 20000) { return { error = 'the text is too long (at most 20000 characters)' field = 'summary' } }
  87. c = connectionOf(slug)
  88. if (c == null) { return { error = 'this repository is not connected to a tickets project (see Settings)' field = '' } }
  89. r = fetch(c.api + '/tickets', { method = 'POST' json = { subject = s summary = body } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  90. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' field = '' } }
  91. j = r.json()
  92. if ((r.status != 201 && r.status != 200) || j == null || j.ticket == null) {
  93. 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 = '' } }
  94. return { error = 'tickets.worldapi.org refused the ticket (' + r.status + whyOf(r).slice(0, 200) + ')' field = '' }
  95. }
  96. return { ticket = viewOf(j.ticket) }
  97. }
  98. // a comment on ticket n as a person → true when tickets took it, or answered it with a refusal (no retry then)
  99. static commentTicket = (c, identity, n, text) => {
  100. r = fetch(c.api + '/tickets/' + n + '/comments', { method = 'POST' json = { text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  101. return r != null && r.status != null && r.status > 0 && r.status < 500
  102. }
  103. static stateTicket = (c, identity, n, state, text) => {
  104. r = fetch(c.api + '/tickets/' + n + '/state', { method = 'POST' json = { state = state text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  105. return r != null && r.status != null && r.status > 0 && r.status < 500
  106. }
  107. // ---- after a push or a merge: tell tickets ---------------------------------------------------------------
  108. static seen = (key) => { return countOfList(seenTable.find('key', key)) > 0 }
  109. static markSeen = (key) => { seenTable.put({ key = key created = now() }) }
  110. // The recent commits of every branch that name a ticket: "mentioned in commit …" as a comment on it, once per
  111. // commit and ticket. `quiet` only marks them (the moment of connecting: old history is not announced).
  112. // Then every MERGED pull request whose title says "fixes #N" sets ticket N to review, once. The person is the repo's
  113. // owner (pushes and merges are the owner's). Failures are silent (the push has succeeded); a refusal by tickets is not retried.
  114. static notifyPush = (slug, quiet) => {
  115. c = connectionOf(slug)
  116. repo = repoBySlug(slug)
  117. if (c == null || repo == null) { return null }
  118. owner = userRecord(repo.owner)
  119. if (owner == null || owner.identity == null) { return null }
  120. lg = gitRaw(slug, ['log', '-100', '--branches', '--format=%H%x09%s'])
  121. if (lg != null && lg.exit == 0) {
  122. for (l of lg.lines) {
  123. let f = tabs(l)
  124. if (f.length >= 2 && isHexId(f[0])) {
  125. let sub = f.slice(1).join(' ')
  126. for (x of refsOf(sub)) {
  127. let key = slug + ' commit ' + f[0] + ' ' + x.n
  128. if (!seen(key)) {
  129. 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) }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. got = pullsNow(slug, repo.branch != null ? repo.branch : '')
  136. if (got != null && !got.isMessage) {
  137. for (p of got.pulls) {
  138. if (p.isMerged) {
  139. for (n of fixedBy(p.title)) {
  140. let key = slug + ' pr ' + p.sha + ' ' + n
  141. if (!seen(key)) {
  142. 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) }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. return null
  149. }

Branches

Latest commits

  • 4f47843egate: ticket links use the tickets short URL (/<slug>/<n>, tickets#25)mre
  • 86605446mission 002 (code order) 4/4: README file map + import order + 'Same output' test + gate run with a tickets HEAD copy, STATUS (entry, lessons), LOG, report; tests/realdata-baseline.mjs + realdata-compare.py (a cleanup answers the same on live data: pages, modules, API, git over HTTPS and SSH, faces), tests/letcount.pymre
  • cc7bf7bamission 002 (code order) 3/4: let only where a variable is reassigned or re-bound in a loop body (289 lets → plain declarations; 213 left: 125 reassigned, 88 loop-bound; no member/import/param clash). gates 200/0, 46/0, 44/0; real-data reads + writes identical (browser modules: var → const only)mre
  • 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