gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit841da497841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria841da497/lib/pulls.hl

11.1 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, pickDefault } from './git.hl'
  10. import { enc, isHexId, tabs, parsePull, safeName } from './git-helpers.hl'
  11. import { repoBySlug } from './repos.hl'
  12. import { recordBySha, sourceResult } 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. // Database numbers must be read after the cache: notifyPush can scan a new marker before syncPulls numbers it.
  35. // Every caller, including status/approval checks and mergePullNow, receives the current number.
  36. static livePullNumbers = (Hybrid result, Hybrid repo) => {
  37. if (result == null || result.isMessage) { return result }
  38. rows = []
  39. for (p of result.pulls) {
  40. q = p
  41. rec = repo == null ? null : recordBySha(repo.id, p.sha)
  42. q.number = rec == null ? null : rec.number
  43. rows.push(q)
  44. }
  45. result.pulls = rows
  46. return result
  47. }
  48. // The list, synchronously, as before. A failed fingerprint or scan is never retained.
  49. static pullsNow = (String slug, String setting) => {
  50. refs = gitRaw(slug, ['for-each-ref', '--format=%(refname)%09%(objectname)', 'refs/heads'])
  51. record = repoBySlug(slug)
  52. key = refs != null && refs.exit == 0 ? sha256(JSON.stringify([record == null ? null : record.id, setting, refs.lines])) : ''
  53. hit = pullCache[slug]
  54. if (key != '' && hit != null && hit.key == key) { return livePullNumbers(hit.result, record) }
  55. let out = null
  56. let scanned = false
  57. pulls(slug, setting, (Hybrid result, Boolean ok) => {
  58. out = result
  59. scanned = ok == true
  60. return null
  61. })
  62. if (key != '' && scanned) { keepPulls(slug, key, out) }
  63. return livePullNumbers(out, record)
  64. }
  65. static pullShape = (state, message) => {
  66. return { state = state isMessage = message != '' message = message pulls = [] hasPulls = false noPulls = false defaultName = '' openCount = 0 mergedCount = 0 }
  67. }
  68. // every pull request of a repo → done(result). setting = the repo's saved main branch ('' = none)
  69. static pulls = (slug, setting, done) => {
  70. repo = repoBySlug(slug)
  71. git(slug, ['for-each-ref', '--format=%(refname:short)', 'refs/heads'], 500, (branches) => {
  72. names = []
  73. if (branches.ok) { for (name of branches.lines) { if (safeName(name)) { names.push(name) } } }
  74. if (names.length == 0) {
  75. done(pullShape('empty', 'This repository has no commits yet.'), branches.ok)
  76. return null
  77. }
  78. dflt = pickDefault(names, setting)
  79. git(slug, ['log', '-' + pullScan, '--branches', '--format=%H%x09%ct%x09%an%x09%s'], pullScan, (lg) => {
  80. let complete = branches.ok && lg.ok
  81. found = []
  82. for (l of lg.ok ? lg.lines : []) {
  83. f = tabs(l)
  84. if (f.length >= 4) {
  85. sub = f.slice(3).join(' ')
  86. pr = parsePull(sub)
  87. if (pr != null && isHexId(f[0]) && found.length < maxPulls) {
  88. 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 })
  89. }
  90. }
  91. }
  92. out = pullShape('ok', '')
  93. out.defaultName = dflt
  94. next = (i) => {
  95. if (i >= found.length) {
  96. out.hasPulls = out.pulls.length > 0
  97. out.noPulls = out.pulls.length == 0
  98. done(out, complete)
  99. return null
  100. }
  101. c = found[i]
  102. rec = repo == null ? null : recordBySha(repo.id, c.sha)
  103. sourceRead = sourceResult(slug, c.sha, c.target, rec == null ? '' : rec.source, rec == null ? c.sha : rec.head)
  104. if (sourceRead == null || !sourceRead.ok) { complete = false }
  105. source = sourceRead == null ? '' : sourceRead.source
  106. finish = (merged, targetMissing) => {
  107. title = c.title != '' ? c.title : (source != '' ? source : c.short)
  108. out.pulls.push({ 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 })
  109. if (merged) { out.mergedCount = out.mergedCount + 1 } else { out.openCount = out.openCount + 1 }
  110. next(i + 1)
  111. return null
  112. }
  113. if (!names.includes(c.target)) { return finish(false, true) }
  114. mb = gitRaw(slug, ['merge-base', '--is-ancestor', c.sha, c.target])
  115. if (mb == null || (mb.exit != 0 && mb.exit != 1)) { complete = false }
  116. return finish(mb != null && mb.exit == 0, false)
  117. }
  118. next(0)
  119. return null
  120. })
  121. })
  122. }
  123. // 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
  124. // `for` row is not re-evaluated when the list is reassigned (the old button stayed), an attribute is — so the button
  125. // is always there and `.nomerge` hides it (styles.hl)
  126. static withMerge = (list, owner) => {
  127. out = []
  128. for (p of list) {
  129. q = p
  130. q.canMerge = owner && p.mergeable
  131. q.mergeClass = q.canMerge ? 'quiet mergepr' : 'nomerge'
  132. out.push(q)
  133. }
  134. return out
  135. }
  136. // ---- MERGE a pull request (ticket gitoria#17): only when the owner clicks the button ---------------------------
  137. // The repo is BARE (no work tree), so the merge is made with plumbing: `merge-tree --write-tree` merges the two
  138. // branches into a tree (exit 1 = conflict: nothing is written to any branch), `commit-tree` makes the merge commit
  139. // (parents: target, source), `update-ref` moves the target — with the old value given, so a push that arrived meanwhile
  140. // makes it fail instead of being overwritten. Always a merge commit (like GitHub's "Create a merge commit").
  141. // The pull request is found again on the SERVER by the sha of its `|||PR` commit (the client only names that).
  142. // → { ok } | { error, reason, details? }. who = the user's name (author of the merge commit).
  143. // expectHead (optional, '' = none): the head the caller looked at; another head now → { error } and nothing merged.
  144. static mergePullNow = (slug, setting, sha, who, expectHead) => {
  145. let found = null
  146. if (sha == null || hlTypeName(sha) != 'String' || !isHexId(sha) || sha.length != 40) { return { error = 'no such pull request' } }
  147. got = pullsNow(slug, setting)
  148. if (got == null || got.isMessage) { return { error = 'no such pull request' } }
  149. for (p of got.pulls) { if (p.sha == sha) { found = p } }
  150. if (found == null) { return { error = 'no such pull request' } }
  151. if (found.isMerged) { return { error = 'this pull request is already merged' reason = 'already_merged' } }
  152. if (found.noSource) { return { error = 'the branch of this pull request is gone' reason = 'branch_gone' } }
  153. if (found.targetMissing) { return { error = 'the target branch ' + found.target + ' does not exist' reason = 'target_missing' } }
  154. target = 'refs/heads/' + found.target
  155. old = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  156. tip = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + found.source])
  157. 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' } }
  158. oldSha = old.lines[0]
  159. tipSha = tip.lines[0]
  160. if (!isHexId(oldSha) || !isHexId(tipSha)) { return { error = 'could not read the branches' reason = 'git_error' } }
  161. if (expectHead != null && expectHead != '' && expectHead != tipSha) { return { error = 'the head of this pull request changed (now ' + tipSha + ') — nothing merged' reason = 'head_mismatch' } }
  162. // REQUIRED BEFORE MERGE (#46, #52): the centralized app-or-person rule on the exact head that would be merged
  163. missing = missingOf(repoBySlug(slug).id, tipSha, found.number)
  164. blocked = missingMessage(tipSha, missing)
  165. if (blocked != '') { return { error = blocked reason = 'checks_failed' details = missingDetails(missing) approval = missingApproval(missing) } }
  166. m = gitRaw(slug, ['merge-tree', '--write-tree', '--name-only', '--no-messages', oldSha, tipSha])
  167. if (m == null) { return { error = 'could not merge (git did not answer)' reason = 'git_error' } }
  168. if (m.exit == 1) {
  169. files = []
  170. let i = 1
  171. while (i < m.lines.length && m.lines[i] != '') {
  172. if (files.length < 5) { files.push(m.lines[i]) }
  173. i = i + 1
  174. }
  175. 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' }
  176. }
  177. if (m.exit != 0 || m.lines.length == 0 || !isHexId(m.lines[0])) { return { error = 'could not merge' reason = 'git_error' } }
  178. name = who != null && who != '' ? who : 'gitoria'
  179. msg = "Merge branch '" + found.source + "' into " + found.target
  180. c = gitRaw(slug, ['-c', 'user.name=' + name, '-c', 'user.email=noreply@gitoria', 'commit-tree', m.lines[0], '-p', oldSha, '-p', tipSha, '-m', msg])
  181. 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' } }
  182. u = gitRaw(slug, ['update-ref', '-m', msg, target, c.lines[0], oldSha])
  183. if (u == null || u.exit != 0) {
  184. current = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  185. reason = current != null && current.exit == 0 && current.lines.length > 0 && current.lines[0] != oldSha ? 'target_moved' : 'git_error'
  186. return { error = 'The branch ' + found.target + ' changed meanwhile — nothing merged, try again.' reason = reason }
  187. }
  188. return { ok = true mergeCommit = c.lines[0] }
  189. }
  190. // the list read again, with the Merge buttons and the ticket links: the answer of the Merge face (components/pulls.hl)
  191. static pullsView = (slug, setting, owner, project) => {
  192. got = pullsNow(slug, setting)
  193. return { result = got list = withStatuses(withPullParts(withMerge(got.pulls, owner), project), slug) }
  194. }

Branches

Latest commits

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