Skip to content

Commit 7538fa1

Browse files
authored
feat: create comment with discussion instead of note (#27)
close #16
1 parent 1cfe49e commit 7538fa1

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

.changeset/eight-buttons-argue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"changesets-gitlab": minor
3+
---
4+
5+
feat: use discussion instead of comment

src/comment.ts

+40-18
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,31 @@ ${changedPackages.map(x => `"${x}": patch`).join('\n')}
9999
${title}
100100
`)
101101

102-
const getCommentId = (api: Gitlab, mrIid: number | string) =>
103-
api.MergeRequestNotes.all(context.projectId, mrIid).then(comments => {
104-
const changesetBotComment = comments.find(
105-
comment =>
106-
comment.author.username === process.env.GITLAB_CI_USER_NAME &&
107-
// We need to ensure the comment is generated by us but we don't have a app bot like GitHub
108-
// @see https://github.com/apps/changeset-bot
109-
comment.body.includes('Generated By Changesets GitLab Bot'),
110-
)
111-
return changesetBotComment ? changesetBotComment.id : null
112-
})
102+
const getNoteInfo = (api: Gitlab, mrIid: number | string) =>
103+
api.MergeRequestDiscussions.all(context.projectId, mrIid).then(
104+
discussions => {
105+
for (const discussion of discussions) {
106+
if (!discussion.notes) {
107+
continue
108+
}
109+
110+
const changesetBotNote = discussion.notes.find(
111+
note =>
112+
note.author.username === process.env.GITLAB_CI_USER_NAME &&
113+
// We need to ensure the note is generated by us but we don't have a app bot like GitHub
114+
// @see https://github.com/apps/changeset-bot
115+
note.body.includes('Generated By Changesets GitLab Bot'),
116+
)
117+
118+
if (changesetBotNote) {
119+
return {
120+
discussionId: discussion.id,
121+
noteId: changesetBotNote.id,
122+
}
123+
}
124+
}
125+
},
126+
)
113127

114128
const hasChangesetBeenAdded = (
115129
changedFilesPromise: ReturnType<MergeRequests['changes']>,
@@ -154,9 +168,9 @@ export const comment = async () => {
154168
mrIid,
155169
)
156170

157-
const [commentId, hasChangeset, { changedPackages, releasePlan }] =
171+
const [noteInfo, hasChangeset, { changedPackages, releasePlan }] =
158172
await Promise.all([
159-
getCommentId(api, mrIid),
173+
getNoteInfo(api, mrIid),
160174
hasChangesetBeenAdded(changedFilesPromise),
161175
getChangedPackages({
162176
changedFiles: changedFilesPromise.then(x =>
@@ -193,15 +207,23 @@ export const comment = async () => {
193207
: getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
194208
errFromFetchingChangedFiles
195209

196-
if (commentId != null) {
197-
return api.MergeRequestNotes.edit(
210+
if (noteInfo != null) {
211+
return api.MergeRequestDiscussions.editNote(
198212
context.projectId,
199213
mrIid,
200-
commentId,
201-
prComment,
214+
// @ts-expect-error - https://github.com/jdalrymple/gitbeaker/pull/523#issuecomment-975276068
215+
noteInfo.discussionId,
216+
noteInfo.noteId,
217+
{
218+
body: prComment,
219+
},
202220
)
203221
}
204-
return api.MergeRequestNotes.create(context.projectId, mrIid, prComment)
222+
return api.MergeRequestDiscussions.create(
223+
context.projectId,
224+
mrIid,
225+
prComment,
226+
)
205227
} catch (err: unknown) {
206228
console.error(err)
207229
throw err

0 commit comments

Comments
 (0)