Files
archived-orca/mobile/src/tasks/smart-source-paste-intent.ts
Neil 19043a3d76 refactor(mobile): organize pairing, session, RPC and host owners (#39846)
* refactor(mobile): give relay and encryption concrete owners

* refactor(mobile): organize session and review feature owners

* refactor(mobile): organize RPC and host ownership

* test(mobile): point render fixtures at concrete owners

* refactor(mobile): use explicit type-only owner imports
2026-11-08 07:19:05 -08:00

175 lines
5.7 KiB
TypeScript

import type { GitHubWorkItem } from '../../../src/shared/github/work-item-types'
import type { GitLabWorkItem } from '../../../src/shared/gitlab-types'
import {
normalizeGitHubLinkQuery,
parseGitHubIssueOrPRLink,
type GitHubIssueOrPRLink,
type RepoSlug
} from '../../../src/shared/new-workspace/github-links'
import { parseGitLabIssueOrMRLink } from '../../../src/shared/new-workspace/gitlab-links'
import { isSmartWorkspaceSourceQueryWithinLimit } from '../../../src/shared/new-workspace/smart-workspace-source-results'
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
import { isMethodNotFoundRefusal } from '../transport/rpc/rpc-acceptance-policies'
import {
githubRepoSlugRead,
githubWorkItemBySlugRead,
readGitHubWorkItemByNumber,
gitlabWorkItemByPathRead
} from './mobile-task-source-search-operations'
import { githubRepoIdentityKey } from '../../../src/shared/github/repository-identity-key'
export type PasteRepoCandidate = {
id: string
displayName: string
slug: RepoSlug | null
}
export type GitHubPasteIntent =
| { kind: 'github-link'; link: GitHubIssueOrPRLink }
| { kind: 'github-number'; number: number }
export type GitLabPasteIntent = {
kind: 'gitlab-link'
link: NonNullable<ReturnType<typeof parseGitLabIssueOrMRLink>>
}
export type PasteIntent = GitHubPasteIntent | GitLabPasteIntent | null
export function resolvePasteIntent(query: string): PasteIntent {
if (!isSmartWorkspaceSourceQueryWithinLimit(query)) {
return null
}
const trimmed = query.trim()
if (!trimmed) {
return null
}
const ghLink = parseGitHubIssueOrPRLink(trimmed)
if (ghLink) {
return { kind: 'github-link', link: ghLink }
}
const normalizedGh = normalizeGitHubLinkQuery(trimmed)
if (normalizedGh.directNumber !== null && !/^https?:\/\//i.test(trimmed)) {
return { kind: 'github-number', number: normalizedGh.directNumber }
}
const glLink = parseGitLabIssueOrMRLink(trimmed)
if (glLink) {
return { kind: 'gitlab-link', link: glLink }
}
return null
}
export function deriveRepoSlug(repo: {
upstream?: { owner: string; repo: string; host?: string } | null
gitRemoteIdentity?: { remoteUrl?: string; canonicalKey?: string } | null
}): RepoSlug | null {
if (repo.upstream?.owner && repo.upstream.repo) {
return {
owner: repo.upstream.owner,
repo: repo.upstream.repo,
...(repo.upstream.host ? { host: repo.upstream.host } : {})
}
}
const source = repo.gitRemoteIdentity?.remoteUrl ?? repo.gitRemoteIdentity?.canonicalKey ?? ''
const match = /(?:github\.com[/:]|^)([^/\s:]+)\/([^/\s]+?)(?:\.git)?$/i.exec(source)
if (match) {
return { owner: match[1], repo: match[2] }
}
return null
}
function slugsEqual(a: RepoSlug | null, b: RepoSlug | null): boolean {
if (!a || !b) {
return false
}
return githubRepoIdentityKey(a) === githubRepoIdentityKey(b)
}
export function findRepoMatchingSlug(
repos: readonly PasteRepoCandidate[],
slug: RepoSlug
): PasteRepoCandidate | null {
return repos.find((repo) => slugsEqual(repo.slug, slug)) ?? null
}
export async function findRepoMatchingSlugForPaste(
client: RpcClient,
repos: readonly PasteRepoCandidate[],
slug: RepoSlug,
cache: Map<string, RepoSlug | null>
): Promise<PasteRepoCandidate | null> {
const projected = findRepoMatchingSlug(repos, slug)
if (projected) {
return projected
}
// Projected SSH/GHES metadata may be incomplete; resolve slugs through each owning runtime.
for (const repo of repos) {
let resolved = cache.get(repo.id)
if (!cache.has(repo.id)) {
try {
const reply = await githubRepoSlugRead.request(client, { repo: `id:${repo.id}` })
// Inspect refusal code to retire missing methods host-wide before acceptance policy discards it.
if (isMethodNotFoundRefusal(reply)) {
// Do not repeat an unsupported host-wide probe for each repo or paste.
repos.forEach((candidate) => cache.set(candidate.id, null))
return null
}
const slug = githubRepoSlugRead.interpret(reply)
resolved = slug.accepted ? slug.value : null
} catch {
resolved = null
}
cache.set(repo.id, resolved ?? null)
}
if (slugsEqual(resolved ?? null, slug)) {
return repo
}
}
return null
}
export async function lookupGitHubItemByNumber(
client: RpcClient,
repoId: string,
number: number
): Promise<GitHubWorkItem | null> {
const item = await readGitHubWorkItemByNumber(client, { repo: `id:${repoId}`, number })
return item ? { ...item, repoId } : null
}
export async function lookupGitHubItemByOwnerRepo(
client: RpcClient,
repoId: string,
slug: RepoSlug,
number: number,
type: 'issue' | 'pr'
): Promise<GitHubWorkItem | null> {
const reply = await githubWorkItemBySlugRead.request(client, {
repo: `id:${repoId}`,
owner: slug.owner,
ownerRepo: slug.repo,
...(slug.host ? { host: slug.host } : {}),
number,
type
})
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: as above.
const item = githubWorkItemBySlugRead.interpret(reply) as GitHubWorkItem | null
return item ? { ...item, repoId } : null
}
export async function lookupGitLabItemByPath(
client: RpcClient,
repoId: string,
link: NonNullable<ReturnType<typeof parseGitLabIssueOrMRLink>>
): Promise<GitLabWorkItem | null> {
const reply = await gitlabWorkItemByPathRead.request(client, {
repo: `id:${repoId}`,
host: link.slug.host,
path: link.slug.path,
iid: link.number,
type: link.type
})
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: as above, against `GitLabWorkItem`.
const item = gitlabWorkItemByPathRead.interpret(reply) as GitLabWorkItem | null
return item ? { ...item, repoId } : null
}