gitoria
All repositories: gitoria
8.4 KB
// lib/pulls.hl — PULL REQUESTS (ticket gitoria#11): /pulls of a repo address.// A pull request is a COMMIT whose subject starts `|||PR ` (target = the repo's main branch) or// `|||PR|<branch>] ` (target = <branch>). Nothing is stored: the list is read from git each time.// Title = the text after the marker (else the source branch's name). Source = the branch holding the// commit that is not the target. State: `merged` when the commit is in the target branch, else `open`.// One request per source branch (its newest marker commit). Merging happens with git itself (push to the target).import { localStamp, px } from './util.hl'import { git, gitRaw, branchNames, pickDefault } from './git.hl'import { enc, isHexId, tabs, safeName, parsePull } from './git-helpers.hl'import { withPullParts } from './tickets-helpers.hl'import { missingText, withStatuses } from './statuses.hl'static maxPulls = 50static pullScan = 500// the list, SYNCHRONOUSLY (git() calls back before it returns): what the pulls page's root usesstatic pullsNow = (slug, setting) => {let out = nullpulls(slug, setting, (result) => {out = resultreturn null})return out}static pullShape = (state, message) => {return { state = state isMessage = message != '' message = message pulls = [] hasPulls = false noPulls = false defaultName = '' openCount = 0 mergedCount = 0 }}// every pull request of a repo → done(result). setting = the repo's saved main branch ('' = none)static pulls = (slug, setting, done) => {branchNames(slug, (names) => {if (names.length == 0) {done(pullShape('empty', 'This repository has no commits yet.'))return null}dflt = pickDefault(names, setting)git(slug, ['log', '-' + pullScan, '--branches', '--format=%H%x09%ct%x09%an%x09%s'], pullScan, (lg) => {found = []for (l of lg.ok ? lg.lines : []) {f = tabs(l)if (f.length >= 4) {sub = f.slice(3).join(' ')pr = parsePull(sub)if (pr != null && isHexId(f[0]) && found.length < maxPulls) {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 })}}}out = pullShape('ok', '')out.defaultName = dfltseen = []next = (i) => {if (i >= found.length) {out.hasPulls = out.pulls.length > 0out.noPulls = out.pulls.length == 0done(out)return null}c = found[i]git(slug, ['for-each-ref', '--contains', c.sha, '--format=%(refname:short)', 'refs/heads'], 500, (ct) => {let source = ''for (n of ct.ok ? ct.lines : []) { if (source == '' && safeName(n) && n != c.target) { source = n } }if (source != '' && seen.includes(source)) {next(i + 1)return null}if (source != '') { seen.push(source) }finish = (merged, targetMissing) => {let title = c.title != '' ? c.title : (source != '' ? source : c.short)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 })if (merged) { out.mergedCount = out.mergedCount + 1 } else { out.openCount = out.openCount + 1 }next(i + 1)return null}if (!names.includes(c.target)) { return finish(false, true) }git(slug, ['merge-base', '--is-ancestor', c.sha, c.target], 1, (mb) => {return finish(mb.ok, false)})return null})return null}next(0)return null})})}// 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// `for` row is not re-evaluated when the list is reassigned (the old button stayed), an attribute is — so the button// is always there and `.nomerge` hides it (styles.hl)static withMerge = (list, owner) => {out = []for (p of list) {q = pq.canMerge = owner && p.mergeableq.mergeClass = q.canMerge ? 'quiet mergepr' : 'nomerge'out.push(q)}return out}// ---- MERGE a pull request (ticket gitoria#17): only when the owner clicks the button ---------------------------// The repo is BARE (no work tree), so the merge is made with plumbing: `merge-tree --write-tree` merges the two// branches into a tree (exit 1 = conflict: nothing is written to any branch), `commit-tree` makes the merge commit// (parents: target, source), `update-ref` moves the target — with the old value given, so a push that arrived meanwhile// makes it fail instead of being overwritten. Always a merge commit (like GitHub's "Create a merge commit").// The pull request is found again on the SERVER by the sha of its `|||PR` commit (the client only names that).// → { ok } | { error }. who = the user's name (author of the merge commit).// expectHead (optional, '' = none): the head the caller looked at; another head now → { error } and nothing merged.static mergePullNow = (slug, setting, sha, who, expectHead) => {let found = nullif (sha == null || hlTypeName(sha) != 'String' || !isHexId(sha) || sha.length != 40) { return { error = 'no such pull request' } }got = pullsNow(slug, setting)if (got == null || got.isMessage) { return { error = 'no such pull request' } }for (p of got.pulls) { if (p.sha == sha) { found = p } }if (found == null) { return { error = 'no such pull request' } }if (found.isMerged) { return { error = 'this pull request is already merged' } }if (found.noSource) { return { error = 'the branch of this pull request is gone' } }if (found.targetMissing) { return { error = 'the target branch ' + found.target + ' does not exist' } }target = 'refs/heads/' + found.targetold = gitRaw(slug, ['rev-parse', '--verify', '-q', target])tip = gitRaw(slug, ['rev-parse', '--verify', '-q', 'refs/heads/' + found.source])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' } }oldSha = old.lines[0]tipSha = tip.lines[0]if (!isHexId(oldSha) || !isHexId(tipSha)) { return { error = 'could not read the branches' } }if (expectHead != null && expectHead != '' && expectHead != tipSha) { return { error = 'the head of this pull request changed (now ' + tipSha + ') — nothing merged' } }// REQUIRED BEFORE MERGE (gitoria#46): every context the owner requires must be success on the head that would be mergedblocked = missingText(slug, tipSha)if (blocked != '') { return { error = blocked } }m = gitRaw(slug, ['merge-tree', '--write-tree', '--name-only', '--no-messages', oldSha, tipSha])if (m == null) { return { error = 'could not merge (git did not answer)' } }if (m.exit == 1) {files = []let i = 1while (i < m.lines.length && m.lines[i] != '') {if (files.length < 5) { files.push(m.lines[i]) }i = i + 1}return { error = 'Not merged: ' + found.source + ' conflicts with ' + found.target + (files.length > 0 ? ' in ' + files.join(', ') : '') + '. Resolve it with git, then push.' }}if (m.exit != 0 || m.lines.length == 0 || !isHexId(m.lines[0])) { return { error = 'could not merge' } }name = who != null && who != '' ? who : 'gitoria'msg = "Merge branch '" + found.source + "' into " + found.targetc = gitRaw(slug, ['-c', 'user.name=' + name, '-c', 'user.email=noreply@gitoria', 'commit-tree', m.lines[0], '-p', oldSha, '-p', tipSha, '-m', msg])if (c == null || c.exit != 0 || c.lines.length == 0 || !isHexId(c.lines[0])) { return { error = 'could not write the merge commit' } }u = gitRaw(slug, ['update-ref', '-m', msg, target, c.lines[0], oldSha])if (u == null || u.exit != 0) { return { error = 'The branch ' + found.target + ' changed meanwhile — nothing merged, try again.' } }return { ok = true mergeCommit = c.lines[0] }}// the list read again, with the Merge buttons and the ticket links: the answer of the Merge face (components/pulls.hl)static pullsView = (slug, setting, owner, project) => {got = pullsNow(slug, setting)return { result = got list = withStatuses(withPullParts(withMerge(got.pulls, owner), project), slug) }}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 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
- 1075c38cW48 mission, logmre
- 17bdd0d6V37/V39 missions, logmre
- 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
- e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
- 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
- 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
- c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
- 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
- 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