kunibert
All repositories: gitoria
5.7 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) }\\ repo.id + its signature identify the repo; api/clone change on a rename (gitoria#53), runs use the configured URLs (old ones forward).}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
- 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