gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit24b479e724b479e7W60: record final gates, Chrome failures and AE evidencemre24b479e7/lib/tickets.hl

11.3 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 { randomBytes } from 'hl:crypto'
  14. import { fetch } from 'hl:fetch'
  15. import { MPackDB } from 'hl:mpackdb'
  16. import { now } from 'hl:time'
  17. import { NL, storageDir, countOfList, plainError, repoOrigin } from './util.hl'
  18. import { userRecord, userIdOfSession } from './users.hl'
  19. import { repoBySlug, setTicketsConnection, ownsRepo } from './repos.hl'
  20. import { gitRaw } from './git.hl'
  21. import { tabs, isHexId } from './git-helpers.hl'
  22. import { pullsNow } from './pulls.hl'
  23. import { ticketsUrl, maxRows, agent, isCode, viewOf, writeHeaders, whyOf, refsOf, fixedBy } from './tickets-helpers.hl'
  24. // what was already told to tickets (a push arrives again with the same commits): one row per `<slug> <what> <sha> <n>`
  25. static seenTable = new MPackDB(file = storageDir + '/ticketlinks.db', primaryKey = '@id', indexes = ['!key'])
  26. // ---- the connect flow ------------------------------------------------------------------------------
  27. static connectHref = (slug, state) => {
  28. back = repoOrigin(slug) + '/settings/connected'
  29. return ticketsUrl + '/connect?app=gitoria&label=' + encodeURIComponent(slug) + '&return=' + encodeURIComponent(back) + '&state=' + encodeURIComponent(state)
  30. }
  31. // the code tickets sent back → { key, project, title, api } | { error }
  32. static exchangeConnect = (code) => {
  33. if (!isCode(code)) { return { error = 'that is not a code from tickets' } }
  34. r = fetch(ticketsUrl + '/api/connect/exchange', { method = 'POST' json = { code = code } headers = { 'user-agent' = agent } timeoutMs = 10000 })
  35. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }
  36. j = r.json()
  37. if (r.status != 200 || j == null || j.key == null || j.project == null || j.api == null) {
  38. return { error = 'tickets.worldapi.org refused the connection (' + r.status + (j != null && j.error != null ? ': ' + j.error : '') + ')' }
  39. }
  40. 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' } }
  41. return { key = j.key project = '' + j.project title = j.title != null ? '' + j.title : '' + j.project api = j.api }
  42. }
  43. // THE END OF THE CONNECT FLOW (/settings/connected, lib/api.hl connectBack): tickets' answer `q` (?code&state, or ?error),
  44. // `want` = the nonce parked in the session at the start → '' (connected: the key is stored per repo, the history so far
  45. // is marked as told) or the note for the settings page. The caller checked that the person owns the repo.
  46. static finishConnect = (slug, want, q) => {
  47. if (q.error != null && q.code == null) { return 'Tickets did not connect: ' + ('' + q.error).slice(0, 100) }
  48. if (want == null || want != slug + '.' + q.state) { return 'That connection was not started here (or was used already) — click "Tickets: connect" again.' }
  49. x = exchangeConnect(q.code)
  50. if (x.error != null) { return x.error }
  51. r = setTicketsConnection(slug, x.key, x.project, x.title, x.api)
  52. if (r.error != null) { return r.error }
  53. notifyPush(slug, true)
  54. return ''
  55. }
  56. // ---- reading ---------------------------------------------------------------------------------------
  57. // the project of a connected repo (the server-side record has the key): { key, api, project } or null
  58. static connectionOf = (slug) => {
  59. r = repoBySlug(slug)
  60. if (r == null || r.tktKey == null || r.tktKey == '' || r.tktApi == null || r.tktApi == '') { return null }
  61. return { key = r.tktKey api = r.tktApi project = r.tktProject title = r.tktTitle }
  62. }
  63. // the repo's tickets, newest update first (tickets' order): { tickets, count, truncated, project, url } or { error }
  64. static loadTickets = (slug) => {
  65. c = connectionOf(slug)
  66. if (c == null) { return { error = 'not connected' } }
  67. r = fetch(c.api + '/tickets', { method = 'GET' headers = { 'user-agent' = agent } timeoutMs = 10000 })
  68. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' } }
  69. base = { project = c.project url = ticketsUrl + '/projects/' + c.project }
  70. j = r.status == 200 ? r.json() : null
  71. if (j == null || j.tickets == null) { return { error = 'tickets.worldapi.org answered ' + r.status } }
  72. out = []
  73. for (t of j.tickets) { if (out.length < maxRows) { out.push(viewOf(t)) } }
  74. base.tickets = out
  75. base.count = j.tickets.length
  76. base.truncated = j.tickets.length > out.length
  77. return base
  78. }
  79. // ---- writing (as a person) ---------------------------------------------------------------------------
  80. // open a ticket for a repo as the person with this ident id: { ticket } or { error, field }
  81. static openTicket = (slug, identity, subject, summary) => {
  82. s = subject == null ? '' : ('' + subject).trim()
  83. if (s == '') { return { error = 'the ticket needs a subject' field = 'subject' } }
  84. bad = plainError(s, 200, 'the subject')
  85. if (bad != null) { return { error = bad field = 'subject' } }
  86. body = summary == null ? '' : ('' + summary).trim()
  87. if (body.length > 20000) { return { error = 'the text is too long (at most 20000 characters)' field = 'summary' } }
  88. c = connectionOf(slug)
  89. if (c == null) { return { error = 'this repository is not connected to a tickets project (see Settings)' field = '' } }
  90. r = fetch(c.api + '/tickets', { method = 'POST' json = { subject = s summary = body } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  91. if (r == null || r.status == null || r.status == 0) { return { error = 'tickets.worldapi.org did not answer' field = '' } }
  92. j = r.json()
  93. if ((r.status != 201 && r.status != 200) || j == null || j.ticket == null) {
  94. 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 = '' } }
  95. return { error = 'tickets.worldapi.org refused the ticket (' + r.status + whyOf(r).slice(0, 200) + ')' field = '' }
  96. }
  97. return { ticket = viewOf(j.ticket) }
  98. }
  99. // a comment on ticket n as a person → true when tickets took it, or answered it with a refusal (no retry then)
  100. static commentTicket = (c, identity, n, text) => {
  101. r = fetch(c.api + '/tickets/' + n + '/comments', { method = 'POST' json = { text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  102. return r != null && r.status != null && r.status > 0 && r.status < 500
  103. }
  104. static stateTicket = (c, identity, n, state, text) => {
  105. r = fetch(c.api + '/tickets/' + n + '/state', { method = 'POST' json = { state = state text = text } headers = writeHeaders(c, identity) timeoutMs = 10000 })
  106. return r != null && r.status != null && r.status > 0 && r.status < 500
  107. }
  108. // ---- after a push or a merge: tell tickets ---------------------------------------------------------------
  109. static seen = (key) => { return countOfList(seenTable.find('key', key)) > 0 }
  110. static markSeen = (key) => { seenTable.put({ key = key created = now() }) }
  111. // The recent commits of every branch that name a ticket: "mentioned in commit …" as a comment on it, once per
  112. // commit and ticket. `quiet` only marks them (the moment of connecting: old history is not announced).
  113. // Then every MERGED pull request whose title says "fixes #N" sets ticket N to review, once. The person is the repo's
  114. // owner (pushes and merges are the owner's). Failures are silent (the push has succeeded); a refusal by tickets is not retried.
  115. static notifyPush = (slug, quiet) => {
  116. c = connectionOf(slug)
  117. repo = repoBySlug(slug)
  118. if (c == null || repo == null) { return null }
  119. owner = userRecord(repo.owner)
  120. if (owner == null || owner.identity == null) { return null }
  121. lg = gitRaw(slug, ['log', '-100', '--branches', '--format=%H%x09%s'])
  122. if (lg != null && lg.exit == 0) {
  123. for (l of lg.lines) {
  124. f = tabs(l)
  125. if (f.length >= 2 && isHexId(f[0])) {
  126. sub = f.slice(1).join(' ')
  127. for (x of refsOf(sub)) {
  128. key = slug + ' commit ' + f[0] + ' ' + x.n
  129. if (!seen(key)) {
  130. 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) }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. got = pullsNow(slug, repo.branch != null ? repo.branch : '')
  137. if (got != null && !got.isMessage) {
  138. for (p of got.pulls) {
  139. if (p.isMerged) {
  140. for (n of fixedBy(p.title)) {
  141. key = slug + ' pr ' + p.sha + ' ' + n
  142. if (!seen(key)) {
  143. 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) }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. return null
  150. }
  151. // Session state belongs to the tickets connect flow, shared by its thin function routes.
  152. static startConnect = (slug, session, &sessions) => {
  153. nonce = randomBytes(16)
  154. session.data.connectState = slug + '.' + nonce
  155. session.data.connectNote = null
  156. sessions.save(session)
  157. return connectHref(slug, nonce)
  158. }
  159. static connectLog = (slug, outcome, hasState) => {
  160. console.log('connect-back ' + slug + ' ' + outcome + ' state=' + (hasState ? 'present' : 'absent'))
  161. }
  162. static completeConnect = (slug, session, q, &sessions) => {
  163. hasState = q.state != null
  164. let s = session
  165. fresh = s == null
  166. if (s == null || userIdOfSession(s) == null || !ownsRepo(slug, s)) {
  167. why = s == null ? 'no-session' : (userIdOfSession(s) == null ? 'session-not-signed-in' : 'not-owner')
  168. connectLog(slug, 'refused reason=' + why, hasState)
  169. if (fresh) { s = sessions.mint() }
  170. s.data.connectNote = 'The connection to tickets did not finish: this browser is not signed in to gitoria as the repo owner. Sign in and press Connect again.'
  171. sessions.save(s)
  172. return { session = s fresh = fresh }
  173. }
  174. want = s.data.connectState
  175. s.data.connectState = null
  176. note = finishConnect(slug, want, q)
  177. s.data.connectNote = note == '' ? null : note
  178. sessions.save(s)
  179. if (note == '') {
  180. connectLog(slug, 'connected', hasState)
  181. } else if (q.error != null && q.code == null) {
  182. connectLog(slug, 'tickets-error', hasState)
  183. } else if (want == null || want != slug + '.' + q.state) {
  184. connectLog(slug, 'state-mismatch', hasState)
  185. } else {
  186. connectLog(slug, 'exchange-failed', hasState)
  187. }
  188. return { session = s fresh = false }
  189. }

Branches

Latest commits

  • 24b479e7W60: record final gates, Chrome failures and AE evidencemre
  • 5e11184cW60: record partial validation, framework gaps and cleanupmre
  • 20fe13fagitoria#33: centralize login and tickets connection statemre
  • 859754c0gitoria#29: retain the offline note and record the framework gapmre
  • d140bc04gitoria#30: reproduce the upsert ordering constraintmre
  • 6960e8f8gitoria#28: keep form values in bound component membersmre
  • 0db27200gitoria#32: share character checks and environment lookupmre
  • 452f6a1amission W60: logic clean-upsmre
  • 1448c899log: cleanup, #49 GPTBot, tracker mentions on #36mre
  • fcf95936log: v1.5.1 provenmre
  • 22105420log: W51b livemre
  • 7e8aabeareport: W51b pull source verificationmre
  • 6c18de76fix: preserve pull sources across containing branchesmre
  • fc36fa2elog: W51b pull source bugmre
  • 56bc1220log: v1.5.1 livemre
  • 845fc0aareport: W51mre
  • 83127391fix: return v1.5.1 app and merge error detailsmre
  • b07d0e97log: W51 v1.5.1 startedmre
  • 0b417d06docs: apps-api v1.5 user tokens (#48, W49)mre
  • d046940alog: vinzenz contract note sentmre