Просмотр исходного кода

Merge pull request #3592 from weseek/feat/4937-5561-implement-test-for-slack-from-growi

Feat/4937 5561 implement test for slack from growi
itizawa 5 лет назад
Родитель
Сommit
d289bea772

+ 18 - 10
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettings.jsx

@@ -23,24 +23,31 @@ const CustomBotWithoutProxySettings = (props) => {
   const [siteName, setSiteName] = useState('');
   // eslint-disable-next-line no-unused-vars
   const [isSetupSlackBot, setIsSetupSlackBot] = useState(null);
+  const [isConnectedToSlack, setIsConnectedToSlack] = useState(null);
   const currentBotType = 'custom-bot-without-proxy';
 
-  const getSlackWSInWithoutProxy = useCallback(async() => {
-    try {
-      const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name');
-      setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName);
-    }
-    catch (err) {
-      toastError(err);
+  useEffect(() => {
+    const fetchData = async() => {
+      try {
+        const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name');
+        setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName);
+      }
+      catch (err) {
+        toastError(err);
+      }
+    };
+    setSlackWSNameInWithoutProxy(null);
+    if (isConnectedToSlack) {
+      fetchData();
     }
-  }, [appContainer]);
+  }, [appContainer, isConnectedToSlack]);
 
   const fetchData = useCallback(async() => {
     try {
       await adminAppContainer.retrieveAppSettingsData();
       const res = await appContainer.apiv3.get('/slack-integration/');
       const {
-        slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, isSetupSlackBot,
+        slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, isSetupSlackBot, isConnectedToSlack,
       } = res.data.slackBotSettingParams.customBotWithoutProxySettings;
       setSlackSigningSecret(slackSigningSecret);
       setSlackBotToken(slackBotToken);
@@ -48,6 +55,7 @@ const CustomBotWithoutProxySettings = (props) => {
       setSlackBotTokenEnv(slackBotTokenEnvVars);
       setSiteName(adminAppContainer.state.title);
       setIsSetupSlackBot(isSetupSlackBot);
+      setIsConnectedToSlack(isConnectedToSlack);
     }
     catch (err) {
       toastError(err);
@@ -65,7 +73,7 @@ const CustomBotWithoutProxySettings = (props) => {
         slackBotToken,
         currentBotType,
       });
-      getSlackWSInWithoutProxy();
+      fetchData();
       toastSuccess(t('toaster.update_successed', { target: t('admin:slack_integration.custom_bot_without_proxy_settings') }));
     }
     catch (err) {

+ 15 - 22
src/server/routes/apiv3/slack-integration.js

@@ -3,7 +3,6 @@ const loggerFactory = require('@alias/logger');
 const logger = loggerFactory('growi:routes:apiv3:notification-setting');
 const express = require('express');
 const { body } = require('express-validator');
-const { WebClient } = require('@slack/web-api');
 const crypto = require('crypto');
 const ErrorV3 = require('../../models/vo/error-apiv3');
 
@@ -102,6 +101,7 @@ module.exports = (crowi) => {
         slackSigningSecret: crowi.configManager.getConfig('crowi', 'slackbot:signingSecret'),
         slackBotToken: crowi.configManager.getConfig('crowi', 'slackbot:token'),
         isSetupSlackBot: crowi.slackBotService.isSetupSlackBot,
+        isConnectedToSlack: crowi.slackBotService.isConnectedToSlack,
       },
       // TODO imple when creating with proxy
       customBotWithProxySettings: {
@@ -143,7 +143,7 @@ module.exports = (crowi) => {
         await updateSlackBotSettings(requestParams);
 
         // initialize slack service
-        crowi.slackBotService.initialize();
+        await crowi.slackBotService.initialize();
         crowi.slackBotService.publishUpdatedMessage();
 
         const slackIntegrationSettingsParams = {
@@ -154,7 +154,7 @@ module.exports = (crowi) => {
       catch (error) {
         const msg = 'Error occured in updating Slack bot setting';
         logger.error('Error', error);
-        return res.apiv3Err(new ErrorV3(msg, 'update-SlackIntegrationSetting-failed'));
+        return res.apiv3Err(new ErrorV3(msg, 'update-SlackIntegrationSetting-failed'), 500);
       }
     });
 
@@ -191,7 +191,7 @@ module.exports = (crowi) => {
         await updateSlackBotSettings(requestParams);
 
         // initialize slack service
-        crowi.slackBotService.initialize();
+        await crowi.slackBotService.initialize();
         crowi.slackBotService.publishUpdatedMessage();
 
         // TODO Impl to delete AccessToken both of Proxy and GROWI when botType changes.
@@ -222,25 +222,18 @@ module.exports = (crowi) => {
    *          200:
    *            description: Succeeded to get slack ws name for custom bot without proxy
    */
-  router.get('/custom-bot-without-proxy/slack-workspace-name', async(req, res) => {
-    // get ws name in custom bot from slackbot token
-    const slackBotToken = crowi.configManager.getConfig('crowi', 'slackbot:token');
+  router.get('/custom-bot-without-proxy/slack-workspace-name', loginRequiredStrictly, adminRequired, async(req, res) => {
 
-    let slackWorkSpaceName = null;
-    if (slackBotToken != null) {
-      const web = new WebClient(slackBotToken);
-      try {
-        const slackTeamInfo = await web.team.info();
-        slackWorkSpaceName = slackTeamInfo.team.name;
-      }
-      catch (error) {
-        const msg = 'Error occured in slack_bot_token';
-        logger.error('Error', msg);
-        return res.apiv3Err(new ErrorV3(msg, 'get-SlackWorkSpaceName-failed'));
-      }
+    try {
+      const slackWorkSpaceName = await crowi.slackBotService.getSlackChannelName();
+      return res.apiv3({ slackWorkSpaceName });
+    }
+    catch (error) {
+      const msg = 'Error occured in slack_bot_token';
+      logger.error('Error', error);
+      return res.apiv3Err(new ErrorV3(msg, 'get-SlackWorkSpaceName-failed'), 500);
     }
 
-    return res.apiv3({ slackWorkSpaceName });
   });
 
   /**
@@ -263,7 +256,7 @@ module.exports = (crowi) => {
       await updateSlackBotSettings({ 'slackbot:access-token': accessToken });
 
       // initialize slack service
-      crowi.slackBotService.initialize();
+      await crowi.slackBotService.initialize();
       crowi.slackBotService.publishUpdatedMessage();
 
       return res.apiv3({ accessToken });
@@ -294,7 +287,7 @@ module.exports = (crowi) => {
       await updateSlackBotSettings({ 'slackbot:access-token': null });
 
       // initialize slack service
-      crowi.slackBotService.initialize();
+      await crowi.slackBotService.initialize();
       crowi.slackBotService.publishUpdatedMessage();
 
       return res.apiv3({});

+ 18 - 3
src/server/service/slackbot.js

@@ -20,22 +20,25 @@ class SlackBotService extends S2sMessageHandlable {
     this.searchService = null;
 
     this.isSetupSlackBot = false;
+    this.isConnectedToSlack = false;
+
     this.lastLoadedAt = null;
 
     this.initialize();
   }
 
-  initialize() {
+  async initialize() {
     this.isSetupSlackBot = false;
 
     const token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
 
     if (token != null) {
       this.client = new WebClient(token, { logLevel: LogLevel.DEBUG });
+      logger.debug('SlackBot: setup is done');
+      this.isSetupSlackBot = true;
+      await this.sendAuthTest();
     }
 
-    logger.debug('SlackBot: setup is done');
-    this.isSetupSlackBot = true;
     this.lastLoadedAt = new Date();
   }
 
@@ -78,6 +81,13 @@ class SlackBotService extends S2sMessageHandlable {
     }
   }
 
+  async sendAuthTest() {
+    this.isConnectedToSlack = false;
+
+    await this.client.api.test();
+    this.isConnectedToSlack = true;
+  }
+
   notCommand(body) {
     logger.error('Invalid first argument');
     this.client.chat.postEphemeral({
@@ -153,6 +163,11 @@ class SlackBotService extends S2sMessageHandlable {
     };
   }
 
+  async getSlackChannelName() {
+    const slackTeamInfo = await this.client.team.info();
+    return slackTeamInfo.team.name;
+  }
+
   shareSearchResults(payload) {
     this.client.chat.postMessage({
       channel: payload.channel.id,