gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchw53-pulls-speed-pr80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymrew53-pulls-speed-pr/lib/statuses.hl

14.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> <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, number) is the shared app-or-person merge check; app-only requirements keep their existing rule.
  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, mayPush } from './repos.hl'
  18. import { nameOfUser, userRecord } from './users.hl'
  19. import { memberIds, refOfRepo, refOfOrg } from './members.hl'
  20. import { orgBySlug } from './orgs.hl'
  21. import { hasApproval, approvalsOf } from './approvals.hl'
  22. import { appsTable } from './apps-table.hl'
  23. import { gitRaw } from './git.hl'
  24. import { isHexId } from './git-helpers.hl'
  25. static statusRows = new MPackDB(file = storageDir + '/statuses.db', primaryKey = '@id', indexes = ['!key', 'shaKey'])
  26. static requiredRows = new MPackDB(file = storageDir + '/statusreq.db', primaryKey = '@id', indexes = ['!key', 'rid'])
  27. static states = ['pending', 'success', 'failure', 'error']
  28. static maxSummary = 65536
  29. static maxRequired = 20
  30. // ---- the PUT --------------------------------------------------------------------------------------------------
  31. // is the sha a commit of this repo? (the object exists and is a commit)
  32. static isCommitOf = (slug, sha) => {
  33. if (sha == null || hlTypeName(sha) != 'String' || sha.length != 40 || !isHexId(sha)) { return false }
  34. r = gitRaw(slug, ['cat-file', '-e', sha + '^{commit}'])
  35. return r != null && r.exit == 0
  36. }
  37. static httpsUrl = (u) => {
  38. return hlTypeName(u) == 'String' && u.length <= 500 && u.startsWith('https://') && u.length > 10 && plainError(u, 500, 'the url') == null && !u.includes(' ')
  39. }
  40. // the body's complaints as contract `details` ([] = fine)
  41. static problems = (j) => {
  42. out = []
  43. if (hlTypeName(j.context) != 'String' || j.context.trim() == '') {
  44. out.push({ field = 'context' reason = 'required' })
  45. } else if (plainError(j.context, 100, 'the context') != null) {
  46. out.push({ field = 'context' reason = 'too_long_or_not_one_line' })
  47. }
  48. if (hlTypeName(j.state) != 'String' || !states.includes(j.state)) { out.push({ field = 'state' reason = 'one_of_pending_success_failure_error' }) }
  49. 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' }) }
  50. if (j.summary != null && (hlTypeName(j.summary) != 'String' || j.summary.length > maxSummary)) { out.push({ field = 'summary' reason = 'text_at_most_65536' }) }
  51. if (j.url != null && !httpsUrl(j.url)) { out.push({ field = 'url' reason = 'https_only' }) }
  52. return out
  53. }
  54. static statusJson = (row) => {
  55. 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) }
  56. }
  57. // 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).
  58. // 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
  59. static ownRow = (rid, sha, appId, ctx) => {
  60. f = firstOf(statusRows.find('key', rid + ' ' + sha + ' ' + appId + ' ' + ctx))
  61. if (f != null) { return f }
  62. for (r of statusesOf(rid, sha)) { if (r.appId == appId && r.context == ctx) { return r } }
  63. return null
  64. }
  65. static putStatus = (repo, app, sha, j) => {
  66. bad = problems(j)
  67. if (bad.length > 0) { return { invalid = bad } }
  68. if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }
  69. ctx = j.context.trim()
  70. 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() }
  71. old = ownRow(repo.id, sha, app.id, ctx)
  72. if (old != null) { statusRows.update(old.id, row) } else { statusRows.put(row) }
  73. saved = ownRow(repo.id, sha, app.id, ctx)
  74. if (saved == null) { return { invalid = [{ field = 'status' reason = 'could_not_store' }] } }
  75. return { status = statusJson(saved) }
  76. }
  77. // 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
  78. static listStatuses = (repo, sha) => {
  79. if (!isCommitOf(repo.slug, sha)) { return { unknown = true } }
  80. out = []
  81. for (r of statusesOf(repo.id, sha)) { out.push(statusJson(r)) }
  82. return { statuses = out }
  83. }
  84. // ---- required before merge --------------------------------------------------------------------------------------
  85. static appNameOf = (appId) => {
  86. a = appId == null ? null : appsTable.fetch(appId)
  87. return a == null ? '(removed app)' : a.name
  88. }
  89. // The owner's entries: app + context, or userId (a required person).
  90. static requiredEntries = (repoId) => {
  91. all = requiredRows.find('rid', repoId)
  92. out = []
  93. if (countOfList(all) > 0) { for (r of all) { out.push({ appId = r.appId context = r.context userId = r.userId }) } }
  94. return out
  95. }
  96. // Legacy pull.required remains app + context only. People have their own additive field.
  97. static requiredOf = (repoId) => {
  98. out = []
  99. for (e of requiredEntries(repoId)) {
  100. if (e.userId == null) { out.push({ app = { id = e.appId name = appNameOf(e.appId) } context = e.context }) }
  101. }
  102. return out
  103. }
  104. static approversOf = (String repoId) => {
  105. out = []
  106. for (e of requiredEntries(repoId)) { if (e.userId != null) { out.push({ user = { id = e.userId name = nameOfUser(e.userId) } }) } }
  107. return out
  108. }
  109. // the owner's list for the Settings page: [{ id, name }] with name "App name · context"
  110. static requiredList = (repoId) => {
  111. all = requiredRows.find('rid', repoId)
  112. out = []
  113. if (countOfList(all) > 0) { for (r of all) { out.push({ id = r.id name = r.userId != null ? 'Person · ' + nameOfUser(r.userId) : appNameOf(r.appId) + ' · ' + r.context }) } }
  114. return out
  115. }
  116. // the owner requires app + context → { rows } or { error }. The app is picked by its id (an app of THIS repo).
  117. static requireContext = (userId, session, slug, appId, ctxName) => {
  118. repo = repoBySlug(slug)
  119. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }
  120. app = appId == null || hlTypeName(appId) != 'String' ? null : appsTable.fetch(appId)
  121. if (app == null || app.repo != repo.id) { return { error = 'pick one of the apps connected to this repository' } }
  122. bad = plainError(ctxName, 100, 'the context')
  123. if (bad != null) { return { error = bad } }
  124. ctx = ('' + ctxName).trim()
  125. if (ctx == '') { return { error = 'give the context the app sets, for example kunibert' } }
  126. key = repo.id + ' ' + app.id + ' ' + ctx
  127. if (firstOf(requiredRows.find('key', key)) != null) { return { rows = requiredList(repo.id) } }
  128. if (requiredEntries(repo.id).length >= maxRequired) { return { error = 'at most ' + maxRequired + ' contexts can be required' } }
  129. id = requiredRows.put({ rid = repo.id key = key appId = app.id context = ctx })
  130. if (id == null) { return { error = 'could not store the setting: ' + requiredRows.lastError() } }
  131. return { rows = requiredList(repo.id) }
  132. }
  133. // Owners select known users with the same push rights used by Merge (including organization members).
  134. static peopleOfRepo = (String slug) => {
  135. repo = repoBySlug(slug)
  136. out = []
  137. if (repo == null) { return out }
  138. ids = [repo.owner]
  139. for (id of memberIds(refOfRepo(slug))) { if (!ids.includes(id)) { ids.push(id) } }
  140. org = orgBySlug(repo.org)
  141. if (org != null) {
  142. if (!ids.includes(org.owner)) { ids.push(org.owner) }
  143. for (id of memberIds(refOfOrg(org.slug))) { if (!ids.includes(id)) { ids.push(id) } }
  144. }
  145. for (id of ids) { if (userRecord(id) != null && mayPush(id, repo)) { out.push({ id = id name = nameOfUser(id) }) } }
  146. return out
  147. }
  148. static requirePerson = (String userId, Hybrid session, String slug, String personId) => {
  149. repo = repoBySlug(slug)
  150. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }
  151. if (personId == null || !mayPush(personId, repo)) { return { error = 'pick an owner or member of this repository' } }
  152. key = repo.id + ' user ' + personId
  153. if (firstOf(requiredRows.find('key', key)) != null) { return { rows = requiredList(repo.id) } }
  154. if (requiredEntries(repo.id).length >= maxRequired) { return { error = 'at most ' + maxRequired + ' entries can be required' } }
  155. id = requiredRows.put({ rid = repo.id key = key userId = personId })
  156. return id == null ? { error = 'could not store the setting' } : { rows = requiredList(repo.id) }
  157. }
  158. static dropRequired = (userId, session, slug, id) => {
  159. repo = repoBySlug(slug)
  160. if (repo == null || userId == null || !ownsRepo(slug, session)) { return { error = 'only the owner of this repository can change what a merge requires' } }
  161. row = id == null || hlTypeName(id) != 'String' ? null : requiredRows.fetch(id)
  162. if (row == null || row.rid != repo.id) { return { error = 'no such requirement' } }
  163. requiredRows.delete(row.id)
  164. return { rows = requiredList(repo.id) }
  165. }
  166. // ---- what a pull request shows and what blocks it ---------------------------------------------------------------
  167. static statusesOf = (repoId, head) => {
  168. all = statusRows.find('shaKey', repoId + ' ' + head)
  169. return countOfList(all) == 0 ? [] : all
  170. }
  171. // the badge of one status row: its state (class too), "<app>: <context>" and the title as text
  172. static badgeOf = (row) => {
  173. 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 }
  174. }
  175. // Missing alternatives on this PR/head: all app contexts, or any listed person. App-only details keep their old shape.
  176. static missingOf = (String repoId, String head, Number number, Hybrid approvals = null) => {
  177. have = statusesOf(repoId, head)
  178. out = []
  179. people = []
  180. let appCount = 0
  181. let approved = false
  182. repoRows = requiredEntries(repoId)
  183. rows = approvals == null ? approvalsOf(repoId, number) : approvals
  184. for (e of repoRows) {
  185. if (e.userId != null) {
  186. people.push({ id = e.userId name = nameOfUser(e.userId) })
  187. if (hasApproval(rows, e.userId, head)) { approved = true }
  188. continue
  189. }
  190. appCount = appCount + 1
  191. let state = 'missing'
  192. if (e.appId != null && appsTable.fetch(e.appId) != null) {
  193. 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 } }
  194. }
  195. if (state != 'success') { out.push({ app = { id = e.appId name = appNameOf(e.appId) } context = e.context state = state }) }
  196. }
  197. if (people.length > 0) {
  198. if (approved || (appCount > 0 && out.length == 0)) { return [] }
  199. out.push({ approval = { users = people } state = 'missing' })
  200. }
  201. return out
  202. }
  203. // Preserve the Merge button and error sentence, including the old labels for absent or removed apps.
  204. static missingLabels = (missing) => {
  205. out = []
  206. for (e of missing) {
  207. if (e.approval != null) {
  208. names = []
  209. for (u of e.approval.users) { names.push(u.name) }
  210. out.push('an approval from ' + names.join(' or '))
  211. continue
  212. }
  213. state = e.app.id == null || appsTable.fetch(e.app.id) == null ? 'app removed' : (e.state == 'missing' ? 'not set' : e.state)
  214. out.push(e.app.name + ' · ' + e.context + ' (' + state + ')')
  215. }
  216. return out
  217. }
  218. static missingWords = (Hybrid missing) => {
  219. labels = missingLabels(missing)
  220. if (missing.length > 0 && missing[missing.length - 1].approval != null && labels.length > 1) {
  221. return labels.slice(0, labels.length - 1).join(', ') + ' or ' + labels[labels.length - 1]
  222. }
  223. return labels.join(', ')
  224. }
  225. // Internal alternatives stay separate from the legacy API detail rows.
  226. static missingDetails = (Hybrid missing) => {
  227. out = []
  228. for (e of missing) { if (e.app != null) { out.push(e) } }
  229. return out
  230. }
  231. static missingApproval = (Hybrid missing) => {
  232. for (e of missing) { if (e.approval != null) { return { users = e.approval.users state = 'missing' } } }
  233. return null
  234. }
  235. static missingMessage = (String head, Hybrid missing) => {
  236. if (missing.length == 0) { return '' }
  237. return 'Not merged: the required checks are not successful on the current head ' + head.slice(0, 8) + ': ' + missingWords(missing) + '.'
  238. }
  239. static missingText = (String slug, String head, Number number, Hybrid approvals = null) => {
  240. repo = repoBySlug(slug)
  241. return repo == null ? '' : missingMessage(head, missingOf(repo.id, head, number, approvals))
  242. }
  243. // the branch's tip, '' when unknown
  244. static headOf = (slug, branch) => {
  245. if (branch == null || branch == '') { return '' }
  246. r = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + branch])
  247. return r != null && r.exit == 0 && r.lines.length > 0 && isHexId(r.lines[0]) ? r.lines[0] : ''
  248. }
  249. // the rows of /pulls with `statuses` (badges of the CURRENT head), and the Merge button off while a required context is not success there
  250. static withStatuses = (list, slug) => {
  251. out = []
  252. repo = repoBySlug(slug)
  253. for (p of list) {
  254. q = p
  255. head = repo != null ? headOf(slug, p.source) : ''
  256. badges = []
  257. let blocked = ''
  258. if (head != '') {
  259. for (s of statusesOf(repo.id, head)) { badges.push(badgeOf(s)) }
  260. m = missingOf(repo.id, head, p.number)
  261. if (m.length > 0) { blocked = 'Merge waits for: ' + missingWords(m) }
  262. }
  263. q.statuses = badges
  264. q.hasStatuses = badges.length > 0
  265. q.blockText = blocked
  266. if (blocked != '') {
  267. q.canMerge = false
  268. q.mergeClass = 'nomerge'
  269. }
  270. out.push(q)
  271. }
  272. return out
  273. }

Branches

Latest commits

  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
  • 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
  • 83f0c953Report person approval verificationmre
  • 3ae94b86Record browser failures and verify owned tab cleanupmre
  • 54eec687Gate person approvals and merge alternativesmre
  • 12b08829Add person approvals for pull request headsmre
  • 55c84779Merge branch 'gp-gate-ports' into mainGitoria
  • 57af42bbREADME: point to deploy.sh for the port listmre
  • d63375d2README: gate commands with explicit portsmre
  • feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
  • 6cc1f943Merge branch 'records-1112' into mainGitoria
  • 9448c438|||PR records: LOG, missions and reports since 11-09mre
  • 6dc08a8eMerge branch 'p51-public-pr-reads' into mainGitoria
  • 7b88cfe0Type publicReaderOfRequest parameters (L10)Gitoria
  • 2a4b3dc1|||PR Anonymous reads of public repos' pulls and statusesmre
  • 21b2c141Merge branch 's25-no-session-on-login-error' into mainGitoria