Skip to content

feat: add GITLAB_COMMENT_TYPE env variable to use discussions or notes #98

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-foxes-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'changesets-gitlab': minor
---

feat: add new `GITLAB_COMMENT_TYPE` environment variable to use discussions or notes
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
```

Expand Down
38 changes: 32 additions & 6 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,38 @@ const hasChangesetBeenAdded = (
),
)

const getCommentFunctions = (api: Gitlab, commentType: string) => {
if (commentType === 'discussion') {
return {
editComment: api.MergeRequestDiscussions.editNote.bind(
api.MergeRequestDiscussions,
),
createComment: api.MergeRequestDiscussions.create.bind(
api.MergeRequestDiscussions,
),
}
}

if (commentType === 'note') {
return {
editComment: api.MergeRequestNotes.edit.bind(api.MergeRequestNotes),
createComment: api.MergeRequestNotes.create.bind(api.MergeRequestNotes),
}
}

throw new Error(
`Invalid comment type "${commentType}", should be "discussion" or "note"`,
)
}

export const comment = async () => {
const {
CI_MERGE_REQUEST_IID,
CI_MERGE_REQUEST_PROJECT_URL,
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: mrBranch,
CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
CI_MERGE_REQUEST_TITLE,
GITLAB_COMMENT_TYPE = 'discussion',
} = process.env

if (!mrBranch) {
Expand Down Expand Up @@ -211,8 +236,13 @@ export const comment = async () => {
: getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
errFromFetchingChangedFiles

const { editComment, createComment } = getCommentFunctions(
api,
GITLAB_COMMENT_TYPE,
)

if (noteInfo != null) {
return api.MergeRequestDiscussions.editNote(
return editComment(
context.projectId,
mrIid,
// @ts-expect-error - https://github.com/jdalrymple/gitbeaker/pull/523#issuecomment-975276068
Expand All @@ -223,11 +253,7 @@ export const comment = async () => {
},
)
}
return api.MergeRequestDiscussions.create(
context.projectId,
mrIid,
prComment,
)
return createComment(context.projectId, mrIid, prComment)
} catch (err: unknown) {
console.error(err)
throw err
Expand Down