Skip to content

Commit

Permalink
fix: comment username could be random
Browse files Browse the repository at this point in the history
close #145
  • Loading branch information
JounQin committed Dec 18, 2023
1 parent 569933f commit bb24f37
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
93 changes: 57 additions & 36 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -130,28 +135,34 @@ 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))

const username = await getUsername(api)

for (const discussionOrNote of discussionOrNotes) {
if (isMrNote(discussionOrNote)) {
if (isChangesetBotNote(discussionOrNote, username)) {
if (isChangesetBotNote(discussionOrNote, username, random)) {
return {
noteId: discussionOrNote.id,
}
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bb24f37

Please sign in to comment.