gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit802f229a802f229alog: kunibert repo ping provenmre802f229a/lib/statuses.hl

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

Branches

Latest commits

  • 802f229alog: kunibert repo ping provenmre
  • 2d2eaa92log: Kunibert connected to kunibert repomre
  • 65d5f80alogmre
  • dab74b0dW49 report, logmre
  • fd4fc028Exercise member status reads with a populated check statemre
  • 536ca6f9Allow repository members to read pull workflow APIs with user tokensmre
  • 705dd834W49 missionmre
  • b554cd5flogmre
  • f4c9440ddeploy.sh: tar --hard-dereference for the live data backupmre
  • 1a687b15Merge branch 'w48-member-merge'mre
  • 166c775fW48 report, logmre
  • 1b4623f0Members merge pull requests: API route, button on /pulls and /pulls/N, one shared merge pathmre
  • 1075c38cW48 mission, logmre
  • 17bdd0d6V37/V39 missions, logmre
  • 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
  • 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