gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit34143d2c34143d2cgitoria#46: closing brace of appComments lost in the mergemre34143d2c/lib/statuses.hl

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

Branches

Latest commits

  • 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
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre