Files
deepseek-harness/packages/experimental/forge-gitlab/tests/writes.spec.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

309 lines
21 KiB
TypeScript

import type {
ForgeCommentId, ForgeLineComment, ForgeReviewId, ForgeReviewRequest, ForgeReviewWrite, ForgeThreadId,
} from '@deepseek-ai/dsh-experimental-forge'
import { describe, expect, it } from 'vitest'
import { GitLabApi } from '../src/api.ts'
import type { GlabOutput } from '../src/glab.ts'
import type { VersionFile } from '../src/position.ts'
import { submitGitLabReview, writeGitLabReview } from '../src/writes.ts'
import type { VersionFiles } from '../src/writes.ts'
import { fakeGlab, json, merge, mrPath, output, sha } from './fixtures.ts'
import type { GlabCall } from './fixtures.ts'
const signal = new AbortController().signal
const HEAD = sha('a')
const DISCUSSION = 'f'.repeat(40) as ForgeThreadId
const REVIEW = 'draft-notes' as ForgeReviewId
const URL = 'https://gitlab.example.test/group/sub/app/-/merge_requests/6'
const file: VersionFile = { oldPath: 'src/a.ts', newPath: 'src/a.ts', diff: '@@ -1,2 +1,2 @@\n a\n-b\n+B\n' }
const files: VersionFiles = async version => version === 3 ? new Map([['src/a.ts', file]]) : undefined
const version = { id: 3, head_commit_sha: HEAD, base_commit_sha: sha('b'), start_commit_sha: sha('c') }
const comment: ForgeLineComment = { path: 'src/a.ts', side: 'R', startLine: 2, endLine: 2, body: 'Fix this' }
const position = {
position_type: 'text', base_sha: sha('b'), start_sha: sha('c'), head_sha: HEAD, old_path: 'src/a.ts', new_path: 'src/a.ts', old_line: null, new_line: 2,
}
/** A review write without the head commit, for each kind of write. */
type WriteBody = ForgeReviewWrite extends infer Write ? Write extends ForgeReviewWrite ? Omit<Write, 'headSha'> : never : never
/** One scripted answer per request, keyed by method and path below the merge request. */
type Answers = Readonly<Record<string, GlabOutput | (() => GlabOutput)>>
/** A `glab` whose merge request is at `HEAD` unless `answers` overrides a request. */
function gitlab(answers: Answers = {}) {
return fakeGlab((path, call) => {
const method = call.args[call.args.indexOf('-X') + 1] ?? 'GET'
const key = `${call.args.includes('-X') ? method : 'GET'} ${path.startsWith(mrPath(6)) ? path.slice(mrPath(6).length) : path}`
const answer = answers[key] ?? defaults[key]
if (answer === undefined) throw new Error(`unexpected request ${key}`)
return typeof answer === 'function' ? answer() : answer
})
}
const defaults: Answers = {
'GET /versions?per_page=1': json([version]),
'GET /draft_notes': json([]),
'GET /approvals': json({ user_has_approved: false }),
}
/** One write request: its method, its path below the merge request, and its parsed body when it sent one. */
type Sent = readonly [method: string, path: string, body?: unknown]
/** The write requests a run sent. */
function writes(calls: readonly GlabCall[]): Sent[] {
return calls.filter(call => call.args.includes('-X')).map((call): Sent => {
const method = call.args[call.args.indexOf('-X') + 1] ?? ''
const path = (call.args[call.args.indexOf('-X') + 2] ?? '').slice(mrPath(6).length)
if (call.input === undefined) return [method, path]
const body: unknown = JSON.parse(call.input)
return [method, path, body]
})
}
async function write(body: WriteBody, answers: Answers = {}, headSha = HEAD) {
const glab = gitlab(answers)
const result = await writeGitLabReview(new GitLabApi(glab.run), files, merge(6), { ...body, headSha }, signal)
return { result, sent: writes(glab.calls) }
}
async function submit(request: ForgeReviewRequest, answers: Answers = {}) {
const glab = gitlab(answers)
const result = await submitGitLabReview(new GitLabApi(glab.run), files, merge(6), request, signal)
return { result, sent: writes(glab.calls) }
}
const refused = (status: number, message = 'no'): GlabOutput => output(JSON.stringify({ message }), 1, `glab: ${message} (HTTP ${status})`)
/** Answer the draft note list with `first`, then with `later` on every later read. */
function listed(first: readonly { readonly id: number }[], later: GlabOutput | readonly { readonly id: number }[]): () => GlabOutput {
let reads = 0
return () => reads++ === 0 ? json(first) : Array.isArray(later) ? json(later) : later as GlabOutput
}
describe('GitLab review writes', () => {
it.each([
['a reply at once', { kind: 'reply', thread: DISCUSSION, body: 'Yes', pending: false }, ['POST', `/discussions/${DISCUSSION}/notes`, { body: 'Yes' }]],
['a pending reply', { kind: 'reply', thread: DISCUSSION, body: 'Yes', pending: true },
['POST', '/draft_notes', { note: 'Yes', in_reply_to_discussion_id: DISCUSSION }]],
['a resolution', { kind: 'resolve', thread: DISCUSSION, resolved: true }, ['PUT', `/discussions/${DISCUSSION}`, { resolved: true }]],
['an unresolution', { kind: 'resolve', thread: DISCUSSION, resolved: false }, ['PUT', `/discussions/${DISCUSSION}`, { resolved: false }]],
['an edit', { kind: 'edit-comment', comment: '12' as ForgeCommentId, body: 'New' }, ['PUT', '/notes/12', { body: 'New' }]],
['a draft edit', { kind: 'edit-comment', comment: 'draft-7' as ForgeCommentId, body: 'New' }, ['PUT', '/draft_notes/7', { note: 'New' }]],
['a deletion', { kind: 'delete-comment', comment: '12' as ForgeCommentId }, ['DELETE', '/notes/12']],
['a draft deletion', { kind: 'delete-comment', comment: 'draft-7' as ForgeCommentId }, ['DELETE', '/draft_notes/7']],
['a pending comment', { kind: 'add-pending', comment }, ['POST', '/draft_notes', { note: 'Fix this', position }]],
] as const)('sends %s through the merge request', async (_label, body, request) => {
const { result, sent } = await write(body, { [`${request[0]} ${request[1]}`]: json({ id: 1 }) })
expect(result).toEqual({ ok: true })
expect(sent).toEqual([request])
})
it('refuses a moved head and ids it never issued without writing', async () => {
await expect(write({ kind: 'resolve', thread: DISCUSSION, resolved: true }, {}, sha('9'))).resolves.toEqual({
result: { ok: false, code: 'stale', message: `the merge request head moved from ${sha('9')} to ${HEAD}` }, sent: [],
})
const unknown = { ok: false, code: 'failed', message: 'merge request 6 has no discussion or note with that id' }
for (const body of [
{ kind: 'reply', thread: 'draft-4' as ForgeThreadId, body: 'x', pending: false },
{ kind: 'resolve', thread: '../x' as ForgeThreadId, resolved: true },
{ kind: 'edit-comment', comment: '1/2' as ForgeCommentId, body: 'x' },
{ kind: 'delete-comment', comment: 'note-1' as ForgeCommentId },
] as const) {
await expect(write(body)).resolves.toEqual({ result: unknown, sent: [] })
}
await expect(write({ kind: 'resolve', thread: DISCUSSION, resolved: true }, { 'GET /versions?per_page=1': refused(500, 'down') }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'down (HTTP 500)' }, sent: [] })
})
it('refuses a comment on lines the newest version does not change', async () => {
const result = { ok: false, code: 'failed', message: 'merge request 6 does not change lines 2-2 of src/b.ts on that side' }
await expect(write({ kind: 'add-pending', comment: { ...comment, path: 'src/b.ts' } })).resolves.toEqual({ result, sent: [] })
await expect(write({ kind: 'add-pending', comment }, { 'GET /versions?per_page=1': json([{ ...version, id: 4 }]) })).resolves.toEqual({
result: { ok: false, code: 'failed', message: 'GitLab did not list the changed files of the merge request' }, sent: [],
})
})
it.each([
[refused(404), 'failed', 'no (HTTP 404)'],
[refused(401), 'failed', 'no (HTTP 401)'],
[output('', 1, 'glab: connection reset'), 'unknown', 'connection reset'],
] as const)('reports a write GitLab did not take as %s', async (answer, code, message) => {
await expect(write({ kind: 'resolve', thread: DISCUSSION, resolved: true }, { [`PUT /discussions/${DISCUSSION}`]: answer }))
.resolves.toMatchObject({ result: { ok: false, code, message } })
})
it('submits the pending review with a summary and the decision\'s review state', async () => {
const pending = { 'GET /draft_notes': json([{ id: 7 }, { id: 8 }]), 'POST /draft_notes/bulk_publish': output('') }
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'request-changes', body: 'Please fix' }, pending)).resolves.toEqual({
result: { ok: true }, sent: [['POST', '/draft_notes/bulk_publish', { note: 'Please fix', reviewer_state: 'requested_changes' }]],
})
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'comment', body: ' ' }, pending)).resolves.toEqual({
result: { ok: true }, sent: [['POST', '/draft_notes/bulk_publish', { reviewer_state: 'reviewed' }]],
})
})
it('approves at the head before it submits an approving review, unless the user already approved', async () => {
const pending = { 'GET /draft_notes': json([{ id: 7 }]), 'POST /approve': json({}), 'POST /draft_notes/bulk_publish': output('') }
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: 'LGTM' }, pending)).resolves.toEqual({
result: { ok: true }, sent: [['POST', '/approve', { sha: HEAD }], ['POST', '/draft_notes/bulk_publish', { note: 'LGTM' }]],
})
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: '' },
{ ...pending, 'GET /approvals': json({ user_has_approved: true }) })).resolves.toEqual({
result: { ok: true }, sent: [['POST', '/draft_notes/bulk_publish', {}]],
})
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: '' }, { ...pending, 'POST /approve': refused(409, 'SHA does not match') }))
.resolves.toEqual({ result: { ok: false, code: 'stale', message: 'SHA does not match (HTTP 409)' }, sent: [['POST', '/approve', { sha: HEAD }]] })
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: '' }, { ...pending, 'POST /approve': refused(403) }))
.resolves.toMatchObject({ result: { ok: false, code: 'failed' } })
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: '' }, { ...pending, 'GET /approvals': refused(500, 'down') }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'down (HTTP 500)' }, sent: [] })
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'approve', body: '' }, { ...pending, 'GET /approvals': json({}) }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'unexpected GitLab response to the approval read' }, sent: [] })
})
it('refuses to submit or discard a pending review the user no longer has', async () => {
const gone = { ok: false, code: 'failed', message: 'the pending review draft-notes is no longer the signed-in user\'s pending review' }
await expect(write({ kind: 'submit-pending', review: REVIEW, event: 'comment', body: '' })).resolves.toEqual({ result: gone, sent: [] })
await expect(write({ kind: 'discard-pending', review: 'other' as ForgeReviewId }, { 'GET /draft_notes': json([{ id: 7 }]) }))
.resolves.toEqual({ result: { ...gone, message: 'the pending review other is no longer the signed-in user\'s pending review' }, sent: [] })
await expect(write({ kind: 'discard-pending', review: REVIEW }, { 'GET /draft_notes': refused(500, 'down') }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'down (HTTP 500)' }, sent: [] })
await expect(write({ kind: 'discard-pending', review: REVIEW }, { 'GET /draft_notes': json({ drafts: [] }) }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'unexpected GitLab response to the draft note read' }, sent: [] })
})
it('discards the pending review one draft at a time and reports how far it got', async () => {
const drafts = { 'GET /draft_notes': json([{ id: 7 }, { id: 8 }]), 'DELETE /draft_notes/7': output(''), 'DELETE /draft_notes/8': output('') }
await expect(write({ kind: 'discard-pending', review: REVIEW }, drafts)).resolves.toEqual({
result: { ok: true }, sent: [['DELETE', '/draft_notes/7'], ['DELETE', '/draft_notes/8']],
})
await expect(write({ kind: 'discard-pending', review: REVIEW }, { ...drafts, 'DELETE /draft_notes/8': refused(404) })).resolves.toMatchObject({
result: { ok: false, code: 'failed', message: 'GitLab deleted 1 of 2 pending comments: no (HTTP 404)' },
})
await expect(write({ kind: 'discard-pending', review: REVIEW }, { ...drafts, 'DELETE /draft_notes/7': output('', 1, 'glab: reset') })).resolves.toMatchObject({
result: { ok: false, code: 'unknown', message: 'reset' }, sent: [['DELETE', '/draft_notes/7']],
})
})
})
describe('GitLab review submissions', () => {
it('starts a discussion for one comment and links its first note', async () => {
const started = { 'POST /discussions': json({ id: 'd', notes: [{ id: 41 }] }) }
await expect(submit({ kind: 'comment', headSha: HEAD, comment }, started)).resolves.toEqual({
result: { ok: true, url: `${URL}#note_41` }, sent: [['POST', '/discussions', { body: 'Fix this', position }]],
})
await expect(submit({ kind: 'comment', headSha: HEAD, comment }, { 'POST /discussions': output('') }))
.resolves.toMatchObject({ result: { ok: true, url: URL } })
await expect(submit({ kind: 'comment', headSha: HEAD, comment }, { 'POST /discussions': refused(400, 'line_code invalid') }))
.resolves.toMatchObject({ result: { ok: false, code: 'failed', message: 'line_code invalid (HTTP 400)' } })
await expect(submit({ kind: 'comment', headSha: HEAD, comment: { ...comment, path: 'x' } })).resolves.toMatchObject({ result: { ok: false, code: 'failed' }, sent: [] })
await expect(submit({ kind: 'comment', headSha: sha('9'), comment })).resolves.toMatchObject({ result: { ok: false, code: 'stale' }, sent: [] })
})
it('publishes a review as draft notes with its summary and review state', async () => {
let next = 70
const answers = { 'POST /draft_notes': () => json({ id: next++ }), 'POST /draft_notes/bulk_publish': output('') }
const second = { ...comment, side: 'L' as const, startLine: 2, endLine: 2, body: 'And this' }
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: [comment, second] }, answers)).resolves.toEqual({
result: { ok: true, url: URL },
sent: [
['POST', '/draft_notes', { note: 'Fix this', position }],
['POST', '/draft_notes', { note: 'And this', position: { ...position, old_line: 2, new_line: null } }],
['POST', '/draft_notes/bulk_publish', { reviewer_state: 'reviewed' }],
],
})
})
it('approves after writing the drafts and skips publishing an approval without text', async () => {
const answers = { 'POST /draft_notes': json({ id: 70 }), 'POST /approve': json({}), 'POST /draft_notes/bulk_publish': output('') }
const approved = await submit({ kind: 'review', headSha: HEAD, event: 'approve', body: 'LGTM', comments: [comment] }, answers)
expect(approved.result).toEqual({ ok: true, url: URL })
expect(approved.sent.map(([method, path]) => `${method} ${path}`)).toEqual(['POST /draft_notes', 'POST /approve', 'POST /draft_notes/bulk_publish'])
expect(approved.sent[2]?.[2]).toEqual({ note: 'LGTM' })
await expect(submit({ kind: 'review', headSha: HEAD, event: 'approve', body: '', comments: [] }, answers)).resolves.toEqual({
result: { ok: true, url: URL }, sent: [['POST', '/approve', { sha: HEAD }]],
})
})
it('deletes the drafts it wrote when GitLab refuses a draft, the approval, or the publication', async () => {
let next = 70
const base = { 'POST /draft_notes': () => json({ id: next++ }), 'DELETE /draft_notes/70': output(''), 'DELETE /draft_notes/71': output('') }
const two = [comment, { ...comment, body: 'Again' }]
next = 70
const draftRefused = await submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: two },
{ ...base, 'POST /draft_notes': () => next === 70 ? json({ id: next++ }) : refused(400, 'bad position') })
expect(draftRefused).toEqual({
result: { ok: false, code: 'failed', message: 'bad position (HTTP 400)' },
sent: [['POST', '/draft_notes', expect.anything()], ['POST', '/draft_notes', expect.anything()], ['DELETE', '/draft_notes/70']],
})
next = 70
const odd = await submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: two }, { ...base, 'POST /draft_notes': () => json({}) })
expect(odd.result).toEqual({ ok: false, code: 'unknown', message: 'unexpected GitLab response to a draft note' })
next = 70
const stale = await submit({ kind: 'review', headSha: HEAD, event: 'approve', body: '', comments: two }, { ...base, 'POST /approve': refused(409, 'SHA does not match') })
expect(stale.result).toMatchObject({ ok: false, code: 'stale' })
expect(stale.sent).toEqual(expect.arrayContaining([['DELETE', '/draft_notes/70'], ['DELETE', '/draft_notes/71']]))
next = 70
const unpublished = await submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: two },
{ ...base, 'GET /draft_notes': listed([], [{ id: 70 }, { id: 71 }]), 'POST /draft_notes/bulk_publish': refused(403) })
expect(unpublished.result).toEqual({ ok: false, code: 'failed', message: 'no (HTTP 403)' })
expect(unpublished.sent).toEqual(expect.arrayContaining([['DELETE', '/draft_notes/70'], ['DELETE', '/draft_notes/71']]))
next = 70
const uncertain = await submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: two },
{ ...base, 'POST /draft_notes/bulk_publish': output('', 1, 'glab: timeout') })
expect(uncertain.result).toEqual({ ok: false, code: 'unknown', message: 'timeout' })
expect(uncertain.sent).not.toContainEqual(['DELETE', '/draft_notes/70'])
})
it('keeps the drafts and asks for a check when GitLab may have published them before refusing the publication', async () => {
let next = 70
const two = [comment, { ...comment, body: 'Again' }]
const answers = (after: GlabOutput) => ({
'POST /draft_notes': () => json({ id: next++ }), 'GET /draft_notes': listed([], after), 'POST /draft_notes/bulk_publish': refused(422, 'Note is too long'),
})
const message = 'Note is too long (HTTP 422); GitLab may have published the comments of this review, so check the merge request before you submit again'
for (const after of [json([]), json([{ id: 71 }]), refused(500, 'down')]) {
next = 70
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: 'x'.repeat(8), comments: two }, answers(after))).resolves.toEqual({
result: { ok: false, code: 'unknown', message },
sent: [['POST', '/draft_notes', expect.anything()], ['POST', '/draft_notes', expect.anything()], ['POST', '/draft_notes/bulk_publish', expect.anything()]],
})
}
})
it('reports the drafts a rollback could not delete', async () => {
let next = 70
const two = [comment, { ...comment, body: 'Again' }]
const result = await submit({ kind: 'review', headSha: HEAD, event: 'approve', body: '', comments: two }, {
'POST /draft_notes': () => json({ id: next++ }), 'POST /approve': refused(409, 'SHA does not match'),
'DELETE /draft_notes/70': refused(500, 'down'), 'DELETE /draft_notes/71': output(''),
})
expect(result.result).toEqual({
ok: false, code: 'unknown',
message: 'SHA does not match (HTTP 409); 1 pending comment of yours may remain on merge request 6, so check its pending comments before you submit again',
})
next = 70
const both = await submit({ kind: 'review', headSha: HEAD, event: 'approve', body: '', comments: two }, {
'POST /draft_notes': () => json({ id: next++ }), 'POST /approve': refused(409, 'SHA does not match'),
'DELETE /draft_notes/70': refused(500, 'down'), 'DELETE /draft_notes/71': output('', 1, 'glab: timeout'),
})
expect(both.result.ok ? '' : both.result.message).toContain('2 pending comments of yours may remain')
expect(both.result).toMatchObject({ code: 'unknown' })
})
it('refuses a review while the user has drafts, an empty comment review, and a comment it cannot place', async () => {
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: 'x', comments: [] }, { 'GET /draft_notes': json([{ id: 7 }]) })).resolves.toEqual({
result: { ok: false, code: 'failed', message: 'GitLab holds pending comments of yours on this merge request; submit or discard them first' }, sent: [],
})
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: ' ', comments: [] })).resolves.toEqual({
result: { ok: false, code: 'failed', message: 'A review without a decision needs a comment or a summary' }, sent: [],
})
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: '', comments: [comment, { ...comment, path: 'x' }] }))
.resolves.toMatchObject({ result: { ok: false, code: 'failed' }, sent: [] })
await expect(submit({ kind: 'review', headSha: HEAD, event: 'comment', body: 'x', comments: [] }, { 'GET /draft_notes': refused(500, 'down') }))
.resolves.toEqual({ result: { ok: false, code: 'failed', message: 'down (HTTP 500)' }, sent: [] })
await expect(submit({ kind: 'review', headSha: sha('9'), event: 'comment', body: 'x', comments: [] })).resolves.toMatchObject({ result: { ok: false, code: 'stale' } })
})
})