gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbdcbbaa9bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mrebdcbbaa9/lib/statuses.hl

9.2 KB

  1. // lib/statuses.hl — THE STATUS OF A COMMIT SET BY AN APP (gitoria#46, part of the push API gitoria#38; contract docs/apps-api.md §5 "Status").
  2. // Two tables, both keyed by the repo's ID (never the slug), every write to them is here:
  3. // statusRows pk @id index !key, shaKey storage/mpackdb/statuses.db
  4. // { rid, key "<rid> <sha> <context>", shaKey "<rid> <sha>", sha, context, state pending|success|failure|error, title, summary (≤ 65,536),
  5. // url (https or null), appId (apps @id), appName, updatedMs } one row per (head sha, context); a newer PUT replaces it
  6. // requiredRows pk @id index !key, rid storage/mpackdb/statusreq.db
  7. // { rid, key "<rid> <context>", context } the contexts the owner requires before a pull request may be merged
  8. // listStatuses(repo, sha) the GET route's work (v1.3) → { statuses } or { unknown: true }
  9. // putStatus(repo, app, body) the PUT route's work → { status } or { invalid: [details] } or { unknown: true }
  10. // badgesOf / requirementOf what /pulls shows for a pull request's CURRENT head, and what blocks its Merge button
  11. // missingText(slug, head) the server-side check of the Merge (lib/pulls.hl mergePullNow): '' when every required context is success on that head
  12. import { MPackDB } from 'hl:mpackdb'
  13. import { now, timestamp } from 'hl:time'
  14. import { storageDir, countOfList, firstOf, plainError } from './util.hl'
  15. import { repoBySlug, ownsRepo } from './repos.hl'
  16. import { gitRaw } from './git.hl'
  17. import { isHexId } from './git-helpers.hl'
  18. static statusRows = new MPackDB(file = storageDir + '/statuses.db', primaryKey = '@id', indexes = ['!key', 'shaKey'])
  19. static requiredRows = new MPackDB(file = storageDir + '/statusreq.db', primaryKey = '@id', indexes = ['!key', 'rid'])
  20. static states = ['pending', 'success', 'failure', 'error']
  21. static maxSummary = 65536
  22. static maxRequired = 20
  23. // ---- the PUT --------------------------------------------------------------------------------------------------
  24. // is the sha a commit of this repo? (the object exists and is a commit)
  25. static isCommitOf = (slug, sha) => {
  26. if (sha == null || hlTypeName(sha) != 'String' || sha.length != 40 || !isHexId(sha)) { return false }
  27. r = gitRaw(slug, ['cat-file', '-e', sha + '^{commit}'])
  28. return r != null && r.exit == 0
  29. }
  30. static httpsUrl = (u) => {
  31. return hlTypeName(u) == 'String' && u.length <= 500 && u.startsWith('https://') && u.length > 10 && plainError(u, 500, 'the url') == null && !u.includes(' ')
  32. }
  33. // the body's complaints as contract `details` ([] = fine)
  34. static problems = (j) => {
  35. out = []
  36. if (hlTypeName(j.context) != 'String' || j.context.trim() == '') {
  37. out.push({ field = 'context' reason = 'required' })
  38. } else if (plainError(j.context, 100, 'the context') != null) {
  39. out.push({ field = 'context' reason = 'too_long_or_not_one_line' })
  40. }
  41. if (hlTypeName(j.state) != 'String' || !states.includes(j.state)) { out.push({ field = 'state' reason = 'one_of_pending_success_failure_error' }) }
  42. if (j.title != null && (hlTypeName(j.title) != 'String' || plainError(j.title, 200, 'the title') != null)) { out.push({ field = 'title' reason = 'one_line_at_most_200' }) }
  43. if (j.summary != null && (hlTypeName(j.summary) != 'String' || j.summary.length > maxSummary)) { out.push({ field = 'summary' reason = 'text_at_most_65536' }) }
  44. if (j.url != null && !httpsUrl(j.url)) { out.push({ field = 'url' reason = 'https_only' }) }
  45. return out
  46. }
  47. static statusJson = (row) => {
  48. return { head = row.sha sha = row.sha context = row.context state = row.state title = row.title summary = row.summary url = row.url app = { id = row.appId name = row.appName } updated = timestamp(row.updatedMs) }
  49. }
  50. // PUT {api}/statuses/<sha> for `app` on `repo` (both from appOfRequest). The sha is a path value: anything that is not a commit of the repo is `unknown` (422).
  51. static putStatus = (repo, app, sha, j) => {
  52. bad = problems(j)
  53. if (bad.length > 0) { return { invalid = bad } }
  54. if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }
  55. ctx = j.context.trim()
  56. row = { rid = repo.id key = repo.id + ' ' + sha + ' ' + ctx shaKey = repo.id + ' ' + sha sha = sha context = ctx state = j.state title = j.title == null ? '' : j.title.trim() summary = j.summary == null ? '' : j.summary url = j.url appId = app.id appName = app.name updatedMs = now() }
  57. old = firstOf(statusRows.find('key', row.key))
  58. if (old != null) { statusRows.update(old.id, row) } else { statusRows.put(row) }
  59. saved = firstOf(statusRows.find('key', row.key))
  60. if (saved == null) { return { invalid = [{ field = 'status' reason = 'could_not_store' }] } }
  61. return { status = statusJson(saved) }
  62. }
  63. // GET {api}/statuses/<sha> (v1.3): every context on the sha, from every app → { statuses } or { unknown: true } when the repo has no such commit
  64. static listStatuses = (repo, sha) => {
  65. if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }
  66. out = []
  67. for (r of statusesOf(repo.id, sha)) { out.push(statusJson(r)) }
  68. return { statuses = out }
  69. }
  70. // ---- required before merge --------------------------------------------------------------------------------------
  71. static requiredOf = (repoId) => {
  72. all = requiredRows.find('rid', repoId)
  73. out = []
  74. if (countOfList(all) > 0) { for (r of all) { out.push(r.context) } }
  75. return out
  76. }
  77. // the owner's list for the Settings page: [{ id, name }]
  78. static requiredList = (repoId) => {
  79. all = requiredRows.find('rid', repoId)
  80. out = []
  81. if (countOfList(all) > 0) { for (r of all) { out.push({ id = r.id name = r.context }) } }
  82. return out
  83. }
  84. // the owner requires a context → { rows } or { error }
  85. static requireContext = (userId, session, slug, ctxName) => {
  86. repo = repoBySlug(slug)
  87. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }
  88. bad = plainError(ctxName, 100, 'the context')
  89. if (bad != null) { return { error = bad } }
  90. ctx = ('' + ctxName).trim()
  91. if (ctx == '') { return { error = 'give the context an app sets, for example kunibert' } }
  92. if (requiredOf(repo.id).includes(ctx)) { return { rows = requiredList(repo.id) } }
  93. if (requiredOf(repo.id).length >= maxRequired) { return { error = 'at most ' + maxRequired + ' contexts can be required' } }
  94. id = requiredRows.put({ rid = repo.id key = repo.id + ' ' + ctx context = ctx })
  95. if (id == null) { return { error = 'could not store the setting: ' + requiredRows.lastError() } }
  96. return { rows = requiredList(repo.id) }
  97. }
  98. static dropRequired = (userId, session, slug, id) => {
  99. repo = repoBySlug(slug)
  100. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }
  101. row = id == null || hlTypeName(id) != 'String' ? null : requiredRows.fetch(id)
  102. if (row == null || row.rid != repo.id) { return { error = 'no such requirement' } }
  103. requiredRows.delete(row.id)
  104. return { rows = requiredList(repo.id) }
  105. }
  106. // ---- what a pull request shows and what blocks it ---------------------------------------------------------------
  107. static statusesOf = (repoId, head) => {
  108. all = statusRows.find('shaKey', repoId + ' ' + head)
  109. return countOfList(all) == 0 ? [] : all
  110. }
  111. // the badge of one status row: its state (class too), "<app>: <context>" and the title as text
  112. static badgeOf = (row) => {
  113. return { id = row.id state = row.state cls = 'badge ' + row.state label = row.appName + ': ' + row.context title = row.title hasTitle = row.title != '' url = row.url == null ? '' : row.url hasUrl = row.url != null }
  114. }
  115. // the contexts that are not success on `head`: ["ctx (pending)", "ctx (not set)"]
  116. static missingOf = (repoId, head) => {
  117. have = statusesOf(repoId, head)
  118. out = []
  119. for (c of requiredOf(repoId)) {
  120. let state = 'not set'
  121. for (s of have) { if (s.context == c && s.state == 'success') { state = 'success' } else if (s.context == c && state == 'not set') { state = s.state } }
  122. if (state != 'success') { out.push(c + ' (' + state + ')') }
  123. }
  124. return out
  125. }
  126. // the SERVER-SIDE merge check: '' when allowed, else the sentence that says which context is missing
  127. static missingText = (slug, head) => {
  128. repo = repoBySlug(slug)
  129. if (repo == null) { return '' }
  130. m = missingOf(repo.id, head)
  131. if (m.length == 0) { return '' }
  132. return 'Not merged: the required checks are not successful on the current head ' + head.slice(0, 8) + ': ' + m.join(', ') + '.'
  133. }
  134. // the branch's tip, '' when unknown
  135. static headOf = (slug, branch) => {
  136. if (branch == null || branch == '') { return '' }
  137. r = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + branch])
  138. return r != null && r.exit == 0 && r.lines.length > 0 && isHexId(r.lines[0]) ? r.lines[0] : ''
  139. }
  140. // the rows of /pulls with `statuses` (badges of the CURRENT head), and the Merge button off while a required context is not success there
  141. static withStatuses = (list, slug) => {
  142. out = []
  143. repo = repoBySlug(slug)
  144. for (p of list) {
  145. q = p
  146. head = repo != null ? headOf(slug, p.source) : ''
  147. badges = []
  148. let blocked = ''
  149. if (head != '') {
  150. for (s of statusesOf(repo.id, head)) { badges.push(badgeOf(s)) }
  151. m = missingOf(repo.id, head)
  152. if (m.length > 0) { blocked = 'Merge waits for: ' + m.join(', ') }
  153. }
  154. q.statuses = badges
  155. q.hasStatuses = badges.length > 0
  156. q.blockText = blocked
  157. if (blocked != '') {
  158. q.canMerge = false
  159. q.mergeClass = 'nomerge'
  160. }
  161. out.push(q)
  162. }
  163. return out
  164. }

Branches

Latest commits

  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre
  • b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
  • fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
  • f7198131gitoria#45: tests/pr-page.mjs gatemre
  • 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
  • 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre
  • 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre
  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 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