gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8d3fb2e78d3fb2e7verdict guard: unknown severity blocks; L2mre8d3fb2e7/lib/review-post.hl

12.1 KB

  1. $mode = 'static'
  2. import { run } from 'hl:proc'
  3. import { require } from './review-output.hl'
  4. import { call, must, putStatus, scrub, getToken } from './gitoria-api.hl'
  5. import { reviewsRoute, repliesRoute, resolveRoute } from './gitoria-api.hl'
  6. import { threadUpdatesFrom } from './review-memory.hl'
  7. NL = JSON.parse('"\n"') // HL strings have no backslash escapes: JSON.parse decodes them
  8. TAB = JSON.parse('"\t"')
  9. maxInline = 100
  10. maxBody = 65536 // [OPEN] contract v1.2: body limit 65,536 characters (ONE constant)
  11. badges = { blocker = '🛑', major = '🔶', minor = '🔹', nit = '💬' }
  12. axisNames = { correctness = 'Correctness', spec = 'Spec', standards = 'Standards' }
  13. axisOrder = ['correctness', 'spec', 'standards']
  14. // What this process told gitoria about the current run: the error status needs it.
  15. ctx = { pr = null, head = null, pending = false, finished = false }
  16. cap = (String text, Number n) => {
  17. if (text.length <= n) { return text }
  18. return text.slice(0, n - 1) + '…'
  19. }
  20. // ---- status
  21. startStatus = (Number pr, String head, Boolean willPost) => {
  22. ctx.pr = pr
  23. ctx.head = head
  24. ctx.finished = false
  25. ctx.pending = false
  26. if (!willPost) { return false }
  27. r = putStatus(head, 'pending', 'Kunibert review', 'Review running')
  28. ctx.pending = r.ok
  29. return r.ok
  30. }
  31. finishStatus = (String state, String summary) => {
  32. r = putStatus(ctx.head, state, 'Kunibert review', cap(summary, 300))
  33. ctx.finished = true
  34. return r.ok
  35. }
  36. // A failed run: status `error` with a short message (first line only, no stack), never a PR comment. The caller logs the full error.
  37. errorStatus = (String message) => {
  38. if (ctx.head == null || ctx.finished || !ctx.pending) { return false }
  39. // hlError text starts with "<file>.hl:<line>:<col>: ": the PR gets the reason only
  40. let reason = message.split(NL)[0]
  41. if (reason.includes('.hl:')) {
  42. let parts = reason.split('.hl:')
  43. let tail = parts.slice(1).join('.hl:').split(': ')
  44. if (tail.length > 1) { reason = tail.slice(1).join(': ') }
  45. }
  46. return finishStatus('error', 'Review could not run: ' + cap(scrub(reason), 140))
  47. }
  48. // ---- threads from gitoria -> input of review-memory's threadUpdatesFrom (humans only; seq is the watermark)
  49. // Findings posted before severities were stored and kept off the diff (gid null) exist only as a line of an earlier review body.
  50. needsBodies = (Hybrid state) => {
  51. for (t of state.threads) { if (!t.vendored && !t.resolved && !t.settled && t.gid == null && t.severity == null) { return true } }
  52. return false
  53. }
  54. threadInput = (Hybrid state, Hybrid apiThreads, Hybrid apiReviews = []) => {
  55. let input = []
  56. for (t of state.threads) {
  57. if (t.vendored || t.resolved || t.settled || t.gid != null || t.severity != null) { continue }
  58. for (sev of ['blocker', 'major', 'minor', 'nit']) {
  59. line = '- ' + title({ severity = sev, axis = t.axis, title = t.title }) + ' (`' + t.path + ':' + t.line + '`)'
  60. for (rv of apiReviews) { if (rv.body != null && rv.body.includes(line)) { input.push({ key = t.key, severity = sev }) } }
  61. }
  62. }
  63. for (a of apiThreads) {
  64. let key = null
  65. for (t of state.threads) { if (t.gid == a.id) { key = t.key } }
  66. if (key == null) { continue }
  67. let comments = []
  68. for (c of a.comments) {
  69. if (c.author.type == 'user') { comments.push({ id = c.seq, author = c.author.name, body = c.body }) }
  70. }
  71. let severity = null
  72. if (a.comments.length > 0 && a.comments[0].author.type == 'app') {
  73. for (sev of ['blocker', 'major', 'minor', 'nit']) {
  74. if (a.comments[0].body.startsWith('**' + badges[sev] + ' ' + sev.slice(0, 1).toUpperCase() + sev.slice(1) + ' · ')) { severity = sev }
  75. }
  76. }
  77. input.push({ =key, resolved = a.resolved, =severity, =comments })
  78. }
  79. return input
  80. }
  81. // ---- the diff gitoria checks lines against: git diff -M -U3 <merge-base> <head>, new side
  82. newSideRanges = (String gitDir, String mergeBase, String head) => {
  83. r = run(['git', '-c', 'core.quotePath=false', 'diff', '--no-ext-diff', '--no-textconv', '-M', '-U3', mergeBase + '..' + head, '--'], { cwd = gitDir, timeout = 60 })
  84. require(r.exit == 0, 'git diff failed (' + r.exit + ')')
  85. let ranges = {}
  86. let path = null
  87. for (line of r.lines) {
  88. if (line.startsWith('+++ ')) {
  89. p = line.slice(4).split(TAB)[0]
  90. if (p == '/dev/null') { path = null } else { path = p.slice(2) if (ranges[path] == null) { ranges[path] = [] } }
  91. continue
  92. }
  93. if (path == null || !line.startsWith('@@ ')) { continue }
  94. plus = line.split(' ')[2]
  95. parts = plus.slice(1).split(',')
  96. start = toNumber(parts[0])
  97. count = parts.length > 1 ? toNumber(parts[1]) : 1
  98. if (count > 0) { ranges[path].push([start, start + count - 1]) }
  99. }
  100. return ranges
  101. }
  102. onDiff = (Hybrid ranges, String path, Number line) => {
  103. if (ranges[path] == null) { return false }
  104. for (rg of ranges[path]) { if (line >= rg[0] && line <= rg[1]) { return true } }
  105. return false
  106. }
  107. // ---- the earlier reviewer's comment format
  108. title = (Hybrid f) => {
  109. sev = f.severity.slice(0, 1).toUpperCase() + f.severity.slice(1)
  110. return badges[f.severity] + ' ' + sev + ' · ' + axisNames[f.axis] + ': ' + f.title
  111. }
  112. // No GitHub-style `suggestion` blocks: a fix is a normal code block.
  113. plain = (String text) => { return text.replaceAll('```suggestion', '```') }
  114. inlineBody = (Hybrid f) => { return '**' + title(f) + '**' + NL + NL + plain(f.body) }
  115. // inline[i] says whether finding i goes inline. newKeys[i] is its thread key.
  116. reviewBody = (Hybrid state, Number firstNew, Hybrid result, Hybrid inline) => {
  117. let parts = [result.summary]
  118. for (axis of axisOrder) {
  119. let fresh = 0
  120. for (f of result.findings) { if (f.axis == axis) { fresh = fresh + 1 } }
  121. let open = []
  122. for (let i = 0; i < firstNew; i = i + 1) {
  123. t = state.threads[i]
  124. if (t.axis == axis && !t.resolved && !t.settled) { open.push('- ' + t.key + ' ' + t.path + ':' + t.line + ' ' + t.title) }
  125. }
  126. let section = '### ' + axisNames[axis] + ' (' + fresh + ' new, ' + open.length + ' still open)' + NL + result.axes[axis]
  127. let off = []
  128. for (let i = 0; i < result.findings.length; i = i + 1) {
  129. f = result.findings[i]
  130. if (f.axis == axis && !inline[i]) { off.push('- ' + title(f) + ' (`' + f.path + ':' + f.line + '`)' + NL + ' ' + plain(f.body).replaceAll(NL, NL + ' ')) }
  131. }
  132. if (off.length > 0) { section = section + NL + NL + '_Not on changed lines:_' + NL + off.join(NL) }
  133. if (open.length > 0) { section = section + NL + NL + '_Still open from earlier rounds:_' + NL + open.join(NL) }
  134. parts.push(section)
  135. }
  136. if (result.framework != null && result.framework.length > 0) {
  137. let section = '### Framework (vendored, not blocking)' + NL + "These paths are vendored framework code; the findings go to the framework's maintainers and do not affect this verdict."
  138. if (result.vendoredLink != null) { section = section + NL + result.vendoredLink }
  139. for (f of result.framework) { section = section + NL + '- ' + title(f) + ' (`' + f.path + ':' + f.line + '`)' + NL + ' ' + plain(f.body).replaceAll(NL, NL + ' ') }
  140. parts.push(section)
  141. }
  142. if (result.vendoredOmitted == true) { parts.push('### Framework (vendored, not reviewed)' + NL + 'The full diff is over the size limit, so only the app files were reviewed; the vendored files were left out.') }
  143. if (result.heldBy != null && result.heldBy.length > 0) { parts.push('_Verdict held at request changes: open Blocker/Major thread(s) (or of unknown severity) ' + result.heldBy.join(', ') + '. A thread counts as closed once it is resolved on gitoria._') }
  144. parts.push('Gates: ' + result.gates)
  145. let body = plain(parts.join(NL + NL))
  146. if (body.length > maxBody) { body = body.slice(0, maxBody - 60) + NL + NL + '_(truncated: the review was longer than the body limit)_' }
  147. return body
  148. }
  149. // ---- the review post. Returns { skipped } (stale head: nothing posted) or { posted, moved, warnings }.
  150. // `gids`: [{ index (in state.threads), id }] for the caller to store (state is passed by value).
  151. // prev = { head, verdict, forced } of BEFORE this run: a review is posted only when there is something to say (new commits, new
  152. // findings, a changed verdict, or the run was asked for); otherwise { quiet } and the replies go to their threads only.
  153. postReview = (Number pr, String head, Hybrid state, Number firstNew, Hybrid result, String mergeBase, String gitDir, Hybrid prev) => {
  154. if (prev.head == head && result.findings.length == 0 && prev.verdict == result.verdict && !prev.forced) { return { quiet = true } }
  155. getToken(true) // a fresh token right before the write phase (the model run may have taken long)
  156. ranges = newSideRanges(gitDir, mergeBase, head)
  157. let inline = []
  158. let used = 0
  159. for (f of result.findings) {
  160. ok = used < maxInline && onDiff(ranges, f.path, f.line)
  161. if (ok) { used = used + 1 }
  162. inline.push(ok)
  163. }
  164. let retried = false
  165. let r = null
  166. while (true) {
  167. let comments = []
  168. let at = []
  169. for (let i = 0; i < result.findings.length; i = i + 1) {
  170. if (inline[i]) { comments.push({ path = result.findings[i].path, line = result.findings[i].line, body = inlineBody(result.findings[i]) }) at.push(i) }
  171. }
  172. r = call('POST', reviewsRoute(pr), { =head, verdict = result.verdict, body = reviewBody(state, firstNew, result, inline), =comments })
  173. if (r.status == 409 && r.code == 'stale_head') { return { skipped = 'stale_head' } }
  174. if (r.status == 422 && r.details != null && !retried) {
  175. retried = true
  176. for (d of r.details) { if (d.index != null && d.index >= 0 && d.index < at.length) { inline[at[d.index]] = false } }
  177. continue
  178. }
  179. must(r, 'POST review')
  180. // thread ids: the answer lists the threads of the inline comments in order; match by path and line, first unused
  181. let taken = []
  182. let gids = []
  183. for (let k = 0; k < at.length; k = k + 1) {
  184. f = result.findings[at[k]]
  185. for (let j = 0; j < r.body.threads.length; j = j + 1) {
  186. th = r.body.threads[j]
  187. if (taken.includes(j) || th.path != f.path || th.line != f.line) { continue }
  188. taken.push(j)
  189. gids.push({ index = firstNew + at[k], id = th.id })
  190. break
  191. }
  192. }
  193. let offDiff = 0
  194. for (let i = 0; i < inline.length; i = i + 1) { if (!inline[i]) { offDiff = offDiff + 1 } }
  195. return { posted = r.body.review.id, inline = at.length, off = offDiff, resent = retried, threads = r.body.threads.length, =gids }
  196. }
  197. }
  198. // Replies and resolves of the model, to the thread ids gitoria gave. Failures are warnings (the review is already posted).
  199. postReplies = (Number pr, Hybrid state, Hybrid result) => {
  200. let warnings = []
  201. let sent = 0
  202. for (reply of result.replies) {
  203. let gid = null
  204. for (t of state.threads) { if (t.key == reply.thread) { gid = t.gid } }
  205. if (gid == null) { warnings.push(reply.thread + ': no gitoria thread id, reply not posted') continue }
  206. a = call('POST', repliesRoute(pr, gid), { body = reply.body })
  207. if (!a.ok) { warnings.push(reply.thread + ': reply failed HTTP ' + a.status) continue }
  208. sent = sent + 1
  209. if (reply.resolve) {
  210. b = call('POST', resolveRoute(pr, gid), { resolved = true })
  211. if (!b.ok) { warnings.push(reply.thread + ': resolve failed HTTP ' + b.status) }
  212. }
  213. }
  214. return { =sent, =warnings }
  215. }
  216. // Final status after a posted review.
  217. statusFor = (Hybrid result, Hybrid posted) => {
  218. if (posted != null && posted.quiet) { return false }
  219. let summary = result.findings.length + ' new finding(s), ' + result.replies.length + ' thread replies'
  220. if (result.framework != null && result.framework.length > 0) { summary = summary + ', ' + result.framework.length + ' framework note(s)' }
  221. return finishStatus(result.verdict == 'request_changes' ? 'failure' : 'success', summary)
  222. }

Branches

Latest commits

  • 8d3fb2e7verdict guard: unknown severity blocks; L2mre
  • 9cb7994fverdict guard: severity of older off-diff findings from review bodies; tolerant read; L2mre
  • fa2ba67c|||PR verdict guard: an open Blocker/Major thread holds request_changesmre
  • 9e89de3cSTATUS 11-24mre
  • 841ce528LOG: gitoria PR 25 round 2mre
  • 3c6c05feLOG: gitoria PR 25 reviewmre
  • 5a5c1accLOG: hycon PR 382 round 3mre
  • 0ab030b9LOG: gitoria PR 24 approvedmre
  • 0e7be144LOG: gitoria PR 24 round 4mre
  • 58075360LOG: hycon PR 382 clarificationmre
  • b5c8d9c5LOG: gitoria PR 24 round 3mre
  • d9c9857bLOG: gitoria PR 24 re-reviewmre
  • d678c1e3LOG: gitoria PR 24 reviewmre
  • 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