orca/src/main/ipc/github-work-item-args.ts

43 lines
1.2 KiB
TypeScript

import type { TaskSourceContext } from '../../shared/task-source-context'
export type WorkItemArgs = {
repoPath: string
repoId?: string | null
sourceContext?: TaskSourceContext | null
number: number
type?: 'issue' | 'pr'
}
type RegisteredRepoContext = {
path: string
connectionId?: string | null
}
type LocalGitExecOptions = {
wslDistro?: string
}
// Why: renderer input crosses the IPC boundary and is untrusted. Reject
// non-integer or < 1 numbers, and coerce unrecognised `type` values to
// undefined so getWorkItem falls through to its issue-then-PR probe rather
// than silently dispatching to the wrong branch.
export function dispatchWorkItem<T>(
args: WorkItemArgs,
repo: RegisteredRepoContext,
fn: (
path: string,
n: number,
t?: 'issue' | 'pr',
connectionId?: string | null,
localGitOptions?: LocalGitExecOptions
) => Promise<T | null>,
localGitOptions?: LocalGitExecOptions
): Promise<T | null> | null {
const { number, type } = args
if (typeof number !== 'number' || !Number.isInteger(number) || number < 1) {
return null
}
const safeType = type === 'issue' || type === 'pr' ? type : undefined
return fn(repo.path, number, safeType, repo.connectionId ?? null, localGitOptions)
}