gitoria
All repositories: gitoria
4.5 KB
// 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.import { timestamp } from 'hl:time'import { countOfList } from './util.hl'static maxBody = 65536static maxComments = 100// ---- answers: { status, body }; lib/api.hl turns them into the Response (docs/apps-api.md §6 error shape)static answer = (status, body) => { return { status = status body = body } }static failure = (status, code, message, details) => {return { status = status body = { error = { code = code message = message details = details } } }}static notFound = (what) => { return failure(404, 'not_found', 'no such ' + what, null) }static invalid = (message, details) => { return failure(422, 'invalid', message, details) }// ---- times: ISO with milliseconds (the contract's comment.created), hl:time's timestamp() has whole secondsstatic pad3 = (n) => { return n < 10 ? '00' + n : (n < 100 ? '0' + n : '' + n) }static isoMs = (ms) => {sec = ms - ms % 1000return timestamp(sec).slice(0, 19) + '.' + pad3(ms % 1000) + 'Z'}// ---- input checks → null or the complaintstatic bodyError = (s, what, required) => {if (s == null) { return required ? what + ' is required' : null }if (hlTypeName(s) != 'String') { return what + ' must be text' }if (required && s.trim() == '') { return what + ' must not be empty' }if (s.length > maxBody) { return what + ' is too long (at most ' + maxBody + ' characters)' }return null}static wholeNumber = (n) => { return n != null && hlTypeName(n) == 'Number' && n % 1 == 0 && n >= 1 }// the PR number of a path segment, or nullstatic numberOf = (text) => {if (text == null || hlTypeName(text) != 'String' || text.length == 0 || text.length > 9) { return null }n = toNumber(text)return wholeNumber(n) && '' + n == text ? n : null}// ?limit= → 1…100 (default 50)static limitOf = (q) => {n = q != null && q.limit != null ? toNumber(q.limit) : nullif (n == null || n < 1) { return 50 }return n > 100 ? 100 : n}// ---- sorting (an insertion sort: no sort(), hybriel #1; stable); `before(a, b)` = a goes ahead of b. Sorts a copy of the list.static sortRows = (rows, before) => {out = []if (countOfList(rows) > 0) { for (r of rows) { out.push(r) } }let i = 1while (i < out.length) {cur = out[i]let j = i - 1while (j >= 0 && before(cur, out[j])) {out[j + 1] = out[j]j = j - 1}out[j + 1] = curi = i + 1}return out}// 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 followstatic pageOf = (rows, limit, isAfter, cursorOf) => {out = []let more = falsefor (r of rows) {if (isAfter(r)) {if (out.length < limit) { out.push(r) } else { more = true }}}return { items = out next = more && out.length > 0 ? cursorOf(out[out.length - 1]) : null }}// ---- 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.// 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.// A deleted file has no new side (`+++ /dev/null`); a path git quotes (a control character in the name) is not listed.static commentable = (lines) => {files = {}let path = nulllet oldLeft = 0let newLeft = 0let cur = 0for (l of lines) {if (oldLeft > 0 || newLeft > 0) {c = l.slice(0, 1)backslash = l.length > 0 && l.charCodeAt(0) == 92if (c == '+') {if (path != null) { files[path]['' + cur] = true }cur = cur + 1newLeft = newLeft - 1} else if (c == '-') {oldLeft = oldLeft - 1} else if (!backslash) {if (path != null) { files[path]['' + cur] = true }cur = cur + 1oldLeft = oldLeft - 1newLeft = newLeft - 1}} else if (l.startsWith('diff --git ')) {path = null} else if (l.startsWith('+++ ')) {p = l.slice(4)path = p.startsWith('b/') && !p.startsWith('b/"') ? p.slice(2) : nullif (path != null && files[path] == null) { files[path] = {} }} else if (l.startsWith('@@ ')) {parts = l.split(' ')if (parts.length >= 3) {o = parts[1].slice(1).split(',')n = parts[2].slice(1).split(',')oldLeft = o.length > 1 ? toNumber(o[1]) : 1newLeft = n.length > 1 ? toNumber(n[1]) : 1cur = toNumber(n[0])if (oldLeft == null || newLeft == null || cur == null) {oldLeft = 0newLeft = 0}}}}return files}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
- 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
- dc77fb95main: offline note from __hlOnline, tick removed (#29)mre
- 37a86d93tests/reviews: seeded fake hook's DNS failures are not server errors (flaky gate)mre
- 07ab845bvendor: Hybriel set-day50 (run() leak fix #335)mre
- 71b2fa4blog: hybriel#335mre
- bd94cc7elog: #25 cause found (run() leak)mre
- 565cb508log: H25 round 1mre
- 90a0af84log: H25 startedmre
- 87bd53b8mission H25mre
- 71a746cflog: day48 livemre
- bf568e14log: V48 mergedmre