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

354 lines
16 KiB
TypeScript

/** GitLab merge request status through GraphQL, mapped to forge snapshots. */
import type {
ChangeRequestCheck, ChangeRequestIssue, ChangeRequestRef, ChangeRequestReviewer, ChangeRequestSnapshot, ChecksRollup,
CheckState, ForgeCheckId, ForgeFailure, MergeMethod, ReviewDecision, ReviewVerdict,
} from '@deepseek-ai/dsh-experimental-forge'
import { z } from 'zod'
import { avatarUrlOf, parseIssueUrl } from './urls.ts'
/** Reviewers and approvers read per merge request; GitLab counts list sizes toward a query's complexity. */
export const PEOPLE_PAGE = 20
/** Jobs of the head pipeline a status poll reads; `fetchChecks` reads the rest. */
export const POLL_JOBS = 50
/** Jobs per page of a complete check read. */
export const JOB_PAGE = 100
/** Job fields every job page reads. */
const JOB_FIELDS = 'name status allowFailure stage { name } webPath startedAt finishedAt'
/**
* Merge request fields one status read takes; `$jobs` sizes the first page of head pipeline jobs, which leaves out jobs a
* retry replaced, since GitLab lists those beside their retries. The merge status comes before `diffStatsSummary`, after
* which GitLab 19.5 reports `CONFLICT` for a merge request it found mergeable.
*/
export const MERGE_REQUEST_FRAGMENT = `fragment MergeRequestStatus on MergeRequest {
iid title webUrl state draft updatedAt closedAt mergedAt
author { username }
sourceBranch targetBranch
diffRefs { baseSha headSha }
detailedMergeStatus mergeStatusEnum conflicts divergedFromTargetBranch rebaseInProgress
diffStatsSummary { additions deletions }
squash squashOnMerge squashReadOnly approved
resolvableDiscussionsCount resolvedDiscussionsCount
userPermissions { canMerge pushToSourceBranch updateMergeRequest }
approvedBy(first: ${PEOPLE_PAGE}) { pageInfo { hasNextPage } nodes { username avatarUrl bot } }
reviewers(first: ${PEOPLE_PAGE}) { pageInfo { hasNextPage } nodes { username avatarUrl bot mergeRequestInteraction { reviewState } } }
headPipeline { id status userPermissions { updatePipeline } jobs(first: $jobs, retried: false) { pageInfo { hasNextPage endCursor } nodes { ${JOB_FIELDS} } } }
}`
/** Following page of head pipeline jobs, without jobs a retry replaced; variables `path`, `iid`, and `cursor` are strings. */
export const JOBS_QUERY = `query($path: ID!, $iid: String!, $cursor: String!) {
project(fullPath: $path) { mergeRequest(iid: $iid) { diffRefs { headSha } headPipeline { jobs(first: ${JOB_PAGE}, after: $cursor, retried: false) {
pageInfo { hasNextPage endCursor } nodes { ${JOB_FIELDS} } } } } }
}`
/**
* Build one query that reads the status of several merge requests, each under the alias `m<index>` with the variables
* `p<index>` (project path) and `i<index>` (number as a string).
* @param count - number of merge requests.
* @returns the GraphQL document.
*/
export function statusQuery(count: number): string {
const declared = Array.from({ length: count }, (_, index) => `$p${index}: ID!, $i${index}: String!`)
const selected = Array.from({ length: count }, (_, index) =>
`m${index}: project(fullPath: $p${index}) { mergeRequestsFfOnlyEnabled mergeRequest(iid: $i${index}) { ...MergeRequestStatus } }`)
return `query(${['$jobs: Int!', ...declared].join(', ')}) {\n ${selected.join('\n ')}\n}\n${MERGE_REQUEST_FRAGMENT}`
}
const pageInfo = z.object({ hasNextPage: z.boolean(), endCursor: z.string().nullable().optional() })
const person = z.object({ username: z.string(), avatarUrl: z.string().nullable(), bot: z.boolean() })
const jobSchema = z.object({
name: z.string(),
status: z.string().nullable(),
allowFailure: z.boolean(),
stage: z.object({ name: z.string() }).nullable(),
webPath: z.string().nullable(),
startedAt: z.string().nullable(),
finishedAt: z.string().nullable(),
})
const jobPageSchema = z.object({ pageInfo, nodes: z.array(jobSchema) })
/** One merge request as {@link MERGE_REQUEST_FRAGMENT} reads it. */
export const mergeRequestSchema = z.object({
iid: z.string(),
title: z.string(),
webUrl: z.string(),
state: z.string(),
draft: z.boolean(),
updatedAt: z.string(),
closedAt: z.string().nullable(),
mergedAt: z.string().nullable(),
author: z.object({ username: z.string() }).nullable(),
sourceBranch: z.string(),
targetBranch: z.string(),
diffRefs: z.object({ baseSha: z.string().nullable(), headSha: z.string() }).nullable(),
diffStatsSummary: z.object({ additions: z.number(), deletions: z.number() }).nullable(),
detailedMergeStatus: z.string().nullable(),
mergeStatusEnum: z.string().nullable(),
conflicts: z.boolean(),
divergedFromTargetBranch: z.boolean(),
rebaseInProgress: z.boolean(),
squash: z.boolean(),
squashOnMerge: z.boolean(),
squashReadOnly: z.boolean(),
approved: z.boolean(),
resolvableDiscussionsCount: z.number().nullable(),
resolvedDiscussionsCount: z.number().nullable(),
userPermissions: z.object({ canMerge: z.boolean(), pushToSourceBranch: z.boolean(), updateMergeRequest: z.boolean() }),
approvedBy: z.object({ pageInfo, nodes: z.array(person) }).nullable(),
reviewers: z.object({
pageInfo,
nodes: z.array(person.extend({ mergeRequestInteraction: z.object({ reviewState: z.string().nullable() }).nullable() })),
}),
headPipeline: z.object({
id: z.string(),
status: z.string(),
userPermissions: z.object({ updatePipeline: z.boolean() }),
jobs: jobPageSchema.nullable(),
}).nullable(),
})
/** One aliased project of a status query. */
export const projectSchema = z.object({
mergeRequestsFfOnlyEnabled: z.boolean(),
mergeRequest: mergeRequestSchema.nullable(),
}).nullable()
/** A following page of head pipeline jobs as {@link JOBS_QUERY} reads it. */
export const jobsPageSchema = z.object({
project: z.object({
mergeRequest: z.object({
diffRefs: z.object({ headSha: z.string() }).nullable(),
headPipeline: z.object({ jobs: jobPageSchema.nullable() }).nullable(),
}).nullable(),
}).nullable(),
})
/** One merge request as GitLab returned it. */
export type MergeRequestNode = z.infer<typeof mergeRequestSchema>
/** One job as GitLab returned it. */
export type JobNode = z.infer<typeof jobSchema>
/** Merge statuses during which GitLab has not decided whether the merge request can merge. */
const UNDECIDED = new Set(['UNCHECKED', 'CHECKING', 'PREPARING', 'APPROVALS_SYNCING'])
/**
* Conflict states GitLab has not checked since the merge request or its target changed. GitLab checks them again only
* when a REST read of the merge request asks it to, and meanwhile reports its last outcome, such as `CONFLICT`.
*/
const UNCHECKED = new Set(['UNCHECKED', 'CANNOT_BE_MERGED_RECHECK'])
/**
* Whether GitLab has not checked the merge request for conflicts since it or its target changed.
* @param node - the merge request.
* @returns whether a REST read of the merge request should ask GitLab to check it.
*/
export function needsMergeCheck(node: MergeRequestNode): boolean {
return node.mergeStatusEnum !== null && UNCHECKED.has(node.mergeStatusEnum)
}
/** Review states of a reviewer whose review is still awaited. */
const AWAITED = new Set(['UNREVIEWED', 'REVIEW_STARTED'])
/** A merge request whose status is known, and the parts of it read elsewhere. */
export interface StatusParts {
/** The merge request. */
readonly ref: ChangeRequestRef
/** Whether the project merges by fast-forward only. */
readonly fastForwardOnly: boolean
/** The merge request node. */
readonly node: MergeRequestNode
/** Every job read for the head pipeline, in GitLab's order. */
readonly jobs: readonly JobNode[]
/** Whether GitLab listed more jobs than `jobs` holds. */
readonly jobsTruncated: boolean
/** Issues the merge request closes, in GitLab's order. */
readonly closingIssues: readonly ChangeRequestIssue[]
/** Whether GitLab listed more closing issues than `closingIssues` holds. */
readonly closingIssuesTruncated: boolean
}
/**
* Map a merge request to a forge snapshot. GitLab's open, closed, merged, and locked states map to open, closed, merged,
* and open; the merge status decides mergeability: a status GitLab has not decided or not checked since a change is
* unknown, whatever `conflicts` and `CONFLICT` say, and `CONFLICT` is conflicting even while `conflicts` is false. Every
* job of the head pipeline names the pipeline as its rerun, since GitLab retries a pipeline's failed and cancelled jobs
* together. GitLab updates a head branch only by rebasing it onto the target branch, which a user who can push to the
* head branch may start while the branches have diverged without conflicts and no rebase runs.
* @param parts - the merge request and the parts read with it.
* @returns the snapshot, or a failure when GitLab reports no head commit.
*/
export function snapshotOf(parts: StatusParts): ChangeRequestSnapshot | ForgeFailure {
const { ref, node } = parts
if (node.diffRefs === null) return { code: 'unavailable', message: `merge request ${ref.number} has no diff yet` }
const state = node.state === 'merged' ? 'merged' : node.state === 'closed' ? 'closed' : 'open'
const status = node.detailedMergeStatus ?? 'UNCHECKED'
const undecided = UNDECIDED.has(status) || needsMergeCheck(node) || node.mergeStatusEnum === 'CHECKING'
const mergeability = undecided ? 'unknown' : node.conflicts || status === 'CONFLICT' ? 'conflicting' : 'mergeable'
const host = ref.repository.host
const open = state === 'open'
const rerun = (node.headPipeline?.id ?? null) as ForgeCheckId | null
const rebase = open && node.divergedFromTargetBranch && !node.rebaseInProgress && mergeability !== 'conflicting'
&& node.userPermissions.pushToSourceBranch
return {
ref,
url: node.webUrl,
title: node.title,
state,
draft: node.draft,
author: node.author?.username ?? null,
headBranch: node.sourceBranch,
headSha: node.diffRefs.headSha,
baseBranch: node.targetBranch,
...node.diffRefs.baseSha === null ? {} : { baseSha: node.diffRefs.baseSha },
additions: node.diffStatsSummary?.additions ?? 0,
deletions: node.diffStatsSummary?.deletions ?? 0,
mergeability,
mergeReady: open && !node.draft && status === 'MERGEABLE' && mergeability === 'mergeable' && node.userPermissions.canMerge,
mergeMethods: mergeMethods(parts.fastForwardOnly, node),
branchUpdates: rebase ? ['rebase'] : [],
reviewDecision: reviewDecision(node, status),
reviewers: reviewers(node, host),
reviewersTruncated: node.reviewers.pageInfo.hasNextPage || (node.approvedBy?.pageInfo.hasNextPage ?? false),
canRequestReviews: open && node.userPermissions.updateMergeRequest,
checks: parts.jobs.map(job => checkOf(host, job, rerun)),
checksRollup: rollup(node.headPipeline?.status ?? null),
checksTruncated: parts.jobsTruncated,
canRerunChecks: open && (node.headPipeline?.userPermissions.updatePipeline ?? false),
unresolvedThreads: Math.max(0, (node.resolvableDiscussionsCount ?? 0) - (node.resolvedDiscussionsCount ?? 0)),
threadsTruncated: false,
closingIssues: parts.closingIssues,
closingIssuesTruncated: parts.closingIssuesTruncated,
updatedAt: node.updatedAt,
closedAt: state === 'merged' ? node.mergedAt ?? node.closedAt : state === 'closed' ? node.closedAt : null,
}
}
/**
* The methods a merge may use: the project's fast-forward setting decides between a merge commit and a rebase, and the
* merge request's squash setting adds or forces squashing, with GitLab's default for the merge request first.
*/
function mergeMethods(fastForwardOnly: boolean, node: MergeRequestNode): MergeMethod[] {
const base: MergeMethod = fastForwardOnly ? 'rebase' : 'merge'
if (node.squashReadOnly) return [node.squashOnMerge ? 'squash' : base]
return node.squash ? ['squash', base] : [base, 'squash']
}
/** A reviewer who requested changes decides first, then an approval, then GitLab's demand for one. */
function reviewDecision(node: MergeRequestNode, status: string): ReviewDecision {
if (node.reviewers.nodes.some(reviewer => reviewer.mergeRequestInteraction?.reviewState === 'REQUESTED_CHANGES')) return 'changes-requested'
if (node.approved) return 'approved'
if (status === 'NOT_APPROVED') return 'review-required'
return 'none'
}
/** Reviewers in GitLab's order, then approvers who are not reviewers; the author is never listed. */
function reviewers(node: MergeRequestNode, host: string): ChangeRequestReviewer[] {
const author = node.author?.username.toLowerCase()
const listed: ChangeRequestReviewer[] = []
const seen = new Set<string>()
const add = (login: string, entry: ChangeRequestReviewer): void => {
if (login.toLowerCase() === author || seen.has(login.toLowerCase())) return
seen.add(login.toLowerCase())
listed.push(entry)
}
for (const reviewer of node.reviewers.nodes) {
const state = reviewer.mergeRequestInteraction?.reviewState ?? 'UNREVIEWED'
add(reviewer.username, {
kind: reviewer.bot ? 'bot' : 'user', login: reviewer.username, avatarUrl: avatarUrlOf(host, reviewer.avatarUrl), requested: AWAITED.has(state),
codeOwner: false, verdict: verdictOf(state), reviewedSha: null,
})
}
for (const approver of node.approvedBy?.nodes ?? []) {
add(approver.username, {
kind: approver.bot ? 'bot' : 'user', login: approver.username, avatarUrl: avatarUrlOf(host, approver.avatarUrl), requested: false,
codeOwner: false, verdict: 'approved', reviewedSha: null,
})
}
return listed
}
/** A reviewer state as the outcome of their latest review; states outside the documented set read as none. */
function verdictOf(state: string): ReviewVerdict | null {
switch (state) {
case 'APPROVED': return 'approved'
case 'REQUESTED_CHANGES': return 'changes-requested'
case 'REVIEWED': return 'commented'
case 'UNAPPROVED': return 'dismissed'
default: return null
}
}
/**
* Map one job to a check. A failed job allowed to fail is neutral, as GitLab shows it passing with a warning; a manual
* job allowed to fail is skipped, and one that blocks the pipeline is pending until someone runs it.
* @param host - GitLab host, for the job's page.
* @param job - the job.
* @param rerun - global id of the job's pipeline, which a rerun retries.
* @returns the check.
*/
export function checkOf(host: string, job: JobNode, rerun: ForgeCheckId | null): ChangeRequestCheck {
return {
name: job.name,
state: jobState(job),
url: job.webPath === null ? null : `https://${host}${job.webPath}`,
workflow: job.stage?.name ?? null,
startedAt: job.startedAt,
completedAt: job.finishedAt,
rerun,
}
}
/** A job status as a check state; statuses outside the documented set read as pending. */
function jobState(job: JobNode): CheckState {
switch (job.status) {
case 'SUCCESS': return 'success'
case 'FAILED': return job.allowFailure ? 'neutral' : 'failure'
case 'CANCELED':
case 'CANCELING': return 'cancelled'
case 'SKIPPED': return 'skipped'
case 'MANUAL': return job.allowFailure ? 'skipped' : 'pending'
default: return 'pending'
}
}
/** A pipeline status as the combined check outcome; a merge request without a pipeline has none. */
function rollup(status: string | null): ChecksRollup {
switch (status) {
case null: return 'none'
case 'SUCCESS':
case 'SKIPPED': return 'success'
case 'FAILED':
case 'CANCELED': return 'failure'
default: return 'pending'
}
}
/** One closing issue as REST `closes_issues` lists it. */
const issueSchema = z.object({ iid: z.number(), title: z.string(), state: z.string(), web_url: z.string() })
/**
* Map the REST list of issues a merge request closes, keeping the ones whose page names a project.
* @param body - the parsed `closes_issues` answer.
* @param limit - the most issues to keep.
* @returns the issues and whether more were listed, or undefined when the answer is not such a list.
*/
export function closingIssuesOf(
body: unknown, limit: number,
): { readonly issues: ChangeRequestIssue[]; readonly truncated: boolean } | undefined {
const parsed = z.array(issueSchema).safeParse(body)
if (!parsed.success) return undefined
const issues = parsed.data.flatMap((issue): ChangeRequestIssue[] => {
const location = parseIssueUrl(issue.web_url)
return location === undefined ? [] : [{
repository: { forge: 'gitlab', host: location.host, owner: location.owner, name: location.name },
number: issue.iid, title: issue.title, state: issue.state === 'closed' ? 'closed' : 'open', url: issue.web_url,
}]
})
return { issues: issues.slice(0, limit), truncated: issues.length > limit }
}