gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainfe209ffeMerge branch 'w56-owner-members' into mainGitoriamain/lib/prpage.hl

11.2 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 { shownTime } from './util.hl'
  8. import { slugOfRequest } from './orgs.hl'
  9. import { repoBySlug, repoAt, mayPush } from './repos.hl'
  10. import { userOfSession } from './users.hl'
  11. import { ensurePulls, pullObject, recordByNumber } from './pullsync.hl'
  12. import { appsTable, appsOfRepo } from './apps-table.hl'
  13. import { statusesOf, badgeOf, missingText, headOf } from './statuses.hl'
  14. import { refParts } from './tickets-helpers.hl'
  15. import { markdownBlocks } from './markdown.hl'
  16. import { currentVerdict, pullTalk, personComment, personReply, personResolve, anyThreadOf, threadShape } from './reviews.hl'
  17. import { numberOf, isoMs } from './reviews-helpers.hl'
  18. import { raiseEvent } from './events.hl'
  19. import { approvalPageRows } from './approvals-helpers.hl'
  20. import { setApproval, hasApproval, approvalsOf } from './approvals.hl'
  21. import { mergeAndAnnounce } from './pullmerge.hl'
  22. // ---- who may write -----------------------------------------------------------------------------------------------
  23. // the one place that decides it (Atlas's reading, not the creator's words: "who may push"); `record` = the repos table record
  24. static mayWritePull = (user, record) => {
  25. return user != null && record != null && mayPush(user.id, record)
  26. }
  27. // ---- the PR of a request: the record, or null (no such repo for this viewer, unknown number) ------------------------
  28. static prOfRequest = (host, orepo, session, nText) => {
  29. slug = slugOfRequest(host, orepo)
  30. if (repoAt(slug, session) == null) { return null }
  31. n = numberOf(nText)
  32. record = repoBySlug(slug)
  33. if (n == null || record == null) { return null }
  34. ensurePulls(slug)
  35. return recordByNumber(record.id, n)
  36. }
  37. // ---- the view ------------------------------------------------------------------------------------------------------
  38. static verdictWords = (v) => { return v == 'approve' ? 'approved' : (v == 'request_changes' ? 'requested changes' : 'commented') }
  39. static appNameOf = (appId) => {
  40. a = appId == null ? null : appsTable.fetch(appId)
  41. return a == null ? '(removed app)' : a.name
  42. }
  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. // everything the page shows → the members of components/pull.hl. `viewer` = the session's user (or null)
  48. static prView = (slug, rec, session) => {
  49. repo = repoBySlug(slug)
  50. user = userOfSession(session)
  51. approvalRows = approvalsOf(repo.id, rec.number)
  52. pull = pullObject(slug, rec.number, approvalRows)
  53. apps = appsOfRepo(repo.id)
  54. talk = pullTalk(repo, rec)
  55. badges = []
  56. for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }
  57. verdicts = []
  58. appList = []
  59. for (a of apps) {
  60. cv = currentVerdict(repo.id, rec.number, a.id)
  61. text = cv == null ? 'no review yet' : verdictWords(cv.verdict) + (cv.head == rec.head ? '' : ' on an earlier head ' + cv.head.slice(0, 8))
  62. verdicts.push({ id = a.id app = a.name text = text cls = cv == null ? 'none' : cv.verdict })
  63. appList.push({ id = a.id name = a.name })
  64. }
  65. reviews = []
  66. for (r of talk.reviews) {
  67. 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)) })
  68. }
  69. threads = []
  70. for (t of talk.threads) { threads.push(threadRow(t)) }
  71. conversation = []
  72. for (c of talk.talk) { conversation.push(commentRow(c)) }
  73. 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) }
  74. canAct = mayWritePull(user, repo)
  75. // 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
  76. blocked = missingText(slug, rec.head, rec.number, approvalRows)
  77. showMerge = canAct && rec.state == 'open' && blocked == ''
  78. approvals = approvalPageRows(approvalRows, rec.head)
  79. approved = canAct && hasApproval(approvalRows, user.id, rec.head)
  80. 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' }
  81. }
  82. static threadRow = (t) => {
  83. cs = []
  84. for (c of t.shape.comments) { cs.push(commentRow(c)) }
  85. marks = []
  86. if (t.shape.outdated) { marks.push('outdated') }
  87. if (t.shape.resolved) { marks.push('resolved') }
  88. 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 }
  89. }
  90. // ---- the writes (the faces in components/pull.hl are thin wrappers around these) ----------------------------------
  91. // who + which PR → { repo, rec, user } | { error }; a viewer who may not write gets the same answer as a forged call would
  92. static actorFor = (slug, nText, session) => {
  93. if (repoAt(slug, session) == null) { return { error = 'no such pull request' } }
  94. user = userOfSession(session)
  95. if (user == null) { return { error = 'sign in to do that' } }
  96. repo = repoBySlug(slug)
  97. if (!mayWritePull(user, repo)) { return { error = 'only the owner and the members of this repository can do that' } }
  98. if (user.name == '') { return { error = 'choose a display name first (top of the page)' } }
  99. n = numberOf(nText)
  100. rec = n == null ? null : recordByNumber(repo.id, n)
  101. if (rec == null) { return { error = 'no such pull request' } }
  102. return { repo = repo rec = rec user = user }
  103. }
  104. // 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
  105. static mergeThePull = (slug, nText, session) => {
  106. a = actorFor(slug, nText, session)
  107. if (a.error != null) { return { error = a.error } }
  108. r = mergeAndAnnounce(a.repo, a.rec.sha, a.user, '')
  109. if (r.error != null) { return { error = r.error } }
  110. b = recordByNumber(a.repo.id, a.rec.number)
  111. return { view = prView(slug, b, session) notice = 'Merged.' }
  112. }
  113. static done = (slug, a, session) => { return { view = prView(slug, a.rec, session) } }
  114. static commentOnPull = (slug, nText, session, body) => {
  115. a = actorFor(slug, nText, session)
  116. if (a.error != null) { return { error = a.error } }
  117. r = personComment(a.repo, a.rec, a.user, body)
  118. if (r.error != null) { return { error = r.error } }
  119. raiseEvent(a.repo, 'pull_comment', 'created', { pull = pullObject(slug, a.rec.number) comment = r.comment }, a.user.id)
  120. return done(slug, a, session)
  121. }
  122. static replyInThread = (slug, nText, session, threadId, body) => {
  123. a = actorFor(slug, nText, session)
  124. if (a.error != null) { return { error = a.error } }
  125. r = personReply(a.repo, a.rec, a.user, threadId, body)
  126. if (r.error != null) { return { error = r.error } }
  127. t = anyThreadOf(a.repo, a.rec, threadId)
  128. shape = threadShape(t, a.rec.head)
  129. raiseEvent(a.repo, 'review_comment', 'created', { pull = pullObject(slug, a.rec.number) thread = shape comment = r.comment onlyApp = t.app }, a.user.id)
  130. return done(slug, a, session)
  131. }
  132. static setThreadResolved = (slug, nText, session, threadId, resolved) => {
  133. a = actorFor(slug, nText, session)
  134. if (a.error != null) { return { error = a.error } }
  135. r = personResolve(a.repo, a.rec, threadId, resolved)
  136. if (r.error != null) { return { error = r.error } }
  137. if (r.changed) {
  138. raiseEvent(a.repo, 'review_thread', resolved ? 'resolved' : 'unresolved', { pull = pullObject(slug, a.rec.number) thread = r.shape onlyApp = r.thread.app }, a.user.id)
  139. }
  140. return done(slug, a, session)
  141. }
  142. static reviewAgain = (slug, nText, session, appId) => {
  143. a = actorFor(slug, nText, session)
  144. if (a.error != null) { return { error = a.error } }
  145. let found = null
  146. for (x of appsOfRepo(a.repo.id)) { if (x.id == appId) { found = x } }
  147. if (found == null) { return { error = 'no such app on this repository' } }
  148. raiseEvent(a.repo, 'review_requested', 'requested', { pull = pullObject(slug, a.rec.number) onlyApp = found.id }, a.user.id)
  149. return { view = prView(slug, a.rec, session) notice = 'Asked ' + found.name + ' to review again.' }
  150. }
  151. // A stale tab must never approve a head the person has not seen. The session is resolved again on every click.
  152. static approveThePull = (String slug, String nText, Hybrid session, String head, Boolean wanted) => {
  153. a = actorFor(slug, nText, session)
  154. if (a.error != null) { return { error = a.error } }
  155. if (wanted != true && wanted != false) { return { error = 'approval must be true or false' } }
  156. if (a.rec.state != 'open') { return { error = 'this pull request is not open' } }
  157. if (head != a.rec.head || head != headOf(slug, a.rec.source)) { return { error = 'the head changed; read the pull request again before approving' } }
  158. r = setApproval(a.repo.id, a.rec.number, a.user.id, head, wanted)
  159. if (r.error != null) { return r }
  160. result = done(slug, a, session)
  161. emit client gitoriaPullApprovalChanged(slug, a.rec.number, { approvals = result.view.approvals head = head blockText = result.view.blockText })
  162. return result
  163. }
  164. // One invalidation, independent of the number of PRs. Only mounted pages fetch their own state.
  165. static announceRequirements = (String slug) => {
  166. emit client gitoriaRequiredChanged(slug)
  167. return null
  168. }
  169. static approvalStateFor = (String slug, String nText, Hybrid session) => {
  170. if (repoAt(slug, session) == null) { return null }
  171. repo = repoBySlug(slug)
  172. number = numberOf(nText)
  173. rec = number == null ? null : recordByNumber(repo.id, number)
  174. if (rec == null) { return null }
  175. rows = approvalsOf(repo.id, number)
  176. badges = []
  177. for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }
  178. return { approvals = approvalPageRows(rows, rec.head) head = rec.head blockText = missingText(slug, rec.head, number, rows) statuses = badges hasStatuses = badges.length > 0 }
  179. }

Branches

Latest commits

  • fe209ffeMerge branch 'w56-owner-members' into mainGitoria
  • fa0754c8|||PR repository owner transfer and members API (#55)mre
  • 8862e537Merge branch 'w55-rename-org' into mainGitoria
  • 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
  • 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
  • 499f25f2Review fixes for the Settings organization move (#53)mre
  • ed3b922b|||PR move a repository into an organization in Settings (#53)mre
  • 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
  • 9f7ce8d8Preserve rename compatibility and live page titlesmre
  • c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
  • ce08920dMerge branch 'w52c-header-phone' into mainGitoria
  • 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 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