From abb734a42006f603c00d4d31e7bfbbe5c26873b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Fencl?= Date: Tue, 3 Dec 2024 15:59:30 +0100 Subject: [PATCH 1/3] Add GITLAB_COMMENT_DISCUSSION_AUTORESOLVE optional environment --- .changeset/swift-berries-exist.md | 5 +++++ README.md | 15 ++++++++------- src/comment.ts | 3 +++ src/types.ts | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 .changeset/swift-berries-exist.md diff --git a/.changeset/swift-berries-exist.md b/.changeset/swift-berries-exist.md new file mode 100644 index 00000000..b8bc532b --- /dev/null +++ b/.changeset/swift-berries-exist.md @@ -0,0 +1,5 @@ +--- +'changesets-gitlab': minor +--- + +Add GITLAB_COMMENT_DISCUSSION_AUTORESOLVE optional environment variable to automaticly resolve added discussion with options to resolve discussion by default (value `all`) or resolve only when changeset is present (value `hasChangeset`) diff --git a/README.md b/README.md index 50683cf2..3d6bd288 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,14 @@ GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests GITLAB_HOST # optional, if you're using custom GitLab host, will fallback to `CI_SERVER_URL` if not provided -GITLAB_TOKEN # required, token with accessibility to push -GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_TOKEN. defaults to personal access token. can be `job` if you provide the Gitlab CI_JOB_TOKEN or `oauth` if you use Gitlab Oauth token -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_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI -DEBUG_GITLAB_CREDENTIAL # optional, default `false` +GITLAB_TOKEN # required, token with accessibility to push +GITLAB_TOKEN_TYPE # optional, type of the provided token in GITLAB_TOKEN. defaults to personal access token. can be `job` if you provide the Gitlab CI_JOB_TOKEN or `oauth` if you use Gitlab Oauth token +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_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI +GITLAB_COMMENT_DISCUSSION_AUTORESOLVE # optional, automaticly resolve added discussion with options to resolve discussion by default (value `all`) or resolve only when changeset is present (value `hasChangeset`) +DEBUG_GITLAB_CREDENTIAL # optional, default `false` ``` ### Example workflow diff --git a/src/comment.ts b/src/comment.ts index 5dea359a..702b129f 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -251,6 +251,8 @@ export const comment = async () => { CI_MERGE_REQUEST_SOURCE_BRANCH_SHA, CI_MERGE_REQUEST_TITLE, GITLAB_COMMENT_TYPE, + GITLAB_COMMENT_DISCUSSION_AUTORESOLVE, + GITLAB_COMMENT_DISCUSSION_AUTORESOLVE_ONLY_CHANGESET_EXISTS, GITLAB_ADD_CHANGESET_MESSAGE, } = env @@ -321,6 +323,7 @@ export const comment = async () => { noteInfo.noteId, { body: prComment, + resolved: GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'always' || (GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'hasChangeset' && hasChangeset), }, ) } diff --git a/src/types.ts b/src/types.ts index dd3932aa..3561bd17 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,6 +20,7 @@ export type Env = GitLabCIPredefinedVariables & GITLAB_CI_USER_NAME?: string GITLAB_CI_USER_EMAIL: string GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'> + GITLAB_COMMENT_DISCUSSION_AUTORESOLVE: LooseString<'always' | 'hasChangeset'> GITLAB_ADD_CHANGESET_MESSAGE?: string DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'> From a57a2dfc247408f6be36664f0d9647d5e8b0783b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Fencl?= Date: Tue, 3 Dec 2024 21:33:37 +0100 Subject: [PATCH 2/3] Use MergeRequestDiscussions.resolve --- src/comment.ts | 15 +++++++++++++-- src/types.ts | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index 702b129f..3309fad1 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -252,7 +252,6 @@ export const comment = async () => { CI_MERGE_REQUEST_TITLE, GITLAB_COMMENT_TYPE, GITLAB_COMMENT_DISCUSSION_AUTORESOLVE, - GITLAB_COMMENT_DISCUSSION_AUTORESOLVE_ONLY_CHANGESET_EXISTS, GITLAB_ADD_CHANGESET_MESSAGE, } = env @@ -316,6 +315,19 @@ export const comment = async () => { switch (GITLAB_COMMENT_TYPE) { case 'discussion': { if (noteInfo) { + if ( + GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'always' || + (GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'hasChangeset' && + hasChangeset) + ) { + await api.MergeRequestDiscussions.resolve( + context.projectId, + mrIid, + noteInfo.discussionId, + true, + ) + } + return api.MergeRequestDiscussions.editNote( context.projectId, mrIid, @@ -323,7 +335,6 @@ export const comment = async () => { noteInfo.noteId, { body: prComment, - resolved: GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'always' || (GITLAB_COMMENT_DISCUSSION_AUTORESOLVE === 'hasChangeset' && hasChangeset), }, ) } diff --git a/src/types.ts b/src/types.ts index 3561bd17..be5532a7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,7 +20,9 @@ export type Env = GitLabCIPredefinedVariables & GITLAB_CI_USER_NAME?: string GITLAB_CI_USER_EMAIL: string GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'> - GITLAB_COMMENT_DISCUSSION_AUTORESOLVE: LooseString<'always' | 'hasChangeset'> + GITLAB_COMMENT_DISCUSSION_AUTORESOLVE: LooseString< + 'always' | 'hasChangeset' + > GITLAB_ADD_CHANGESET_MESSAGE?: string DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'> From e57dece6e3fd2667c4265a691349fe056e976de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Fencl?= Date: Tue, 3 Dec 2024 21:34:57 +0100 Subject: [PATCH 3/3] Fix value in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d6bd288..1c5c8ba6 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ GITLAB_CI_USER_NAME # optional, username with accessibility to 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_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI -GITLAB_COMMENT_DISCUSSION_AUTORESOLVE # optional, automaticly resolve added discussion with options to resolve discussion by default (value `all`) or resolve only when changeset is present (value `hasChangeset`) +GITLAB_COMMENT_DISCUSSION_AUTORESOLVE # optional, automaticly resolve added discussion with options to resolve discussion by default (value `always`) or resolve only when changeset is present (value `hasChangeset`) DEBUG_GITLAB_CREDENTIAL # optional, default `false` ```