gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainbd3591c7conductor: gates run on the live day44 binarymremain/lib/tickets.hl

8.8 KB

  1. $mode = 'static'
  2. import { run } from 'hl:proc'
  3. import { readFile } from 'hl:fs'
  4. import { tryFetch } from 'hl:fetch'
  5. import { sha256 } from 'hl:crypto'
  6. import { require } from './review-output.hl'
  7. // Linked tickets as the spec source (kunibert#8). gitoria lists the ids (`pull.tickets`, `project#n`); we read the content from
  8. // the tickets service ourselves with our own read token. GET only. The token lives only in this process (never in an argv, a file
  9. // we write, a log or an error text).
  10. maxTickets = 10
  11. requestTimeoutMs = 15000 // the ONE timeout: per request
  12. maxSpecBytes = 60000 // cap of .kunibert/spec.md
  13. nameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'
  14. digits = '0123456789'
  15. // strict `project#n`: project of [A-Za-z0-9._-] (1..100, not starting with '.', no '..'), n a positive integer without leading zero (max 9 digits)
  16. validId = (String id) => {
  17. parts = id.split('#')
  18. if (parts.length != 2) { return false }
  19. project = parts[0]
  20. n = parts[1]
  21. if (project.length < 1 || project.length > 100 || project.startsWith('.') || project.includes('..')) { return false }
  22. if (n.length < 1 || n.length > 9 || n.startsWith('0')) { return false }
  23. for (ch of project.split('')) { if (!nameChars.includes(ch)) { return false } }
  24. for (ch of n.split('')) { if (!digits.includes(ch)) { return false } }
  25. return true
  26. }
  27. // The token from its file (mode 600 required, first line); null when no file is configured. A configured but unusable file is an error.
  28. readToken = (String file) => {
  29. if (file == null || file == '') { return null }
  30. mode = run(['stat', '-c', '%a', file], { timeout = 10 })
  31. require(mode.exit == 0, 'tickets token file not readable')
  32. require(mode.lines.join('').trim().endsWith('00'), 'tickets token file must have mode 600 (no group/other access)')
  33. token = readFile(file).split('\n')[0].trim()
  34. require(token != '', 'tickets token file is empty')
  35. return token
  36. }
  37. // One GET. { id, ok, status, text } ; 404/403/other failures are { ok = false, status } (the caller says "not readable"), never an error.
  38. fetchTicket = (String base, String token, String id) => {
  39. parts = id.split('#')
  40. url = base + '/api/projects/' + encodeURIComponent(parts[0]) + '/tickets/' + parts[1]
  41. r = tryFetch(url, { method = 'GET', headers = { authorization = 'Bearer ' + token, accept = 'text/markdown', 'user-agent' = 'kunibert-review' }, timeoutMs = requestTimeoutMs, maxRedirects = 0 })
  42. if (!r.ok) { return { =id, ok = false, status = r.status, text = '' } }
  43. return { =id, ok = true, status = r.status, text = r.text() }
  44. }
  45. NL = '
  46. '
  47. // "# project #n: Title" is the first line of the served markdown.
  48. titleOf = (String md) => {
  49. first = md.split(NL)[0]
  50. i = first.indexOf(': ')
  51. if (first.startsWith('# ') && i > 0) { return first.slice(i + 2).trim() }
  52. return '(no title)'
  53. }
  54. // One fetched ticket as { id, link, title, head, entries[] } : `head` is title..description (everything before "## History"),
  55. // entries are the history items ("### n · date · ..." with their text), oldest first as the service sends them.
  56. parseTicket = (String base, String id, String md) => {
  57. parts = id.split('#')
  58. let top = md
  59. let hist = ''
  60. k = md.indexOf(NL + '## History' + NL)
  61. if (k >= 0) { top = md.slice(0, k) hist = md.slice(k + (NL + '## History' + NL).length) }
  62. let entries = []
  63. let cur = null
  64. for (line of hist.split(NL)) {
  65. if (line.startsWith('### ')) {
  66. if (cur != null) { entries.push(cur) }
  67. cur = { heading = line, body = [] }
  68. } else if (cur != null) { cur.body.push(line) }
  69. }
  70. if (cur != null) { entries.push(cur) }
  71. let rest = top.split(NL)
  72. rest.shift()
  73. items = entries.map((e) => { return { heading = e.heading, body = e.body.join(NL).trim(), cut = false } })
  74. return { =id, link = base + '/' + parts[0] + '/' + parts[1], title = titleOf(md), head = rest.join(NL).trim(), entries = items }
  75. }
  76. section = (Hybrid t) => {
  77. let lines = ['## ' + t.id + ': ' + t.title, 'link: ' + t.link, '', t.head, '', '### History (oldest first)']
  78. for (e of t.entries) {
  79. lines.push('')
  80. lines.push(e.heading)
  81. if (e.cut) { lines.push('[comment text cut to keep this file under the size limit]') }
  82. else if (e.body != '') { lines.push(e.body) }
  83. }
  84. return lines.join(NL)
  85. }
  86. header = 'Spec file of Kunibert (kunibert#8): the tickets this pull request links to, read from the tickets service just now, each with its description and its history (comments and state changes) OLDEST FIRST.
  87. A later comment overrules the description and every earlier comment: where they disagree, the newest comment is the requirement.'
  88. total = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  89. return toBytes(render(tickets, notReadable, notes)).length
  90. }
  91. render = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  92. // keep the order of the PR's list: readable ones carry their index
  93. let blocks = []
  94. for (let a = 1; a <= 200; a = a + 1) {
  95. for (t of tickets) { if (t.at == a) { blocks.push({ text = section(t) }) } }
  96. for (n of notReadable) { if (n.at == a) { blocks.push({ text = '## ' + n.id + ': not readable (' + n.why + ')' }) } }
  97. }
  98. let lines = [header]
  99. for (n of notes) { lines.push('') lines.push(n) }
  100. for (b of blocks) { lines.push('') lines.push('---') lines.push('') lines.push(b.text) }
  101. return lines.join(NL) + NL
  102. }
  103. // Cut to the cap: the oldest history entries first (round robin over the tickets: every ticket's oldest, then the next oldest), their text only, the
  104. // heading stays; then, only if still over, the description of the biggest ticket. Says what was cut.
  105. fit = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  106. limit = maxSpecBytes - 400 // room for the NOTE line added at the end
  107. let cutEntries = 0
  108. let cutDesc = 0
  109. let more = true
  110. let i = 0
  111. while (total(tickets, notReadable, notes) > limit && more) {
  112. more = false
  113. for (t of tickets) {
  114. if (i < t.entries.length) {
  115. more = true
  116. if (!t.entries[i].cut && total(tickets, notReadable, notes) > limit) { t.entries[i].cut = true cutEntries = cutEntries + 1 }
  117. }
  118. }
  119. i = i + 1
  120. }
  121. let guard = 0
  122. while (total(tickets, notReadable, notes) > limit && guard < 20) {
  123. guard = guard + 1
  124. let big = tickets[0]
  125. for (t of tickets) { if (t.head.length > big.head.length) { big = t } }
  126. excess = total(tickets, notReadable, notes) - limit
  127. let keep = big.head.length - excess - 200
  128. if (keep < 0) { keep = 0 }
  129. cutDesc = cutDesc + 1
  130. big.head = big.head.slice(0, keep) + NL + '[description cut to keep this file under the size limit]'
  131. }
  132. let parts = []
  133. if (cutEntries > 0) { parts.push('the text of the ' + cutEntries + ' oldest history entries') }
  134. if (cutDesc > 0) { parts.push('part of a description') }
  135. if (parts.length > 0) { notes.push('NOTE: this file was cut to stay under ' + maxSpecBytes + ' bytes: ' + parts.join(' and ') + ' (the newest comments and the descriptions are kept first). Read the full tickets at the links if a cut matters.') }
  136. return render(tickets, notReadable, notes)
  137. }
  138. // ids -> { text, hash, ids, readable, notReadable } ; text == null when there is nothing to show (no ids, or no token configured).
  139. // base/tokenFile null: no tickets are read at all (today's "no ticket linked").
  140. buildSpec = (String base, String tokenFile, Hybrid ids) => {
  141. if (base == null || tokenFile == null || ids == null || ids.length == 0) { return { text = null, hash = null, readable = 0, notReadable = 0 } }
  142. token = readToken(tokenFile)
  143. let root = base.endsWith('/') ? base.slice(0, base.length - 1) : base
  144. let tickets = []
  145. let bad = []
  146. let notes = []
  147. let seen = []
  148. let at = 0
  149. for (id of ids.slice(0, 100)) {
  150. if (seen.includes(id)) { continue }
  151. seen.push(id)
  152. at = at + 1
  153. if (at > 200) { break }
  154. if (!validId(id)) { bad.push({ id = id.slice(0, 60), why = 'not a valid project#n id, ignored', =at }) continue }
  155. if (seen.length > maxTickets) { notes.push('NOTE: the PR links more than ' + maxTickets + ' tickets; only the first ' + maxTickets + ' are read.') break }
  156. r = fetchTicket(root, token, id)
  157. if (!r.ok) {
  158. why = r.status == 404 ? 'HTTP 404, no such ticket' : (r.status == 403 || r.status == 401 ? 'HTTP ' + r.status + ', no access' : 'HTTP ' + r.status)
  159. bad.push({ =id, =why, =at })
  160. } else {
  161. t = parseTicket(root, id, r.text)
  162. t.at = at
  163. tickets.push(t)
  164. }
  165. }
  166. if (tickets.length == 0 && bad.length == 0) { return { text = null, hash = null, readable = 0, notReadable = 0 } }
  167. text = fit(tickets, bad, notes)
  168. return { =text, hash = sha256(text), readable = tickets.length, notReadable = bad.length }
  169. }

Branches

Latest commits

  • bd3591c7conductor: gates run on the live day44 binarymre
  • 47f747a7records: jukebox #9 fix livemre
  • 0a3169fdMerge branch 'vendored-size' into mainKunibert
  • e447bb09Vendored size fix: keep renames with an app side whole; type the filter parameter (L10)mre
  • 99ccc742Vendored size fix: no renames in the app diff, filter the resumed delta toomre
  • 1a52e100|||PR Diff size limit counts only app files when vendored paths are setmre
  • 50a1867estatus: jukebox #9mre
  • c57484e6log: jukebox #9 diff sizemre
  • 795a20carecords: guard fix live (PR 8)mre
  • 6981d96fMerge branch 'fresh-on-nocmd' into mainKunibert
  • 37c47840runner: one shared prompt for both fresh-session fallbacks (L16)mre
  • f945d2e1|||PR runner: a resumed run with no command on a new head gets one fresh session instead of a tool errormre
  • 10b2a49estatus: PR 314, guard fix proposedmre
  • e73995a3log: hycon PR 314 approved after fresh reviewmre
  • f60427falog: hycon PR 314 false tool errormre
  • 481974f7log: Hermes accepts PR 259 findingmre
  • bb0976acrecords: hycon PR 259 design findingmre
  • 5c637c24records: hycon PR 259 false tool errormre
  • a344c406log: routger 132 reviewedmre
  • 56e70dcbrecords: review time limit 1800 s (routger 132)mre