gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3a8b2fb83a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre3a8b2fb8/lib/prpage.hl

11.5 KB

  1. // lib/prpage.hl — THE PULL REQUEST PAGE (gitoria#45, part of the push API #38): what /pulls/N shows and what a person can do there.
  2. // Reads (all keyed by the repo's id and the PR number, never a list position): the PR, its statuses on the current head, every app's current verdict,
  3. // the reviews, the threads and the conversation (tables: lib/reviews.hl). Writes (a person's comment, reply, resolve / reopen, "Review again") store through
  4. // lib/reviews.hl and raise the contract's events (§4) through lib/events.hl: pull_comment → every app, review_comment / review_thread / review_requested → THAT app only.
  5. // WHO MAY WRITE is ONE helper, mayWritePull: signed in AND owner or member of the repo (the people who may push). Everybody else reads.
  6. // Bodies are Markdown shown as TEXT NODES (lib/markdown.hl blocks, components/markdown.hl): no raw HTML anywhere.
  7. import { slugOfRequest } from './orgs.hl'
  8. import { repoBySlug, repoAt, mayPush } from './repos.hl'
  9. import { userOfSession, nameOfUser } from './users.hl'
  10. import { ensurePulls, pullObject, recordByNumber, recordsOf } from './pullsync.hl'
  11. import { appsTable, appsOfRepo } from './apps-table.hl'
  12. import { statusesOf, badgeOf, missingText, headOf } from './statuses.hl'
  13. import { refParts } from './tickets-helpers.hl'
  14. import { markdownBlocks } from './markdown.hl'
  15. import { currentVerdict, pullTalk, personComment, personReply, personResolve, anyThreadOf, threadShape } from './reviews.hl'
  16. import { numberOf, isoMs } from './reviews-helpers.hl'
  17. import { raiseEvent } from './events.hl'
  18. import { setApproval, hasApproval, approvalsOf } from './approvals.hl'
  19. import { mergeAndAnnounce } from './pullmerge.hl'
  20. // ---- who may write -----------------------------------------------------------------------------------------------
  21. // the one place that decides it (Atlas's reading, not the creator's words: "who may push"); `record` = the repos table record
  22. static mayWritePull = (user, record) => {
  23. return user != null && record != null && mayPush(user.id, record)
  24. }
  25. // ---- the PR of a request: the record, or null (no such repo for this viewer, unknown number) ------------------------
  26. static prOfRequest = (host, orepo, session, nText) => {
  27. slug = slugOfRequest(host, orepo)
  28. if (repoAt(slug, session) == null) { return null }
  29. n = numberOf(nText)
  30. record = repoBySlug(slug)
  31. if (n == null || record == null) { return null }
  32. ensurePulls(slug)
  33. return recordByNumber(record.id, n)
  34. }
  35. // ---- the view ------------------------------------------------------------------------------------------------------
  36. static verdictWords = (v) => { return v == 'approve' ? 'approved' : (v == 'request_changes' ? 'requested changes' : 'commented') }
  37. static appNameOf = (appId) => {
  38. a = appId == null ? null : appsTable.fetch(appId)
  39. return a == null ? '(removed app)' : a.name
  40. }
  41. // a comment's ISO time as "2026-11-02 06:00:00" (UTC, as the contract gives it)
  42. static shownTime = (iso) => { return iso.slice(0, 10) + ' ' + iso.slice(11, 19) + ' UTC' }
  43. static commentRow = (c) => {
  44. isApp = c.author.type == 'app'
  45. return { id = c.id author = c.author.name kind = isApp ? 'app' : 'user' isApp = isApp blocks = markdownBlocks(c.body) created = shownTime(c.created) }
  46. }
  47. static approvalPageRows = (Hybrid rows, String head) => {
  48. out = []
  49. for (a of rows) {
  50. current = a.head == head
  51. out.push({ id = a.id userId = a.userId sha = a.head name = nameOfUser(a.userId) head = a.head.slice(0, 8) words = current ? 'approved the current head' : 'approved an earlier head' created = shownTime(isoMs(a.created)) }) }
  52. return out
  53. }
  54. // everything the page shows → the members of components/pull.hl. `viewer` = the session's user (or null)
  55. static prView = (slug, rec, session) => {
  56. repo = repoBySlug(slug)
  57. user = userOfSession(session)
  58. approvalRows = approvalsOf(repo.id, rec.number)
  59. pull = pullObject(slug, rec.number, approvalRows)
  60. apps = appsOfRepo(repo.id)
  61. talk = pullTalk(repo, rec)
  62. badges = []
  63. for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }
  64. verdicts = []
  65. appList = []
  66. for (a of apps) {
  67. cv = currentVerdict(repo.id, rec.number, a.id)
  68. text = cv == null ? 'no review yet' : verdictWords(cv.verdict) + (cv.head == rec.head ? '' : ' on an earlier head ' + cv.head.slice(0, 8))
  69. verdicts.push({ id = a.id app = a.name text = text cls = cv == null ? 'none' : cv.verdict })
  70. appList.push({ id = a.id name = a.name })
  71. }
  72. reviews = []
  73. for (r of talk.reviews) {
  74. reviews.push({ id = r.id app = appNameOf(r.app) verdict = r.verdict words = verdictWords(r.verdict) head = r.head.slice(0, 8) earlier = r.head != rec.head ? 'earlier head' : 'current head' blocks = markdownBlocks(r.body) created = shownTime(isoMs(r.created)) })
  75. }
  76. threads = []
  77. for (t of talk.threads) { threads.push(threadRow(t)) }
  78. conversation = []
  79. for (c of talk.talk) { conversation.push(commentRow(c)) }
  80. pr = { number = rec.number numberText = '#' + rec.number title = pull.title parts = refParts(pull.title, repo.tktProject != null ? repo.tktProject : '') state = rec.state source = rec.source target = rec.target author = pull.author.name head = rec.head.slice(0, 8) }
  81. canAct = mayWritePull(user, repo)
  82. // the Merge button: always in the page, `.nomerge` hides it (an attribute is re-read when the view is reassigned, an `if` is not); the face checks again
  83. blocked = missingText(slug, rec.head, rec.number, approvalRows)
  84. showMerge = canAct && rec.state == 'open' && blocked == ''
  85. approvals = approvalPageRows(approvalRows, rec.head)
  86. approved = canAct && hasApproval(approvalRows, user.id, rec.head)
  87. return { pr = pr approvals = approvals approvalHead = rec.head approvalLabel = approved ? 'Withdraw approval' : 'Approve' canApprove = canAct && rec.state == 'open' approved = approved blockText = blocked statuses = badges hasStatuses = badges.length > 0 verdicts = verdicts hasVerdicts = verdicts.length > 0 apps = appList hasApps = appList.length > 0 reviews = reviews hasReviews = reviews.length > 0 threads = threads hasThreads = threads.length > 0 conversation = conversation hasConversation = conversation.length > 0 canAct = canAct readOnly = !canAct mergeClass = showMerge ? 'mergepr' : 'nomerge' }
  88. }
  89. static threadRow = (t) => {
  90. cs = []
  91. for (c of t.shape.comments) { cs.push(commentRow(c)) }
  92. marks = []
  93. if (t.shape.outdated) { marks.push('outdated') }
  94. if (t.shape.resolved) { marks.push('resolved') }
  95. return { id = t.shape.id app = appNameOf(t.row.app) path = t.shape.path line = t.shape.line where = t.shape.path + ':' + t.shape.line mark = marks.join(' · ') resolved = t.shape.resolved cls = t.shape.resolved ? 'thread resolved' : 'thread' toggleLabel = t.shape.resolved ? 'Reopen' : 'Resolve' nextState = !t.shape.resolved comments = cs }
  96. }
  97. // ---- the writes (the faces in components/pull.hl are thin wrappers around these) ----------------------------------
  98. // who + which PR → { repo, rec, user } | { error }; a viewer who may not write gets the same answer as a forged call would
  99. static actorFor = (slug, nText, session) => {
  100. if (repoAt(slug, session) == null) { return { error = 'no such pull request' } }
  101. user = userOfSession(session)
  102. if (user == null) { return { error = 'sign in to do that' } }
  103. repo = repoBySlug(slug)
  104. if (!mayWritePull(user, repo)) { return { error = 'only the owner and the members of this repository can do that' } }
  105. if (user.name == '') { return { error = 'choose a display name first (top of the page)' } }
  106. n = numberOf(nText)
  107. rec = n == null ? null : recordByNumber(repo.id, n)
  108. if (rec == null) { return { error = 'no such pull request' } }
  109. return { repo = repo rec = rec user = user }
  110. }
  111. // the PR page's Merge button: the same merge as the API's and the list's (lib/pullmerge.hl); the page is read again
  112. static mergeThePull = (slug, nText, session) => {
  113. a = actorFor(slug, nText, session)
  114. if (a.error != null) { return { error = a.error } }
  115. r = mergeAndAnnounce(a.repo, a.rec.sha, a.user, '')
  116. if (r.error != null) { return { error = r.error } }
  117. b = recordByNumber(a.repo.id, a.rec.number)
  118. return { view = prView(slug, b, session) notice = 'Merged.' }
  119. }
  120. static done = (slug, a, session) => { return { view = prView(slug, a.rec, session) } }
  121. static commentOnPull = (slug, nText, session, body) => {
  122. a = actorFor(slug, nText, session)
  123. if (a.error != null) { return { error = a.error } }
  124. r = personComment(a.repo, a.rec, a.user, body)
  125. if (r.error != null) { return { error = r.error } }
  126. raiseEvent(a.repo, 'pull_comment', 'created', { pull = pullObject(slug, a.rec.number) comment = r.comment }, a.user.id)
  127. return done(slug, a, session)
  128. }
  129. static replyInThread = (slug, nText, session, threadId, body) => {
  130. a = actorFor(slug, nText, session)
  131. if (a.error != null) { return { error = a.error } }
  132. r = personReply(a.repo, a.rec, a.user, threadId, body)
  133. if (r.error != null) { return { error = r.error } }
  134. t = anyThreadOf(a.repo, a.rec, threadId)
  135. shape = threadShape(t, a.rec.head)
  136. raiseEvent(a.repo, 'review_comment', 'created', { pull = pullObject(slug, a.rec.number) thread = shape comment = r.comment onlyApp = t.app }, a.user.id)
  137. return done(slug, a, session)
  138. }
  139. static setThreadResolved = (slug, nText, session, threadId, resolved) => {
  140. a = actorFor(slug, nText, session)
  141. if (a.error != null) { return { error = a.error } }
  142. r = personResolve(a.repo, a.rec, threadId, resolved)
  143. if (r.error != null) { return { error = r.error } }
  144. if (r.changed) {
  145. raiseEvent(a.repo, 'review_thread', resolved ? 'resolved' : 'unresolved', { pull = pullObject(slug, a.rec.number) thread = r.shape onlyApp = r.thread.app }, a.user.id)
  146. }
  147. return done(slug, a, session)
  148. }
  149. static reviewAgain = (slug, nText, session, appId) => {
  150. a = actorFor(slug, nText, session)
  151. if (a.error != null) { return { error = a.error } }
  152. let found = null
  153. for (x of appsOfRepo(a.repo.id)) { if (x.id == appId) { found = x } }
  154. if (found == null) { return { error = 'no such app on this repository' } }
  155. raiseEvent(a.repo, 'review_requested', 'requested', { pull = pullObject(slug, a.rec.number) onlyApp = found.id }, a.user.id)
  156. return { view = prView(slug, a.rec, session) notice = 'Asked ' + found.name + ' to review again.' }
  157. }
  158. // A stale tab must never approve a head the person has not seen. The session is resolved again on every click.
  159. static approveThePull = (String slug, String nText, Hybrid session, String head, Boolean wanted) => {
  160. a = actorFor(slug, nText, session)
  161. if (a.error != null) { return { error = a.error } }
  162. if (wanted != true && wanted != false) { return { error = 'approval must be true or false' } }
  163. if (a.rec.state != 'open') { return { error = 'this pull request is not open' } }
  164. if (head != a.rec.head || head != headOf(slug, a.rec.source)) { return { error = 'the head changed; read the pull request again before approving' } }
  165. r = setApproval(a.repo.id, a.rec.number, a.user.id, head, wanted)
  166. if (r.error != null) { return r }
  167. result = done(slug, a, session)
  168. emit client gitoriaPullApprovalChanged(slug, a.rec.number, { approvals = result.view.approvals head = head blockText = result.view.blockText })
  169. return result
  170. }
  171. // Public PR state only; the audience checks repo visibility and each receiving page checks its number.
  172. static announceRequirements = (String slug, Hybrid rows) => {
  173. emit client gitoriaRequiredChanged(slug, rows)
  174. repo = repoBySlug(slug)
  175. if (repo == null) { return null }
  176. for (rec of recordsOf(repo.id)) {
  177. if (rec.state == 'open') {
  178. approvalRows = approvalsOf(repo.id, rec.number)
  179. shown = approvalPageRows(approvalRows, rec.head)
  180. emit client gitoriaPullApprovalChanged(slug, rec.number, { approvals = shown head = rec.head blockText = missingText(slug, rec.head, rec.number, approvalRows) })
  181. }
  182. }
  183. return null
  184. }

Branches

Latest commits

  • 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
  • 698458e2|||PR No session for cookieless login errors: READMEmre
  • cce7b785|||PR No session for cookieless login errorsmre
  • 678d6303Merge branch 'v59' into mainGitoria
  • 5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre