Skip to content

Commit e5087a3

Browse files
committed
feat: allow to set different type of comment if changeset is missing
Closes: un-ts#190
1 parent 31fb0bc commit e5087a3

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

.changeset/plenty-badgers-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'changesets-gitlab': minor
3+
---
4+
5+
Add configuration for comment if changeset is missing

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ GLOBAL_AGENT_NO_PROXY # Like above but for no proxied requests
4848

4949
GITLAB_HOST # optional, if you're using custom GitLab host, will fallback to `CI_SERVER_URL` if not provided
5050

51-
GITLAB_TOKEN # required, token with accessibility to push
52-
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
53-
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
54-
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
56-
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
57-
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
51+
GITLAB_TOKEN # required, token with accessibility to push
52+
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
53+
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
54+
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
56+
GITLAB_COMMENT_TYPE_IF_MISSING # optional, type of the comment when changeset is missing, default set to `GITLAB_COMMENT_TYPE`, should be `note` or `discussion`
57+
GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
58+
DEBUG_GITLAB_CREDENTIAL # optional, default `false`
5859
```
5960

6061
### Example workflow

src/comment.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,12 @@ export const comment = async () => {
251251
CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
252252
CI_MERGE_REQUEST_TITLE,
253253
GITLAB_COMMENT_TYPE,
254+
GITLAB_COMMENT_TYPE_IF_MISSING,
254255
GITLAB_ADD_CHANGESET_MESSAGE,
255256
} = env
256257

258+
let comment_type = GITLAB_COMMENT_TYPE
259+
257260
if (mrBranch.startsWith('changeset-release')) {
258261
return
259262
}
@@ -311,7 +314,10 @@ export const comment = async () => {
311314
: getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
312315
errFromFetchingChangedFiles
313316

314-
switch (GITLAB_COMMENT_TYPE) {
317+
if (!hasChangeset) {
318+
comment_type = GITLAB_COMMENT_TYPE_IF_MISSING
319+
}
320+
switch (comment_type) {
315321
case 'discussion': {
316322
if (noteInfo) {
317323
return api.MergeRequestDiscussions.editNote(
@@ -345,7 +351,7 @@ export const comment = async () => {
345351
}
346352
default: {
347353
throw new Error(
348-
`Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`,
354+
`Invalid comment type "${comment_type}", should be "discussion" or "note"`,
349355
)
350356
}
351357
}

src/env.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dotenv.config()
77

88
let isGitlabTokenValidated = false
99

10-
export const env = {
10+
const env = {
1111
...process.env,
1212

1313
CI_MERGE_REQUEST_IID: +process.env.CI_MERGE_REQUEST_IID!,
@@ -32,3 +32,8 @@ export const env = {
3232
return process.env.GITLAB_TOKEN!
3333
},
3434
} as Env
35+
36+
env.GITLAB_COMMENT_TYPE_IF_MISSING =
37+
process.env.GITLAB_COMMENT_TYPE_IF_MISSING ?? env.GITLAB_COMMENT_TYPE
38+
39+
export { env }

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type Env = GitLabCIPredefinedVariables &
2020
GITLAB_CI_USER_NAME?: string
2121
GITLAB_CI_USER_EMAIL: string
2222
GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'>
23+
GITLAB_COMMENT_TYPE_IF_MISSING: LooseString<'discussion' | 'note'>
2324
GITLAB_ADD_CHANGESET_MESSAGE?: string
2425
DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'>
2526

0 commit comments

Comments
 (0)