kunibert
All repositories: gitoria
5.9 KB
$mode = 'static'import { run } from 'hl:proc'import { readFile } from 'hl:fs'import { tryFetch } from 'hl:fetch'import { sleep, timestamp, now } from 'hl:time'import { require } from './review-output.hl'// ONE place for every [OPEN] name of the gitoria apps contract (apps-api.md v1) and the limits.tokenRoute = '/apps/token'pullRoute = (Number n) => { return '/pulls/' + n }threadsRoute = (Number n) => { return '/pulls/' + n + '/threads' }reviewsRoute = (Number n) => { return '/pulls/' + n + '/reviews' }repliesRoute = (Number n, String id) => { return '/pulls/' + n + '/threads/' + id + '/replies' }resolveRoute = (Number n, String id) => { return '/pulls/' + n + '/threads/' + id + '/resolve' }statusRoute = (String sha) => { return '/statuses/' + sha }statusContext = 'kunibert' // [OPEN] field name `context`pageLimit = 100maxRetryAfter = 60 // seconds a 429 may make us wait, per trymaxRateTries = 3tokenMarginSeconds = 120 // fetch a new token this long before `expires`requestTimeoutMs = 30000// The client state of this process. The key and the token live ONLY here (and the key file); nothing writes them anywhere.client = { api = null, key = null, token = null, expires = null }// Key file: first line, must not be readable by group or others.connect = (String api, String keyFile) => {require(api.startsWith('http://') || api.startsWith('https://'), 'api base must be an http(s) URL')mode = run(['stat', '-c', '%a', keyFile], { timeout = 10 })require(mode.exit == 0, 'key file not readable')require(mode.lines.join('').trim().endsWith('00'), 'key file must have mode 600 (no group/other access)')key = readFile(keyFile).split('\n')[0].trim()require(key != '', 'key file is empty')client.api = api.endsWith('/') ? api.slice(0, api.length - 1) : apiclient.key = keyclient.token = nullclient.expires = null}// Never let a secret into a message, a log or a status summary.scrub = (String text) => {let t = textif (client.key != null && client.key != '') { t = t.replaceAll(client.key, '[key]') }if (client.token != null && client.token != '') { t = t.replaceAll(client.token, '[token]') }return t}// Structured result of one HTTP exchange: { status, ok, body (parsed or null), code, message, details, headers }.exchange = (String method, String route, String bearer, Hybrid payload) => {let options = { method = method, headers = { authorization = 'Bearer ' + bearer, accept = 'application/json' }, timeoutMs = requestTimeoutMs, maxRedirects = 0 }if (payload != null) { options.json = payload }r = tryFetch(client.api + route, options)if (r.status == 0) { return { status = 0, ok = false, body = null, code = 'network_error', message = 'request failed', details = null, headers = {} } }body = JSON.tryParse(r.text())let out = { status = r.status, ok = r.ok, =body, code = null, message = null, details = null, headers = r.headers }if (!r.ok && body == null) { out.code = 'not_json' }if (!r.ok && body != null && body.error != null) {\\ contract: { error: { code, message, details } }; a route gitoria has not built yet answers { error: "<text>" }if (hlTypeName(body.error) == 'String') { out.code = 'not_found' out.message = body.error }if (hlTypeName(body.error) != 'String') { out.code = body.error.code out.message = body.error.message out.details = body.error.details }}return out}// A valid token: cached in memory until `tokenMarginSeconds` before `expires` (ISO strings compare as text).getToken = (Boolean fresh = false) => {if (!fresh && client.token != null && client.expires > timestamp(now() + tokenMarginSeconds * 1000).slice(0, 19)) { return client.token }r = exchange('POST', tokenRoute, client.key, {})require(r.ok && r.body != null && hlTypeName(r.body.token) == 'String' && hlTypeName(r.body.expires) == 'String', 'token request failed: HTTP ' + r.status + ' ' + r.code)client.token = r.body.tokenclient.expires = r.body.expires.slice(0, 19)return client.token}// One API call. 401 -> one new token and one retry; 429 -> wait Retry-After (capped), up to maxRateTries; anything else is returned// as the structured result (callers decide: 409 and 422 are normal answers). Use must() to turn a failure into an error.call = (String method, String route, Hybrid payload = null) => {let refreshed = falselet tries = 0while (true) {r = exchange(method, route, getToken(), payload)if (r.status == 401 && !refreshed) {refreshed = truegetToken(true)continue}if (r.status == 429 && tries < maxRateTries) {tries = tries + 1let wait = toNumber(r.headers['retry-after'])if (wait == null || wait < 0) { wait = 1 }sleep(wait > maxRetryAfter ? maxRetryAfter : wait)continue}return r}}must = (Hybrid r, String what) => {if (!r.ok) { hlError(scrub(what + ': HTTP ' + r.status + ' ' + r.code + ' ' + r.message)) }return r.body}// Every page of a list answer: key = 'threads' | 'comments' | 'pulls'.listAll = (String route, String key) => {let items = []let cursor = nulllet pages = 0while (true) {body = must(call('GET', route + '?limit=' + pageLimit + (cursor == null ? '' : '&cursor=' + encodeURIComponent(cursor))), 'GET ' + route)for (x of body[key]) { items.push(x) }cursor = body.nextpages = pages + 1require(pages < 1000, 'too many pages')if (cursor == null) { break }}return items}getPull = (Number n) => { return must(call('GET', pullRoute(n)), 'GET ' + pullRoute(n)) }getThreads = (Number n) => { return listAll(threadsRoute(n), 'threads') }putStatus = (String sha, String state, String title, String summary) => {return call('PUT', statusRoute(sha), { context = statusContext, =state, =title, summary = scrub(summary), url = null })}
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