Files
deepseek-harness/packages/experimental/forge-github/src/urls.ts
T
Yichen Jiang dcbfd8079a feat(experimental): bind code host change requests to Sessions
Add the experimental forge capability seam:

- dsh-experimental-forge: provider-neutral repository and change request
  references, a provider registry, and shared status polling. Watchers of
  one change request share one loop; each forge has one batched fetch in
  flight, and the cadence follows check, mergeability, and lifecycle state.
- dsh-experimental-forge-github: GitHub provider that runs the GitHub CLI
  on the host with a scrubbed environment and reads every due pull request
  of a host in one GraphQL query.
- dsh-experimental-forge-session: a host-only projection derives Session
  bindings from gh pr create and git push shell results and from
  plugin:forge-session/binding records; push results trigger head-branch
  discovery of open pull requests, and Session watchers poll bound change
  requests through ctx.forge.
2026-10-20 11:49:25 +08:00

60 lines
2.4 KiB
TypeScript

/** GitHub remote URL and pull request URL parsing. */
/** Characters GitHub permits in owner and repository names. */
const SEGMENT = /^[A-Za-z0-9_.-]+$/
/** Host, owner, and repository name addressed by a URL. */
export interface GitHubLocation {
/** Lowercase host name. */
readonly host: string
/** Owner as the URL spells it. */
readonly owner: string
/** Repository name without a `.git` suffix. */
readonly name: string
}
/**
* Check one owner or repository name segment.
* @param value - candidate segment.
* @returns whether GitHub permits the segment and it is not a dot path.
*/
export function isSegment(value: string): boolean {
return SEGMENT.test(value) && value !== '.' && value !== '..'
}
/**
* Parse a Git remote URL. Accepts `http(s)://[user@]host[:port]/owner/name`,
* `ssh://[user@]host[:port]/owner/name`, `git://host/owner/name`, and the
* scp-like `[user@]host:owner/name`, each with an optional `.git` suffix or
* trailing slash.
* @param url - fetch or push URL as Git reports it.
* @returns the location, or undefined for other URL shapes.
*/
export function parseRemoteUrl(url: string): GitHubLocation | undefined {
const trimmed = url.trim()
const scheme = /^(?:https?|ssh|git):\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?\/(.+)$/i.exec(trimmed)
const scp = scheme === null ? /^(?:[^@/:]+@)?([^/:]+):(?!\/\/)(.+)$/.exec(trimmed) : null
const match = scheme ?? scp
if (match === null) return undefined
const [, host = '', path = ''] = match
const parts = path.replace(/\/+$/, '').replace(/\.git$/i, '').split('/')
if (parts.length !== 2) return undefined
const [owner = '', name = ''] = parts
if (!isSegment(owner) || !isSegment(name)) return undefined
return { host: host.toLowerCase(), owner, name }
}
/**
* Parse a pull request web URL such as `https://github.com/owner/name/pull/12/files`.
* @param url - web URL of a pull request.
* @returns the location and number, or undefined for other URLs.
*/
export function parsePullRequestUrl(url: string): (GitHubLocation & { readonly number: number }) | undefined {
const match = /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)\/pull\/(\d+)(?:[/?#].*)?$/i.exec(url.trim())
if (match === null) return undefined
const [, host = '', owner = '', name = '', digits = ''] = match
const number = Number(digits)
if (!isSegment(owner) || !isSegment(name) || !Number.isSafeInteger(number) || number <= 0) return undefined
return { host: host.toLowerCase(), owner, name, number }
}