gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchhl25-recordsa002ecb7|||PR records: HL25 report, heap helper, logmrehl25-records/lib/api.hl

24.1 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (project.hl wires them): thin wrappers — check the request and the session, call the
  2. // topic, answer. The plumbing (JSON / Markdown / plain answers, redirects, the ssh guard, `?next=`) is lib/api-helpers.hl.
  3. // /api/repos, /api/repos/:slug the public reads (repos.hl; Accept: text/markdown → the Markdown read view)
  4. // /login/callback the login button's return (users.hl)
  5. // /settings/connect, /settings/connected connecting a repo to a tickets project (tickets.hl)
  6. // /api/apps/token an app trades its key for a one-hour token (app-tokens.hl, gitoria#42)
  7. // /settings/apps/connect, /settings/apps/confirm, /api/apps/exchange connecting an app to a repo (apps.hl, gitoria#41)
  8. // POST /api/pulls/N/merge a repo member merges a PR with a gtr_ token (gitoria#48, apiMergePull)
  9. // /api/pulls… an app reads PRs, posts reviews with inline threads, replies, resolves, comments (reviews.hl, gitoria#44)
  10. // /__git/keys, /__git/access what the sshd container asks (sshkeys.hl, repos.hl)
  11. // (The git protocol's routes are lib/transport.hl gitTransport.)
  12. // A route that WRITES THE SESSION gets the request and the framework's session store BY REFERENCE (`&req`, `&sessions`,
  13. // from project.hl): copied into a second call, the session inside `req` would be a copy too and the write would be lost.
  14. import { userOfToken } from './tokens.hl'
  15. import { Response } from 'hl:http1'
  16. import { randomBytes } from 'hl:crypto'
  17. import { NL, px, slugOfHost } from './util.hl'
  18. import { login, failedLogin } from './users.hl'
  19. import { createRepoFromInput, repoRows, repoRow, repoBySlug, repoFor, orgFor, ownsRepo, mayPush, mayRead } from './repos.hl'
  20. import { fullNameError, repoDocument, listDocument } from './repos-helpers.hl'
  21. import { forwardOf } from './forwards.hl'
  22. import { userIdOfSession, userRecord } from './users.hl'
  23. import { startConnect, completeConnect } from './tickets.hl'
  24. import { keyLine } from './sshkeys.hl'
  25. import { urlAllowed, withQuery, formFields } from './apps-helpers.hl'
  26. import { nameError, makeCode, exchangeCode, appOfKey } from './apps.hl'
  27. import { issueToken } from './app-tokens.hl'
  28. import { bearerOf, unauthorized, notFound, tooMany, appOfRequest, readerOfRequest, appTokenRequired } from './app-auth.hl'
  29. import { answerOf, jsonOfRequest, reply, fail, apiError, wantsMarkdown, markdownReply, textReply, goTo, sshGuard, repoOfRequest, safeNext } from './api-helpers.hl'
  30. import { ensurePulls, recordByNumber, pullObject } from './pullsync.hl'
  31. import { mergeAndAnnounce } from './pullmerge.hl'
  32. import { numberOf } from './reviews-helpers.hl'
  33. import { prOfRequest } from './prpage.hl'
  34. import { listPulls, getPull, postReview, listReviews, listThreads, postReply, resolveThread, listComments, postComment } from './reviews.hl'
  35. import { putStatus, listStatuses } from './statuses.hl'
  36. import { isHexId } from './git-helpers.hl'
  37. import { limitCheck } from './limits.hl'
  38. import { syncAndRaise, deliveryPage, redeliver } from './events.hl'
  39. import { notifyPush } from './tickets.hl'
  40. // ---- the API: public reads and creation with a person's access token ------------------------
  41. static apiRepos = (route, req) => {
  42. if (req.method == 'POST') { return apiCreateRepo(route, req) }
  43. if (req.method != 'GET') { return fail(405, 'GET only') }
  44. rows = repoRows(userIdOfSession(req.session))
  45. if (wantsMarkdown(req)) { return markdownReply(listDocument(rows)) }
  46. return reply(200, { repos = rows })
  47. }
  48. // POST /api/repos: app tokens remain read-only; owner comes only from the gtr_ token.
  49. static apiCreateRepo = (route, req) => {
  50. token = bearerOf(req)
  51. if (token != null && token.startsWith('gat_')) { return apiError(403, 'forbidden', 'app tokens cannot create repositories', null) }
  52. uid = userOfToken(token)
  53. if (uid == null) { return apiError(401, 'unauthorized', 'a user access token is required', null) }
  54. j = jsonOfRequest(req)
  55. if (j.error != null) { return j.error }
  56. r = createRepoFromInput(uid, j.json)
  57. if (r.error != null) {
  58. status = r.status == null ? 422 : r.status
  59. code = status == 403 ? 'forbidden' : (status == 409 ? 'conflict' : (status == 500 ? 'internal' : 'invalid'))
  60. return apiError(status, code, r.error, { field = r.field })
  61. }
  62. return reply(201, r.repo)
  63. }
  64. static apiRepo = (route, req) => {
  65. if (req.method != 'GET') { return fail(405, 'GET only') }
  66. row = repoRow(route.params.slug, userIdOfSession(req.session))
  67. if (row == null) { return fail(404, 'no such repository') }
  68. if (wantsMarkdown(req)) { return markdownReply(repoDocument(row)) }
  69. return reply(200, row)
  70. }
  71. // ---- NOTHING ELSE (mission MG, as ident's notFoundPage): the last route of project.hl answers a path no route knows with the
  72. // shell's 404 page (components/notfound.hl) — status 404, the page rendered by the framework with the request's session and host
  73. static notFoundPage = (route, req, &srv, pageComponent) => {
  74. m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { path = req.path } path = req.path }
  75. res = srv.page(m, req.session, srv.hostOf(req.headers['host']), srv.requestHeaders(req.headers))
  76. res.status = 404
  77. return res
  78. }
  79. // `/:orepo` (GO34): on an organization's address (one the viewer may see) the first segment is a repo's Readme page, rendered like the 404 page is (a function route:
  80. // a stray single-segment path on any other address keeps its real 404 status — /.env, /host.js, a private organization's /x)
  81. static orgRepoPage = (route, req, &srv, pageComponent, missingComponent) => {
  82. h = req.headers['host']
  83. label = slugOfHost(h == null ? '' : h.split(':')[0])
  84. if (label == '' || orgFor(label, userIdOfSession(req.session)) == null) { return notFoundPage(route, req, &srv, missingComponent) }
  85. m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { orepo = req.path.slice(1) } path = req.path }
  86. return srv.page(m, req.session, srv.hostOf(h), srv.requestHeaders(req.headers))
  87. }
  88. // `/pulls/:n` and `/:orepo/pulls/:n` (gitoria#45): the PR's page when the repo is visible to the viewer and the number is one of its pull requests, else the
  89. // shell's 404 page (a function route, so the status is a real 404 for an unknown number)
  90. static pullPage = (route, req, &srv, pageComponent, missingComponent) => {
  91. h = req.headers['host']
  92. host = h == null ? '' : h.split(':')[0]
  93. orepo = route.params.orepo != null ? route.params.orepo : ''
  94. nText = route.params.n
  95. if (prOfRequest(host, orepo, req.session, nText) == null) { return notFoundPage(route, req, &srv, missingComponent) }
  96. m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { orepo = orepo n = nText } path = req.path }
  97. return srv.page(m, req.session, srv.hostOf(h), srv.requestHeaders(req.headers))
  98. }
  99. // any other /api path: the contract's JSON 404 (docs/apps-api.md §6)
  100. static notFoundApi = (route, req) => { return apiError(404, 'not_found', 'no such endpoint', null) }
  101. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident") ----------------------------
  102. // ident sends the browser back with ?ident_code= (and our ?next=); the code is exchanged here (users.hl userOfLoginCode),
  103. // the user goes into THIS browser's session (`req.session`, the cookie's — hybriel #11; minted when it brought none),
  104. // then → `?next=` (api-helpers.hl safeNext). A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked
  105. // in the session, then → /login/failed.
  106. static loginFailed = (&req, &sessions, why) => {
  107. fresh = req.session == null
  108. s = failedLogin(why, req.session, &sessions)
  109. res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  110. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  111. return res
  112. }
  113. static loginCallback = (route, &req, &sessions) => {
  114. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  115. q = req.query != null ? req.query : {}
  116. code = q.ident_code
  117. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  118. x = login(code, req.session, &sessions)
  119. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  120. s = x.session
  121. res = new Response('logged in', { status = 302 headers = { 'Location' = safeNext(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Set-Cookie' = sessions.cookieHeader(s.id) } })
  122. return res
  123. }
  124. // ---- ONE LOGIN FOR THE MAIN ADDRESS AND EVERY REPO ADDRESS ---------------------------------------------------------------------
  125. // Nothing here: hl:web (batch 20) does it. The session cookie carries `Domain=.<host>` (project.hl sessionDomain); among several gitoriasid
  126. // cookies the signed-in one wins and is re-issued with the Domain (the stray host-only copy expires), and a signed-in cookie is re-issued when its
  127. // attributes change (a login from before the Domain was set moves to it on its next request) and otherwise once a day. Gate: tests/browser.mjs "gitoria#24".
  128. // ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------
  129. // /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.
  130. // /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per
  131. // repo) → back to /settings. A failure is a note on the settings page.
  132. static connectStart = (route, &req, &sessions) => {
  133. if (req.method != 'GET') { return fail(405, 'GET only') }
  134. repo = repoOfRequest(route, req)
  135. if (repo == null) { return fail(404, 'no such repository') }
  136. s = req.session
  137. if (s == null || !ownsRepo(repo.slug, s)) { return goTo(px(repo.slug) + '/settings') }
  138. return goTo(startConnect(repo.slug, s, &sessions))
  139. }
  140. static connectBack = (route, &req, &sessions) => {
  141. if (req.method != 'GET') { return fail(405, 'GET only') }
  142. repo = repoOfRequest(route, req)
  143. if (repo == null) { return fail(404, 'no such repository') }
  144. q = req.query != null ? req.query : {}
  145. x = completeConnect(repo.slug, req.session, q, &sessions)
  146. res = goTo(px(repo.slug) + '/settings')
  147. if (x.fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(x.session.id) }
  148. return res
  149. }
  150. // ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)
  151. // GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key
  152. // (sshkeys.hl keyLine), empty for an unknown one
  153. // GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);
  154. // write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)
  155. static gitKeys = (route, req) => {
  156. no = sshGuard(req)
  157. if (no != null) { return no }
  158. q = req.query != null ? req.query : {}
  159. return textReply(200, keyLine(q.type, q.key))
  160. }
  161. // GET /__git/pushed?user=<id>&slug=<slug>&phase=before|after gitoria-shell around a push over ssh (what transport.hl does for HTTPS):
  162. // before → the repo's existing pull requests are numbered (ensurePulls); after (git exited 0) → notifyPush + syncPulls for that user. `ok`; never an error for the push.
  163. static gitPushed = (route, req) => {
  164. no = sshGuard(req)
  165. if (no != null) { return no }
  166. q = req.query != null ? req.query : {}
  167. slug = q.slug
  168. if (slug == null || hlTypeName(slug) != 'String' || fullNameError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  169. let repo = repoBySlug(slug)
  170. if (repo == null && forwardOf(slug) != null) { repo = repoBySlug(forwardOf(slug)) }
  171. if (repo == null || !mayPush(q.user, repo)) { return textReply(403, 'not allowed' + NL) }
  172. if (q.phase == 'before') { ensurePulls(repo.slug) }
  173. else if (q.phase == 'after') {
  174. notifyPush(repo.slug, false)
  175. syncAndRaise(repo.slug, q.user)
  176. } else { return textReply(400, 'phase is before or after' + NL) }
  177. return textReply(200, 'ok' + NL)
  178. }
  179. static gitAccess = (route, req) => {
  180. no = sshGuard(req)
  181. if (no != null) { return no }
  182. q = req.query != null ? req.query : {}
  183. slug = q.slug
  184. if (slug == null || hlTypeName(slug) != 'String' || fullNameError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  185. let repo = repoBySlug(slug)
  186. if (repo == null && forwardOf(slug) != null) { repo = repoBySlug(forwardOf(slug)) }
  187. // the asker is the key's user: a repo they may not see does not exist for them (404, as over HTTPS)
  188. if (repo == null || !mayRead(q.user, repo)) { return textReply(404, 'no such repository' + NL) }
  189. if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner or a member of this repository can push to it' + NL) }
  190. // the sshd container runs git in the repo's real folder: tell it the name (it differs after a move)
  191. return textReply(200, 'ok ' + repo.slug + NL)
  192. }
  193. // ---- CONNECT AN APP TO A REPO (gitoria#41; apps.hl, docs/apps-api.md §1 flow A) ----------------------------------------------------
  194. // /settings/apps/connect?app&hook&return&state: the app sends the owner's browser here. The four values are checked (the URL allow-list:
  195. // anything else is 400 invalid_url) and PARKED in the session with a nonce; then → the repo's settings page, which asks the person to sign in
  196. // (the same session goes on after the login) and, for the owner, shows "Connect <app> to <repo>?" with a Yes / No form.
  197. // /settings/apps/confirm (POST): the form's answer → the owner check, the nonce, a one-time code → redirect to return?code&state (No: return?error=denied&state).
  198. static appConnectStart = (route, &req, &sessions) => {
  199. if (req.method != 'GET') { return apiError(405, 'invalid', 'GET only', null) }
  200. repo = repoOfRequest(route, req)
  201. if (repo == null) { return apiError(404, 'not_found', 'no such repository', null) }
  202. q = req.query != null ? req.query : {}
  203. if (!urlAllowed(q.hook)) { return apiError(400, 'invalid_url', 'hook is not an allowed receiver address', { field = 'hook' }) }
  204. if (!urlAllowed(q['return'])) { return apiError(400, 'invalid_url', 'return is not an allowed address', { field = 'return' }) }
  205. bad = nameError(q.app)
  206. if (bad != null) { return apiError(422, 'invalid', bad, { field = 'app' }) }
  207. if (q.state == null || hlTypeName(q.state) != 'String' || q.state.length == 0 || q.state.length > 200) { return apiError(422, 'invalid', 'state is missing or too long', { field = 'state' }) }
  208. let s = req.session
  209. fresh = s == null
  210. if (fresh) { s = sessions.mint() }
  211. s.data.appConnect = { slug = repo.slug app = ('' + q.app).trim() hook = q.hook ret = q['return'] state = q.state nonce = randomBytes(16) }
  212. sessions.save(s)
  213. res = goTo(px(repo.slug) + '/settings')
  214. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  215. return res
  216. }
  217. static appConnectConfirm = (route, &req, &sessions) => {
  218. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  219. repo = repoOfRequest(route, req)
  220. if (repo == null) { return apiError(404, 'not_found', 'no such repository', null) }
  221. home = px(repo.slug) + '/settings'
  222. let s = req.session
  223. if (s == null || userIdOfSession(s) == null || !ownsRepo(repo.slug, s)) { return goTo(home) }
  224. p = s.data.appConnect
  225. f = formFields(req.body)
  226. if (p == null || p.slug != repo.slug || f.nonce == null || f.nonce != p.nonce) { return goTo(home) }
  227. // one try: the parked request goes whatever the answer is
  228. s.data.appConnect = null
  229. sessions.save(s)
  230. if (f.decision != 'yes') { return goTo(withQuery(p.ret, { error = 'denied' state = p.state })) }
  231. code = makeCode(repo, p.app, p.hook, userIdOfSession(s))
  232. if (code == null) { return goTo(home) }
  233. return goTo(withQuery(p.ret, { code = code state = p.state }))
  234. }
  235. // POST /api/apps/exchange {"code"} (no auth): the app's server trades the code → { key, secret, api, repo, app } (apps.hl exchangeCode)
  236. static appExchange = (route, req) => {
  237. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  238. repo = repoOfRequest(route, req)
  239. if (repo == null) { return apiError(404, 'not_found', 'no such repository', null) }
  240. j = jsonOfRequest(req)
  241. if (j.error != null) { return j.error }
  242. x = exchangeCode(repo, j.json.code)
  243. if (x.notFound == true) { return apiError(404, 'not_found', 'unknown, used or expired code', null) }
  244. return reply(200, x.body)
  245. }
  246. // POST {api}/apps/token Authorization: Bearer gak_… → 200 { token, expires } (app-tokens.hl); the key must be the key of the repo the address names
  247. static appToken = (route, req) => {
  248. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  249. no = appTokenRequired(req)
  250. if (no != null) { return no }
  251. app = appOfKey(bearerOf(req))
  252. if (app == null) { return unauthorized() }
  253. repo = repoOfRequest(route, req)
  254. if (repo == null || repo.id != app.repo) { return notFound() }
  255. wait = limitCheck(app.id, req.method)
  256. if (wait > 0) { return tooMany(wait) }
  257. t = issueToken(app)
  258. if (t.error != null) { return apiError(500, 'internal', 'could not make a token', null) }
  259. return reply(200, { token = t.token expires = t.expires })
  260. }
  261. // GET {api}/deliveries?cursor=&limit= → { deliveries: [{ id, event, action, time, status, tries, lastCode }], next } — the app's OWN deliveries (events.hl)
  262. static appDeliveries = (route, req) => {
  263. if (req.method != 'GET') { return apiError(405, 'invalid', 'GET only', null) }
  264. who = appOfRequest(route, req)
  265. if (who.error != null) { return who.error }
  266. q = req.query != null ? req.query : {}
  267. return reply(200, deliveryPage(who.app.id, q.cursor, q.limit == null ? null : toNumber(q.limit)))
  268. }
  269. // POST {api}/deliveries/:id/redeliver → 202; the same delivery id goes out again. Another app's delivery (or none) is a 404, like any foreign thing
  270. static appRedeliver = (route, req) => {
  271. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  272. who = appOfRequest(route, req)
  273. if (who.error != null) { return who.error }
  274. if (!redeliver(who.app.id, route.params.id)) { return apiError(404, 'not_found', 'no such delivery', null) }
  275. return reply(202, { accepted = true })
  276. }
  277. // ---- the apps API's PR routes (gitoria#44; lib/reviews.hl does the work, these only check the method and the app, and answer)
  278. // GET {api}/pulls?state=&cursor=&limit=
  279. static appPulls = (route, req) => {
  280. if (req.method != 'GET') { return apiError(405, 'invalid', 'GET only', null) }
  281. who = readerOfRequest(route, req)
  282. if (who.error != null) { return who.error }
  283. return answerOf(listPulls(who.repo, req.query))
  284. }
  285. // POST {api}/pulls/N/merge Authorization: Bearer gtr_… body optional {"head":"<40 hex>"} (gitoria#48): a person who may push merges the PR once the required checks
  286. // are green (lib/pullmerge.hl, the same as the Merge button). Apps (gat_) never merge. → 200 { merged, mergeCommit, pull }
  287. static apiMergePull = (route, req) => {
  288. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  289. token = bearerOf(req)
  290. if (token != null && token.startsWith('gat_')) { return apiError(403, 'forbidden', 'apps cannot merge', null) }
  291. uid = userOfToken(token)
  292. if (uid == null) { return apiError(401, 'unauthorized', 'a user access token is required', null) }
  293. repo = repoOfRequest(route, req)
  294. if (repo == null || !mayRead(uid, repo)) { return apiError(404, 'not_found', 'no such repository', null) }
  295. if (!mayPush(uid, repo)) { return apiError(403, 'forbidden', 'only the owner and the members of this repository can merge', null) }
  296. let head = ''
  297. if (req.body != null && req.body.trim() != '') {
  298. j = jsonOfRequest(req)
  299. if (j.error != null) { return j.error }
  300. h = j.json.head
  301. if (h != null) {
  302. if (hlTypeName(h) != 'String' || h.length != 40 || !isHexId(h)) { return apiError(422, 'invalid', 'head must be a 40 character commit id', [{ field = 'head' reason = 'must be 40 hex characters' }]) }
  303. head = h
  304. }
  305. }
  306. ensurePulls(repo.slug)
  307. n = numberOf(route.params.n)
  308. rec = n == null ? null : recordByNumber(repo.id, n)
  309. if (rec == null) { return apiError(404, 'not_found', 'no such pull request', null) }
  310. if (rec.state == 'merged') { return apiError(409, 'conflict', 'this pull request is already merged', { reason = 'already_merged' }) }
  311. r = mergeAndAnnounce(repo, rec.sha, userRecord(uid), head)
  312. if (r.error != null) {
  313. if (r.error == 'no such pull request') { return apiError(404, 'not_found', r.error, null) }
  314. if (r.reason == 'checks_failed') { return apiError(422, 'checks_failed', r.error, r.details) }
  315. return apiError(409, 'conflict', r.error, { reason = r.reason })
  316. }
  317. return reply(200, { merged = true mergeCommit = r.mergeCommit pull = pullObject(repo.slug, rec.number) })
  318. }
  319. // GET {api}/pulls/N
  320. static appPull = (route, req) => {
  321. if (req.method != 'GET') { return apiError(405, 'invalid', 'GET only', null) }
  322. who = readerOfRequest(route, req)
  323. if (who.error != null) { return who.error }
  324. return answerOf(getPull(who.repo, route.params.n))
  325. }
  326. // GET / POST {api}/pulls/N/reviews
  327. static appReviews = (route, req) => {
  328. if (req.method != 'GET' && req.method != 'POST') { return apiError(405, 'invalid', 'GET or POST only', null) }
  329. who = req.method == 'GET' ? readerOfRequest(route, req) : appOfRequest(route, req)
  330. if (who.error != null) { return who.error }
  331. if (req.method == 'GET') { return answerOf(listReviews(who.app, who.repo, route.params.n, req.query)) }
  332. j = jsonOfRequest(req)
  333. if (j.error != null) { return j.error }
  334. return answerOf(postReview(who.app, who.repo, route.params.n, j.json))
  335. }
  336. // GET {api}/pulls/N/threads
  337. static appThreads = (route, req) => {
  338. if (req.method != 'GET') { return apiError(405, 'invalid', 'GET only', null) }
  339. who = readerOfRequest(route, req)
  340. if (who.error != null) { return who.error }
  341. return answerOf(listThreads(who.app, who.repo, route.params.n, req.query))
  342. }
  343. // POST {api}/pulls/N/threads/:id/replies
  344. static appReply = (route, req) => {
  345. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  346. who = appOfRequest(route, req)
  347. if (who.error != null) { return who.error }
  348. j = jsonOfRequest(req)
  349. if (j.error != null) { return j.error }
  350. return answerOf(postReply(who.app, who.repo, route.params.n, route.params.id, j.json))
  351. }
  352. // POST {api}/pulls/N/threads/:id/resolve
  353. static appResolve = (route, req) => {
  354. if (req.method != 'POST') { return apiError(405, 'invalid', 'POST only', null) }
  355. who = appOfRequest(route, req)
  356. if (who.error != null) { return who.error }
  357. j = jsonOfRequest(req)
  358. if (j.error != null) { return j.error }
  359. return answerOf(resolveThread(who.app, who.repo, route.params.n, route.params.id, j.json))
  360. }
  361. // GET / POST {api}/pulls/N/comments (the PR's conversation)
  362. static appComments = (route, req) => {
  363. if (req.method != 'GET' && req.method != 'POST') { return apiError(405, 'invalid', 'GET or POST only', null) }
  364. who = req.method == 'GET' ? readerOfRequest(route, req) : appOfRequest(route, req)
  365. if (who.error != null) { return who.error }
  366. if (req.method == 'GET') { return answerOf(listComments(who.repo, route.params.n, req.query)) }
  367. j = jsonOfRequest(req)
  368. if (j.error != null) { return j.error }
  369. return answerOf(postComment(who.app, who.repo, route.params.n, j.json))
  370. }
  371. // PUT {api}/statuses/<sha> {"context","state","title","summary","url"} → 200 { status } (statuses.hl): the app sets the state of a commit, a newer PUT replaces the older one
  372. static appStatus = (route, req) => {
  373. if (req.method != 'PUT' && req.method != 'GET') { return apiError(405, 'invalid', 'GET or PUT only', null) }
  374. who = req.method == 'GET' ? readerOfRequest(route, req) : appOfRequest(route, req)
  375. if (who.error != null) { return who.error }
  376. // GET (v1.3): every context on that sha, from every app; a sha this repo does not have is 404 not_found (§6 shape)
  377. if (req.method == 'GET') {
  378. l = listStatuses(who.repo, route.params.sha)
  379. if (l.unknown == true) { return apiError(404, 'not_found', 'that sha is not a commit of this repository', null) }
  380. return reply(200, { statuses = l.statuses })
  381. }
  382. j = jsonOfRequest(req)
  383. if (j.error != null) { return j.error }
  384. r = putStatus(who.repo, who.app, route.params.sha, j.json)
  385. if (r.invalid != null) { return apiError(422, 'invalid', 'the status is not valid', r.invalid) }
  386. if (r.unknown == true) { return apiError(422, 'invalid', 'that sha is not a commit of this repository', [{ field = 'sha' reason = 'no_such_commit' }]) }
  387. return reply(200, { status = r.status })
  388. }

Branches

Latest commits

  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre
  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre