gitoria
All repositories: gitoria
11.2 KB
// 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.// 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,// the reviews, the threads and the conversation (tables: lib/reviews.hl). Writes (a person's comment, reply, resolve / reopen, "Review again") store through// 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.// WHO MAY WRITE is ONE helper, mayWritePull: signed in AND owner or member of the repo (the people who may push). Everybody else reads.// Bodies are Markdown shown as TEXT NODES (lib/markdown.hl blocks, components/markdown.hl): no raw HTML anywhere.import { shownTime } from './util.hl'import { slugOfRequest } from './orgs.hl'import { repoBySlug, repoAt, mayPush } from './repos.hl'import { userOfSession } from './users.hl'import { ensurePulls, pullObject, recordByNumber } from './pullsync.hl'import { appsTable, appsOfRepo } from './apps-table.hl'import { statusesOf, badgeOf, missingText, headOf } from './statuses.hl'import { refParts } from './tickets-helpers.hl'import { markdownBlocks } from './markdown.hl'import { currentVerdict, pullTalk, personComment, personReply, personResolve, anyThreadOf, threadShape } from './reviews.hl'import { numberOf, isoMs } from './reviews-helpers.hl'import { raiseEvent } from './events.hl'import { approvalPageRows } from './approvals-helpers.hl'import { setApproval, hasApproval, approvalsOf } from './approvals.hl'import { mergeAndAnnounce } from './pullmerge.hl'// ---- who may write -----------------------------------------------------------------------------------------------// the one place that decides it (Atlas's reading, not the creator's words: "who may push"); `record` = the repos table recordstatic mayWritePull = (user, record) => {return user != null && record != null && mayPush(user.id, record)}// ---- the PR of a request: the record, or null (no such repo for this viewer, unknown number) ------------------------static prOfRequest = (host, orepo, session, nText) => {slug = slugOfRequest(host, orepo)if (repoAt(slug, session) == null) { return null }n = numberOf(nText)record = repoBySlug(slug)if (n == null || record == null) { return null }ensurePulls(slug)return recordByNumber(record.id, n)}// ---- the view ------------------------------------------------------------------------------------------------------static verdictWords = (v) => { return v == 'approve' ? 'approved' : (v == 'request_changes' ? 'requested changes' : 'commented') }static appNameOf = (appId) => {a = appId == null ? null : appsTable.fetch(appId)return a == null ? '(removed app)' : a.name}static commentRow = (c) => {isApp = c.author.type == 'app'return { id = c.id author = c.author.name kind = isApp ? 'app' : 'user' isApp = isApp blocks = markdownBlocks(c.body) created = shownTime(c.created) }}// everything the page shows → the members of components/pull.hl. `viewer` = the session's user (or null)static prView = (slug, rec, session) => {repo = repoBySlug(slug)user = userOfSession(session)approvalRows = approvalsOf(repo.id, rec.number)pull = pullObject(slug, rec.number, approvalRows)apps = appsOfRepo(repo.id)talk = pullTalk(repo, rec)badges = []for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }verdicts = []appList = []for (a of apps) {cv = currentVerdict(repo.id, rec.number, a.id)text = cv == null ? 'no review yet' : verdictWords(cv.verdict) + (cv.head == rec.head ? '' : ' on an earlier head ' + cv.head.slice(0, 8))verdicts.push({ id = a.id app = a.name text = text cls = cv == null ? 'none' : cv.verdict })appList.push({ id = a.id name = a.name })}reviews = []for (r of talk.reviews) {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)) })}threads = []for (t of talk.threads) { threads.push(threadRow(t)) }conversation = []for (c of talk.talk) { conversation.push(commentRow(c)) }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) }canAct = mayWritePull(user, repo)// 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 againblocked = missingText(slug, rec.head, rec.number, approvalRows)showMerge = canAct && rec.state == 'open' && blocked == ''approvals = approvalPageRows(approvalRows, rec.head)approved = canAct && hasApproval(approvalRows, user.id, rec.head)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' }}static threadRow = (t) => {cs = []for (c of t.shape.comments) { cs.push(commentRow(c)) }marks = []if (t.shape.outdated) { marks.push('outdated') }if (t.shape.resolved) { marks.push('resolved') }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 }}// ---- the writes (the faces in components/pull.hl are thin wrappers around these) ----------------------------------// who + which PR → { repo, rec, user } | { error }; a viewer who may not write gets the same answer as a forged call wouldstatic actorFor = (slug, nText, session) => {if (repoAt(slug, session) == null) { return { error = 'no such pull request' } }user = userOfSession(session)if (user == null) { return { error = 'sign in to do that' } }repo = repoBySlug(slug)if (!mayWritePull(user, repo)) { return { error = 'only the owner and the members of this repository can do that' } }if (user.name == '') { return { error = 'choose a display name first (top of the page)' } }n = numberOf(nText)rec = n == null ? null : recordByNumber(repo.id, n)if (rec == null) { return { error = 'no such pull request' } }return { repo = repo rec = rec user = user }}// the PR page's Merge button: the same merge as the API's and the list's (lib/pullmerge.hl); the page is read againstatic mergeThePull = (slug, nText, session) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }r = mergeAndAnnounce(a.repo, a.rec.sha, a.user, '')if (r.error != null) { return { error = r.error } }b = recordByNumber(a.repo.id, a.rec.number)return { view = prView(slug, b, session) notice = 'Merged.' }}static done = (slug, a, session) => { return { view = prView(slug, a.rec, session) } }static commentOnPull = (slug, nText, session, body) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }r = personComment(a.repo, a.rec, a.user, body)if (r.error != null) { return { error = r.error } }raiseEvent(a.repo, 'pull_comment', 'created', { pull = pullObject(slug, a.rec.number) comment = r.comment }, a.user.id)return done(slug, a, session)}static replyInThread = (slug, nText, session, threadId, body) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }r = personReply(a.repo, a.rec, a.user, threadId, body)if (r.error != null) { return { error = r.error } }t = anyThreadOf(a.repo, a.rec, threadId)shape = threadShape(t, a.rec.head)raiseEvent(a.repo, 'review_comment', 'created', { pull = pullObject(slug, a.rec.number) thread = shape comment = r.comment onlyApp = t.app }, a.user.id)return done(slug, a, session)}static setThreadResolved = (slug, nText, session, threadId, resolved) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }r = personResolve(a.repo, a.rec, threadId, resolved)if (r.error != null) { return { error = r.error } }if (r.changed) {raiseEvent(a.repo, 'review_thread', resolved ? 'resolved' : 'unresolved', { pull = pullObject(slug, a.rec.number) thread = r.shape onlyApp = r.thread.app }, a.user.id)}return done(slug, a, session)}static reviewAgain = (slug, nText, session, appId) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }let found = nullfor (x of appsOfRepo(a.repo.id)) { if (x.id == appId) { found = x } }if (found == null) { return { error = 'no such app on this repository' } }raiseEvent(a.repo, 'review_requested', 'requested', { pull = pullObject(slug, a.rec.number) onlyApp = found.id }, a.user.id)return { view = prView(slug, a.rec, session) notice = 'Asked ' + found.name + ' to review again.' }}// A stale tab must never approve a head the person has not seen. The session is resolved again on every click.static approveThePull = (String slug, String nText, Hybrid session, String head, Boolean wanted) => {a = actorFor(slug, nText, session)if (a.error != null) { return { error = a.error } }if (wanted != true && wanted != false) { return { error = 'approval must be true or false' } }if (a.rec.state != 'open') { return { error = 'this pull request is not open' } }if (head != a.rec.head || head != headOf(slug, a.rec.source)) { return { error = 'the head changed; read the pull request again before approving' } }r = setApproval(a.repo.id, a.rec.number, a.user.id, head, wanted)if (r.error != null) { return r }result = done(slug, a, session)emit client gitoriaPullApprovalChanged(slug, a.rec.number, { approvals = result.view.approvals head = head blockText = result.view.blockText })return result}// One invalidation, independent of the number of PRs. Only mounted pages fetch their own state.static announceRequirements = (String slug) => {emit client gitoriaRequiredChanged(slug)return null}static approvalStateFor = (String slug, String nText, Hybrid session) => {if (repoAt(slug, session) == null) { return null }repo = repoBySlug(slug)number = numberOf(nText)rec = number == null ? null : recordByNumber(repo.id, number)if (rec == null) { return null }rows = approvalsOf(repo.id, number)badges = []for (s of statusesOf(repo.id, rec.head)) { badges.push(badgeOf(s)) }return { approvals = approvalPageRows(rows, rec.head) head = rec.head blockText = missingText(slug, rec.head, number, rows) statuses = badges hasStatuses = badges.length > 0 }}
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