gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1a76055c1a76055cDocument repository creation APImre1a76055c/lib/prpage.hl

8.3 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 } from './users.hl'
  10. import { ensurePulls, pullObject, recordByNumber } from './pullsync.hl'
  11. import { appsTable, appsOfRepo } from './apps-table.hl'
  12. import { statusesOf, badgeOf } 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. // ---- who may write -----------------------------------------------------------------------------------------------
  19. // the one place that decides it (Atlas's reading, not the creator's words: "who may push"); `record` = the repos table record
  20. static mayWritePull = (user, record) => {
  21. return user != null && record != null && mayPush(user.id, record)
  22. }
  23. // ---- the PR of a request: the record, or null (no such repo for this viewer, unknown number) ------------------------
  24. static prOfRequest = (host, orepo, session, nText) => {
  25. slug = slugOfRequest(host, orepo)
  26. if (repoAt(slug, session) == null) { return null }
  27. n = numberOf(nText)
  28. record = repoBySlug(slug)
  29. if (n == null || record == null) { return null }
  30. ensurePulls(slug)
  31. return recordByNumber(record.id, n)
  32. }
  33. // ---- the view ------------------------------------------------------------------------------------------------------
  34. static verdictWords = (v) => { return v == 'approve' ? 'approved' : (v == 'request_changes' ? 'requested changes' : 'commented') }
  35. static appNameOf = (appId) => {
  36. a = appId == null ? null : appsTable.fetch(appId)
  37. return a == null ? '(removed app)' : a.name
  38. }
  39. // a comment's ISO time as "2026-11-02 06:00:00" (UTC, as the contract gives it)
  40. static shownTime = (iso) => { return iso.slice(0, 10) + ' ' + iso.slice(11, 19) + ' UTC' }
  41. static commentRow = (c) => {
  42. isApp = c.author.type == 'app'
  43. return { id = c.id author = c.author.name kind = isApp ? 'app' : 'user' isApp = isApp blocks = markdownBlocks(c.body) created = shownTime(c.created) }
  44. }
  45. // everything the page shows → the members of components/pull.hl. `viewer` = the session's user (or null)
  46. static prView = (slug, rec, session) => {
  47. repo = repoBySlug(slug)
  48. user = userOfSession(session)
  49. pull = pullObject(slug, rec.number)
  50. apps = appsOfRepo(repo.id)
  51. talk = pullTalk(repo, rec)
  52. badges = []
  53. for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }
  54. verdicts = []
  55. appList = []
  56. for (a of apps) {
  57. cv = currentVerdict(repo.id, rec.number, a.id)
  58. text = cv == null ? 'no review yet' : verdictWords(cv.verdict) + (cv.head == rec.head ? '' : ' on an earlier head ' + cv.head.slice(0, 8))
  59. verdicts.push({ id = a.id app = a.name text = text cls = cv == null ? 'none' : cv.verdict })
  60. appList.push({ id = a.id name = a.name })
  61. }
  62. reviews = []
  63. for (r of talk.reviews) {
  64. 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)) })
  65. }
  66. threads = []
  67. for (t of talk.threads) { threads.push(threadRow(t)) }
  68. conversation = []
  69. for (c of talk.talk) { conversation.push(commentRow(c)) }
  70. 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) }
  71. canAct = mayWritePull(user, repo)
  72. return { pr = pr 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 }
  73. }
  74. static threadRow = (t) => {
  75. cs = []
  76. for (c of t.shape.comments) { cs.push(commentRow(c)) }
  77. marks = []
  78. if (t.shape.outdated) { marks.push('outdated') }
  79. if (t.shape.resolved) { marks.push('resolved') }
  80. 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 }
  81. }
  82. // ---- the writes (the faces in components/pull.hl are thin wrappers around these) ----------------------------------
  83. // who + which PR → { repo, rec, user } | { error }; a viewer who may not write gets the same answer as a forged call would
  84. static actorFor = (slug, nText, session) => {
  85. if (repoAt(slug, session) == null) { return { error = 'no such pull request' } }
  86. user = userOfSession(session)
  87. if (user == null) { return { error = 'sign in to do that' } }
  88. repo = repoBySlug(slug)
  89. if (!mayWritePull(user, repo)) { return { error = 'only the owner and the members of this repository can do that' } }
  90. if (user.name == '') { return { error = 'choose a display name first (top of the page)' } }
  91. n = numberOf(nText)
  92. rec = n == null ? null : recordByNumber(repo.id, n)
  93. if (rec == null) { return { error = 'no such pull request' } }
  94. return { repo = repo rec = rec user = user }
  95. }
  96. static done = (slug, a, session) => { return { view = prView(slug, a.rec, session) } }
  97. static commentOnPull = (slug, nText, session, body) => {
  98. a = actorFor(slug, nText, session)
  99. if (a.error != null) { return { error = a.error } }
  100. r = personComment(a.repo, a.rec, a.user, body)
  101. if (r.error != null) { return { error = r.error } }
  102. raiseEvent(a.repo, 'pull_comment', 'created', { pull = pullObject(slug, a.rec.number) comment = r.comment }, a.user.id)
  103. return done(slug, a, session)
  104. }
  105. static replyInThread = (slug, nText, session, threadId, body) => {
  106. a = actorFor(slug, nText, session)
  107. if (a.error != null) { return { error = a.error } }
  108. r = personReply(a.repo, a.rec, a.user, threadId, body)
  109. if (r.error != null) { return { error = r.error } }
  110. t = anyThreadOf(a.repo, a.rec, threadId)
  111. shape = threadShape(t, a.rec.head)
  112. raiseEvent(a.repo, 'review_comment', 'created', { pull = pullObject(slug, a.rec.number) thread = shape comment = r.comment onlyApp = t.app }, a.user.id)
  113. return done(slug, a, session)
  114. }
  115. static setThreadResolved = (slug, nText, session, threadId, resolved) => {
  116. a = actorFor(slug, nText, session)
  117. if (a.error != null) { return { error = a.error } }
  118. r = personResolve(a.repo, a.rec, threadId, resolved)
  119. if (r.error != null) { return { error = r.error } }
  120. if (r.changed) {
  121. raiseEvent(a.repo, 'review_thread', resolved ? 'resolved' : 'unresolved', { pull = pullObject(slug, a.rec.number) thread = r.shape onlyApp = r.thread.app }, a.user.id)
  122. }
  123. return done(slug, a, session)
  124. }
  125. static reviewAgain = (slug, nText, session, appId) => {
  126. a = actorFor(slug, nText, session)
  127. if (a.error != null) { return { error = a.error } }
  128. let found = null
  129. for (x of appsOfRepo(a.repo.id)) { if (x.id == appId) { found = x } }
  130. if (found == null) { return { error = 'no such app on this repository' } }
  131. raiseEvent(a.repo, 'review_requested', 'requested', { pull = pullObject(slug, a.rec.number) onlyApp = found.id }, a.user.id)
  132. return { view = prView(slug, a.rec, session) notice = 'Asked ' + found.name + ' to review again.' }
  133. }

Branches

Latest commits

  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre
  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre
  • 9f502db1gitoria#46: W46b briefmre
  • 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre