gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit477d7bdc477d7bdcMerge branch 'p50'mre477d7bdc/lib/reviews-helpers.hl

4.5 KB

  1. // lib/reviews-helpers.hl — what lib/reviews.hl looks up (gitoria#44): the answer shapes, paging, sorting, the diff's commentable lines, texts and times. Statics only; no table here.
  2. import { timestamp } from 'hl:time'
  3. import { countOfList } from './util.hl'
  4. static maxBody = 65536
  5. static maxComments = 100
  6. // ---- answers: { status, body }; lib/api.hl turns them into the Response (docs/apps-api.md §6 error shape)
  7. static answer = (status, body) => { return { status = status body = body } }
  8. static failure = (status, code, message, details) => {
  9. return { status = status body = { error = { code = code message = message details = details } } }
  10. }
  11. static notFound = (what) => { return failure(404, 'not_found', 'no such ' + what, null) }
  12. static invalid = (message, details) => { return failure(422, 'invalid', message, details) }
  13. // ---- times: ISO with milliseconds (the contract's comment.created), hl:time's timestamp() has whole seconds
  14. static pad3 = (n) => { return n < 10 ? '00' + n : (n < 100 ? '0' + n : '' + n) }
  15. static isoMs = (ms) => {
  16. sec = ms - ms % 1000
  17. return timestamp(sec).slice(0, 19) + '.' + pad3(ms % 1000) + 'Z'
  18. }
  19. // ---- input checks → null or the complaint
  20. static bodyError = (s, what, required) => {
  21. if (s == null) { return required ? what + ' is required' : null }
  22. if (hlTypeName(s) != 'String') { return what + ' must be text' }
  23. if (required && s.trim() == '') { return what + ' must not be empty' }
  24. if (s.length > maxBody) { return what + ' is too long (at most ' + maxBody + ' characters)' }
  25. return null
  26. }
  27. static wholeNumber = (n) => { return n != null && hlTypeName(n) == 'Number' && n % 1 == 0 && n >= 1 }
  28. // the PR number of a path segment, or null
  29. static numberOf = (text) => {
  30. if (text == null || hlTypeName(text) != 'String' || text.length == 0 || text.length > 9) { return null }
  31. n = toNumber(text)
  32. return wholeNumber(n) && '' + n == text ? n : null
  33. }
  34. // ?limit= → 1…100 (default 50)
  35. static limitOf = (q) => {
  36. n = q != null && q.limit != null ? toNumber(q.limit) : null
  37. if (n == null || n < 1) { return 50 }
  38. return n > 100 ? 100 : n
  39. }
  40. // ---- sorting (an insertion sort: no sort(), hybriel #1; stable); `before(a, b)` = a goes ahead of b. Sorts a copy of the list.
  41. static sortRows = (rows, before) => {
  42. out = []
  43. if (countOfList(rows) > 0) { for (r of rows) { out.push(r) } }
  44. let i = 1
  45. while (i < out.length) {
  46. cur = out[i]
  47. let j = i - 1
  48. while (j >= 0 && before(cur, out[j])) {
  49. out[j + 1] = out[j]
  50. j = j - 1
  51. }
  52. out[j + 1] = cur
  53. i = i + 1
  54. }
  55. return out
  56. }
  57. // one page of an ordered list: the rows for which isAfter(row) holds (the cursor already passed), at most `limit`, and the cursor of the last one when more follow
  58. static pageOf = (rows, limit, isAfter, cursorOf) => {
  59. out = []
  60. let more = false
  61. for (r of rows) {
  62. if (isAfter(r)) {
  63. if (out.length < limit) { out.push(r) } else { more = true }
  64. }
  65. }
  66. return { items = out next = more && out.length > 0 ? cursorOf(out[out.length - 1]) : null }
  67. }
  68. // ---- THE LINES AN INLINE COMMENT MAY STAND ON (contract §5): the NEW side of `git diff -M -U3 <merge-base> <head>`, an added or a context line.
  69. // diff lines → { <new-side path>: { "<line>": true } }. The hunk header's counts say where a hunk ends, so a content line that looks like `+++ b/x` is no header.
  70. // A deleted file has no new side (`+++ /dev/null`); a path git quotes (a control character in the name) is not listed.
  71. static commentable = (lines) => {
  72. files = {}
  73. let path = null
  74. let oldLeft = 0
  75. let newLeft = 0
  76. let cur = 0
  77. for (l of lines) {
  78. if (oldLeft > 0 || newLeft > 0) {
  79. c = l.slice(0, 1)
  80. backslash = l.length > 0 && l.charCodeAt(0) == 92
  81. if (c == '+') {
  82. if (path != null) { files[path]['' + cur] = true }
  83. cur = cur + 1
  84. newLeft = newLeft - 1
  85. } else if (c == '-') {
  86. oldLeft = oldLeft - 1
  87. } else if (!backslash) {
  88. if (path != null) { files[path]['' + cur] = true }
  89. cur = cur + 1
  90. oldLeft = oldLeft - 1
  91. newLeft = newLeft - 1
  92. }
  93. } else if (l.startsWith('diff --git ')) {
  94. path = null
  95. } else if (l.startsWith('+++ ')) {
  96. p = l.slice(4)
  97. path = p.startsWith('b/') && !p.startsWith('b/"') ? p.slice(2) : null
  98. if (path != null && files[path] == null) { files[path] = {} }
  99. } else if (l.startsWith('@@ ')) {
  100. parts = l.split(' ')
  101. if (parts.length >= 3) {
  102. o = parts[1].slice(1).split(',')
  103. n = parts[2].slice(1).split(',')
  104. oldLeft = o.length > 1 ? toNumber(o[1]) : 1
  105. newLeft = n.length > 1 ? toNumber(n[1]) : 1
  106. cur = toNumber(n[0])
  107. if (oldLeft == null || newLeft == null || cur == null) {
  108. oldLeft = 0
  109. newLeft = 0
  110. }
  111. }
  112. }
  113. }
  114. return files
  115. }

Branches

Latest commits

  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre
  • eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
  • 8c8ba2f1log: PR flow announcedmre
  • fa00cd35log: Kunibert pings provenmre
  • 3a68cfaclog: components decisionmre
  • 87de8e31log: Kunibert connected to 9 app reposmre
  • c22aff69log: PR-only rollout startedmre
  • 25309981log: STOP and paused workers 2026-11-08mre
  • 5286803alog: worker briefs and reports of 2026-11-08mre
  • 85c86605log: 2026-11-08 Arachne on list shapemre
  • cb634512log: 2026-11-08 keyed lists livemre
  • 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
  • 0db0d81elog: 2026-11-08 day52 live, W30 startedmre
  • b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
  • 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
  • f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre