gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit00517faf00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG00517faf/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

  • 00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG
  • d9e977f0reports/MG-mobile.md, LOGMG
  • 2f2725actests/browser.mjs: commit-row check (one row, centred date)MG
  • 4df1ea01tests/browser.mjs: MG phone checks (list first, no dead strip, one branch heading, tab grid, commit rows, PR title row, ticket columns, wrapping commands)MG
  • c52424e9gitoria mobile: repo list before the forms, 3x2 tab grid, no duplicate branch heading, ordered commit rows, aligned ticket rows, pull title on its own row, wrapping clone commands, no dead strip under formsMG
  • 3a95252dG3: cache parsed README blocks by sha256 of the text (bounded LRU), reportmre
  • 570138cddeploy: security gate (tests/security.mjs)mre
  • da6b20fcgitoria SG: security audit fixes (uploadMax 0, Secure cookie on https, session rotation at login, nosniff on function routes, no identity in page data) + tests/security.mjs gate + reportmre
  • f98c981fgitoria B1: report wordingmre
  • c6364cedgitoria B1: compact Markdown blocks (one kind flag per block/span, empty and false fields omitted); same rendered HTML, page values -67%mre
  • 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