kunibert
All repositories: gitoria
5.8 KB
import { Response } from 'hl:http1'import { readFile } from 'hl:fs'import { run } from 'hl:proc'import { timestamp } from 'hl:time'import { hkdfExtract, timingSafeEqual } from 'hl:crypto'import Queue from './queue.hl'import { classify, isKnown, repoId, headerEvent, headerDelivery, headerSignature, signaturePrefix } from './gitoria-events.hl'\\ Gitoria's signed events: check, answer at once, queue. Thin: the queue logic is lib/queue.hl,\\ the event names are lib/gitoria-events.hl. Never logs a secret or the body of a bad request.String secretsFile = ''String dataDir = ''Number maxTries = 3Number maxParallel = 3Number backoffSeconds = 30String reposFile = '' \\ per-repo config: { "<repo.id>": { api, clone, keyFile, secretFile } }; '' = legacy (secretsFile only)String hybriel = 'hybriel'Number timeoutSeconds = 900String runnerData = ''String ticketsBase = ''String ticketsTokenFile = ''String reviewUser = '' \\ unix user the model (codex) runs as; '' = same user as the serviceString codexBin = 'codex'String workDir = '' \\ job worktrees; '' = <runnerData>/workrepoCfg = nullqueue = nulldefaultSecret = nullrepoSecrets = {}\\ a secret/key file must be mode 600; its first line is the valuereadSecret = (String path, String what) => {m = run(['stat', '-c', '%a', path], { timeout = 10 })if (m.exit != 0) { hlError(what + ' file not readable: ' + path) }if (!m.lines.join('').trim().endsWith('00')) { hlError(what + ' file must have mode 600: ' + path) }v = readFile(path).split(JSON.parse('"\n"'))[0].trim()if (v == '') { hlError(what + ' file is empty: ' + path) }return v}start = () => {if (secretsFile != '') {s = JSON.parse(readFile(secretsFile))defaultSecret = s.defaultrepoSecrets = s.reposif (repoSecrets == null) { repoSecrets = {} }}if (reposFile != '') {repoCfg = JSON.parse(readFile(reposFile))for (id in repoCfg) {c = repoCfg[id]if (c.api == null || c.clone == null || c.keyFile == null || c.secretFile == null) { hlError('repo config ' + id + ' needs api, clone, keyFile, secretFile') }readSecret(c.keyFile, 'key')repoSecrets[id] = readSecret(c.secretFile, 'secret')}}queue = new Queue(dataDir = dataDir, maxTries = maxTries, =maxParallel, backoffSeconds = backoffSeconds, repos = repoCfg, hybriel = hybriel, timeoutSeconds = timeoutSeconds, runnerData = runnerData, =ticketsBase, =ticketsTokenFile, =reviewUser, =codexBin, =workDir)queue.load()}reply = (Number status, String code, String line, Hybrid extra = {}) => {console.log(timestamp() + ' hook: ' + status + ' ' + code + (line == '' ? '' : ' ' + line))body = { status = code }for (k in extra) { body[k] = extra[k] }return new Response(JSON.stringify(body), { status = status, headers = { 'Content-Type' = 'application/json' } })}\\ "sha256=" + hex HMAC-SHA256(secret, raw body). hl:crypto has no hmac: hkdfExtract(salt, ikm) IS HMAC(salt, ikm).expected = (String secret, Bytes raw) => {return signaturePrefix + hkdfExtract(toBytes(secret), raw).hex()}anyRepoSecrets = () => {for (k in repoSecrets) { return true }return false}hook = (Hybrid req) => {let delivery = req.headers[headerDelivery]given = req.headers[headerSignature]if (given == null || given == '') { return reply(401, 'missing_signature', 'delivery=' + delivery) }let body = nulllet secret = defaultSecretif (anyRepoSecrets()) {body = JSON.tryParse(req.body == null ? '' : req.body)if (body == null) { return reply(400, 'invalid_json', 'delivery=' + delivery) }id = repoId(body)if (id != null && repoSecrets[id] != null) { secret = repoSecrets[id] }}if (secret == null || !timingSafeEqual(given, expected(secret, req.bytes))) { return reply(401, 'bad_signature', 'delivery=' + delivery) }name = req.headers[headerEvent]if (!isKnown(name)) { return reply(202, 'ignored', 'unknown event ' + name + ' delivery=' + delivery, { reason = 'unknown event' }) }if (body == null) { body = JSON.tryParse(req.body == null ? '' : req.body) }if (body == null) { return reply(400, 'invalid_json', 'delivery=' + delivery) }if (delivery == null || delivery == '') { delivery = body.delivery }if (delivery == null || delivery == '') { return reply(400, 'missing_delivery', 'event=' + name) }if (repoCfg != null && body.repo != null) {id = repoId(body)c = repoCfg[id]if (c == null) { return reply(403, 'repo_mismatch', 'repo ' + id + ' is not configured delivery=' + delivery) }if (body.repo.api != c.api) { return reply(403, 'repo_mismatch', 'repo ' + id + ' api differs from the config delivery=' + delivery) }if (body.repo.clone != c.clone) { return reply(403, 'repo_mismatch', 'repo ' + id + ' clone differs from the config delivery=' + delivery) }}if (queue.wasSeen(delivery)) { return reply(200, 'duplicate', 'delivery=' + delivery) }queue.markSeen(delivery)c = classify(name, body)if (c.kind == 'ping') { return reply(202, 'ping', 'delivery=' + delivery) }if (c.kind == 'ignore') { return reply(202, 'ignored', c.reason + ' delivery=' + delivery, { reason = c.reason }) }if (c.kind == 'close') {r = queue.close(c)return reply(202, 'closed', r.key + ' dropped=' + r.dropped + ' removeSession=true delivery=' + delivery, { job = r.key, dropped = r.dropped })}if (c.kind == 'thread-state') {r = queue.recordThread(c)return reply(202, r.action, r.key + ' ' + c.event + ' delivery=' + delivery, { job = r.key })}r = queue.enqueue(c)return reply(202, r.action, r.key + ' ' + c.event + ' head=' + c.head + ' delivery=' + delivery, { job = r.key, threadReplyOnly = r.threadReplyOnly })}health = () => {return queue.health()}tick = () => {return queue.tick()}
Branches
Latest commits
- 9710e39fType STANDARD as String (L10, Kunibert review)mre
- 4140b664|||PR Review against HL-STANDARD and cite rule ids (gitoria#50 step 3)mre
- 75c6888drecords: gitoria#50 step 1 connected and provenmre
- aa6d997frecords: gitoria#50 connect plan, Anton reportmre
- 8a190ba7Records: kunibert#18 and #15 donemre
- 2eee9dbbMerge branch 'revendor-day44' into mainKunibert
- e96721cb|||PR Re-vendor plugins from Hybriel day44 (hl:fetch redirect fix)mre
- 0c67ab8cLog: kunibert repo ping provenmre
- b6f3039cLog: kunibert repo connectedmre
- b143c3c7CONDUCTOR: push recipemre
- 7a1279eeRule: conductor merges own utility PRs after green reviewmre
- 4038b3e0Log: kunibert#18 openedmre
- 18543db1Log: day42 fetch fix, re-vendor pendingmre
- 81a12f96Statusmre
- 6ce706abRecords: neutral wording, split cleanupmre
- 81a4d31bDocs: neutral wordingKunibert
- 8205b05eRecords: #17 livemre
- c3391204Answer fresh reviews under open threadsKunibert
- a0f3d147Records: #16 livemre
- 04322148Run reviews in parallel across repositoriesKunibert