From 71e09c6c9f5da9e37c08a7ad573ab3e8a4cabe34 Mon Sep 17 00:00:00 2001 From: Bart Schriever Date: Wed, 11 Nov 2026 19:39:21 +0100 Subject: [PATCH 1/4] fix: resolve merge request discussion when a changeset is present The discussion was only resolved when it already existed, so the run that created it left it open. Resolve it on creation too, and reopen it when the changeset is removed. GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE still disables this. Co-authored-by: Cursor --- .changeset/close-discussion-with-changeset.md | 5 + README.md | 2 +- src/comment.ts | 24 +- test/comment.spec.ts | 208 ++++++++++++++++++ 4 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 .changeset/close-discussion-with-changeset.md create mode 100644 test/comment.spec.ts diff --git a/.changeset/close-discussion-with-changeset.md b/.changeset/close-discussion-with-changeset.md new file mode 100644 index 00000000..7ed5c7ef --- /dev/null +++ b/.changeset/close-discussion-with-changeset.md @@ -0,0 +1,5 @@ +--- +"changesets-gitlab": patch +--- + +**Fixed** Close the merge request discussion on the same run that finds a changeset, including the run that creates the discussion, and reopen it when the changeset is removed. diff --git a/README.md b/README.md index f8338bcc..acdc91e5 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_CI_USER_NAME # optional, username with accessibility to push, used in pairs of the above token (if it was personal access token). If not set read it from the Gitlab API GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com` GITLAB_COMMENT_TYPE # optional, type of the comment. defaults to `discussion`. can be set to `note` to not create a discussion instead of a thread -GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE # optional, automatically resolve added discussion when changeset is present, if you want to always resolve the discussion, you should actually use `GITLAB_COMMENT_TYPE=note` instead, default `true` +GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE # optional, resolve the discussion when a changeset is present (including the run that creates it) and reopen it when the changeset is removed. To avoid a resolvable thread, use `GITLAB_COMMENT_TYPE=note` instead. Default `true` GITLAB_COMMENT_CUSTOM_LINKS # optional, override the links content referenced in the cli bot comment, use {{ addChangesetUrl }} placeholder for the dynamic URL to add a changeset GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI DEBUG_GITLAB_CREDENTIAL # optional, set to `1`/`true` to echo the remote URL when debugging git authentication; WARNING: the remote URL may contain credentials diff --git a/src/comment.ts b/src/comment.ts index f9389e3b..1757e484 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -377,16 +377,17 @@ export const comment = async () => { switch (commentType) { case 'discussion': { + const autoResolve = TRUTHY_VALUES.has( + env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE || '1', + ) + if (noteInfo) { - if ( - hasChangeset && - TRUTHY_VALUES.has(env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE || '1') - ) { + if (autoResolve) { await api.MergeRequestDiscussions.resolve( context.projectId, mrIid, noteInfo.discussionId, - true, + hasChangeset, ) } @@ -401,11 +402,22 @@ export const comment = async () => { ) } - return api.MergeRequestDiscussions.create( + const discussion = await api.MergeRequestDiscussions.create( context.projectId, mrIid, prComment, ) + + if (hasChangeset && autoResolve) { + await api.MergeRequestDiscussions.resolve( + context.projectId, + mrIid, + discussion.id, + true, + ) + } + + return discussion } case 'note': { if (noteInfo) { diff --git a/test/comment.spec.ts b/test/comment.spec.ts new file mode 100644 index 00000000..01e5a89a --- /dev/null +++ b/test/comment.spec.ts @@ -0,0 +1,208 @@ +const { MR_IID, PROJECT_ID, mockApi } = vi.hoisted(() => { + const PROJECT_ID = '42' + const MR_IID = 12 + // `src/env.ts` and `src/context.ts` capture CI variables at module load. + process.env.CI_PROJECT_ID = PROJECT_ID + process.env.CI_MERGE_REQUEST_IID = String(MR_IID) + process.env.CI_MERGE_REQUEST_PROJECT_URL = + 'https://gitlab.example/group/project' + process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME = 'feature' + process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA = 'abc123' + process.env.CI_MERGE_REQUEST_TITLE = 'Add feature' + process.env.GITLAB_CI_USER_NAME = 'ci-bot' + process.env.GITLAB_TOKEN = 'glpat-token' + process.env.GITLAB_COMMENT_TYPE = 'discussion' + process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = '1' + return { + MR_IID, + PROJECT_ID, + mockApi: { + MergeRequests: { + allDiffs: vi.fn(), + }, + MergeRequestDiscussions: { + all: vi.fn(), + create: vi.fn(), + editNote: vi.fn(), + resolve: vi.fn(), + }, + MergeRequestNotes: { + all: vi.fn(), + create: vi.fn(), + edit: vi.fn(), + }, + }, + } +}) + +vi.mock('../src/api.ts', () => ({ + createApi: () => mockApi, +})) + +vi.mock('../src/get-changed-packages.js', () => ({ + getChangedPackages: () => + Promise.resolve({ + changedPackages: ['pkg'], + releasePlan: { + changesets: [], + releases: [], + }, + }), +})) + +const { comment } = await import('../src/comment.js') + +const DISCUSSION_ID = 'disc-new' +const EXISTING_DISCUSSION_ID = 'disc-existing' + +const changesetDiff = { + new_file: true, + new_path: '.changeset/brave-lions-sing.md', +} + +const existingDiscussion = { + id: EXISTING_DISCUSSION_ID, + notes: [ + { + id: 11, + author: { username: 'ci-bot' }, + body: 'previous\n__Generated By Changesets GitLab Bot__', + }, + ], +} + +const originalEnv = { ...process.env } + +beforeEach(() => { + vi.clearAllMocks() + + mockApi.MergeRequests.allDiffs.mockResolvedValue([]) + mockApi.MergeRequestDiscussions.all.mockResolvedValue([]) + mockApi.MergeRequestDiscussions.create.mockResolvedValue({ + id: DISCUSSION_ID, + }) + mockApi.MergeRequestDiscussions.editNote.mockResolvedValue({ + id: existingDiscussion.notes[0].id, + }) + mockApi.MergeRequestDiscussions.resolve.mockResolvedValue({ + id: DISCUSSION_ID, + }) + mockApi.MergeRequestNotes.all.mockResolvedValue([]) + mockApi.MergeRequestNotes.create.mockResolvedValue({ id: 99 }) + mockApi.MergeRequestNotes.edit.mockResolvedValue({ id: 99 }) +}) + +afterAll(() => { + process.env = { ...originalEnv } +}) + +describe('comment', () => { + test('creates the discussion and resolves it when a changeset is already present', async () => { + mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) + + await comment() + + expect(mockApi.MergeRequestDiscussions.create).toHaveBeenCalledOnce() + expect(mockApi.MergeRequestDiscussions.create).toHaveBeenCalledWith( + PROJECT_ID, + MR_IID, + expect.stringContaining('Changeset detected'), + ) + expect(mockApi.MergeRequestDiscussions.resolve).toHaveBeenCalledWith( + PROJECT_ID, + MR_IID, + DISCUSSION_ID, + true, + ) + expect(mockApi.MergeRequestDiscussions.create).toHaveBeenCalledBefore( + mockApi.MergeRequestDiscussions.resolve, + ) + }) + + test('creates an open discussion when the merge request has no changeset', async () => { + await comment() + + expect(mockApi.MergeRequestDiscussions.create).toHaveBeenCalledWith( + PROJECT_ID, + MR_IID, + expect.stringContaining('No Changeset found'), + ) + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + }) + + test('resolves an existing discussion when a changeset is present', async () => { + mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) + mockApi.MergeRequestDiscussions.all.mockResolvedValue([existingDiscussion]) + + await comment() + + expect(mockApi.MergeRequestDiscussions.create).not.toHaveBeenCalled() + expect(mockApi.MergeRequestDiscussions.resolve).toHaveBeenCalledWith( + PROJECT_ID, + MR_IID, + EXISTING_DISCUSSION_ID, + true, + ) + expect(mockApi.MergeRequestDiscussions.resolve).toHaveBeenCalledBefore( + mockApi.MergeRequestDiscussions.editNote, + ) + }) + + test('reopens an existing discussion when the changeset is removed', async () => { + mockApi.MergeRequestDiscussions.all.mockResolvedValue([existingDiscussion]) + + await comment() + + expect(mockApi.MergeRequestDiscussions.resolve).toHaveBeenCalledWith( + PROJECT_ID, + MR_IID, + EXISTING_DISCUSSION_ID, + false, + ) + const editCall = mockApi.MergeRequestDiscussions.editNote.mock.calls[0] + expect(editCall[0]).toBe(PROJECT_ID) + expect(editCall[1]).toBe(MR_IID) + expect(editCall[2]).toBe(EXISTING_DISCUSSION_ID) + expect(editCall[3]).toBe(existingDiscussion.notes[0].id) + expect(JSON.stringify(editCall[4])).toContain('No Changeset found') + }) + + test('does not resolve a new discussion when auto-resolve is disabled', async () => { + process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = 'false' + vi.resetModules() + const { comment: commentWithoutAutoResolve } = + await import('../src/comment.js') + mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) + + await commentWithoutAutoResolve() + + expect(mockApi.MergeRequestDiscussions.create).toHaveBeenCalledOnce() + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + }) + + test('does not reopen an existing discussion when auto-resolve is disabled', async () => { + process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = 'false' + vi.resetModules() + const { comment: commentWithoutAutoResolve } = + await import('../src/comment.js') + mockApi.MergeRequestDiscussions.all.mockResolvedValue([existingDiscussion]) + + await commentWithoutAutoResolve() + + expect(mockApi.MergeRequestDiscussions.editNote).toHaveBeenCalledOnce() + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + }) + + test('posts a note instead of a discussion when the comment type is note', async () => { + process.env.GITLAB_COMMENT_TYPE = 'note' + vi.resetModules() + const { comment: commentAsNote } = await import('../src/comment.js') + mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) + + await commentAsNote() + + expect(mockApi.MergeRequestNotes.create).toHaveBeenCalledOnce() + expect(mockApi.MergeRequestDiscussions.create).not.toHaveBeenCalled() + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + }) +}) From 08071515ec5997dac8f733ee88595718c6e490fa Mon Sep 17 00:00:00 2001 From: Bart Schriever Date: Wed, 11 Nov 2026 19:52:50 +0100 Subject: [PATCH 2/4] fix: skip resolving individual notes that cannot be reopened After switching from note to discussion comments, the selected comment can be an individual note. Resolving it with resolved: false is rejected by GitLab before the note body is updated, so skip that call for individual notes when no changeset exists. Co-authored-by: Cursor --- src/comment.ts | 25 ++++++++++++++++++++++--- test/comment.spec.ts | 12 ++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index 1757e484..a4fa213e 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -162,7 +162,11 @@ async function getNoteInfo( mrIid: number, commentType: LooseString<'discussion'>, random?: boolean, -): Promise<{ discussionId: string; noteId: number } | null | undefined> +): Promise< + | { discussionId: string; individualNote: boolean; noteId: number } + | null + | undefined +> async function getNoteInfo( api: Gitlab, mrIid: number, @@ -175,7 +179,7 @@ async function getNoteInfo( commentType: LooseString<'discussion' | 'note'>, random?: boolean, ): Promise< - | { discussionId: string; noteId: number } + | { discussionId: string; individualNote: boolean; noteId: number } | { noteId: number } | null | undefined @@ -207,6 +211,7 @@ async function getNoteInfo( if (changesetBotNote) { return { discussionId: discussionOrNote.id, + individualNote: discussionOrNote.individual_note, noteId: changesetBotNote.id, } } @@ -348,6 +353,14 @@ ${newChangesetTemplate} return { body: prComment, hasChangeset } } +// Individual notes cannot be reopened. GitLab rejects `resolved: false` and +// the comment update never runs. +const shouldResolveDiscussion = ( + autoResolve: boolean, + hasChangeset: boolean, + individualNote: boolean, +) => autoResolve && (hasChangeset || !individualNote) + export const comment = async () => { const mrBranch = env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME if (!mrBranch) { @@ -382,7 +395,13 @@ export const comment = async () => { ) if (noteInfo) { - if (autoResolve) { + if ( + shouldResolveDiscussion( + autoResolve, + hasChangeset, + noteInfo.individualNote, + ) + ) { await api.MergeRequestDiscussions.resolve( context.projectId, mrIid, diff --git a/test/comment.spec.ts b/test/comment.spec.ts index 01e5a89a..0b66d303 100644 --- a/test/comment.spec.ts +++ b/test/comment.spec.ts @@ -62,6 +62,7 @@ const changesetDiff = { const existingDiscussion = { id: EXISTING_DISCUSSION_ID, + individual_note: false, notes: [ { id: 11, @@ -167,6 +168,17 @@ describe('comment', () => { expect(JSON.stringify(editCall[4])).toContain('No Changeset found') }) + test('updates an individual note without reopening it when no changeset exists', async () => { + mockApi.MergeRequestDiscussions.all.mockResolvedValue([ + { ...existingDiscussion, individual_note: true }, + ]) + + await comment() + + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + expect(mockApi.MergeRequestDiscussions.editNote).toHaveBeenCalledOnce() + }) + test('does not resolve a new discussion when auto-resolve is disabled', async () => { process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = 'false' vi.resetModules() From c7e318b3ee551157f36b4be519f460e7c3163cad Mon Sep 17 00:00:00 2001 From: Bart Schriever Date: Thu, 12 Nov 2026 09:09:06 +0100 Subject: [PATCH 3/4] fix: update handling of individual notes in comment logic --- src/comment.ts | 15 ++++----------- test/comment.spec.ts | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index a4fa213e..a583fbca 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -353,13 +353,12 @@ ${newChangesetTemplate} return { body: prComment, hasChangeset } } -// Individual notes cannot be reopened. GitLab rejects `resolved: false` and -// the comment update never runs. +// Individual notes are never resolvable: GitLab rejects both resolving and +// reopening them, and the comment update never runs. const shouldResolveDiscussion = ( autoResolve: boolean, - hasChangeset: boolean, individualNote: boolean, -) => autoResolve && (hasChangeset || !individualNote) +) => autoResolve && !individualNote export const comment = async () => { const mrBranch = env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME @@ -395,13 +394,7 @@ export const comment = async () => { ) if (noteInfo) { - if ( - shouldResolveDiscussion( - autoResolve, - hasChangeset, - noteInfo.individualNote, - ) - ) { + if (shouldResolveDiscussion(autoResolve, noteInfo.individualNote)) { await api.MergeRequestDiscussions.resolve( context.projectId, mrIid, diff --git a/test/comment.spec.ts b/test/comment.spec.ts index 0b66d303..9bfb697c 100644 --- a/test/comment.spec.ts +++ b/test/comment.spec.ts @@ -76,6 +76,7 @@ const originalEnv = { ...process.env } beforeEach(() => { vi.clearAllMocks() + vi.unstubAllEnvs() mockApi.MergeRequests.allDiffs.mockResolvedValue([]) mockApi.MergeRequestDiscussions.all.mockResolvedValue([]) @@ -179,8 +180,23 @@ describe('comment', () => { expect(mockApi.MergeRequestDiscussions.editNote).toHaveBeenCalledOnce() }) + test('updates an individual note without resolving it when a changeset is present', async () => { + mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) + mockApi.MergeRequestDiscussions.all.mockResolvedValue([ + { ...existingDiscussion, individual_note: true }, + ]) + + await comment() + + expect(mockApi.MergeRequestDiscussions.resolve).not.toHaveBeenCalled() + expect(mockApi.MergeRequestDiscussions.editNote).toHaveBeenCalledOnce() + expect( + JSON.stringify(mockApi.MergeRequestDiscussions.editNote.mock.calls[0]), + ).toContain('Changeset detected') + }) + test('does not resolve a new discussion when auto-resolve is disabled', async () => { - process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = 'false' + vi.stubEnv('GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE', 'false') vi.resetModules() const { comment: commentWithoutAutoResolve } = await import('../src/comment.js') @@ -193,7 +209,7 @@ describe('comment', () => { }) test('does not reopen an existing discussion when auto-resolve is disabled', async () => { - process.env.GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE = 'false' + vi.stubEnv('GITLAB_COMMENT_DISCUSSION_AUTO_RESOLVE', 'false') vi.resetModules() const { comment: commentWithoutAutoResolve } = await import('../src/comment.js') @@ -206,7 +222,7 @@ describe('comment', () => { }) test('posts a note instead of a discussion when the comment type is note', async () => { - process.env.GITLAB_COMMENT_TYPE = 'note' + vi.stubEnv('GITLAB_COMMENT_TYPE', 'note') vi.resetModules() const { comment: commentAsNote } = await import('../src/comment.js') mockApi.MergeRequests.allDiffs.mockResolvedValue([changesetDiff]) From 73d2c59c3bcaf0727c0c2bd02b77332dad39c3b9 Mon Sep 17 00:00:00 2001 From: Bart Schriever Date: Thu, 12 Nov 2026 10:00:58 +0100 Subject: [PATCH 4/4] fix: simplify individual notes handling in comment logic --- src/comment.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index a583fbca..8b4cdb4e 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -353,13 +353,7 @@ ${newChangesetTemplate} return { body: prComment, hasChangeset } } -// Individual notes are never resolvable: GitLab rejects both resolving and -// reopening them, and the comment update never runs. -const shouldResolveDiscussion = ( - autoResolve: boolean, - individualNote: boolean, -) => autoResolve && !individualNote - +// eslint-disable-next-line sonarjs/cognitive-complexity export const comment = async () => { const mrBranch = env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME if (!mrBranch) { @@ -394,7 +388,9 @@ export const comment = async () => { ) if (noteInfo) { - if (shouldResolveDiscussion(autoResolve, noteInfo.individualNote)) { + // Individual notes are never resolvable: GitLab rejects both + // resolving and reopening them, and the comment update never runs. + if (autoResolve && !noteInfo.individualNote) { await api.MergeRequestDiscussions.resolve( context.projectId, mrIid,