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

Merge pull request #3650 from weseek/fix/able-to-test-not-setting-Secret-and-Token

Fix/able to test not setting secret and token
Sizma yosimaz 5 лет назад
Родитель
Сommit
d910bed10f

+ 6 - 8
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettings.jsx

@@ -1,4 +1,4 @@
-import React, { useState, useEffect } from 'react';
+import React, { useState, useEffect, useCallback } from 'react';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 import AppContainer from '../../../services/AppContainer';
 import AppContainer from '../../../services/AppContainer';
@@ -15,7 +15,7 @@ const CustomBotWithoutProxySettings = (props) => {
 
 
   const [siteName, setSiteName] = useState('');
   const [siteName, setSiteName] = useState('');
 
 
-  const fetchSlackWorkSpaceName = async() => {
+  const fetchSlackWorkSpaceName = useCallback(async() => {
     try {
     try {
       const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name');
       const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name');
       setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName);
       setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName);
@@ -23,19 +23,17 @@ const CustomBotWithoutProxySettings = (props) => {
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
     }
     }
-  };
+  }, [appContainer.apiv3]);
+
+  useEffect(() => {
 
 
-  const fetchSiteName = () => {
     const siteName = appContainer.config.crowi.title;
     const siteName = appContainer.config.crowi.title;
     setSiteName(siteName);
     setSiteName(siteName);
-  };
 
 
-  useEffect(() => {
-    fetchSiteName();
     if (props.isSetupSlackBot) {
     if (props.isSetupSlackBot) {
       fetchSlackWorkSpaceName();
       fetchSlackWorkSpaceName();
     }
     }
-  }, [appContainer, props.isSetupSlackBot]);
+  }, [appContainer, fetchSlackWorkSpaceName, props.isSetupSlackBot]);
 
 
   return (
   return (
     <>
     <>

+ 26 - 13
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettingsAccordion.jsx

@@ -16,10 +16,10 @@ export const botInstallationStep = {
 };
 };
 
 
 const CustomBotWithoutProxySettingsAccordion = ({
 const CustomBotWithoutProxySettingsAccordion = ({
-  appContainer,
-  activeStep, slackSigningSecret, slackSigningSecretEnv, slackBotToken,
-  slackBotTokenEnv, isRegisterSlackCredentials, isSendTestMessage,
-  setSlackSigningSecret, setSlackBotToken, setIsSendTestMessage, setIsRegisterSlackCredentials,
+  appContainer, activeStep,
+  slackSigningSecret, slackSigningSecretEnv, slackBotToken, slackBotTokenEnv,
+  isRegisterSlackCredentials, isSendTestMessage, isConnectedToSlack,
+  onSetSlackSigningSecret, onSetSlackBotToken, onSetIsSendTestMessage, onSetIsRegisterSlackCredentials,
 }) => {
 }) => {
   const { t } = useTranslation();
   const { t } = useTranslation();
   // TODO: GW-5644 Store default open accordion
   // TODO: GW-5644 Store default open accordion
@@ -38,20 +38,32 @@ const CustomBotWithoutProxySettingsAccordion = ({
         slackBotToken,
         slackBotToken,
         currentBotType,
         currentBotType,
       });
       });
+
+      if (isConnectedToSlack) {
+        onSetIsRegisterSlackCredentials(true);
+      }
+      else {
+        onSetIsRegisterSlackCredentials(false);
+        onSetIsSendTestMessage(false);
+      }
       toastSuccess(t('toaster.update_successed', { target: t('admin:slack_integration.custom_bot_without_proxy_settings') }));
       toastSuccess(t('toaster.update_successed', { target: t('admin:slack_integration.custom_bot_without_proxy_settings') }));
     }
     }
     catch (err) {
     catch (err) {
-      setIsRegisterSlackCredentials(false);
+      onSetIsRegisterSlackCredentials(false);
       toastError(err);
       toastError(err);
     }
     }
   };
   };
 
 
   const onChangeSigningSecretHandler = (signingSecretInput) => {
   const onChangeSigningSecretHandler = (signingSecretInput) => {
-    setSlackSigningSecret(signingSecretInput);
+    if (onSetSlackSigningSecret != null) {
+      onSetSlackSigningSecret(signingSecretInput);
+    }
   };
   };
 
 
   const onChangeBotTokenHandler = (botTokenInput) => {
   const onChangeBotTokenHandler = (botTokenInput) => {
-    setSlackBotToken(botTokenInput);
+    if (onSetSlackBotToken != null) {
+      onSetSlackBotToken(botTokenInput);
+    }
   };
   };
 
 
   const onTestConnectionHandler = async() => {
   const onTestConnectionHandler = async() => {
@@ -63,10 +75,10 @@ const CustomBotWithoutProxySettingsAccordion = ({
         channel: testChannel,
         channel: testChannel,
       });
       });
       setConnectionSuccessMessage(res.data.message);
       setConnectionSuccessMessage(res.data.message);
-      setIsSendTestMessage(true);
+      onSetIsSendTestMessage(true);
     }
     }
     catch (err) {
     catch (err) {
-      setIsSendTestMessage(false);
+      onSetIsSendTestMessage(false);
       setConnectionErrorCode(err[0].code);
       setConnectionErrorCode(err[0].code);
       setConnectionErrorMessage(err[0].message);
       setConnectionErrorMessage(err[0].message);
     }
     }
@@ -202,10 +214,11 @@ CustomBotWithoutProxySettingsAccordion.propTypes = {
   slackBotTokenEnv: PropTypes.string,
   slackBotTokenEnv: PropTypes.string,
   isRegisterSlackCredentials: PropTypes.bool,
   isRegisterSlackCredentials: PropTypes.bool,
   isSendTestMessage: PropTypes.bool,
   isSendTestMessage: PropTypes.bool,
-  setSlackSigningSecret: PropTypes.string,
-  setSlackBotToken: PropTypes.string,
-  setIsSendTestMessage: PropTypes.func,
-  setIsRegisterSlackCredentials: PropTypes.func,
+  isConnectedToSlack: PropTypes.bool,
+  onSetSlackSigningSecret: PropTypes.func,
+  onSetSlackBotToken: PropTypes.func,
+  onSetIsSendTestMessage: PropTypes.func,
+  onSetIsRegisterSlackCredentials: PropTypes.func,
   adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
   adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
   activeStep: PropTypes.oneOf(Object.values(botInstallationStep)).isRequired,
   activeStep: PropTypes.oneOf(Object.values(botInstallationStep)).isRequired,
 };
 };

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

@@ -18,11 +18,11 @@ const SlackIntegration = (props) => {
   const { t } = useTranslation();
   const { t } = useTranslation();
   const [currentBotType, setCurrentBotType] = useState(null);
   const [currentBotType, setCurrentBotType] = useState(null);
   const [selectedBotType, setSelectedBotType] = useState(null);
   const [selectedBotType, setSelectedBotType] = useState(null);
-  const [slackSigningSecret, setSlackSigningSecret] = useState('');
-  const [slackBotToken, setSlackBotToken] = useState('');
+  const [slackSigningSecret, setSlackSigningSecret] = useState(null);
+  const [slackBotToken, setSlackBotToken] = useState(null);
   const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState('');
   const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState('');
   const [slackBotTokenEnv, setSlackBotTokenEnv] = useState('');
   const [slackBotTokenEnv, setSlackBotTokenEnv] = useState('');
-  const [isConnectedToSlack, setIsConnectedToSlack] = useState(null);
+  const [isConnectedToSlack, setIsConnectedToSlack] = useState(false);
   const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
   const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
   const [isSendTestMessage, setIsSendTestMessage] = useState(false);
   const [isSendTestMessage, setIsSendTestMessage] = useState(false);
   const [isSetupSlackBot, setIsSetupSlackBot] = useState(false);
   const [isSetupSlackBot, setIsSetupSlackBot] = useState(false);
@@ -33,7 +33,8 @@ const SlackIntegration = (props) => {
       const response = await appContainer.apiv3.get('slack-integration/');
       const response = await appContainer.apiv3.get('slack-integration/');
       const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams;
       const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams;
       const {
       const {
-        slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, isSetupSlackBot,
+        slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
+        isSetupSlackBot, isConnectedToSlack,
       } = customBotWithoutProxySettings;
       } = customBotWithoutProxySettings;
 
 
       setCurrentBotType(currentBotType);
       setCurrentBotType(currentBotType);
@@ -44,15 +45,18 @@ const SlackIntegration = (props) => {
       setIsConnectedToSlack(isConnectedToSlack);
       setIsConnectedToSlack(isConnectedToSlack);
       setIsSetupSlackBot(isSetupSlackBot);
       setIsSetupSlackBot(isSetupSlackBot);
 
 
-      if ((slackBotToken && slackSigningSecret) || (slackBotTokenEnv && slackSigningSecretEnv)) {
+      if (isConnectedToSlack) {
         setIsRegisterSlackCredentials(true);
         setIsRegisterSlackCredentials(true);
       }
       }
+      else {
+        setIsRegisterSlackCredentials(false);
+      }
 
 
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
     }
     }
-  }, [appContainer.apiv3, isConnectedToSlack, slackBotTokenEnv, slackSigningSecretEnv]);
+  }, [appContainer.apiv3]);
 
 
   useEffect(() => {
   useEffect(() => {
     fetchData();
     fetchData();
@@ -66,9 +70,6 @@ const SlackIntegration = (props) => {
       setCurrentBotType(clickedBotType);
       setCurrentBotType(clickedBotType);
       return;
       return;
     }
     }
-    setIsRegisterSlackCredentials(false);
-    setSlackSigningSecret('');
-    setSlackBotToken('');
     setSelectedBotType(clickedBotType);
     setSelectedBotType(clickedBotType);
   };
   };
 
 
@@ -86,6 +87,10 @@ const SlackIntegration = (props) => {
       setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
       setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
       setSelectedBotType(null);
       setSelectedBotType(null);
       toastSuccess(t('admin:slack_integration.bot_reset_successful'));
       toastSuccess(t('admin:slack_integration.bot_reset_successful'));
+      setIsRegisterSlackCredentials(false);
+      setSlackSigningSecret(null);
+      setSlackBotToken(null);
+      setIsSendTestMessage(false);
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
@@ -108,11 +113,11 @@ const SlackIntegration = (props) => {
           slackBotToken={slackBotToken}
           slackBotToken={slackBotToken}
           slackSigningSecretEnv={slackSigningSecretEnv}
           slackSigningSecretEnv={slackSigningSecretEnv}
           slackSigningSecret={slackSigningSecret}
           slackSigningSecret={slackSigningSecret}
-          setSlackSigningSecret={setSlackSigningSecret}
-          setSlackBotToken={setSlackBotToken}
-          setIsSendTestMessage={setIsSendTestMessage}
-          setIsRegisterSlackCredentials={setIsRegisterSlackCredentials}
           isSetupSlackBot={isSetupSlackBot}
           isSetupSlackBot={isSetupSlackBot}
+          onSetSlackSigningSecret={setSlackSigningSecret}
+          onSetSlackBotToken={setSlackBotToken}
+          onSetIsSendTestMessage={setIsSendTestMessage}
+          onSetIsRegisterSlackCredentials={setIsRegisterSlackCredentials}
         />
         />
       );
       );
       break;
       break;

+ 0 - 2
src/server/routes/apiv3/slack-integration.js

@@ -185,13 +185,11 @@ module.exports = (crowi) => {
   router.put('/custom-bot-without-proxy',
   router.put('/custom-bot-without-proxy',
     accessTokenParser, loginRequiredStrictly, adminRequired, csrf, validator.CustomBotWithoutProxy, apiV3FormValidator, async(req, res) => {
     accessTokenParser, loginRequiredStrictly, adminRequired, csrf, validator.CustomBotWithoutProxy, apiV3FormValidator, async(req, res) => {
       const { slackSigningSecret, slackBotToken, currentBotType } = req.body;
       const { slackSigningSecret, slackBotToken, currentBotType } = req.body;
-
       const requestParams = {
       const requestParams = {
         'slackbot:signingSecret': slackSigningSecret,
         'slackbot:signingSecret': slackSigningSecret,
         'slackbot:token': slackBotToken,
         'slackbot:token': slackBotToken,
         'slackbot:currentBotType': currentBotType,
         'slackbot:currentBotType': currentBotType,
       };
       };
-
       try {
       try {
         await updateSlackBotSettings(requestParams);
         await updateSlackBotSettings(requestParams);
 
 

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

@@ -29,7 +29,7 @@ class SlackBotService extends S2sMessageHandlable {
 
 
   async initialize() {
   async initialize() {
     this.isSetupSlackBot = false;
     this.isSetupSlackBot = false;
-
+    this.isConnectedToSlack = false;
     const token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
     const token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
 
 
     if (token != null) {
     if (token != null) {
@@ -82,8 +82,6 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   async sendAuthTest() {
   async sendAuthTest() {
-    this.isConnectedToSlack = false;
-
     await this.client.api.test();
     await this.client.api.test();
     this.isConnectedToSlack = true;
     this.isConnectedToSlack = true;
   }
   }