From e5087a3b7acdc3bad329beace30f9884621977f7 Mon Sep 17 00:00:00 2001
From: Aurelien LAJOIE <aurelien@neat.eu>
Date: Mon, 22 Jul 2024 11:26:47 +0200
Subject: [PATCH] feat: allow to set different type of comment if changeset is
 missing

Closes: #190
---
 .changeset/plenty-badgers-bathe.md |  5 +++++
 README.md                          | 15 ++++++++-------
 src/comment.ts                     | 10 ++++++++--
 src/env.ts                         |  7 ++++++-
 src/types.ts                       |  1 +
 5 files changed, 28 insertions(+), 10 deletions(-)
 create mode 100644 .changeset/plenty-badgers-bathe.md

diff --git a/.changeset/plenty-badgers-bathe.md b/.changeset/plenty-badgers-bathe.md
new file mode 100644
index 00000000..1b2dfad5
--- /dev/null
+++ b/.changeset/plenty-badgers-bathe.md
@@ -0,0 +1,5 @@
+---
+'changesets-gitlab': minor
+---
+
+Add configuration for comment if changeset is missing
diff --git a/README.md b/README.md
index 50683cf2..de6df230 100644
--- a/README.md
+++ b/README.md
@@ -48,13 +48,14 @@ GLOBAL_AGENT_NO_PROXY    # Like above but for no proxied requests
 
 GITLAB_HOST # optional, if you're using custom GitLab host, will fallback to `CI_SERVER_URL` if not provided
 
-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
-GITLAB_ADD_CHANGESET_MESSAGE # optional, default commit message for adding changesets on GitLab Web UI
-DEBUG_GITLAB_CREDENTIAL      # optional, default `false`
+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
+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`
+GITLAB_ADD_CHANGESET_MESSAGE   # optional, default commit message for adding changesets on GitLab Web UI
+DEBUG_GITLAB_CREDENTIAL        # optional, default `false`
 ```
 
 ### Example workflow
diff --git a/src/comment.ts b/src/comment.ts
index 5dea359a..0945e2a0 100644
--- a/src/comment.ts
+++ b/src/comment.ts
@@ -251,9 +251,12 @@ export const comment = async () => {
     CI_MERGE_REQUEST_SOURCE_BRANCH_SHA,
     CI_MERGE_REQUEST_TITLE,
     GITLAB_COMMENT_TYPE,
+    GITLAB_COMMENT_TYPE_IF_MISSING,
     GITLAB_ADD_CHANGESET_MESSAGE,
   } = env
 
+  let comment_type = GITLAB_COMMENT_TYPE
+
   if (mrBranch.startsWith('changeset-release')) {
     return
   }
@@ -311,7 +314,10 @@ export const comment = async () => {
         : getAbsentMessage(latestCommitSha, addChangesetUrl, releasePlan)) +
       errFromFetchingChangedFiles
 
-    switch (GITLAB_COMMENT_TYPE) {
+    if (!hasChangeset) {
+      comment_type = GITLAB_COMMENT_TYPE_IF_MISSING
+    }
+    switch (comment_type) {
       case 'discussion': {
         if (noteInfo) {
           return api.MergeRequestDiscussions.editNote(
@@ -345,7 +351,7 @@ export const comment = async () => {
       }
       default: {
         throw new Error(
-          `Invalid comment type "${GITLAB_COMMENT_TYPE}", should be "discussion" or "note"`,
+          `Invalid comment type "${comment_type}", should be "discussion" or "note"`,
         )
       }
     }
diff --git a/src/env.ts b/src/env.ts
index 4ac7b289..655b9bd0 100644
--- a/src/env.ts
+++ b/src/env.ts
@@ -7,7 +7,7 @@ dotenv.config()
 
 let isGitlabTokenValidated = false
 
-export const env = {
+const env = {
   ...process.env,
 
   CI_MERGE_REQUEST_IID: +process.env.CI_MERGE_REQUEST_IID!,
@@ -32,3 +32,8 @@ export const env = {
     return process.env.GITLAB_TOKEN!
   },
 } as Env
+
+env.GITLAB_COMMENT_TYPE_IF_MISSING =
+  process.env.GITLAB_COMMENT_TYPE_IF_MISSING ?? env.GITLAB_COMMENT_TYPE
+
+export { env }
diff --git a/src/types.ts b/src/types.ts
index dd3932aa..e0714c88 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -20,6 +20,7 @@ export type Env = GitLabCIPredefinedVariables &
     GITLAB_CI_USER_NAME?: string
     GITLAB_CI_USER_EMAIL: string
     GITLAB_COMMENT_TYPE: LooseString<'discussion' | 'note'>
+    GITLAB_COMMENT_TYPE_IF_MISSING: LooseString<'discussion' | 'note'>
     GITLAB_ADD_CHANGESET_MESSAGE?: string
     DEBUG_GITLAB_CREDENTIAL: LooseString<'1' | 'true'>