gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfc36fa2efc36fa2elog: W51b pull source bugmrefc36fa2e/lib/pulls.hl

9.0 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>). Nothing is stored: the list is read from git each time.
  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. // One request per source branch (its newest marker commit). Merging happens with git itself (push to the target).
  7. import { localStamp, px } from './util.hl'
  8. import { git, gitRaw, branchNames, pickDefault } from './git.hl'
  9. import { enc, isHexId, tabs, safeName, parsePull } from './git-helpers.hl'
  10. import { repoBySlug } from './repos.hl'
  11. import { withPullParts } from './tickets-helpers.hl'
  12. import { missingText, missingOf, withStatuses } from './statuses.hl'
  13. static maxPulls = 50
  14. static pullScan = 500
  15. // the list, SYNCHRONOUSLY (git() calls back before it returns): what the pulls page's root uses
  16. static pullsNow = (slug, setting) => {
  17. let out = null
  18. pulls(slug, setting, (result) => {
  19. out = result
  20. return null
  21. })
  22. return out
  23. }
  24. static pullShape = (state, message) => {
  25. return { state = state isMessage = message != '' message = message pulls = [] hasPulls = false noPulls = false defaultName = '' openCount = 0 mergedCount = 0 }
  26. }
  27. // every pull request of a repo → done(result). setting = the repo's saved main branch ('' = none)
  28. static pulls = (slug, setting, done) => {
  29. branchNames(slug, (names) => {
  30. if (names.length == 0) {
  31. done(pullShape('empty', 'This repository has no commits yet.'))
  32. return null
  33. }
  34. dflt = pickDefault(names, setting)
  35. git(slug, ['log', '-' + pullScan, '--branches', '--format=%H%x09%ct%x09%an%x09%s'], pullScan, (lg) => {
  36. found = []
  37. for (l of lg.ok ? lg.lines : []) {
  38. f = tabs(l)
  39. if (f.length >= 4) {
  40. sub = f.slice(3).join(' ')
  41. pr = parsePull(sub)
  42. if (pr != null && isHexId(f[0]) && found.length < maxPulls) {
  43. 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 })
  44. }
  45. }
  46. }
  47. out = pullShape('ok', '')
  48. out.defaultName = dflt
  49. seen = []
  50. next = (i) => {
  51. if (i >= found.length) {
  52. out.hasPulls = out.pulls.length > 0
  53. out.noPulls = out.pulls.length == 0
  54. done(out)
  55. return null
  56. }
  57. c = found[i]
  58. git(slug, ['for-each-ref', '--contains', c.sha, '--format=%(refname:short)', 'refs/heads'], 500, (ct) => {
  59. let source = ''
  60. for (n of ct.ok ? ct.lines : []) { if (source == '' && safeName(n) && n != c.target) { source = n } }
  61. if (source != '' && seen.includes(source)) {
  62. next(i + 1)
  63. return null
  64. }
  65. if (source != '') { seen.push(source) }
  66. finish = (merged, targetMissing) => {
  67. let title = c.title != '' ? c.title : (source != '' ? source : c.short)
  68. 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 })
  69. if (merged) { out.mergedCount = out.mergedCount + 1 } else { out.openCount = out.openCount + 1 }
  70. next(i + 1)
  71. return null
  72. }
  73. if (!names.includes(c.target)) { return finish(false, true) }
  74. git(slug, ['merge-base', '--is-ancestor', c.sha, c.target], 1, (mb) => {
  75. return finish(mb.ok, false)
  76. })
  77. return null
  78. })
  79. return null
  80. }
  81. next(0)
  82. return null
  83. })
  84. })
  85. }
  86. // 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
  87. // `for` row is not re-evaluated when the list is reassigned (the old button stayed), an attribute is — so the button
  88. // is always there and `.nomerge` hides it (styles.hl)
  89. static withMerge = (list, owner) => {
  90. out = []
  91. for (p of list) {
  92. q = p
  93. q.canMerge = owner && p.mergeable
  94. q.mergeClass = q.canMerge ? 'quiet mergepr' : 'nomerge'
  95. out.push(q)
  96. }
  97. return out
  98. }
  99. // ---- MERGE a pull request (ticket gitoria#17): only when the owner clicks the button ---------------------------
  100. // The repo is BARE (no work tree), so the merge is made with plumbing: `merge-tree --write-tree` merges the two
  101. // branches into a tree (exit 1 = conflict: nothing is written to any branch), `commit-tree` makes the merge commit
  102. // (parents: target, source), `update-ref` moves the target — with the old value given, so a push that arrived meanwhile
  103. // makes it fail instead of being overwritten. Always a merge commit (like GitHub's "Create a merge commit").
  104. // The pull request is found again on the SERVER by the sha of its `|||PR` commit (the client only names that).
  105. // → { ok } | { error, reason, details? }. who = the user's name (author of the merge commit).
  106. // expectHead (optional, '' = none): the head the caller looked at; another head now → { error } and nothing merged.
  107. static mergePullNow = (slug, setting, sha, who, expectHead) => {
  108. let found = null
  109. if (sha == null || hlTypeName(sha) != 'String' || !isHexId(sha) || sha.length != 40) { return { error = 'no such pull request' } }
  110. got = pullsNow(slug, setting)
  111. if (got == null || got.isMessage) { return { error = 'no such pull request' } }
  112. for (p of got.pulls) { if (p.sha == sha) { found = p } }
  113. if (found == null) { return { error = 'no such pull request' } }
  114. if (found.isMerged) { return { error = 'this pull request is already merged' reason = 'already_merged' } }
  115. if (found.noSource) { return { error = 'the branch of this pull request is gone' reason = 'branch_gone' } }
  116. if (found.targetMissing) { return { error = 'the target branch ' + found.target + ' does not exist' reason = 'target_missing' } }
  117. target = 'refs/heads/' + found.target
  118. old = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  119. tip = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + found.source])
  120. 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' } }
  121. oldSha = old.lines[0]
  122. tipSha = tip.lines[0]
  123. if (!isHexId(oldSha) || !isHexId(tipSha)) { return { error = 'could not read the branches' reason = 'git_error' } }
  124. if (expectHead != null && expectHead != '' && expectHead != tipSha) { return { error = 'the head of this pull request changed (now ' + tipSha + ') — nothing merged' reason = 'head_mismatch' } }
  125. // REQUIRED BEFORE MERGE (gitoria#46): every context the owner requires must be success on the head that would be merged
  126. blocked = missingText(slug, tipSha)
  127. if (blocked != '') { return { error = blocked reason = 'checks_failed' details = missingOf(repoBySlug(slug).id, tipSha) } }
  128. m = gitRaw(slug, ['merge-tree', '--write-tree', '--name-only', '--no-messages', oldSha, tipSha])
  129. if (m == null) { return { error = 'could not merge (git did not answer)' reason = 'git_error' } }
  130. if (m.exit == 1) {
  131. files = []
  132. let i = 1
  133. while (i < m.lines.length && m.lines[i] != '') {
  134. if (files.length < 5) { files.push(m.lines[i]) }
  135. i = i + 1
  136. }
  137. 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' }
  138. }
  139. if (m.exit != 0 || m.lines.length == 0 || !isHexId(m.lines[0])) { return { error = 'could not merge' reason = 'git_error' } }
  140. name = who != null && who != '' ? who : 'gitoria'
  141. msg = "Merge branch '" + found.source + "' into " + found.target
  142. c = gitRaw(slug, ['-c', 'user.name=' + name, '-c', 'user.email=noreply@gitoria', 'commit-tree', m.lines[0], '-p', oldSha, '-p', tipSha, '-m', msg])
  143. 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' } }
  144. u = gitRaw(slug, ['update-ref', '-m', msg, target, c.lines[0], oldSha])
  145. if (u == null || u.exit != 0) {
  146. current = gitRaw(slug, ['rev-parse', '--verify', '-q', target])
  147. reason = current != null && current.exit == 0 && current.lines.length > 0 && current.lines[0] != oldSha ? 'target_moved' : 'git_error'
  148. return { error = 'The branch ' + found.target + ' changed meanwhile — nothing merged, try again.' reason = reason }
  149. }
  150. return { ok = true mergeCommit = c.lines[0] }
  151. }
  152. // the list read again, with the Merge buttons and the ticket links: the answer of the Merge face (components/pulls.hl)
  153. static pullsView = (slug, setting, owner, project) => {
  154. got = pullsNow(slug, setting)
  155. return { result = got list = withStatuses(withPullParts(withMerge(got.pulls, owner), project), slug) }
  156. }

Branches

Latest commits

  • fc36fa2elog: W51b pull source bugmre
  • 56bc1220log: v1.5.1 livemre
  • 845fc0aareport: W51mre
  • 83127391fix: return v1.5.1 app and merge error detailsmre
  • b07d0e97log: W51 v1.5.1 startedmre
  • 0b417d06docs: apps-api v1.5 user tokens (#48, W49)mre
  • d046940alog: vinzenz contract note sentmre
  • db443456log: vinzenz note retry 2mre
  • 802f229alog: kunibert repo ping provenmre
  • 2d2eaa92log: Kunibert connected to kunibert repomre
  • 65d5f80alogmre
  • dab74b0dW49 report, logmre
  • fd4fc028Exercise member status reads with a populated check statemre
  • 536ca6f9Allow repository members to read pull workflow APIs with user tokensmre
  • 705dd834W49 missionmre
  • b554cd5flogmre
  • f4c9440ddeploy.sh: tar --hard-dereference for the live data backupmre
  • 1a687b15Merge branch 'w48-member-merge'mre
  • 166c775fW48 report, logmre
  • 1b4623f0Members merge pull requests: API route, button on /pulls and /pulls/N, one shared merge pathmre