gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite7af3ecee7af3eceMerge branch 'w52b-approval-followup' into mainGitoriae7af3ece/lib/pulls.hl

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

Branches

Latest commits

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