gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit483141d0483141d0gitoria#45: Settings shows the Apps heading only to the ownermre483141d0/lib/api.hl

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

Branches

Latest commits

  • 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
  • e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
  • 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre