gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit154d333f154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre154d333f/lib/pulls.hl

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

Branches

Latest commits

  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre
  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre
  • 9f502db1gitoria#46: W46b briefmre