Files
deepseek-harness/packages/experimental/forge-gitlab/src/overview.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

192 lines
11 KiB
TypeScript

/** GitLab writes and reads of the merge request overview: pipeline retries, review requests, reviewer candidates, and rebases. */
import type {
BranchUpdateRequest, ChangeRequestRef, ChangeRequestSnapshot, ForgeActionFailure, ForgeFailure, ForgeRerunRequest,
ForgeReviewerCandidate, ForgeReviewersRequest,
} from '@deepseek-ai/dsh-experimental-forge'
import { z } from 'zod'
import type { GitLabApi } from './api.ts'
import { avatarUrlOf, checkedProject, mergeRequestPath } from './urls.ts'
/** Most accounts one candidate listing returns. */
const CANDIDATES = 20
/** Reviewers read before a review request; more than GitLab's paid tiers usually assign. */
const REVIEWERS = 100
/** Global id of a CI pipeline, which names the rerun of its jobs. */
const PIPELINE = /^gid:\/\/gitlab\/Ci::Pipeline\/([1-9]\d{0,17})$/
/** The merge request's reviewers and whether it takes more than one; variables `path` and `iid` are strings. */
export const REVIEWERS_QUERY = `query($path: ID!, $iid: String!) {
project(fullPath: $path) { mergeRequest(iid: $iid) { allowsMultipleReviewers reviewers(first: ${REVIEWERS}) { nodes { id username } } } }
}`
/** Add reviewers by username; variables `path`, `iid`, and `usernames` name them. */
export const SET_REVIEWERS_MUTATION = `mutation($path: ID!, $iid: String!, $usernames: [String!]!) {
mergeRequestSetReviewers(input: { projectPath: $path, iid: $iid, reviewerUsernames: $usernames, operationMode: APPEND }) { errors }
}`
/** Ask a reviewer for another review; variables `path`, `iid`, and `user`, the reviewer's global id. */
export const REREVIEW_MUTATION = `mutation($path: ID!, $iid: String!, $user: UserID!) {
mergeRequestReviewerRereview(input: { projectPath: $path, iid: $iid, userId: $user }) { errors }
}`
/** Accounts that match a search and the merge request's author; variables `path`, `iid`, and `search` are strings. */
export const CANDIDATES_QUERY = `query($path: ID!, $iid: String!, $search: String!) {
project(fullPath: $path) { autocompleteUsers(search: $search) { username name avatarUrl } mergeRequest(iid: $iid) { author { username } } }
}`
const reviewersSchema = z.object({
project: z.object({
mergeRequest: z.object({
allowsMultipleReviewers: z.boolean(),
reviewers: z.object({ nodes: z.array(z.object({ id: z.string(), username: z.string() })) }),
}).nullable(),
}).nullable(),
})
const candidatesSchema = z.object({
project: z.object({
autocompleteUsers: z.array(z.object({ username: z.string(), name: z.string(), avatarUrl: z.string().nullable() })).nullable(),
mergeRequest: z.object({ author: z.object({ username: z.string() }).nullable() }).nullable(),
}).nullable(),
})
/**
* Retry the head pipeline when it has failed jobs and none pending. GitLab retries a pipeline's failed and cancelled jobs
* together, so every job names its pipeline as its rerun; the head is read first through `read`, and a head other than
* `request.headSha` is refused with `stale`.
* @param api - GitLab writes.
* @param read - reads the merge request's status with all its checks.
* @param ref - merge request, with a project path already checked for inlining.
* @param request - the head commit whose checks the user saw.
* @param signal - cancellation of the read and the retries.
* @returns undefined once GitLab accepted every retry, or why it did not.
*/
export async function rerunGitLabChecks(
api: GitLabApi, read: () => Promise<ChangeRequestSnapshot | ForgeFailure>, ref: ChangeRequestRef, request: ForgeRerunRequest,
signal: AbortSignal,
): Promise<ForgeActionFailure | undefined> {
const snapshot = await read()
if ('code' in snapshot) return { code: snapshot.code === 'unauthenticated' ? 'unauthenticated' : 'failed', message: snapshot.message }
if (snapshot.headSha.toLowerCase() !== request.headSha.toLowerCase()) {
return { code: 'stale', message: `the merge request head moved from ${request.headSha} to ${snapshot.headSha}` }
}
const failed = new Set<string>()
const running = new Set<string>()
for (const check of snapshot.checks) {
const pipeline = check.rerun === null ? undefined : PIPELINE.exec(check.rerun)?.[1]
if (pipeline === undefined) continue
if (check.state === 'failure') failed.add(pipeline)
else if (check.state === 'pending') running.add(pipeline)
}
const pipelines = [...failed].filter(pipeline => !running.has(pipeline))
if (pipelines.length === 0) {
return { code: 'failed', message: failed.size === 0 ? 'No failed GitLab job of the head pipeline can be retried'
: 'The head pipeline still runs jobs; retry its failed jobs once they finish' }
}
const project = encodeURIComponent(checkedProject(ref))
for (const pipeline of pipelines) {
const retried = await api.write(ref.repository.host, ['-X', 'POST', `projects/${project}/pipelines/${pipeline}/retry`], signal)
if (!retried.ok) return retried.failure
}
return undefined
}
/**
* Request reviews, adding to GitLab's reviewers. Accounts that are not reviewers yet are added by username; a reviewer
* named again is asked for another review. GitLab has no team reviewers, and a merge request that takes one reviewer,
* as on GitLab's free tier, refuses a second before anything is sent.
* @param api - GitLab reads and writes.
* @param ref - merge request, with a project path already checked for inlining.
* @param request - the reviewers to ask.
* @param signal - cancellation of every request.
* @returns undefined once GitLab accepted every request, or why it refused; a refused second review request follows
* accepted earlier ones.
*/
export async function requestGitLabReviews(
api: GitLabApi, ref: ChangeRequestRef, request: ForgeReviewersRequest, signal: AbortSignal,
): Promise<ForgeActionFailure | undefined> {
if (request.reviewers.length === 0) return { code: 'failed', message: 'No reviewer was named' }
if (request.reviewers.some(reviewer => reviewer.kind === 'team')) return { code: 'failed', message: 'GitLab has no team reviewers' }
const host = ref.repository.host
const variables = { path: checkedProject(ref), iid: String(ref.number) }
const read = await api.graphql(host, REVIEWERS_QUERY, variables, signal)
if (!read.ok) return { code: read.failure.code === 'unauthenticated' ? 'unauthenticated' : 'failed', message: read.failure.message }
const merge = reviewersSchema.safeParse(read.body).data?.project?.mergeRequest
if (merge == null) return { code: 'failed', message: 'unexpected GitLab response to the reviewer read' }
const current = new Map(merge.reviewers.nodes.map(reviewer => [reviewer.username.toLowerCase(), reviewer.id]))
const again = [...new Set(request.reviewers.flatMap(reviewer => current.get(reviewer.login.toLowerCase()) ?? []))]
const added: string[] = []
for (const { login } of request.reviewers) {
const key = login.toLowerCase()
if (!current.has(key) && !added.some(entry => entry.toLowerCase() === key)) added.push(login)
}
if (!merge.allowsMultipleReviewers && current.size + added.length > 1) {
return { code: 'failed', message: 'This GitLab merge request takes one reviewer; change its reviewer on GitLab' }
}
if (added.length > 0) {
const set = await api.mutate(host, SET_REVIEWERS_MUTATION, { ...variables, usernames: added }, 'mergeRequestSetReviewers', signal)
if (!('ok' in set)) return set
}
for (const user of again) {
const asked = await api.mutate(host, REREVIEW_MUTATION, { ...variables, user }, 'mergeRequestReviewerRereview', signal)
if (!('ok' in asked)) return asked
}
return undefined
}
/**
* List accounts a review can be requested from: GitLab's matches for `query` among the users who can see the project,
* without the author. GitLab suggests no reviewers on its free tier, so no account is marked suggested.
* @param api - GitLab reads.
* @param ref - merge request, with a project path already checked for inlining.
* @param query - text the login or name contains.
* @param signal - cancellation of the read.
* @returns at most 20 accounts, each with an absolute avatar URL, or why GitLab did not list them.
*/
export async function gitLabReviewerCandidates(
api: GitLabApi, ref: ChangeRequestRef, query: string, signal: AbortSignal,
): Promise<readonly ForgeReviewerCandidate[] | ForgeFailure> {
const host = ref.repository.host
const variables = { path: checkedProject(ref), iid: String(ref.number), search: query.trim() }
const read = await api.graphql(host, CANDIDATES_QUERY, variables, signal)
if (!read.ok) return read.failure
const project = candidatesSchema.safeParse(read.body).data?.project
if (project === undefined) return { code: 'unavailable', message: 'unexpected GitLab response to the reviewer listing' }
if (project === null) return { code: 'not-found', message: `project ${checkedProject(ref)} is not visible` }
const author = project.mergeRequest?.author?.username.toLowerCase()
return (project.autocompleteUsers ?? []).filter(user => user.username.toLowerCase() !== author).slice(0, CANDIDATES).map(user => ({
login: user.username, name: user.name, suggested: false,
avatarUrl: avatarUrlOf(host, user.avatarUrl),
}))
}
/**
* Rebase the head branch onto the target branch. GitLab names no head commit in a rebase request, so the merge request is
* read first and the rebase is refused with `stale` once its head is another commit; a push between the read and the
* request is rebased too. GitLab rebases in the background and answers once it accepted the rebase.
* @param api - GitLab reads and writes.
* @param ref - merge request, with a project path already checked for inlining.
* @param request - update method and the head commit the user confirmed.
* @param signal - cancellation of the read and the request.
* @returns undefined once GitLab accepted the rebase, `stale` when the head moved, or why GitLab refused.
*/
export async function updateGitLabBranch(
api: GitLabApi, ref: ChangeRequestRef, request: BranchUpdateRequest, signal: AbortSignal,
): Promise<ForgeActionFailure | undefined> {
if (request.method !== 'rebase') return { code: 'failed', message: 'GitLab updates a merge request branch only by rebasing it' }
const host = ref.repository.host
const read = await api.rest(host, `${mergeRequestPath(ref)}?include_rebase_in_progress=true`, signal)
if (!read.ok) return { code: read.failure.code === 'unauthenticated' ? 'unauthenticated' : 'failed', message: read.failure.message }
const merge = z.object({ sha: z.string(), rebase_in_progress: z.boolean().optional() }).safeParse(read.body)
if (!merge.success) return { code: 'failed', message: 'unexpected GitLab response to the merge request read' }
if (merge.data.sha.toLowerCase() !== request.headSha.toLowerCase()) {
return { code: 'stale', message: `the merge request head moved from ${request.headSha} to ${merge.data.sha}` }
}
if (merge.data.rebase_in_progress === true) return { code: 'failed', message: 'GitLab is already rebasing the merge request' }
const rebased = await api.write(host, ['-X', 'PUT', `${mergeRequestPath(ref)}/rebase`], signal)
return rebased.ok ? undefined : rebased.failure
}