gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3cdb02c63cdb02c6Own settings feedback styles and explain connection-state classesmre3cdb02c6/lib/pulls.hl

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

  • 3cdb02c6Own settings feedback styles and explain connection-state classesmre
  • 00d5d6c4Reference code-entry styles and nest code-browser media and pseudosmre
  • 3c425eddReference PR content styles and explain dynamic state classesmre
  • e46fa222Own shell feedback styles and retain documented selector formsmre
  • c26084c4Reference repository list styles and nest its breakpointmre
  • f3e196b5Document Markdown direct-child selector remaindersmre
  • 70597528Reference pull list rules and explain runtime class predicatesmre
  • 1ce7ac24Reference ticket row and feedback styles from its Viewmre
  • 10cdf54bGive organization repository rows their own Style referencesmre
  • 0d019521Document shared class selectors in the styles-only filemre
  • c67404a7Scope token labels and feedback through the token Viewmre
  • 944e3dabScope key names, fingerprints and errors to the keys Viewmre
  • 7bcf7bc2Nest repo navigation media rules and document selector gapsmre
  • 46a76272Reference release row styles from the Viewmre
  • a383f428Tie README definition and path styles to its own Viewmre
  • a90812ffConsolidate add-code styles under its existing View idmre
  • 89107a3eDocument the index direct-child selector remaindermre
  • 07403362Scope reply form layout through the component form rulemre
  • 097c52baStyle the login error through its View idmre
  • c6d9c86aOwn the not-found code rule in its componentmre