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

Merge pull request #3538 from weseek/imprv/create-endpoint-for-setting-slackbot

Imprv/create endpoint for setting slackbot
Sizma yosimaz 5 лет назад
Родитель
Сommit
6b29720d46

+ 1 - 0
src/server/routes/apiv3/index.js

@@ -47,6 +47,7 @@ module.exports = (crowi) => {
   router.use('/attachment', require('./attachment')(crowi));
   router.use('/attachment', require('./attachment')(crowi));
 
 
   router.use('/slack-bot', require('./slack-bot')(crowi));
   router.use('/slack-bot', require('./slack-bot')(crowi));
+  router.use('/slack-integration', require('./slack-integration')(crowi));
   router.use('/staffs', require('./staffs')(crowi));
   router.use('/staffs', require('./staffs')(crowi));
 
 
   return router;
   return router;

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

@@ -0,0 +1,140 @@
+const loggerFactory = require('@alias/logger');
+
+const logger = loggerFactory('growi:routes:apiv3:notification-setting');
+const express = require('express');
+const { body } = require('express-validator');
+const ErrorV3 = require('../../models/vo/error-apiv3');
+
+const router = express.Router();
+
+/**
+ * @swagger
+ *  tags:
+ *    name: SlackIntegration
+ */
+
+/**
+ * @swagger
+ *
+ *  components:
+ *    schemas:
+ *      CustomBotNonProxy:
+ *        description: CustomBotNonProxy
+ *        type: object
+ *        properties:
+ *          slackSigningSecret:
+ *            type: string
+ *          slackBotToken:
+ *            type: string
+ *          botType:
+ *            type: string
+ */
+
+
+module.exports = (crowi) => {
+  const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
+  const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
+  const adminRequired = require('../../middlewares/admin-required')(crowi);
+  const csrf = require('../../middlewares/csrf')(crowi);
+  const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
+
+
+  const validator = {
+    CusotmBotNonProxy: [
+      body('slackSigningSecret').isString(),
+      body('slackBotToken').isString(),
+      body('botType').isString(),
+    ],
+  };
+
+  async function updateSlackBotSettings(params) {
+    const { configManager } = crowi;
+    // update config without publishing S2sMessage
+    return configManager.updateConfigsInTheSameNamespace('crowi', params, true);
+  }
+
+  /**
+   * @swagger
+   *
+   *    /slack-integration/:
+   *      get:
+   *        tags: [SlackBotSettingParams]
+   *        operationId: getSlackBotSettingParams
+   *        summary: /slack-integration
+   *        description: Get slackBot setting params.
+   *        responses:
+   *          200:
+   *            description: Succeeded to get slackBot setting params.
+   */
+  router.get('/', accessTokenParser, loginRequiredStrictly, adminRequired, async(req, res) => {
+
+    const slackBotSettingParams = {
+      slackBotType: crowi.configManager.getConfig('crowi', 'slackbot:type'),
+      // TODO impl when creating official bot
+      officialBotSettings: {
+        // TODO impl this after GW-4939
+        // AccessToken: "tempaccessdatahogehoge",
+      },
+      cusotmBotNonProxySettings: {
+        // TODO impl this after GW-4939
+        // AccessToken: "tempaccessdatahogehoge",
+        slackSigningSecret: crowi.configManager.getConfig('crowi', 'slackbot:signingSecret'),
+        slackBotToken: crowi.configManager.getConfig('crowi', 'slackbot:token'),
+      },
+      // TODO imple when creating with proxy
+      cusotmBotWithProxySettings: {
+        // TODO impl this after GW-4939
+        // AccessToken: "tempaccessdatahogehoge",
+      },
+    };
+    return res.apiv3({ slackBotSettingParams });
+  });
+
+  /**
+   * @swagger
+   *
+   *    /slack-integration/custom-bot-non-proxy/:
+   *      put:
+   *        tags: [CustomBotNonProxy]
+   *        operationId: putCustomBotNonProxy
+   *        summary: /slack-integration/custom-bot-non-proxy
+   *        description: Put customBotNonProxy setting.
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                $ref: '#/components/schemas/CustomBotNonProxy'
+   *        responses:
+   *           200:
+   *             description: Succeeded to put CustomBotNonProxy setting.
+   */
+  router.put('/custom-bot-non-proxy',
+    accessTokenParser, loginRequiredStrictly, adminRequired, csrf, validator.CusotmBotNonProxy, apiV3FormValidator, async(req, res) => {
+      const { slackSigningSecret, slackBotToken, botType } = req.body;
+
+      const requestParams = {
+        'slackbot:signingSecret': slackSigningSecret,
+        'slackbot:token': slackBotToken,
+        'slackbot:type': botType,
+      };
+
+      try {
+        await updateSlackBotSettings(requestParams);
+        // TODO Impl to delete AccessToken both of Proxy and GROWI when botType changes.
+        const customBotNonProxySettingParams = {
+          slackSigningSecret: crowi.configManager.getConfig('crowi', 'slackbot:signingSecret'),
+          slackBotToken: crowi.configManager.getConfig('crowi', 'slackbot:token'),
+          slackBotType: crowi.configManager.getConfig('crowi', 'slackbot:type'),
+        };
+        return res.apiv3({ customBotNonProxySettingParams });
+      }
+      catch (error) {
+        const msg = 'Error occured in updating Custom bot setting';
+        logger.error('Error', error);
+        return res.apiv3Err(new ErrorV3(msg, 'update-CustomBotSetting-failed'));
+      }
+    });
+
+  return router;
+};

+ 6 - 0
src/server/service/config-loader.js

@@ -410,6 +410,12 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
     type:    TYPES.STRING,
     type:    TYPES.STRING,
     default: null,
     default: null,
   },
   },
+  SLACK_BOT_TYPE: {
+    ns:      'crowi',
+    key:     'slackbot:type', // eg. official || custom-non-proxy || custom-with-proxy
+    type:    TYPES.STRING,
+    default: null,
+  },
 };
 };
 
 
 class ConfigLoader {
 class ConfigLoader {