2
0
Эх сурвалжийг харах

Merge branch 'feat/growi-bot' into imprv/5519-5573-update-InstallationRepository

itizawa 5 жил өмнө
parent
commit
151af614e7

+ 16 - 18
.github/workflows/ci-slackbot-proxy.yml

@@ -7,15 +7,11 @@ on:
       - tmp/**
     paths:
       - .github/workflows/ci-slackbot-proxy.yml
-      - packages/slack/*
-      - packages/slackbot-proxy/*
+      - packages/slack/**
+      - packages/slackbot-proxy/**
       - package.json
       - yarn.lock
 
-defaults:
-  run:
-    working-directory: packages/slackbot-proxy
-
 jobs:
 
   test:
@@ -36,7 +32,7 @@ jobs:
       uses: actions/cache@v2
       with:
         path: node_modules
-        key: ${{ runner.OS }}-node_modules-${{ matrix.node-version }}-${{ hashFiles('./yarn.lock') }}
+        key: ${{ runner.OS }}-node_modules-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
     - name: Get yarn cache dir
       if: steps.cache-dependencies.outputs.cache-hit != 'true'
       id: cache-yarn
@@ -46,21 +42,21 @@ jobs:
       uses: actions/cache@v2
       with:
         path: ${{ steps.cache-yarn.outputs.dir }}
-        key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('./yarn.lock') }}
+        key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
         restore-keys: |
           ${{ runner.os }}-yarn-${{ matrix.node-version }}-
     - name: Install dependencies
       if: steps.cache-dependencies.outputs.cache-hit != 'true'
       run: |
-        yarn --frozen-lockfile
+        npx lerna bootstrap
     - name: Print dependencies
       run: |
         echo -n "node " && node -v
         echo -n "npm " && npm -v
         yarn list --depth=0
-    - name: yarn lint
+    - name: yarn test
       run: |
-        yarn test
+        yarn lerna run test
 
     - name: Slack Notification
       uses: weseek/ghaction-slack-notification@master
@@ -101,7 +97,7 @@ jobs:
       uses: actions/cache@v2
       with:
         path: node_modules
-        key: ${{ runner.OS }}-node_modules_dev-${{ matrix.node-version }}-${{ hashFiles('./yarn.lock') }}
+        key: ${{ runner.OS }}-node_modules_dev-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
     - name: Get yarn cache dir
       if: steps.cache-dependencies.outputs.cache-hit != 'true'
       id: cache-yarn
@@ -111,19 +107,20 @@ jobs:
       uses: actions/cache@v2
       with:
         path: ${{ steps.cache-yarn.outputs.dir }}
-        key: ${{ runner.os }}-yarn-${{ hashFiles('./yarn.lock') }}
+        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
         restore-keys: |
           ${{ runner.os }}-yarn-
     - name: Install dependencies
       if: steps.cache-dependencies.outputs.cache-hit != 'true'
       run: |
-        yarn --frozen-lockfile
+        npx lerna bootstrap
     - name: Print dependencies
       run: |
         echo -n "node " && node -v
         echo -n "npm " && npm -v
         yarn list --depth=0
     - name: yarn dev:ci
+      working-directory: ./packages/slackbot-proxy
       run: |
         cp config/ci/.env.local.for-ci .env.local
         yarn dev:ci
@@ -191,12 +188,12 @@ jobs:
       uses: actions/cache@v2
       with:
         path: ${{ steps.cache-yarn.outputs.dir }}
-        key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('./yarn.lock') }}
+        key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
         restore-keys: |
           ${{ runner.os }}-yarn-
     - name: Install dependencies
       run: |
-        yarn --frozen-lockfile
+        npx lerna bootstrap
     - name: Print dependencies
       run: |
         echo -n "node " && node -v
@@ -204,16 +201,17 @@ jobs:
         yarn list --depth=0
     - name: yarn build
       run: |
-        yarn build
+        yarn lerna run build
     - name: yarn install --production
       run: |
-        yarn install --production
+        yarn lerna exec "yarn install --production"
     - name: Print dependencies
       run: |
         echo -n "node " && node -v
         echo -n "npm " && npm -v
         yarn list --production --depth=0
     - name: yarn start:prod:ci
+      working-directory: ./packages/slackbot-proxy
       run: |
         cp config/ci/.env.local.for-ci .env.local
         yarn start:prod:ci

+ 1 - 1
.gitignore

@@ -16,7 +16,7 @@ coverage
 /build
 
 # dist
-/dist/
+/packages/**/dist/
 /report/
 /public/static/js
 /public/static/styles

+ 8 - 0
lerna.json

@@ -0,0 +1,8 @@
+{
+  "npmClient": "yarn",
+  "useWorkspaces": true,
+  "packages": [
+    "packages/*"
+  ],
+  "version": "independent"
+}

+ 4 - 1
package.json

@@ -21,7 +21,9 @@
   },
   "private": true,
   "workspaces": {
-    "packages": ["packages/*"],
+    "packages": [
+      "packages/*"
+    ],
     "nohoist": []
   },
   "scripts": {
@@ -219,6 +221,7 @@
     "jquery-slimscroll": "^1.3.8",
     "jquery-ui": "^1.12.1",
     "jquery.cookie": "~1.4.1",
+    "lerna": "^4.0.0",
     "load-css-file": "^1.0.0",
     "lodash-webpack-plugin": "^0.11.5",
     "markdown-it": "^10.0.0",

+ 0 - 1
packages/slack/.gitignore

@@ -1 +0,0 @@
-/lib/

+ 5 - 0
packages/slack/package.json

@@ -2,7 +2,11 @@
   "name": "@growi/slack",
   "version": "0.9.0-RC",
   "license": "MIT",
+  "main": "dist/index.js",
+  "typings": "dist/index.d.ts",
   "scripts": {
+    "build": "yarn tsc",
+    "tsc": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
     "test": "yarn test:lint && yarn test:coverage",
     "test:unit": "cross-env NODE_ENV=test jest --passWithNoTests",
     "test:coverage": "yarn test:unit",
@@ -24,6 +28,7 @@
     "eslint-import-resolver-typescript": "^2.4.0",
     "eslint-plugin-jest": "^24.3.2",
     "ts-jest": "^26.5.4",
+    "tsc-alias": "^1.2.9",
     "typescript": "^4.2.3"
   }
 }

+ 4 - 0
packages/slack/src/index.ts

@@ -6,3 +6,7 @@ export const supportedGrowiCommands: string[] = [
   'search',
   'create',
 ];
+
+export * from './interfaces/growi-command';
+export * from './models/errors';
+export * from './utils/slash-command-parser';

+ 7 - 7
packages/slack/src/utils/slash-command-parser.test.ts

@@ -1,13 +1,13 @@
 import { SlashCommand } from '@slack/bolt';
 import { InvalidGrowiCommandError } from '../models/errors';
 
-import { parse } from './slash-command-parser';
+import { parseSlashCommand } from './slash-command-parser';
 
 const SlashCommandMock = jest.fn<SlashCommand, [string]>().mockImplementation((text) => {
   return { text } as SlashCommand;
 });
 
-describe('parse SlashCommand', () => {
+describe('parseSlashCommand', () => {
 
   describe('without growiCommandType', () => {
     test('throws InvalidGrowiCommandError', () => {
@@ -17,7 +17,7 @@ describe('parse SlashCommand', () => {
 
       // when/then
       expect(() => {
-        parse(slashCommand);
+        parseSlashCommand(slashCommand);
       }).toThrowError(InvalidGrowiCommandError);
     });
   });
@@ -28,7 +28,7 @@ describe('parse SlashCommand', () => {
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when
-    const result = parse(slashCommand);
+    const result = parseSlashCommand(slashCommand);
 
     // then
     expect(result.text).toBe(slashCommandText);
@@ -42,7 +42,7 @@ describe('parse SlashCommand', () => {
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when
-    const result = parse(slashCommand);
+    const result = parseSlashCommand(slashCommand);
 
     // then
     expect(result.text).toBe(slashCommandText);
@@ -56,7 +56,7 @@ describe('parse SlashCommand', () => {
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when
-    const result = parse(slashCommand);
+    const result = parseSlashCommand(slashCommand);
 
     // then
     expect(result.text).toBe(slashCommandText);
@@ -70,7 +70,7 @@ describe('parse SlashCommand', () => {
     const slashCommand = new SlashCommandMock(slashCommandText);
 
     // when
-    const result = parse(slashCommand);
+    const result = parseSlashCommand(slashCommand);
 
     // then
     expect(result.text).toBe(slashCommandText);

+ 1 - 1
packages/slack/src/utils/slash-command-parser.ts

@@ -1,7 +1,7 @@
 import { GrowiCommand } from '../interfaces/growi-command';
 import { InvalidGrowiCommandError } from '../models/errors';
 
-export const parse = (slashCommand:{[key:string]:string}): GrowiCommand => {
+export const parseSlashCommand = (slashCommand:{[key:string]:string}): GrowiCommand => {
   const trimmedText = slashCommand.text.trim();
   const splitted = trimmedText.split(' ');
 

+ 1 - 2
packages/slack/tsconfig.build.json

@@ -2,8 +2,7 @@
   "extends": "./tsconfig.json",
   "compilerOptions": {
     "rootDir": "./src",
-    "outDir": "../../dist/slack",
-    "composite": true,
+    "outDir": "dist",
     "declaration": true,
     "noResolve": false,
     "preserveConstEnums": true,

+ 1 - 0
packages/slack/tsconfig.json

@@ -7,6 +7,7 @@
   },
   "exclude": [
     "node_modules",
+    "dist",
     "**/*.test.ts"
   ]
 }

+ 0 - 1
packages/slackbot-proxy/.gitignore

@@ -1 +0,0 @@
-/dist/

+ 3 - 3
packages/slackbot-proxy/package.json

@@ -4,12 +4,12 @@
   "license": "MIT",
   "scripts": {
     "build": "yarn tsc",
-    "tsc": "tsc -b --verbose tsconfig.build.json && tsc-alias -p tsconfig.build.json",
+    "tsc": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
     "tsc:w": "tsc -w",
     "dev:ci": "yarn dev --ci",
     "dev": "cross-env NODE_ENV=development ts-node-dev -r tsconfig-paths/register -r dotenv-flow/config src/index.ts",
     "start:prod:ci": "yarn start:prod --ci",
-    "start:prod": "cross-env NODE_ENV=production node -r dotenv-flow/config ../../dist/slackbot-proxy/index.js",
+    "start:prod": "cross-env NODE_ENV=production node -r dotenv-flow/config dist/index.js",
     "test": "yarn test:lint && yarn test:coverage",
     "test:unit": "cross-env NODE_ENV=test jest --passWithNoTests",
     "test:coverage": "yarn test:unit",
@@ -41,7 +41,7 @@
     "ts-jest": "^26.5.4",
     "ts-node": "^9.1.1",
     "ts-node-dev": "^1.1.6",
-    "tsc-alias": "1.2.6",
+    "tsc-alias": "^1.2.9",
     "tsconfig-paths": "^3.9.0",
     "typescript": "^4.2.3"
   }

+ 2 - 2
packages/slackbot-proxy/src/entities/relation.ts

@@ -1,10 +1,10 @@
 import {
-  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne,Index
+  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
 } from 'typeorm';
 import { Installation } from './installation';
 
 @Entity()
-@Index(["installation", "growiUri"], { unique: true })
+@Index(['installation', 'growiUri'], { unique: true })
 export class Relation {
 
   @PrimaryGeneratedColumn()

+ 2 - 2
packages/slackbot-proxy/src/services/RecieveService.ts

@@ -1,11 +1,11 @@
 import { Service } from '@tsed/di';
-import { parse } from '@growi/slack/utils/slash-command-parser';
+import { parseSlashCommand } from '@growi/slack';
 
 @Service()
 export class ReceiveService {
 
   receiveContentsFromSlack(body:{[key:string]:string}) : string {
-    const parseBody = parse(body);
+    const parseBody = parseSlashCommand(body);
     if (parseBody.growiCommandType === 'register') {
       console.log('register action occured');
       return 'register action occurd';

+ 1 - 0
packages/slackbot-proxy/tsconfig.base.json

@@ -5,6 +5,7 @@
   "exclude": [
     "node_modules",
     "config",
+    "dist",
     "**/*.test.ts"
   ]
 }

+ 3 - 7
packages/slackbot-proxy/tsconfig.build.json

@@ -2,7 +2,7 @@
   "extends": "./tsconfig.base.json",
   "compilerOptions": {
     "rootDir": "./src",
-    "outDir": "../../dist/slackbot-proxy",
+    "outDir": "dist",
     "declaration": true,
     "noResolve": false,
     "preserveConstEnums": true,
@@ -11,11 +11,7 @@
     "inlineSources": true,
     "baseUrl": "./src",
     "paths": {
-      "~/*": ["./*"],
-      "@growi/*": ["../../../dist/*"]
+      "~/*": ["./*"]
     }
-  },
-  "references": [
-    { "path": "../slack/tsconfig.build.json" }
-  ]
+  }
 }

+ 1 - 1
packages/slackbot-proxy/tsconfig.json

@@ -4,7 +4,7 @@
     "baseUrl": ".",
     "paths": {
       "~/*": ["./src/*"],
-      "@growi/slack/*": ["../slack/src/*"],
+      "@growi/*": ["../*/src"]
     }
   }
 }

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

@@ -4,6 +4,7 @@ const logger = loggerFactory('growi:routes:apiv3:notification-setting');
 const express = require('express');
 const { body } = require('express-validator');
 const crypto = require('crypto');
+const { WebClient, LogLevel } = require('@slack/web-api');
 const ErrorV3 = require('../../models/vo/error-apiv3');
 
 const router = express.Router();
@@ -55,6 +56,9 @@ module.exports = (crowi) => {
       body('currentBotType')
         .isIn(['official-bot', 'custom-bot-without-proxy', 'custom-bot-with-proxy']),
     ],
+    NotificationTestToSlackWorkSpace: [
+      body('channel').isString(),
+    ],
   };
 
   async function updateSlackBotSettings(params) {
@@ -268,6 +272,52 @@ module.exports = (crowi) => {
     }
   });
 
+  /**
+   * @swagger
+   *
+   *    /slack-integration/test-notification-to-slack-work-space:
+   *      post:
+   *        tags: [SlackTestToWorkSpace]
+   *        operationId: postSlackMessageToSlackWorkSpace
+   *        summary: test to send message to slack work space
+   *        description: post message to slack work space
+   *        responses:
+   *          200:
+   *            description: Succeeded to send a message to slack work space
+   */
+  router.post('/notification-test-to-slack-work-space',
+    loginRequiredStrictly, adminRequired, csrf, validator.NotificationTestToSlackWorkSpace, apiV3FormValidator, async(req, res) => {
+      const isConnectedToSlack = crowi.slackBotService.isConnectedToSlack;
+      const { channel } = req.body;
+
+      if (isConnectedToSlack === false) {
+        const msg = 'Bot User OAuth Token is not setup.';
+        logger.error('Error', msg);
+        return res.apiv3Err(new ErrorV3(msg, 'not-setup-slack-bot-token', 400));
+      }
+
+      const slackBotToken = crowi.configManager.getConfig('crowi', 'slackbot:token');
+      this.client = new WebClient(slackBotToken, { logLevel: LogLevel.DEBUG });
+      logger.debug('SlackBot: setup is done');
+
+      try {
+        this.client.chat.postMessage({
+          channel: `#${channel}`,
+          text: 'Your test was successful!',
+        });
+        logger.info(`SlackTest: send success massage to slack work space at #${channel}.`);
+        logger.info(`If you do not receive a message, you may not have invited the bot to the #${channel} channel.`);
+        // eslint-disable-next-line max-len
+        const message = `Successfully send message to Slack work space. See #general channel. If you do not receive a message, you may not have invited the bot to the #${channel} channel.`;
+        return res.apiv3({ message });
+      }
+      catch (error) {
+        const msg = 'Error occured in testing to notify slack work space';
+        logger.error('Error', error);
+        return res.apiv3Err(new ErrorV3(msg, 'notification-test-slack-work-space-failed'), 500);
+      }
+    });
+
   /**
    * @swagger
    *

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 881 - 34
yarn.lock


Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно