From bb24f3792d6ab6cb2ef4ac68497c46efdf283aa2 Mon Sep 17 00:00:00 2001 From: JounQin Date: Mon, 18 Dec 2023 22:50:13 +0800 Subject: [PATCH] fix: comment username could be random close #145 --- src/comment.ts | 93 +++++++++++++++++++++++++++++++------------------- src/env.ts | 2 +- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index 74442992..c4212287 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -117,11 +117,16 @@ const isMrNote = ( 'noteable_type' in discussionOrNote && discussionOrNote.noteable_type === 'Merge request' +const RANDOM_BOT_NAME_PATTERN = /^((?:project|group)_\d+_bot)_\w+$/ + const isChangesetBotNote = ( note: DiscussionNoteSchema | NoteSchema, username: string, + random?: boolean, ) => - note.author.username === username && + (note.author.username === username || + (random && + note.author.username.match(RANDOM_BOT_NAME_PATTERN)?.[1] === username)) && // We need to ensure the note is generated by us, but we don't have an app bot like GitHub // @see https://github.com/apps/changeset-bot note.body.includes(generatedByBotNote) @@ -130,20 +135,26 @@ async function getNoteInfo( api: Gitlab, mrIid: number | string, commentType: LooseString<'discussion'>, -): Promise<{ discussionId: string; noteId: number } | undefined> + random?: boolean, +): Promise<{ discussionId: string; noteId: number } | null | undefined> async function getNoteInfo( api: Gitlab, mrIid: number | string, commentType: LooseString<'note'>, -): Promise<{ noteId: number } | undefined> + random?: boolean, +): Promise<{ noteId: number } | null | undefined> async function getNoteInfo( api: Gitlab, mrIid: number | string, commentType: LooseString<'discussion' | 'note'>, -) { - const isDiscussion = commentType === 'discussion' - - const discussionOrNotes = await (isDiscussion + random?: boolean, +): Promise< + | { discussionId: string; noteId: number } + | { noteId: number } + | null + | undefined +> { + const discussionOrNotes = await (commentType === 'discussion' ? api.MergeRequestDiscussions.all(context.projectId, mrIid) : api.MergeRequestNotes.all(context.projectId, +mrIid)) @@ -151,7 +162,7 @@ async function getNoteInfo( for (const discussionOrNote of discussionOrNotes) { if (isMrNote(discussionOrNote)) { - if (isChangesetBotNote(discussionOrNote, username)) { + if (isChangesetBotNote(discussionOrNote, username, random)) { return { noteId: discussionOrNote.id, } @@ -174,6 +185,14 @@ async function getNoteInfo( } } } + + /** + * The `username` used for commenting could be random, if we haven't tested the random `username`, then test it + * + * @see https://docs.gitlab.com/ee/development/internal_users.html + * @see https://github.com/un-ts/changesets-gitlab/issues/145#issuecomment-1860610958 + */ + return random ? null : getNoteInfo(api, mrIid, commentType, true) } const hasChangesetBeenAdded = async ( @@ -262,42 +281,44 @@ export const comment = async () => { : getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) + errFromFetchingChangedFiles - if (GITLAB_COMMENT_TYPE === 'discussion') { - if (noteInfo) { - return api.MergeRequestDiscussions.editNote( + switch (GITLAB_COMMENT_TYPE) { + case 'discussion': { + if (noteInfo) { + return api.MergeRequestDiscussions.editNote( + context.projectId, + mrIid, + noteInfo.discussionId, + noteInfo.noteId, + { + body: prComment, + }, + ) + } + + return api.MergeRequestDiscussions.create( context.projectId, mrIid, - noteInfo.discussionId, - noteInfo.noteId, - { - body: prComment, - }, + prComment, ) } + case 'note': { + if (noteInfo) { + return api.MergeRequestNotes.edit( + context.projectId, + mrIid, + noteInfo.noteId, + { body: prComment }, + ) + } - return api.MergeRequestDiscussions.create( - context.projectId, - mrIid, - prComment, - ) - } - - if (GITLAB_COMMENT_TYPE === 'note') { - if (noteInfo) { - return api.MergeRequestNotes.edit( - context.projectId, - mrIid, - noteInfo.noteId, - { body: prComment }, + return api.MergeRequestNotes.create(context.projectId, mrIid, prComment) + } + default: { + throw new Error( + `Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`, ) } - - return api.MergeRequestNotes.create(context.projectId, mrIid, prComment) } - - throw new Error( - `Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`, - ) } catch (err: unknown) { console.error(err) throw err diff --git a/src/env.ts b/src/env.ts index ac563e41..4ac7b289 100644 --- a/src/env.ts +++ b/src/env.ts @@ -29,6 +29,6 @@ export const env = { setFailed('Please add the `GITLAB_TOKEN` to the changesets action') } } - return process.env.GITLAB_TOKEN as string + return process.env.GITLAB_TOKEN! }, } as Env