gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitcc1be2d6cc1be2d6Merge branch 'w62-pr-close' into mainGitoriacc1be2d6/lib/prpage.hl

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

Branches

Latest commits

  • cc1be2d6Merge branch 'w62-pr-close' into mainGitoria
  • 6e03be0apull state: keep the resync marker until the sync ran (#56)mre
  • 93bdd26cpull state: wait for a claimed row, resync when it stays busy (#56)mre
  • e53d284bpull state: stop on a failed branch scan, claim the row on writes (#56)mre
  • d8608224close and reopen: keep a manual close against sync (#56)mre
  • ffb60746|||PR close and reopen pull requests (#56)mre
  • 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