gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit69141f6569141f65|||PR pulls page: cache the git part per repo by branch tipsmre69141f65/lib/pulls.hl

10.2 KB

  1. // lib/pulls.hl — PULL REQUESTS (ticket gitoria#11): /pulls of a repo address.
  2. // A pull request is a COMMIT whose subject starts `|||PR ` (target = the repo's main branch) or
  3. // `|||PR|<branch>] ` (target = <branch>). The list reads git markers and prefers their recorded source branches.
  4. // Title = the text after the marker (else the source branch's name). Source = the branch holding the
  5. // commit that is not the target. State: `merged` when the commit is in the target branch, else `open`.
  6. // Each marker identifies one request, even when several requests share a containing branch. Merging happens with git itself.
  7. import { sha256 } from 'hl:crypto'
  8. import { localStamp, px } from './util.hl'
  9. import { git, gitRaw, branchNames, pickDefault } from './git.hl'
  10. import { enc, isHexId, tabs, parsePull } from './git-helpers.hl'
  11. import { repoBySlug } from './repos.hl'
  12. import { recordBySha, sourceOf } from './pullsync.hl'
  13. import { withPullParts } from './tickets-helpers.hl'
  14. import { missingText, missingOf, missingDetails, missingApproval, missingMessage, withStatuses } from './statuses.hl'
  15. static maxPulls = 50
  16. static pullScan = 500
  17. // One git-derived list per repository, at most 64 repositories. Every read checks all branch tips;
  18. // HTTPS/SSH pushes, force pushes, branch deletion and either merge path therefore change the key.
  19. // Hybrids copy on return: the live statuses, permissions, ticket links and numbers cannot enter this cache.
  20. static pullCache = {}
  21. static pullOrder = []
  22. static maxPullRepos = 64
  23. static keepPulls = (String slug, String key, Hybrid result) => {
  24. order = []
  25. for (k of pullOrder) { if (k != slug) { order.push(k) } }
  26. if (order.length >= maxPullRepos) {
  27. delete pullCache[order[0]]
  28. pullOrder = order.slice(1)
  29. } else { pullOrder = order }
  30. pullCache[slug] = { key = key result = result }
  31. pullOrder.push(slug)
  32. return null
  33. }
  34. // The list, synchronously, as before. A failed fingerprint or scan is never retained.
  35. static pullsNow = (String slug, String setting) => {
  36. refs = gitRaw(slug, ['for-each-ref', '--format=%(refname)%09%(objectname)', 'refs/heads'])
  37. record = repoBySlug(slug)
  38. key = refs != null && refs.exit == 0 ? sha256(JSON.stringify([record == null ? null : record.id, setting, refs.lines])) : ''
  39. hit = pullCache[slug]
  40. if (key != '' && hit != null && hit.key == key) { return hit.result }
  41. let out = null
  42. let scanned = false
  43. pulls(slug, setting, (Hybrid result, Boolean ok) => {
  44. out = result
  45. scanned = ok == true
  46. return null
  47. })
  48. if (key != '' && scanned) { keepPulls(slug, key, out) }
  49. return out
  50. }
  51. static pullShape = (state, message) => {
  52. return { state = state isMessage = message != '' message = message pulls = [] hasPulls = false noPulls = false defaultName = '' openCount = 0 mergedCount = 0 }
  53. }
  54. // every pull request of a repo → done(result). setting = the repo's saved main branch ('' = none)
  55. static pulls = (slug, setting, done) => {
  56. repo = repoBySlug(slug)
  57. branchNames(slug, (names) => {
  58. if (names.length == 0) {
  59. done(pullShape('empty', 'This repository has no commits yet.'))
  60. return null
  61. }
  62. dflt = pickDefault(names, setting)
  63. git(slug, ['log', '-' + pullScan, '--branches', '--format=%H%x09%ct%x09%an%x09%s'], pullScan, (lg) => {
  64. found = []
  65. for (l of lg.ok ? lg.lines : []) {
  66. f = tabs(l)
  67. if (f.length >= 4) {
  68. sub = f.slice(3).join(' ')
  69. pr = parsePull(sub)
  70. if (pr != null && isHexId(f[0]) && found.length < maxPulls) {
  71. found.push({ sha = f[0] short = f[0].slice(0, 8) date = localStamp(toNumber(f[1]) * 1000) author = f[2] target = pr.target == '' ? dflt : pr.target title = pr.title })
  72. }
  73. }
  74. }
  75. out = pullShape('ok', '')
  76. out.defaultName = dflt
  77. next = (i) => {
  78. if (i >= found.length) {
  79. out.hasPulls = out.pulls.length > 0
  80. out.noPulls = out.pulls.length == 0
  81. done(out, lg.ok)
  82. return null
  83. }
  84. c = found[i]
  85. rec = repo == null ? null : recordBySha(repo.id, c.sha)
  86. source = sourceOf(slug, c.sha, c.target, rec == null ? '' : rec.source, rec == null ? c.sha : rec.head)
  87. finish = (merged, targetMissing) => {
  88. title = c.title != '' ? c.title : (source != '' ? source : c.short)
  89. out.pulls.push({ number = rec != null ? rec.number : null sha = c.sha short = c.short href = px(slug) + '/commit/' + c.sha title = title source = source hasSource = source != '' noSource = source == '' sourceHref = source != '' ? px(slug) + '/branch/' + enc(source) : '' target = c.target targetHref = targetMissing ? '' : px(slug) + '/branch/' + enc(c.target) targetLinked = !targetMissing state = merged ? 'merged' : 'open' isMerged = merged isOpen = !merged mergeable = !merged && source != '' && !targetMissing targetMissing = targetMissing author = c.author date = c.date })
  90. if (merged) { out.mergedCount = out.mergedCount + 1 } else { out.openCount = out.openCount + 1 }
  91. next(i + 1)
  92. return null
  93. }
  94. if (!names.includes(c.target)) { return finish(false, true) }
  95. git(slug, ['merge-base', '--is-ancestor', c.sha, c.target], 1, (mb) => {
  96. return finish(mb.ok, false)
  97. })
  98. return null
  99. }
  100. next(0)
  101. return null
  102. })
  103. })
  104. }
  105. // the rows with `canMerge` (`owner` = may merge: the owner or a member with push rights, lib/repos.hl mayPush; an open request whose branches exist) and the button's class: an `if` inside a
  106. // `for` row is not re-evaluated when the list is reassigned (the old button stayed), an attribute is — so the button
  107. // is always there and `.nomerge` hides it (styles.hl)
  108. static withMerge = (list, owner) => {
  109. out = []
  110. for (p of list) {
  111. q = p
  112. q.canMerge = owner && p.mergeable
  113. q.mergeClass = q.canMerge ? 'quiet mergepr' : 'nomerge'
  114. out.push(q)
  115. }
  116. return out
  117. }
  118. // ---- MERGE a pull request (ticket gitoria#17): only when the owner clicks the button ---------------------------
  119. // The repo is BARE (no work tree), so the merge is made with plumbing: `merge-tree --write-tree` merges the two
  120. // branches into a tree (exit 1 = conflict: nothing is written to any branch), `commit-tree` makes the merge commit
  121. // (parents: target, source), `update-ref` moves the target — with the old value given, so a push that arrived meanwhile
  122. // makes it fail instead of being overwritten. Always a merge commit (like GitHub's "Create a merge commit").
  123. // The pull request is found again on the SERVER by the sha of its `|||PR` commit (the client only names that).
  124. // → { ok } | { error, reason, details? }. who = the user's name (author of the merge commit).
  125. // expectHead (optional, '' = none): the head the caller looked at; another head now → { error } and nothing merged.
  126. static mergePullNow = (slug, setting, sha, who, expectHead) => {
  127. let found = null
  128. if (sha == null || hlTypeName(sha) != 'String' || !isHexId(sha) || sha.length != 40) { return { error = 'no such pull request' } }
  129. got = pullsNow(slug, setting)
  130. if (got == null || got.isMessage) { return { error = 'no such pull request' } }
  131. for (p of got.pulls) { if (p.sha == sha) { found = p } }
  132. if (found == null) { return { error = 'no such pull request' } }
  133. if (found.isMerged) { return { error = 'this pull request is already merged' reason = 'already_merged' } }
  134. if (found.noSource) { return { error = 'the branch of this pull request is gone' reason = 'branch_gone' } }
  135. if (found.targetMissing) { return { error = 'the target branch ' + found.target + ' does not exist' reason = 'target_missing' } }
  136. target = 'refs/heads/' + found.target
  137. old = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  138. tip = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + found.source])
  139. if (old == null || old.exit != 0 || old.lines.length == 0 || tip == null || tip.exit != 0 || tip.lines.length == 0) { return { error = 'could not read the branches' reason = 'git_error' } }
  140. oldSha = old.lines[0]
  141. tipSha = tip.lines[0]
  142. if (!isHexId(oldSha) || !isHexId(tipSha)) { return { error = 'could not read the branches' reason = 'git_error' } }
  143. if (expectHead != null && expectHead != '' && expectHead != tipSha) { return { error = 'the head of this pull request changed (now ' + tipSha + ') — nothing merged' reason = 'head_mismatch' } }
  144. // REQUIRED BEFORE MERGE (#46, #52): the centralized app-or-person rule on the exact head that would be merged
  145. missing = missingOf(repoBySlug(slug).id, tipSha, found.number)
  146. blocked = missingMessage(tipSha, missing)
  147. if (blocked != '') { return { error = blocked reason = 'checks_failed' details = missingDetails(missing) approval = missingApproval(missing) } }
  148. m = gitRaw(slug, ['merge-tree', '--write-tree', '--name-only', '--no-messages', oldSha, tipSha])
  149. if (m == null) { return { error = 'could not merge (git did not answer)' reason = 'git_error' } }
  150. if (m.exit == 1) {
  151. files = []
  152. let i = 1
  153. while (i < m.lines.length && m.lines[i] != '') {
  154. if (files.length < 5) { files.push(m.lines[i]) }
  155. i = i + 1
  156. }
  157. return { error = 'Not merged: ' + found.source + ' conflicts with ' + found.target + (files.length > 0 ? ' in ' + files.join(', ') : '') + '. Resolve it with git, then push.' reason = 'merge_conflict' }
  158. }
  159. if (m.exit != 0 || m.lines.length == 0 || !isHexId(m.lines[0])) { return { error = 'could not merge' reason = 'git_error' } }
  160. name = who != null && who != '' ? who : 'gitoria'
  161. msg = "Merge branch '" + found.source + "' into " + found.target
  162. c = gitRaw(slug, ['-c', 'user.name=' + name, '-c', 'user.email=noreply@gitoria', 'commit-tree', m.lines[0], '-p', oldSha, '-p', tipSha, '-m', msg])
  163. if (c == null || c.exit != 0 || c.lines.length == 0 || !isHexId(c.lines[0])) { return { error = 'could not write the merge commit' reason = 'git_error' } }
  164. u = gitRaw(slug, ['update-ref', '-m', msg, target, c.lines[0], oldSha])
  165. if (u == null || u.exit != 0) {
  166. current = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  167. reason = current != null && current.exit == 0 && current.lines.length > 0 && current.lines[0] != oldSha ? 'target_moved' : 'git_error'
  168. return { error = 'The branch ' + found.target + ' changed meanwhile — nothing merged, try again.' reason = reason }
  169. }
  170. return { ok = true mergeCommit = c.lines[0] }
  171. }
  172. // the list read again, with the Merge buttons and the ticket links: the answer of the Merge face (components/pulls.hl)
  173. static pullsView = (slug, setting, owner, project) => {
  174. got = pullsNow(slug, setting)
  175. return { result = got list = withStatuses(withPullParts(withMerge(got.pulls, owner), project), slug) }
  176. }

Branches

Latest commits

  • 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
  • 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