gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite874e44de874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmree874e44d/lib/api.hl

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

Branches

Latest commits

  • e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
  • 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
  • 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
  • c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre