gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb05f6d92b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmreb05f6d92/lib/statuses.hl

11.4 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. // Required entries not successful on head, with app identity, context and machine-readable state.
  140. static missingOf = (repoId, head) => {
  141. have = statusesOf(repoId, head)
  142. out = []
  143. for (e of requiredEntries(repoId)) {
  144. let state = 'missing'
  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 == 'missing') { state = s.state } }
  147. }
  148. if (state != 'success') { out.push({ app = { id = e.appId name = appNameOf(e.appId) } context = e.context state = state }) }
  149. }
  150. return out
  151. }
  152. // Preserve the Merge button and error sentence, including the old labels for absent or removed apps.
  153. static missingLabels = (missing) => {
  154. out = []
  155. for (e of missing) {
  156. state = e.app.id == null || appsTable.fetch(e.app.id) == null ? 'app removed' : (e.state == 'missing' ? 'not set' : e.state)
  157. out.push(e.app.name + ' · ' + e.context + ' (' + state + ')')
  158. }
  159. return out
  160. }
  161. // the SERVER-SIDE merge check: '' when allowed, else the sentence that says which context is missing
  162. static missingText = (slug, head) => {
  163. repo = repoBySlug(slug)
  164. if (repo == null) { return '' }
  165. m = missingOf(repo.id, head)
  166. if (m.length == 0) { return '' }
  167. return 'Not merged: the required checks are not successful on the current head ' + head.slice(0, 8) + ': ' + missingLabels(m).join(', ') + '.'
  168. }
  169. // the branch's tip, '' when unknown
  170. static headOf = (slug, branch) => {
  171. if (branch == null || branch == '') { return '' }
  172. r = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + branch])
  173. return r != null && r.exit == 0 && r.lines.length > 0 && isHexId(r.lines[0]) ? r.lines[0] : ''
  174. }
  175. // the rows of /pulls with `statuses` (badges of the CURRENT head), and the Merge button off while a required context is not success there
  176. static withStatuses = (list, slug) => {
  177. out = []
  178. repo = repoBySlug(slug)
  179. for (p of list) {
  180. q = p
  181. head = repo != null ? headOf(slug, p.source) : ''
  182. badges = []
  183. let blocked = ''
  184. if (head != '') {
  185. for (s of statusesOf(repo.id, head)) { badges.push(badgeOf(s)) }
  186. m = missingOf(repo.id, head)
  187. if (m.length > 0) { blocked = 'Merge waits for: ' + missingLabels(m).join(', ') }
  188. }
  189. q.statuses = badges
  190. q.hasStatuses = badges.length > 0
  191. q.blockText = blocked
  192. if (blocked != '') {
  193. q.canMerge = false
  194. q.mergeClass = 'nomerge'
  195. }
  196. out.push(q)
  197. }
  198. return out
  199. }

Branches

Latest commits

  • b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
  • 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
  • f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre
  • dc77fb95main: offline note from __hlOnline, tick removed (#29)mre
  • 37a86d93tests/reviews: seeded fake hook's DNS failures are not server errors (flaky gate)mre
  • 07ab845bvendor: Hybriel set-day50 (run() leak fix #335)mre
  • 71b2fa4blog: hybriel#335mre
  • bd94cc7elog: #25 cause found (run() leak)mre
  • 565cb508log: H25 round 1mre
  • 90a0af84log: H25 startedmre
  • 87bd53b8mission H25mre
  • 71a746cflog: day48 livemre
  • bf568e14log: V48 mergedmre
  • 09955dedMerge v48: Hybriel set-day48, theme layouts ce4a0bamre
  • f2a2b0cevendor: Hybriel set-day48, theme layouts ce4a0bamre
  • 5d0834e7log: Apollo model rulemre
  • fe8e5bd5log: #25 sample, V48 missionmre
  • 8580c5d9log: theme fix from Atlasmre
  • 46823d93log: Apollo budget correctionmre
  • f6d32341log: Apollo on budget and #25 nextmre