Files
orca/src/main/github/github-pr-stack-async-merge.ts
T
Neil 77f23b013f refactor(shared): drop the shared/types barrel and import from the real modules (#14447)
#14397 split `shared/types.ts` into 46 per-domain modules but kept the path as
a re-export barrel so the import sites did not have to change. This removes
the barrel: every consumer now imports from the module that actually declares
the type, and `src/shared/types.ts` is deleted.

Barrels hide where a type lives, make every consumer look like it depends on
the whole domain, and let an unrelated edit invalidate a module that ~2,000
files transitively import.

2,323 import declarations across 2,321 files. Rewritten mechanically: each
specifier was resolved to an absolute path via the TypeScript AST and
recomputed, rather than string-substituted, so alias forms (`@/../../shared/
types`) and per-specifier `type` modifiers survive.

Four cases the mechanical pass had to handle, each found by a gate rather than
by reading the diff:

- Modules inside `src/shared` import the barrel as `./types`, not
  `shared/types`. A pre-filter on the latter string skipped 176 of them and
  left imports dangling at a deleted file, which surfaced as confusing
  `Property 'x' is optional in type 'Repo' but required in Pick<Repo, ...>`
  errors rather than "module not found".
- The barrel RENAMED one type on the way through
  (`WorkspaceSource as WorkspaceCreateTelemetrySource`), so the original name
  in the owning module has to be re-aliased at each consumer.
- Three test files put `;(globalThis as ...)` on the line after the import.
  TypeScript parses that `;` as the import statement's terminator, so
  replacing through `statement.getEnd()` deletes it and breaks ASI. The
  rewrite now stops at the module specifier.
- A file that already imported directly from a module got a SECOND import
  from it, because the barrel re-exported those same names — which trips
  `import/no-duplicates` under `--deny-warnings`. A post-pass merges
  declarations sharing a specifier and type-only-ness; the `import type` plus
  `import` pair from one module is left alone, since that form is allowed.

Splitting one barrel import into several genuinely adds lines, which pushed
`terminal-layout-pty-ownership.ts` to 301 counted lines: its 107-character
import must wrap, and neither local type collapses onto one line (101 and 116
characters). Rather than contort a type declaration to fit a line budget,
`collectLeafIds` and `pruneLeaves` move to `terminal-pane-layout-tree.ts` —
they are pure structural operations on the layout tree and independent of PTY
ownership. `visible-worktrees.ts` similarly loses its own mini-barrel
re-export of `isDefaultBranchWorkspace`, with the four real consumers
repointed at the declaring module. No `max-lines` bypass added.

Verified: cold `tsc --noEmit` green on node, cli, and web (buildinfo deleted
first — these projects are `composite: true` and reuse stale caches); the full
`pnpm lint` green, not just bare oxlint — the narrower local check is what let
the duplicate imports reach CI; max-lines ratchet OK at 344.
2026-08-13 22:48:24 -07:00

146 lines
4.3 KiB
TypeScript

import type { GitHubPRMergeMethod } from '../../shared/github/pull-request-types'
import { ghExecFileAsync } from '../git/runner'
import { acquire, release } from './gh-utils'
import {
githubHostExecOptions,
type GitHubApiRepository,
type GitHubRepoExecOptions
} from './github-api-repository'
const POLL_INTERVAL_MS = 1_000
const MAX_POLLS = 180
type AsyncMergeResponse = {
status?: unknown
details?: {
message?: unknown
uuid?: unknown
}
}
type AsyncMergeResult =
| { kind: 'pending'; uuid: string }
| { kind: 'success' }
| { kind: 'failure'; message: string }
export type GitHubPRStackMergeAction = 'direct_merge' | 'merge_queue'
function parseResponse(value: string): AsyncMergeResponse | null {
try {
return JSON.parse(value) as AsyncMergeResponse
} catch {
return null
}
}
function errorResponseBody(error: unknown): AsyncMergeResponse | null {
if (!error || typeof error !== 'object' || !('stdout' in error)) {
return null
}
const stdout = (error as { stdout?: unknown }).stdout
return parseResponse(Buffer.isBuffer(stdout) ? stdout.toString('utf8') : String(stdout))
}
function classifyResult(response: AsyncMergeResponse): AsyncMergeResult {
if (response.status === 'merged' || response.status === 'enqueued') {
return { kind: 'success' }
}
if (response.status === 'pending' && typeof response.details?.uuid === 'string') {
return { kind: 'pending', uuid: response.details.uuid }
}
const message =
typeof response.details?.message === 'string'
? response.details.message
: 'GitHub could not merge this stack.'
return { kind: 'failure', message }
}
function waitForNextPoll(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS))
}
async function runStackMergeCommand(
command: string[],
options: NonNullable<Parameters<typeof ghExecFileAsync>[1]>
) {
await acquire()
try {
return await ghExecFileAsync(command, options)
} finally {
release()
}
}
export async function mergeGitHubPRStack(args: {
repository: GitHubApiRepository
prNumber: number
method: GitHubPRMergeMethod
mergeAction: GitHubPRStackMergeAction
headSha?: string
ghOptions: GitHubRepoExecOptions
}): Promise<{ ok: true } | { ok: false; error: string }> {
const endpoint = `repos/${args.repository.owner}/${args.repository.repo}/pulls/${args.prNumber}/merge-async`
const command = ['api', '-X', 'PUT', endpoint, '-f', `merge_action=${args.mergeAction}`]
if (args.mergeAction === 'direct_merge') {
command.push('-f', `merge_method=${args.method}`)
}
if (args.headSha) {
command.push('-f', `sha=${args.headSha}`)
}
let submitted: AsyncMergeResponse | null
try {
const { stdout } = await runStackMergeCommand(command, {
...args.ghOptions,
...githubHostExecOptions(args.repository),
env: { ...process.env, GH_PROMPT_DISABLED: '1' }
})
submitted = parseResponse(stdout)
} catch (error) {
submitted = errorResponseBody(error)
if (!submitted) {
throw error
}
}
if (!submitted) {
return { ok: false, error: 'GitHub returned an invalid stack merge response.' }
}
let result = classifyResult(submitted)
if (result.kind === 'success') {
return { ok: true }
}
if (result.kind === 'failure') {
return { ok: false, error: result.message }
}
const uuid = encodeURIComponent(result.uuid)
for (let poll = 0; poll < MAX_POLLS; poll++) {
await waitForNextPoll()
let response: AsyncMergeResponse | null
try {
const { stdout } = await runStackMergeCommand(['api', `${endpoint}/${uuid}`], {
...args.ghOptions,
...githubHostExecOptions(args.repository)
})
response = parseResponse(stdout)
} catch (error) {
// Why: the merge was submitted; a transient poll failure cannot make it fail.
response = errorResponseBody(error)
if (!response) {
continue
}
}
if (!response) {
return { ok: false, error: 'GitHub returned an invalid stack merge response.' }
}
result = classifyResult(response)
if (result.kind === 'success') {
return { ok: true }
}
if (result.kind === 'failure') {
return { ok: false, error: result.message }
}
}
return { ok: false, error: 'GitHub is still merging this stack. Refresh to check its status.' }
}