Skip to content

Commit d7126ba

Browse files
QuiiBzJounQin
andauthored
feat: add GITLAB_COMMENT_TYPE env variable to use discussions or notes (#98)
Co-authored-by: JounQin <admin@1stg.me>
1 parent e35b586 commit d7126ba

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

.changeset/thin-foxes-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'changesets-gitlab': minor
3+
---
4+
5+
feat: add new `GITLAB_COMMENT_TYPE` environment variable to use discussions or notes

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ GITLAB_TOKEN # required, token with accessibility to push
5252
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
5353
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
5454
GITLAB_CI_USER_EMAIL # optional, default `gitlab[bot]@users.noreply.gitlab.com`
55+
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
5556
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
5657
```
5758

src/comment.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,38 @@ const hasChangesetBeenAdded = (
141141
),
142142
)
143143

144+
const getCommentFunctions = (api: Gitlab, commentType: string) => {
145+
if (commentType === 'discussion') {
146+
return {
147+
editComment: api.MergeRequestDiscussions.editNote.bind(
148+
api.MergeRequestDiscussions,
149+
),
150+
createComment: api.MergeRequestDiscussions.create.bind(
151+
api.MergeRequestDiscussions,
152+
),
153+
}
154+
}
155+
156+
if (commentType === 'note') {
157+
return {
158+
editComment: api.MergeRequestNotes.edit.bind(api.MergeRequestNotes),
159+
createComment: api.MergeRequestNotes.create.bind(api.MergeRequestNotes),
160+
}
161+
}
162+
163+
throw new Error(
164+
`Invalid comment type "${commentType}", should be "discussion" or "note"`,
165+
)
166+
}
167+
144168
export const comment = async () => {
145169
const {
146170
CI_MERGE_REQUEST_IID,
147171
CI_MERGE_REQUEST_PROJECT_URL,
148172
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: mrBranch,
149173
CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
150174
CI_MERGE_REQUEST_TITLE,
175+
GITLAB_COMMENT_TYPE = 'discussion',
151176
} = process.env
152177

153178
if (!mrBranch) {
@@ -211,8 +236,13 @@ export const comment = async () => {
211236
: getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
212237
errFromFetchingChangedFiles
213238

239+
const { editComment, createComment } = getCommentFunctions(
240+
api,
241+
GITLAB_COMMENT_TYPE,
242+
)
243+
214244
if (noteInfo != null) {
215-
return api.MergeRequestDiscussions.editNote(
245+
return editComment(
216246
context.projectId,
217247
mrIid,
218248
// @ts-expect-error - https://github.com/jdalrymple/gitbeaker/pull/523#issuecomment-975276068
@@ -223,11 +253,7 @@ export const comment = async () => {
223253
},
224254
)
225255
}
226-
return api.MergeRequestDiscussions.create(
227-
context.projectId,
228-
mrIid,
229-
prComment,
230-
)
256+
return createComment(context.projectId, mrIid, prComment)
231257
} catch (err: unknown) {
232258
console.error(err)
233259
throw err

0 commit comments

Comments
 (0)