Browse Source

Merge pull request #7751 from weseek/support/typescriptize-slack-legacy

support: typescriptize SlackLegacyUtil
Yuki Takei 2 years ago
parent
commit
696bb42d67

+ 2 - 1
apps/app/src/server/service/slack-integration.ts

@@ -15,6 +15,7 @@ import loggerFactory from '~/utils/logger';
 import type { EventActionsPermission } from '../interfaces/slack-integration/events';
 import S2sMessage from '../models/vo/s2s-message';
 import { SlackCommandHandlerError } from '../models/vo/slack-command-handler-error';
+import { slackLegacyUtilFactory } from '../util/slack-legacy';
 
 import type { ConfigManager } from './config-manager';
 import type { S2sMessagingService } from './s2s-messaging/base';
@@ -230,7 +231,7 @@ export class SlackIntegrationService implements S2sMessageHandlable {
   }
 
   private async postMessageWithLegacyUtil(messageArgs: ChatPostMessageArguments | IncomingWebhookSendArguments): Promise<void> {
-    const slackLegacyUtil = require('../util/slack-legacy')(this.crowi);
+    const slackLegacyUtil = slackLegacyUtilFactory(this.configManager);
 
     try {
       await slackLegacyUtil.postMessage(messageArgs);

+ 0 - 64
apps/app/src/server/util/slack-legacy.js

@@ -1,64 +0,0 @@
-import { IncomingWebhook } from '@slack/webhook';
-import { WebClient } from '@slack/web-api';
-
-import loggerFactory from '~/utils/logger';
-
-const logger = loggerFactory('growi:util:slack-legacy');
-
-module.exports = function(crowi) {
-
-  const { configManager } = crowi;
-
-  const slackUtilLegacy = {};
-
-  const postWithIwh = async(messageObj) => {
-    const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
-    try {
-      await webhook.send(messageObj);
-    }
-    catch (error) {
-      logger.debug('Post error', error);
-      logger.debug('Sent data to slack is:', messageObj);
-      throw error;
-    }
-  };
-
-  const postWithWebApi = async(messageObj) => {
-    const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
-    try {
-      await client.chat.postMessage(messageObj);
-    }
-    catch (error) {
-      logger.debug('Post error', error);
-      logger.debug('Sent data to slack is:', messageObj);
-      throw error;
-    }
-  };
-
-  slackUtilLegacy.postMessage = async(messageObj) => {
-    // when incoming Webhooks is prioritized
-    if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
-      if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        logger.debug('posting message with IncomingWebhook');
-        return postWithIwh(messageObj);
-      }
-      if (configManager.getConfig('notification', 'slack:token')) {
-        logger.debug('posting message with Web API');
-        return postWithWebApi(messageObj);
-      }
-    }
-    // else
-    else {
-      if (configManager.getConfig('notification', 'slack:token')) {
-        logger.debug('posting message with Web API');
-        return postWithWebApi(messageObj);
-      }
-      if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        logger.debug('posting message with IncomingWebhook');
-        return postWithIwh(messageObj);
-      }
-    }
-  };
-
-  return slackUtilLegacy;
-};

+ 67 - 0
apps/app/src/server/util/slack-legacy.ts

@@ -0,0 +1,67 @@
+import type { ChatPostMessageArguments } from '@slack/web-api';
+import { WebClient } from '@slack/web-api';
+import { IncomingWebhook, type IncomingWebhookSendArguments } from '@slack/webhook';
+
+import loggerFactory from '~/utils/logger';
+
+const logger = loggerFactory('growi:util:slack-legacy');
+
+
+interface SlackLegacyUtil {
+  postMessage(messageObj: IncomingWebhookSendArguments | ChatPostMessageArguments): Promise<void>,
+}
+
+// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+export const slackLegacyUtilFactory = (configManager: any): SlackLegacyUtil => {
+
+  const postWithIwh = async(messageObj: IncomingWebhookSendArguments) => {
+    const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
+    try {
+      await webhook.send(messageObj);
+    }
+    catch (error) {
+      logger.debug('Post error', error);
+      logger.debug('Sent data to slack is:', messageObj);
+      throw error;
+    }
+  };
+
+  const postWithWebApi = async(messageObj?: ChatPostMessageArguments) => {
+    const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
+    try {
+      await client.chat.postMessage(messageObj);
+    }
+    catch (error) {
+      logger.debug('Post error', error);
+      logger.debug('Sent data to slack is:', messageObj);
+      throw error;
+    }
+  };
+
+  return {
+    postMessage: async(messageObj) => {
+      // when incoming Webhooks is prioritized
+      if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
+        if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
+          logger.debug('posting message with IncomingWebhook');
+          return postWithIwh(messageObj as IncomingWebhookSendArguments);
+        }
+        if (configManager.getConfig('notification', 'slack:token')) {
+          logger.debug('posting message with Web API');
+          return postWithWebApi(messageObj as ChatPostMessageArguments);
+        }
+      }
+      // else
+      else {
+        if (configManager.getConfig('notification', 'slack:token')) {
+          logger.debug('posting message with Web API');
+          return postWithWebApi(messageObj as ChatPostMessageArguments);
+        }
+        if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
+          logger.debug('posting message with IncomingWebhook');
+          return postWithIwh(messageObj as IncomingWebhookSendArguments);
+        }
+      }
+    },
+  };
+};

+ 0 - 17
apps/app/test/integration/utils/slack-legacy.test.js

@@ -1,17 +0,0 @@
-const { getInstance } = require('../setup-crowi');
-
-describe('Slack Util', () => {
-
-  let crowi;
-  let slackLegacyUtil;
-
-  beforeEach(async() => {
-    crowi = await getInstance();
-    slackLegacyUtil = require('~/server/util/slack-legacy')(crowi);
-  });
-
-  test('postMessage method exists', () => {
-    expect(slackLegacyUtil.postMessage).toBeInstanceOf(Function);
-  });
-
-});