Files
deepseek-harness/packages/experimental/forge-gitlab/src/diff.ts
T
LegGasai cb44a3c2ee feat(forge): read, review, and bind GitLab merge requests (#8317)
* feat(forge): let providers declare their hosts, names, sign-in, and review decisions

Prepare the forge contract for a second provider:

- ForgeProvider lists the hosts it serves and names itself (label,
  changeRequestNoun); registering a provider that lists a host another
  registered provider lists throws, so each URL has at most one provider.
  ctx.forge.describe(forge) returns the family's ForgeDescription.
- ForgeReadiness carries the sign-in command, which the panel's sign-in
  banner shows instead of building `gh auth login` for the github family.
- ForgeReviewData lists the review decisions the host lets the reader
  record; the review dialog offers only those instead of hardcoding the
  GitHub rule that a pull request's author may only comment.
- ForgeReviewThread.original may name a later commit the host carried the
  thread to while its lines stayed unchanged.
- The changes view names the code host and its word for a change request
  from the entry's ForgeDescription instead of hardcoding GitHub and PR.

The GitHub provider declares its configured hosts, GitHub, pull requests,
`gh auth login --hostname <host>`, and reads viewerDidAuthor for the
review decisions. Upgrade guide: forge-provider-hosts.

* feat(forge-gitlab): read GitLab merge requests through the GitLab CLI

Add the experimental forge-gitlab provider. It serves the configured
GitLab hosts through glab: status batches over GraphQL, head-pinned
patches from merge request diff versions, commit listings, merge bases,
pipeline jobs, closing issues, and review threads mapped from REST
discussions, including outdated threads' hunks.

The forge bundle composes the provider, disabled by default.

* feat(forge-gitlab): write GitLab reviews, retry pipelines, request reviews, and rebase

The GitLab provider now writes through glab as well as reading:
comments start discussions at positions derived from the head's diff
version, pending replies and comments are draft notes that a review
publishes with its summary and review state, and approving checks the
head through GitLab's sha parameter. Review reads carry the signed-in
user's rights from GraphQL, their draft notes as the pending review,
and the decisions GitLab lets them record.

Snapshots name the head pipeline as every job's rerun, offer rebase as
the branch update, and follow GitLab's permissions for reruns and
review requests. updateBranch's contract now states how a provider
whose host takes no expected head checks it.

* feat(forge): let forge providers register the commands that bind change requests

forge-session no longer recognizes gh commands by itself. A provider now
registers a declarative ForgeCommandRule through
ctx.forgeSession.registerCommands: its executable, creation and reference
subcommands, change request page path, JSON URL members, declared
options, and a pure select that maps one parsed reference command to a
URL or a read-only lookup. forge-github registers gh and forge-gitlab
registers glab, which adds glab mr create, view, update, and checkout,
including numbers with -R: a full project URL binds directly, and other
repository forms resolve through glab mr view in the call's shell.

forge-session keeps the shared parts: the literal shell tokenizer,
option parsing, output reading, lookups, and the projection. The
forgeChangeRequests state version derives from the recognized rules, so
a changed rule set refolds every Session, including commands that ran
before the provider loaded. Ending a registration keeps its rule
recognized until restart, so unloading providers at shutdown does not
rewrite every checkpoint.

* refactor(forge-gitlab): publish the GitLab provider as @deepseek-ai/dsh-forge-gitlab

New experimental packages omit the experimental prefix so that promotion
keeps their npm name. Register the directory in
EXPERIMENTAL_PACKAGE_NAME_EXCEPTIONS instead of growing the legacy
prefixed list, and rename every dependency, configuration row, path
mapping, and document that names the package.

* fix(forge-gitlab): keep review writes, comment positions, and reads within GitLab's facts and the limits

- After GitLab refuses a publication, read the drafts again: delete
  them only when all remain, since GitLab can publish them before it
  refuses the summary, and report the outcome as unknown otherwise.
  A rollback that leaves drafts reports how many may remain.
- Place comments on files GitLab collapsed through the diff compared
  from their texts, and let a file's new path name it over another
  file's old path after chained renames.
- Keep the final newline of raw file reads.
- Apply maxOutputBytes to the complete review result, and report a
  read whose rights stop early or skip a discussion's later notes as
  truncated.
- Serve a version over maxCachedVersionBytes without keeping it.
- Resolve uploaded avatars GitLab names by their path on the host.
- Schedule bounded reads through p-limit.

* fix(forge-session): run lookups only for live command rules and keep the projection registered

- Reference lookups use the rules of live registrations, so a disposed
  provider's rule still folds but starts no gh or glab process.
- A failed re-registration registers the projection again with the
  earlier rules, so clients keep its view; an error is logged only when
  that fails too.
- Document the startup re-registration, test that the checkpointed
  state version survives a real Loader start, shutdown, and restart,
  and record that starting without a provider removes the bindings only
  its commands made.
- Describe the changes view's pending review and single comment
  without naming GitHub, which keeps its own rule in forge-github, and
  render registerCommands as code in the generated service reference.

* fix(forge-bundle): name GitLab merge requests in the bundle description

The bundle now follows GitLab merge requests as well as GitHub pull
requests, so the description's first sentence in every locale names
both kinds of change request.

* feat(forge): bind the change requests that gh api and glab api requests name

Models often read a merge request through `glab api` or a pull request
through `gh api` instead of `glab mr view` or `gh pr view`, which left
the change request unbound. A command rule now declares raw API request
subcommands beside its reference subcommands, each group with its own
options, since `-f` is a value of `gh api` and a flag of
`gh pr checkout`.

An API request binds, as referenced, only the change request its REST
path names, such as `projects/group%2Fapp/merge_requests/7/changes` or
`repos/octo/demo/pulls/7/files`; its output is not read, since one
request can list many change requests. A path whose host the command
names binds directly, and one without a host, or with the current
repository's placeholders, resolves through the CLI's view command in
the call's shell. The gh and glab rules move to version 2.

* docs(forge): add forge-gitlab and the forge-session peers to the module graph

Regenerate docs/module-graph for the renamed GitLab provider and the
optional forge-session peer of both providers.

* test(web): refresh the forge bundle description in the recommended plugin catalog

The creation recommendation recording carries every recommended
plugin's description; refresh it for the forge bundle description that
now names GitLab merge requests and the glab sign-in.

* chore(forge-gitlab): carry the 0.2.2-rc.3 release version and move upgrade guides to v0.2.2-rc.3
2026-11-13 07:56:57 +00:00

254 lines
13 KiB
TypeScript

/** Merge request patches pinned to one head through MR diff versions, and comparisons of two commits. */
import type { ChangeRequestRef, ForgeFailure } from '@deepseek-ai/dsh-experimental-forge'
import { structuredPatch } from 'diff'
import { z } from 'zod'
import type { GitLabApi } from './api.ts'
import { checkedProject, mergeRequestPath } from './urls.ts'
/** A full SHA-1 or SHA-256 object name. */
const COMMIT = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i
/**
* Check a commit object name before it is inlined into a request.
* @param value - candidate object name.
* @returns whether it is a full 40- or 64-digit hexadecimal commit.
*/
export function isCommit(value: string): boolean {
return COMMIT.test(value)
}
const versionSchema = z.object({
id: z.number(),
head_commit_sha: z.string(),
base_commit_sha: z.string(),
start_commit_sha: z.string(),
real_size: z.string().nullable().optional(),
})
const fileSchema = z.object({
old_path: z.string(),
new_path: z.string(),
a_mode: z.string().nullable(),
b_mode: z.string().nullable(),
new_file: z.boolean(),
renamed_file: z.boolean(),
deleted_file: z.boolean(),
diff: z.string(),
collapsed: z.boolean().optional(),
too_large: z.boolean().optional(),
})
/** One MR diff version: the head, merge base, and target tip GitLab recorded for one push. */
export type DiffVersion = z.infer<typeof versionSchema>
/** One changed file as GitLab lists it. */
type DiffFileNode = z.infer<typeof fileSchema>
/** Reads the text of a file at a commit, for files whose diff GitLab left out. */
export type ReadText = (commit: string, path: string) => Promise<string | ForgeFailure>
/**
* Find the newest diff version of a merge request and check that it is at `headSha`. GitLab records a version for
* every push, so the newest version's head is the merge request's head once GitLab has processed the push.
* @param api - GitLab reads.
* @param ref - merge request.
* @param headSha - head commit the caller expects.
* @param signal - cancellation of the read.
* @returns the version, `stale` when the newest version is at another head, or the read's failure.
*/
export async function newestVersion(
api: GitLabApi, ref: ChangeRequestRef, headSha: string, signal: AbortSignal,
): Promise<DiffVersion | ForgeFailure> {
const read = await api.rest(ref.repository.host, `${mergeRequestPath(ref)}/versions?per_page=1`, signal)
if (!read.ok) return read.failure
const parsed = z.array(versionSchema).safeParse(read.body)
if (!parsed.success) return { code: 'unavailable', message: 'unexpected GitLab response to the diff version listing' }
const newest = parsed.data[0]
if (newest === undefined) return { code: 'stale', message: `merge request ${ref.number} has no diff version yet` }
if (newest.head_commit_sha.toLowerCase() !== headSha.toLowerCase()) {
return { code: 'stale', message: `the merge request head moved from ${headSha} to ${newest.head_commit_sha}` }
}
return newest
}
/**
* Read the patch of one diff version, the changes from its merge base to its head.
* @param api - GitLab reads.
* @param ref - merge request.
* @param version - the version.
* @param readText - reads a file whose diff GitLab collapsed.
* @param signal - cancellation of the read.
* @returns the unified patch with `a/` and `b/` prefixes, or why it could not be read; a version over GitLab's file
* limit, whose `real_size` ends in `+`, is `too-large`.
*/
export async function versionPatch(
api: GitLabApi, ref: ChangeRequestRef, version: DiffVersion, readText: ReadText, signal: AbortSignal,
): Promise<string | ForgeFailure> {
if (version.real_size?.endsWith('+') === true) return { code: 'too-large', message: `merge request ${ref.number} changes more files than GitLab lists` }
const read = await api.rest(ref.repository.host, `${mergeRequestPath(ref)}/versions/${version.id}?unidiff=true`, signal)
if (!read.ok) return read.failure
const parsed = z.object({ diffs: z.array(fileSchema) }).safeParse(read.body)
if (!parsed.success) return { code: 'unavailable', message: 'unexpected GitLab response to the diff version' }
return await assemble(parsed.data.diffs, version.base_commit_sha, version.head_commit_sha, readText)
}
/**
* Read the changes from commit `base` to commit `head` in the merge request's project.
* @param api - GitLab reads.
* @param ref - merge request whose project holds both commits.
* @param base - full object name of the older commit.
* @param head - full object name of the newer commit.
* @param readText - reads a file whose diff GitLab collapsed.
* @param signal - cancellation of the read.
* @returns the unified patch, or why it could not be read; a comparison GitLab stopped for time is `too-large`.
*/
export async function comparePatch(
api: GitLabApi, ref: ChangeRequestRef, base: string, head: string, readText: ReadText, signal: AbortSignal,
): Promise<string | ForgeFailure> {
const project = encodeURIComponent(checkedProject(ref))
const read = await api.rest(ref.repository.host, `projects/${project}/repository/compare?from=${base}&to=${head}&unidiff=true`, signal)
if (!read.ok) return read.failure
const parsed = z.object({ diffs: z.array(fileSchema), compare_timeout: z.boolean().optional() }).safeParse(read.body)
if (!parsed.success) return { code: 'unavailable', message: 'unexpected GitLab response to the comparison' }
if (parsed.data.compare_timeout === true) return { code: 'too-large', message: `GitLab stopped comparing ${base} with ${head}` }
return await assemble(parsed.data.diffs, base, head, readText)
}
/** One changed file of a diff version with its diff, which GitLab's own diff or a comparison of its texts supplies. */
export interface VersionFileDiff {
readonly oldPath: string
readonly newPath: string
/** Unified diff of the file, with `---` and `+++` lines when it was compared from the texts. */
readonly diff: string
}
/**
* Read the changed files of one diff version with the diff of each, comparing a file GitLab collapsed or found too large
* from its texts at the version's merge base and head.
* @param api - GitLab reads.
* @param ref - merge request.
* @param version - id of the diff version.
* @param readText - reads a file whose diff GitLab collapsed.
* @param signal - cancellation of the read.
* @returns the files in GitLab's order, or undefined when GitLab did not list them or a file's text could not be read.
*/
export async function versionFileDiffs(
api: GitLabApi, ref: ChangeRequestRef, version: number, readText: ReadText, signal: AbortSignal,
): Promise<VersionFileDiff[] | undefined> {
const read = await api.rest(ref.repository.host, `${mergeRequestPath(ref)}/versions/${version}?unidiff=true`, signal)
const parsed = read.ok
? z.object({ base_commit_sha: z.string(), head_commit_sha: z.string(), diffs: z.array(fileSchema) }).safeParse(read.body)
: undefined
if (parsed?.success !== true) return undefined
const files: VersionFileDiff[] = []
for (const file of parsed.data.diffs) {
const diff = await fileDiff(file, parsed.data.base_commit_sha, parsed.data.head_commit_sha, readText)
if (typeof diff !== 'string') return undefined
files.push({ oldPath: file.old_path, newPath: file.new_path, diff })
}
return files
}
/**
* Join GitLab's per-file diffs into one unified patch with Git's headers, ordered by path as GitLab's raw patch is. A
* file whose diff GitLab collapsed or found too large, which it returns empty, is compared here from its text at both
* commits.
* @param files - changed files in GitLab's order.
* @param base - commit of the old sides.
* @param head - commit of the new sides.
* @param readText - reads a file at a commit.
* @returns the patch, or the failure of a file read.
*/
async function assemble(files: readonly DiffFileNode[], base: string, head: string, readText: ReadText): Promise<string | ForgeFailure> {
const parts: string[] = []
// GitLab lists a version's files in its own order; its raw patch, like Git's, orders them by path.
const ordered = [...files].sort((left, right) => compareText(`${left.old_path}\0${left.new_path}`, `${right.old_path}\0${right.new_path}`))
for (const file of ordered) {
const header = [`diff --git a/${file.old_path} b/${file.new_path}`]
if (file.new_file) header.push(`new file mode ${file.b_mode ?? '100644'}`)
else if (file.deleted_file) header.push(`deleted file mode ${file.a_mode ?? '100644'}`)
else if (file.a_mode !== null && file.b_mode !== null && file.a_mode !== file.b_mode) header.push(`old mode ${file.a_mode}`, `new mode ${file.b_mode}`)
if (file.renamed_file) header.push(`rename from ${file.old_path}`, `rename to ${file.new_path}`)
const body = await fileDiff(file, base, head, readText)
if (typeof body !== 'string') return body
parts.push(`${header.join('\n')}\n${body === '' || body.endsWith('\n') ? body : `${body}\n`}`)
}
return parts.join('')
}
/** GitLab's diff of one file, or for a file it collapsed or found too large, which it returns empty, a comparison of its texts. */
async function fileDiff(file: DiffFileNode, base: string, head: string, readText: ReadText): Promise<string | ForgeFailure> {
return file.diff === '' && (file.collapsed === true || file.too_large === true) ? await compareTexts(file, base, head, readText) : file.diff
}
/** Order two strings by their UTF-16 code units, as `<` does, for a sort comparator. */
function compareText(left: string, right: string): number {
return left < right ? -1 : left > right ? 1 : 0
}
/** Compare one file's text at both commits as the `---`, `+++`, and hunk lines of a patch. */
async function compareTexts(file: DiffFileNode, base: string, head: string, readText: ReadText): Promise<string | ForgeFailure> {
const before = file.new_file ? '' : await readText(base, file.old_path)
if (typeof before !== 'string') return before
const after = file.deleted_file ? '' : await readText(head, file.new_path)
if (typeof after !== 'string') return after
const oldName = file.new_file ? '/dev/null' : `a/${file.old_path}`
const newName = file.deleted_file ? '/dev/null' : `b/${file.new_path}`
if (before.includes('\0') || after.includes('\0')) return `Binary files ${oldName} and ${newName} differ\n`
return textPatch(oldName, newName, before, after)
}
/**
* Compare two texts as the `---`, `+++`, and hunk lines of a patch with three lines of context.
* @param oldName - old side's name, such as `a/<path>` or `/dev/null`.
* @param newName - new side's name.
* @param before - old text.
* @param after - new text.
* @returns the patch lines, ending with a newline.
*/
export function textPatch(oldName: string, newName: string, before: string, after: string): string {
const patch = structuredPatch(oldName, newName, before, after, '', '', { context: 3 })
// Git numbers an empty side from the line before it, so an added or deleted file's empty side starts at 0.
const start = (line: number, count: number): number => count === 0 ? line - 1 : line
const hunks = patch.hunks.map(hunk =>
`@@ -${start(hunk.oldStart, hunk.oldLines)},${hunk.oldLines} +${start(hunk.newStart, hunk.newLines)},${hunk.newLines} @@\n${hunk.lines.join('\n')}\n`)
return `--- ${oldName}\n+++ ${newName}\n${hunks.join('')}`
}
/**
* Read the merge base of two commits in the merge request's project.
* @param api - GitLab reads.
* @param ref - merge request whose project holds both commits.
* @param base - full object name of one commit.
* @param head - full object name of the other.
* @param signal - cancellation of the read.
* @returns the merge base's full object name, or why GitLab did not name one.
*/
export async function mergeBaseOf(
api: GitLabApi, ref: ChangeRequestRef, base: string, head: string, signal: AbortSignal,
): Promise<string | ForgeFailure> {
const project = encodeURIComponent(checkedProject(ref))
const read = await api.rest(ref.repository.host, `projects/${project}/repository/merge_base?refs[]=${base}&refs[]=${head}`, signal)
if (!read.ok) return read.failure
const parsed = z.object({ id: z.string() }).safeParse(read.body)
return parsed.success && isCommit(parsed.data.id) ? parsed.data.id
: { code: 'unavailable', message: 'GitLab named no full merge base commit' }
}
/**
* Read the text of a file at a commit.
* @param api - GitLab reads.
* @param ref - merge request whose project holds the file.
* @param commit - full object name of the commit.
* @param path - repository-relative path.
* @param signal - cancellation of the read.
* @returns the text, or why GitLab did not return it.
*/
export async function fileTextOf(
api: GitLabApi, ref: ChangeRequestRef, commit: string, path: string, signal: AbortSignal,
): Promise<string | ForgeFailure> {
const project = encodeURIComponent(checkedProject(ref))
return await api.restText(ref.repository.host, `projects/${project}/repository/files/${encodeURIComponent(path)}/raw?ref=${commit}`, signal)
}