├── .eslintignore ├── config ├── config.prod.ts ├── config.local.ts ├── plugin.ts └── config.default.ts ├── typings ├── app │ ├── index.d.ts │ ├── middleware │ │ └── index.d.ts │ ├── controller │ │ └── index.d.ts │ ├── model │ │ └── index.d.ts │ └── service │ │ └── index.d.ts ├── config │ ├── index.d.ts │ └── plugin.d.ts └── index.d.ts ├── .travis.yml ├── .gitignore ├── appveyor.yml ├── test └── app │ ├── controller │ └── home.test.ts │ └── service │ └── Test.test.ts ├── .sequelizerc ├── app ├── io │ ├── middleware │ │ ├── packet.ts │ │ └── connection.ts │ └── controller │ │ └── nsp.ts ├── exceptions │ └── http_exceptions.ts ├── model │ ├── user.ts │ ├── workflowTpl.ts │ ├── process.ts │ ├── branch.ts │ ├── testRecord.ts │ └── project.ts ├── router.ts ├── helper │ ├── api │ │ ├── gitLab │ │ │ ├── token.ts │ │ │ ├── user.ts │ │ │ ├── project.ts │ │ │ ├── branch.ts │ │ │ └── merge.ts │ │ └── jenkins │ │ │ └── index.ts │ ├── robot │ │ └── ding.ts │ ├── utils │ │ ├── http.ts │ │ └── mail.ts │ └── emailTpl │ │ ├── pass.njk │ │ ├── repulse.njk │ │ └── email.njk ├── service │ ├── build.ts │ ├── process.ts │ ├── project.ts │ ├── user.ts │ └── branch.ts ├── controller │ ├── base.ts │ ├── project.ts │ ├── branch.ts │ ├── build.ts │ ├── process.ts │ ├── user.ts │ └── notices.ts ├── config │ └── default.config.ts └── middleware │ ├── jwt_auth.ts │ └── error_handler.ts ├── .autod.conf.js ├── database ├── migrations │ ├── 20200804130302-init-workflow.js │ ├── 20200804130311-init-workflowTpl.js │ ├── 20200802082448-init-users.js │ ├── 20200802095029-init-process.js │ ├── 20200816014620-init-testRecord.js │ ├── 20200815135841-init-process.js │ ├── 20200802095012-init-projcet.js │ ├── 20200802095017-init-branch.js │ └── 20200807182349-init-projcet.js └── config.json ├── README.md ├── tsconfig.json ├── models └── index.js ├── .github └── workflows │ └── nodejs.yml ├── .eslintrc └── package.json /.eslintignore: -------------------------------------------------------------------------------- 1 | **/*.d.ts 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /config/config.prod.ts: -------------------------------------------------------------------------------- 1 | import { EggAppConfig, PowerPartial } from 'egg'; 2 | 3 | export default () => { 4 | const config: PowerPartial = {}; 5 | return config; 6 | }; 7 | -------------------------------------------------------------------------------- /config/config.local.ts: -------------------------------------------------------------------------------- 1 | import { EggAppConfig, PowerPartial } from 'egg'; 2 | 3 | export default () => { 4 | const config: PowerPartial = {}; 5 | return config; 6 | }; 7 | -------------------------------------------------------------------------------- /typings/app/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | export * from 'egg'; 6 | export as namespace Egg; 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | language: node_js 3 | node_js: 4 | - '8' 5 | before_install: 6 | - npm i npminstall -g 7 | install: 8 | - npminstall 9 | script: 10 | - npm run ci 11 | after_script: 12 | - npminstall codecov && codecov 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | logs/ 8 | .DS_Store 9 | .vscode 10 | *.swp 11 | *.lock 12 | !.autod.conf.js 13 | 14 | app/**/*.js 15 | test/**/*.js 16 | config/**/*.js 17 | app/**/*.map 18 | test/**/*.map 19 | config/**/*.map 20 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '8' 4 | 5 | install: 6 | - ps: Install-Product node $env:nodejs_version 7 | - npm i npminstall && node_modules\.bin\npminstall 8 | 9 | test_script: 10 | - node --version 11 | - npm --version 12 | - npm run test 13 | 14 | build: off 15 | -------------------------------------------------------------------------------- /test/app/controller/home.test.ts: -------------------------------------------------------------------------------- 1 | import * as assert from 'assert'; 2 | import { app } from 'egg-mock/bootstrap'; 3 | 4 | describe('test/app/controller/home.test.ts', () => { 5 | it('should GET /', async () => { 6 | const result = await app.httpRequest().get('/').expect(200); 7 | assert(result.text === 'hi, egg'); 8 | }); 9 | }); 10 | -------------------------------------------------------------------------------- /.sequelizerc: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | 5 | module.exports = { 6 | config: path.join(__dirname, 'database/config.json'), 7 | 'migrations-path': path.join(__dirname, 'database/migrations'), 8 | 'seeders-path': path.join(__dirname, 'database/seeders'), 9 | 'models-path': path.join(__dirname, 'app/model'), 10 | }; -------------------------------------------------------------------------------- /typings/config/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | import { EggAppConfig } from 'egg'; 6 | import ExportConfigDefault from '../../config/config.default'; 7 | type ConfigDefault = ReturnType; 8 | type NewEggAppConfig = ConfigDefault; 9 | declare module 'egg' { 10 | interface EggAppConfig extends NewEggAppConfig { } 11 | } -------------------------------------------------------------------------------- /typings/app/middleware/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | import ExportErrorHandler from '../../../app/middleware/error_handler'; 6 | import ExportJwtAuth from '../../../app/middleware/jwt_auth'; 7 | 8 | declare module 'egg' { 9 | interface IMiddleware { 10 | errorHandler: typeof ExportErrorHandler; 11 | jwtAuth: typeof ExportJwtAuth; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/app/service/Test.test.ts: -------------------------------------------------------------------------------- 1 | import * as assert from 'assert'; 2 | import { Context } from 'egg'; 3 | import { app } from 'egg-mock/bootstrap'; 4 | 5 | describe('test/app/service/Test.test.js', () => { 6 | let ctx: Context; 7 | 8 | before(async () => { 9 | ctx = app.mockContext(); 10 | }); 11 | 12 | it('sayHi', async () => { 13 | const result = await ctx.service.test.sayHi('egg'); 14 | assert(result === 'hi, egg'); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /app/io/middleware/packet.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-07 14:52:29 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-09 08:31:22 6 | * @FilePath: /processSystem/app/io/middleware/packet.ts 7 | * @Description: 消息预处理 8 | */ 9 | 10 | export default (app) => { 11 | return async (ctx, next) => { 12 | ctx.socket.emit("res", "packet received!"); 13 | console.log("packet:", ctx.packet); 14 | await next(); 15 | }; 16 | }; 17 | -------------------------------------------------------------------------------- /typings/index.d.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-30 09:58:22 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 11:57:05 6 | * @FilePath: /processSystem/typings/index.d.ts 7 | * @Description: 重写 8 | */ 9 | 10 | import "egg"; 11 | 12 | declare module "egg" { 13 | interface Application { } 14 | interface CustomController { 15 | nsp: any; 16 | } 17 | 18 | interface EggSocketNameSpace { 19 | emit: any 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | plugin: 'autod-egg', 6 | prefix: '^', 7 | devprefix: '^', 8 | exclude: [ 9 | 'test/fixtures', 10 | 'coverage', 11 | ], 12 | dep: [ 13 | 'egg', 14 | 'egg-scripts', 15 | ], 16 | devdep: [ 17 | 'autod', 18 | 'autod-egg', 19 | 'egg-bin', 20 | 'tslib', 21 | 'typescript', 22 | ], 23 | keep: [ 24 | ], 25 | semver: [ 26 | ], 27 | test: 'scripts', 28 | }; 29 | -------------------------------------------------------------------------------- /app/exceptions/http_exceptions.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-07 23:16:01 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-09 08:12:11 6 | * @Description: 7 | */ 8 | 9 | export default class HttpExceptions extends Error { 10 | code: number; 11 | msg: string; 12 | httpCode: number; 13 | 14 | constructor({ msg = "服务器异常", code = 1, httpCode = 400 }) { 15 | super(); 16 | this.msg = msg; 17 | this.code = code; 18 | this.httpCode = httpCode; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/io/middleware/connection.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-07 14:51:09 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:14:34 6 | * @FilePath: /processSystem/app/io/middleware/connection.ts 7 | * @Description: socket.io 鉴权 8 | */ 9 | 10 | export default (app) => { 11 | return async (ctx, next) => { 12 | ctx.socket.emit("res", "connected!"); 13 | await next(); 14 | 15 | // execute when disconnect. 16 | console.log("disconnection!"); 17 | }; 18 | }; 19 | -------------------------------------------------------------------------------- /database/migrations/20200804130302-init-workflow.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | up: async (queryInterface, Sequelize) => { 3 | const { INTEGER, DATE, STRING } = Sequelize; 4 | await queryInterface.createTable('workflow', { 5 | id: { type: INTEGER, primaryKey: true, }, 6 | name: STRING(30), 7 | workflow_name: STRING(30), 8 | created_at: DATE, 9 | updated_at: DATE, 10 | }); 11 | }, 12 | down: async queryInterface => { 13 | await queryInterface.dropTable('users'); 14 | }, 15 | }; 16 | -------------------------------------------------------------------------------- /app/model/user.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-06 10:07:16 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2019-09-16 16:08:23 6 | * @Description: 7 | */ 8 | 9 | export default app => { 10 | const { STRING, INTEGER, DATE } = app.Sequelize; 11 | 12 | const User = app.model.define('user', { 13 | id: { type: INTEGER, primaryKey: true }, 14 | name: STRING(30), 15 | username: STRING(30), 16 | email: STRING(100), 17 | avatarUrl: STRING(200), 18 | webUrl: STRING(200), 19 | }); 20 | 21 | return User; 22 | }; 23 | -------------------------------------------------------------------------------- /database/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "development": { 3 | "username": "root", 4 | "password": "root123456", 5 | "database": "devops_dev", 6 | "host": "127.0.0.1", 7 | "dialect": "mysql" 8 | }, 9 | "test": { 10 | "username": "root", 11 | "password": "root123456", 12 | "database": "devops_test", 13 | "host": "127.0.0.1", 14 | "dialect": "mysql" 15 | }, 16 | "production": { 17 | "username": "root", 18 | "password": "root123456", 19 | "database": "devops_production", 20 | "host": "127.0.0.1", 21 | "dialect": "mysql" 22 | } 23 | } -------------------------------------------------------------------------------- /app/router.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 14:00:36 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:52:12 6 | * @FilePath: /processSystem/app/router.ts 7 | * @Description: 8 | */ 9 | import { Application } from "egg"; 10 | import { EggShell } from "egg-shell-decorators"; 11 | 12 | export default (app: Application) => { 13 | const { router, controller, io } = app; 14 | 15 | EggShell(app); 16 | 17 | // socket.io 18 | io.of('/').route('io/server', io.controller.nsp.ping); 19 | io.of('/').route('creatJob', io.controller.nsp.creatJob); 20 | }; 21 | -------------------------------------------------------------------------------- /database/migrations/20200804130311-init-workflowTpl.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | up: async (queryInterface, Sequelize) => { 3 | const { UUID, DATE, STRING, UUIDV4 } = Sequelize; 4 | await queryInterface.createTable('workflowTpl', { 5 | id: { 6 | type: UUID, 7 | allowNull: false, 8 | defaultValue: UUIDV4, 9 | }, 10 | name: STRING(30), 11 | workflow_ids: STRING(30), 12 | created_at: DATE, 13 | updated_at: DATE, 14 | }); 15 | }, 16 | down: async queryInterface => { 17 | await queryInterface.dropTable('users'); 18 | }, 19 | }; 20 | -------------------------------------------------------------------------------- /database/migrations/20200802082448-init-users.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | up: async (queryInterface, Sequelize) => { 3 | const { INTEGER, DATE, STRING } = Sequelize; 4 | await queryInterface.createTable('users', { 5 | id: { type: INTEGER, primaryKey: true, }, 6 | name: STRING(30), 7 | username: STRING(30), 8 | email: STRING(100), 9 | avatar_url: STRING(200), 10 | web_url: STRING(200), 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }); 14 | }, 15 | down: async queryInterface => { 16 | await queryInterface.dropTable('users'); 17 | }, 18 | }; 19 | -------------------------------------------------------------------------------- /config/plugin.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 14:00:36 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 16:20:46 6 | * @FilePath: /processSystem/config/plugin.ts 7 | * @Description: 8 | */ 9 | import { EggPlugin } from 'egg'; 10 | 11 | const plugin: EggPlugin = { 12 | static: true, 13 | sequelize: { 14 | enable: true, 15 | package: 'egg-sequelize', 16 | }, 17 | jwt: { 18 | enable: true, 19 | package: 'egg-jwt', 20 | }, 21 | cors: { 22 | enable: true, 23 | package: 'egg-cors', 24 | }, 25 | helper: { 26 | enable: true, 27 | package: 'egg-helper', 28 | }, 29 | io: { 30 | enable: true, 31 | package: 'egg-socket.io', 32 | }, 33 | }; 34 | 35 | export default plugin; 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 8 | 9 | ## QuickStart 10 | 11 | [基于 Node 的 DevOps 实战](https://juejin.cn/book/6948353204648148995) 12 | 13 | 根据教程安装项目环境 14 | 15 | 新建数据库 devops_dev 16 | 17 | ```bash 18 | $ yarn 19 | $ npx sequelize db:migrate // 执行数据库变更 20 | ``` 21 | 22 | ### Development 23 | 24 | ```bash 25 | $ yarn dev 26 | $ open http://localhost:7001/ 27 | ``` 28 | ### Deploy 29 | 30 | ```bash 31 | $ npm run tsc 32 | $ npm start 33 | ``` 34 | 35 | ### Npm Scripts 36 | 37 | - Use `npm run lint` to check code style 38 | - Use `npm test` to run unit test 39 | - se `npm run clean` to clean compiled js at development mode once 40 | 41 | ### Requirement 42 | 43 | - Node.js 8.x 44 | - Typescript 2.8+ 45 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": true, 3 | "compilerOptions": { 4 | "target": "esnext", 5 | "module": "commonjs", 6 | "strict": true, 7 | "noImplicitAny": false, 8 | "experimentalDecorators": true, 9 | "emitDecoratorMetadata": true, 10 | "charset": "utf8", 11 | "allowJs": false, 12 | "pretty": true, 13 | "noEmitOnError": false, 14 | "noUnusedLocals": false, 15 | "noUnusedParameters": false, 16 | "allowUnreachableCode": false, 17 | "allowUnusedLabels": false, 18 | "strictPropertyInitialization": false, 19 | "noFallthroughCasesInSwitch": true, 20 | "skipLibCheck": true, 21 | "skipDefaultLibCheck": true, 22 | "inlineSourceMap": true, 23 | "importHelpers": true 24 | }, 25 | "exclude": [ 26 | "app/public", 27 | "app/views", 28 | "node_modules*" 29 | ] 30 | } -------------------------------------------------------------------------------- /typings/app/controller/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | import ExportBase from '../../../app/controller/base'; 6 | import ExportBranch from '../../../app/controller/branch'; 7 | import ExportBuild from '../../../app/controller/build'; 8 | import ExportNotices from '../../../app/controller/notices'; 9 | import ExportProcess from '../../../app/controller/process'; 10 | import ExportProject from '../../../app/controller/project'; 11 | import ExportUser from '../../../app/controller/user'; 12 | 13 | declare module 'egg' { 14 | interface IController { 15 | base: ExportBase; 16 | branch: ExportBranch; 17 | build: ExportBuild; 18 | notices: ExportNotices; 19 | process: ExportProcess; 20 | project: ExportProject; 21 | user: ExportUser; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /typings/app/model/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | import ExportBranch from '../../../app/model/branch'; 6 | import ExportProcess from '../../../app/model/process'; 7 | import ExportProject from '../../../app/model/project'; 8 | import ExportTestRecord from '../../../app/model/testRecord'; 9 | import ExportUser from '../../../app/model/user'; 10 | import ExportWorkflowTpl from '../../../app/model/workflowTpl'; 11 | 12 | declare module 'egg' { 13 | interface IModel { 14 | Branch: ReturnType; 15 | Process: ReturnType; 16 | Project: ReturnType; 17 | TestRecord: ReturnType; 18 | User: ReturnType; 19 | WorkflowTpl: ReturnType; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/helper/api/gitLab/token.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-29 14:17:50 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-22 19:28:51 6 | * @Description: gitLab 分支模块 api 7 | */ 8 | 9 | import AJAX from "../../utils/http"; 10 | 11 | module.exports = (app) => { 12 | /** 13 | * @author: Cookie 14 | * @description: 获取分支列表 15 | */ 16 | const getDeployTokens = async ({ projectId, access_token }) => { 17 | try { 18 | const { data, code } = await AJAX(app).methodV({ 19 | url: `/projects/${projectId}/deploy_tokens`, 20 | method: "GET", 21 | query: { 22 | access_token, 23 | }, 24 | }); 25 | switch (code) { 26 | case 200: { 27 | return data; 28 | } 29 | default: { 30 | return { msg: data }; 31 | } 32 | } 33 | } catch (e) { 34 | return { msg: e }; 35 | } 36 | }; 37 | 38 | return { 39 | getDeployTokens, 40 | }; 41 | }; 42 | -------------------------------------------------------------------------------- /app/helper/api/gitLab/user.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-29 14:17:50 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-05 17:08:13 6 | * @Description: gitLab 用户模块 api 7 | */ 8 | 9 | import AJAX from "../../utils/http"; 10 | 11 | module.exports = (app) => { 12 | 13 | const getUserInfo = async ({ access_token }) => { 14 | const { data: userInfo } = await AJAX(app).methodV({ 15 | url: "/user", 16 | method: "GET", 17 | query: { 18 | access_token, 19 | }, 20 | }); 21 | 22 | return userInfo; 23 | }; 24 | 25 | const getUsers = async ({ params = {}, pageNum = 1, pageSize = 100 }) => { 26 | const { data } = await AJAX(app).methodV({ 27 | url: "/users", 28 | params: { 29 | ...params, 30 | per_page: pageSize, 31 | page: pageNum, 32 | }, 33 | method: "GET", 34 | }); 35 | return data; 36 | }; 37 | 38 | return { 39 | getUserInfo, 40 | getUsers, 41 | }; 42 | }; 43 | -------------------------------------------------------------------------------- /app/service/build.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-05 16:28:21 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:00:37 6 | * @Description: 构建 7 | */ 8 | import { Service } from "egg"; 9 | 10 | export default class Build extends Service { 11 | /** 12 | * @author: Cookie 13 | * @description: 构建项目 14 | */ 15 | public async buildProject({ 16 | type = "h5", 17 | projectName, 18 | projectVersion, 19 | projectGitPath, 20 | branchName, 21 | buildPath, 22 | cache = false, 23 | }) { 24 | const { ctx } = this; 25 | const callBack = await ctx.helper.api.jenkins.index.buildJenkins({ 26 | type, 27 | job: "fe-base", 28 | params: { 29 | PROJECT_NAME: projectName, 30 | PROJECT_VERSION: projectVersion, 31 | PROJECT_GIT_PATH: projectGitPath, 32 | BRANCH_NAME: branchName, 33 | BUILD_PATH: buildPath, 34 | CACHE: cache, 35 | }, 36 | }); 37 | return callBack; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/model/workflowTpl.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-06 10:07:16 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 10:53:59 6 | * @Description: 7 | */ 8 | 9 | export default app => { 10 | const { UUID, STRING, UUIDV4 } = app.Sequelize; 11 | 12 | const WorkflowTpl = app.model.define( 13 | 'workflowTpl', 14 | { 15 | id: { 16 | type: UUID, 17 | allowNull: false, 18 | defaultValue: UUIDV4, 19 | primaryKey: true, 20 | }, 21 | name: STRING(30), 22 | workflowIds: { 23 | type: STRING(30), 24 | set(val, name) { 25 | const vals = val && val.length > 0 ? val.join(',') : ''; 26 | (this as any).setDataValue(name, vals); 27 | }, 28 | get(val) { 29 | const value = (this as any).getDataValue(val); 30 | return value ? value.split(',') : []; 31 | }, 32 | }, 33 | }, 34 | { freezeTableName: true }, 35 | ); 36 | 37 | return WorkflowTpl; 38 | }; 39 | -------------------------------------------------------------------------------- /app/controller/base.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-02 12:45:19 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-05 22:30:44 6 | * @Description: 基础类 Controller 7 | */ 8 | 9 | import { Controller } from "egg"; 10 | import HttpExceptions from "../exceptions/http_exceptions"; 11 | 12 | export default class BaseController extends Controller { 13 | /** 14 | * @author: Cookie 15 | * @description: 全局用户信息 16 | */ 17 | get user() { 18 | return this.ctx.user.userToken; 19 | } 20 | 21 | get userInfo() { 22 | return this.ctx.user.userInfo; 23 | } 24 | 25 | /** 26 | * @author: Cookie 27 | * @description: 成功回调 28 | */ 29 | success(data) { 30 | this.ctx.body = { 31 | code: 0, 32 | data, 33 | }; 34 | } 35 | 36 | /** 37 | * @author: Cookie 38 | * @description: 根据业务返回不同的错误 code,提供给前端做业务判断处理 39 | */ 40 | error({ msg = "服务器异常", code = 1, httpCode = 400 }) { 41 | throw new HttpExceptions({ 42 | code, 43 | httpCode, 44 | msg, 45 | }); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/config/default.config.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:29:27 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 16:21:25 6 | * @Description: 基础参数配置 7 | */ 8 | 9 | 10 | /** 11 | * @description: 这里的配置内容需要自己替换成本地的 12 | */ 13 | 14 | // 反向代理git url 15 | const GIT_URL = 'http://192.168.160.88:8888/'; 16 | 17 | // app 授权客户端id 与 秘钥 18 | const CLIENT_ID = '606e33d507674f99d1ac16877766eca0db448c26a6fdddf5b76e850dac0d2421'; 19 | const CLIENT_SECRET = '21346c11a255c64b25fd4b75ecbf80d0e702a992dc616acd741608905d61892f'; 20 | 21 | // 钉钉机器人 22 | 23 | 24 | const DING_SECRET = 25 | "SECc477ca6197e14dd888662eb22a33e1b38eb786130d154ed692d855b6f48e132e"; 26 | 27 | const DING_SEND_URL = 28 | "https://oapi.dingtalk.com/robot/send?access_token=5a576c01fdee6bf137a3e3826a3b768ecfc913000545fd67926e7228c57dabe8"; 29 | 30 | // 邮箱配置 31 | const MAIL_CONFIG = { 32 | user_email: '', 33 | service: '', 34 | port: '', 35 | auth_code: '' 36 | } 37 | 38 | 39 | export { GIT_URL, CLIENT_ID, CLIENT_SECRET, DING_SEND_URL, DING_SECRET, MAIL_CONFIG }; 40 | -------------------------------------------------------------------------------- /database/migrations/20200802095029-init-process.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-05 16:29:50 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 16:19:18 6 | * @Description: 7 | */ 8 | 'use strict'; 9 | 10 | module.exports = { 11 | up: async (queryInterface, Sequelize) => { 12 | const { 13 | DATE, STRING, INTEGER, UUID, UUIDV4, TEXT 14 | } = Sequelize; 15 | await queryInterface.createTable('process', { 16 | id: { 17 | type: UUID, 18 | allowNull: false, 19 | defaultValue: UUIDV4, 20 | }, 21 | branch_ids: { 22 | type: STRING(200), 23 | primaryKey: true, 24 | }, 25 | workflow_tpl_id: { 26 | type: UUID, 27 | allowNull: false, 28 | }, 29 | process_status: { 30 | type: INTEGER, 31 | defaultValue: 0, 32 | primaryKey: true, 33 | }, 34 | name: STRING(100), 35 | desc: TEXT('long'), 36 | created_at: DATE, 37 | updated_at: DATE, 38 | }); 39 | }, 40 | down: async (queryInterface, Sequelize) => { } 41 | }; 42 | -------------------------------------------------------------------------------- /typings/app/service/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | type AnyClass = new (...args: any[]) => any; 6 | type AnyFunc = (...args: any[]) => T; 7 | type CanExportFunc = AnyFunc> | AnyFunc>; 8 | type AutoInstanceType : T> = U extends AnyClass ? InstanceType : U; 9 | import ExportBranch from '../../../app/service/branch'; 10 | import ExportBuild from '../../../app/service/build'; 11 | import ExportProcess from '../../../app/service/process'; 12 | import ExportProject from '../../../app/service/project'; 13 | import ExportUser from '../../../app/service/user'; 14 | 15 | declare module 'egg' { 16 | interface IService { 17 | branch: AutoInstanceType; 18 | build: AutoInstanceType; 19 | process: AutoInstanceType; 20 | project: AutoInstanceType; 21 | user: AutoInstanceType; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /models/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const Sequelize = require('sequelize'); 6 | const basename = path.basename(__filename); 7 | const env = process.env.NODE_ENV || 'development'; 8 | const config = require(__dirname + '/../config/config.json')[env]; 9 | const db = {}; 10 | 11 | let sequelize; 12 | if (config.use_env_variable) { 13 | sequelize = new Sequelize(process.env[config.use_env_variable], config); 14 | } else { 15 | sequelize = new Sequelize(config.database, config.username, config.password, config); 16 | } 17 | 18 | fs 19 | .readdirSync(__dirname) 20 | .filter(file => { 21 | return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); 22 | }) 23 | .forEach(file => { 24 | const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); 25 | db[model.name] = model; 26 | }); 27 | 28 | Object.keys(db).forEach(modelName => { 29 | if (db[modelName].associate) { 30 | db[modelName].associate(db); 31 | } 32 | }); 33 | 34 | db.sequelize = sequelize; 35 | db.Sequelize = Sequelize; 36 | 37 | module.exports = db; 38 | -------------------------------------------------------------------------------- /database/migrations/20200816014620-init-testRecord.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-16 09:46:20 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-16 10:31:31 6 | * @Description: 7 | */ 8 | 'use strict'; 9 | 10 | module.exports = { 11 | up: async (queryInterface, Sequelize) => { 12 | const { INTEGER, DATE, STRING, UUIDV4, UUID } = Sequelize; 13 | await queryInterface.createTable('testRecord', { 14 | id: { 15 | type: UUID, 16 | allowNull: false, 17 | defaultValue: UUIDV4, 18 | primaryKey: true, 19 | }, 20 | name: STRING(30), 21 | desc: STRING(200), 22 | submit_user_id: STRING(300), 23 | test_user_id: STRING(300), 24 | branch_ids: { 25 | type: STRING(1000), 26 | }, 27 | test_status: { 28 | type: INTEGER, 29 | }, 30 | created_at: DATE, 31 | updated_at: DATE, 32 | }); 33 | }, 34 | 35 | down: async (queryInterface, Sequelize) => { 36 | /** 37 | * Add reverting commands here. 38 | * 39 | * Example: 40 | * await queryInterface.dropTable('users'); 41 | */ 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /database/migrations/20200815135841-init-process.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-15 21:58:41 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-16 00:21:39 6 | * @Description: 7 | */ 8 | 'use strict'; 9 | 10 | module.exports = { 11 | up: async (queryInterface, Sequelize) => { 12 | /** 13 | * Add altering commands here. 14 | * 15 | * Example: 16 | * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); 17 | */ 18 | const { 19 | DATE, STRING, INTEGER, UUID, UUIDV4 20 | } = Sequelize; 21 | return queryInterface.sequelize.transaction(t => { 22 | return Promise.all([ 23 | queryInterface.addColumn('process', 'created_user', { 24 | type: STRING(100) 25 | }, { transaction: t }), 26 | queryInterface.addColumn('process', 'update_user', { 27 | type: STRING(100) 28 | }, { transaction: t }), 29 | ]); 30 | }); 31 | }, 32 | 33 | down: async (queryInterface, Sequelize) => { 34 | /** 35 | * Add reverting commands here. 36 | * 37 | * Example: 38 | * await queryInterface.dropTable('users'); 39 | */ 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /database/migrations/20200802095012-init-projcet.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-05 10:27:10 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2019-08-21 19:18:02 6 | * @Description: 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = { 12 | up: async (queryInterface, Sequelize) => { 13 | const { 14 | DATE, STRING, INTEGER, UUID, UUIDV4, 15 | } = Sequelize; 16 | await queryInterface.createTable('project', { 17 | id: { 18 | type: UUID, 19 | allowNull: false, 20 | defaultValue: UUIDV4, 21 | }, 22 | project_source: { 23 | type: INTEGER, 24 | defaultValue: 0, 25 | primaryKey: true, 26 | }, 27 | project_source_id: { 28 | type: STRING(30), 29 | primaryKey: true, 30 | }, 31 | created_at: DATE, 32 | updated_at: DATE, 33 | project_name: STRING(30), 34 | project_type: STRING(30), 35 | namespace: STRING(30), 36 | project_url: STRING(100), 37 | project_git_desc: STRING(200), 38 | project_desc: STRING(200), 39 | project_git_name: STRING(30), 40 | }); 41 | }, 42 | 43 | down: (queryInterface, Sequelize) => { 44 | }, 45 | }; 46 | -------------------------------------------------------------------------------- /typings/config/plugin.d.ts: -------------------------------------------------------------------------------- 1 | // This file is created by egg-ts-helper@1.25.9 2 | // Do not modify this file!!!!!!!!! 3 | 4 | import 'egg'; 5 | import 'egg-onerror'; 6 | import 'egg-session'; 7 | import 'egg-i18n'; 8 | import 'egg-watcher'; 9 | import 'egg-multipart'; 10 | import 'egg-security'; 11 | import 'egg-development'; 12 | import 'egg-logrotator'; 13 | import 'egg-schedule'; 14 | import 'egg-static'; 15 | import 'egg-jsonp'; 16 | import 'egg-view'; 17 | import 'egg-sequelize'; 18 | import 'egg-jwt'; 19 | import 'egg-cors'; 20 | import 'egg-helper'; 21 | import 'egg-socket.io'; 22 | import { EggPluginItem } from 'egg'; 23 | declare module 'egg' { 24 | interface EggPlugin { 25 | onerror?: EggPluginItem; 26 | session?: EggPluginItem; 27 | i18n?: EggPluginItem; 28 | watcher?: EggPluginItem; 29 | multipart?: EggPluginItem; 30 | security?: EggPluginItem; 31 | development?: EggPluginItem; 32 | logrotator?: EggPluginItem; 33 | schedule?: EggPluginItem; 34 | static?: EggPluginItem; 35 | jsonp?: EggPluginItem; 36 | view?: EggPluginItem; 37 | sequelize?: EggPluginItem; 38 | jwt?: EggPluginItem; 39 | cors?: EggPluginItem; 40 | helper?: EggPluginItem; 41 | io?: EggPluginItem; 42 | } 43 | } -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | schedule: 12 | - cron: '0 2 * * *' 13 | 14 | jobs: 15 | build: 16 | runs-on: ${{ matrix.os }} 17 | 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | node-version: [8] 22 | os: [ubuntu-latest, windows-latest, macos-latest] 23 | 24 | steps: 25 | - name: Checkout Git Source 26 | uses: actions/checkout@v2 27 | 28 | - name: Use Node.js ${{ matrix.node-version }} 29 | uses: actions/setup-node@v1 30 | with: 31 | node-version: ${{ matrix.node-version }} 32 | 33 | - name: Install Dependencies 34 | run: npm i -g npminstall && npminstall 35 | 36 | - name: Continuous Integration 37 | run: npm run ci 38 | 39 | - name: Code Coverage 40 | uses: codecov/codecov-action@v1 41 | with: 42 | token: ${{ secrets.CODECOV_TOKEN }} 43 | -------------------------------------------------------------------------------- /app/middleware/jwt_auth.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-02 10:32:06 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 16:05:21 6 | * @Description: jwt 中间件 7 | */ 8 | const excludeUrl = ["/user/getUserToken", '/user/getTokenByApp']; 9 | 10 | export default () => { 11 | /** 12 | * @author: Cookie 13 | * @description: jwt 中间件,过滤白名单 + 验证登录权限 14 | */ 15 | const jwtAuth = async (ctx, next) => { 16 | if (excludeUrl.includes(ctx.request.path)) { 17 | await next(); 18 | return; 19 | } 20 | const token = ctx.cookies.get('authorization') || ctx.request.header.authorization 21 | 22 | if (token) { 23 | try { 24 | // 解码token 25 | const deCode = ctx.app.jwt.verify( 26 | token.replace("Bearer ", ""), 27 | ctx.app.config.jwt.secret 28 | ); 29 | console.log(deCode) 30 | ctx.user = deCode; 31 | await next(); 32 | } catch (error) { 33 | ctx.status = 401; 34 | ctx.body = { 35 | code: 401, 36 | message: error.message, 37 | }; 38 | } 39 | return; 40 | } 41 | ctx.status = 401; 42 | ctx.body = { 43 | code: 401, 44 | message: "验证失败", 45 | }; 46 | return; 47 | }; 48 | return jwtAuth; 49 | }; 50 | -------------------------------------------------------------------------------- /app/service/process.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-05 16:28:21 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-16 10:44:45 6 | * @Description: 流程 7 | */ 8 | import { Service } from "egg"; 9 | 10 | export default class Process extends Service { 11 | /** 12 | * @author: Cookie 13 | * @description: 创建任务流关联 branch,任务流模板以及需求 14 | */ 15 | public async createProcess({ 16 | desc, 17 | name, 18 | branchIds, 19 | workflowTplId, 20 | createdUser, 21 | updateUser, 22 | }) { 23 | const { ctx } = this; 24 | const workflowTpl = await ctx.model.Process.create({ 25 | desc, 26 | name, 27 | branchIds, 28 | workflowTplId, 29 | createdUser, 30 | updateUser, 31 | }); 32 | return workflowTpl; 33 | } 34 | 35 | /** 36 | * @author: Cookie 37 | * @description: 创建任务流关联 branch,任务流模板以及需求 38 | */ 39 | public async getProcessList({ pageSize = 10, pageNum = 1, opt = {} }) { 40 | const { ctx } = this; 41 | // 创建任务流模板 42 | let offset = (pageNum - 1) * pageSize; 43 | const processList = await ctx.model.Process.findAndCountAll({ 44 | where: { ...opt }, 45 | limit: pageSize, 46 | offset, 47 | order: [["created_at", "DESC"]], 48 | }); 49 | return processList && JSON.parse(JSON.stringify(processList)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/middleware/error_handler.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-07 23:16:50 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-09 08:15:22 6 | * @Description: 7 | */ 8 | import HttpExceptions from "../exceptions/http_exceptions"; 9 | 10 | export default () => { 11 | return async function errorHandler(ctx, next) { 12 | try { 13 | await next(); 14 | } catch (err) { 15 | // 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志 16 | ctx.app.emit("error", err, ctx); 17 | 18 | let status = err.status || 500; 19 | let error: any = {}; 20 | 21 | if (err instanceof HttpExceptions) { 22 | status = err.httpCode; 23 | error.requestUrl = `${ctx.method} : ${ctx.path}`; 24 | error.msg = err.msg; 25 | error.code = err.code; 26 | error.httpCode = err.httpCode; 27 | } else { 28 | // 未知异常,系统异常,线上不显示堆栈信息 29 | // 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息 30 | error.code = 500; 31 | error.errsInfo = 32 | status === 500 && ctx.app.config.env === "prod" 33 | ? "Internal Server Error" 34 | : err.message; 35 | } 36 | // 从 error 对象上读出各个属性,设置到响应中 37 | ctx.body = error; 38 | if (status === 422) { 39 | ctx.body.detail = err.errors; 40 | } 41 | ctx.status = status; 42 | } 43 | }; 44 | }; 45 | -------------------------------------------------------------------------------- /app/controller/project.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 11:57:56 6 | * @Description: 项目模块 Controller 7 | */ 8 | 9 | import { Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("project") 13 | export default class ProjectController extends BaseController { 14 | 15 | /** 16 | * @author: Cookie 17 | * @description: 获取 gitLab 对应自身的项目列表 18 | */ 19 | @Get("/getList") 20 | public async getProjectList({ request: { query } }) { 21 | const { ctx } = this; 22 | const { access_token } = this.user; 23 | const { id: userId } = this.userInfo; 24 | const { pageSize, pageNum } = query; 25 | const projectList = await ctx.service.project.getProjectList({ 26 | pageSize, 27 | pageNum, 28 | access_token, 29 | userId 30 | }); 31 | this.success(projectList); 32 | } 33 | 34 | /** 35 | * @author: Cookie 36 | * @description: 获取 gitLab 单个项目 37 | */ 38 | @Get("/get") 39 | public async getProject({ request: { query } }) { 40 | const { ctx } = this; 41 | const { projectId } = query; 42 | const { access_token } = this.user; 43 | const project = await ctx.service.project.getProject({ 44 | projectId, 45 | access_token 46 | }); 47 | 48 | this.success(project); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/model/process.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-06 10:07:16 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 10:53:38 6 | * @Description: 7 | */ 8 | 9 | export default app => { 10 | const { UUID, STRING, UUIDV4, INTEGER } = app.Sequelize; 11 | 12 | const Process = app.model.define( 13 | 'process', 14 | { 15 | id: { 16 | type: UUID, 17 | allowNull: false, 18 | defaultValue: UUIDV4, 19 | }, 20 | name: STRING(100), 21 | workflowTplId: { 22 | type: UUID, 23 | allowNull: false, 24 | }, 25 | processStatus: { 26 | type: INTEGER, 27 | defaultValue: 0, 28 | primaryKey: true, 29 | }, 30 | createdUser: STRING(100), 31 | updateUser: STRING(100), 32 | branchIds: { 33 | type: STRING(1000), 34 | set(val, name) { 35 | const vals = val && val.length > 0 ? val.join(',') : ''; 36 | console.log('vals====>', vals); 37 | (this as any).setDataValue(name, vals); 38 | }, 39 | get(val) { 40 | console.log('val====>', val); 41 | const value = (this as any).getDataValue(val); 42 | console.log('value====>', value); 43 | return value ? value.split(',') : []; 44 | }, 45 | }, 46 | }, 47 | { freezeTableName: true }, 48 | ); 49 | 50 | return Process; 51 | }; 52 | -------------------------------------------------------------------------------- /database/migrations/20200802095017-init-branch.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-24 18:49:54 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 16:46:08 6 | * @Description: 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = { 12 | up: async (queryInterface, Sequelize) => { 13 | const { 14 | DATE, STRING, UUID, UUIDV4, INTEGER, TEXT 15 | } = Sequelize; 16 | await queryInterface.createTable('branch', { 17 | id: { 18 | type: UUID, 19 | allowNull: false, 20 | defaultValue: UUIDV4, 21 | }, 22 | project_id: { 23 | type: UUID, 24 | primaryKey: true, 25 | allowNull: false, 26 | }, 27 | project_source_id: { 28 | type: STRING(30), 29 | primaryKey: true, 30 | }, 31 | process_id: { 32 | type: UUID, 33 | }, 34 | branch_name: STRING(30), 35 | branch_git_name: STRING(30), 36 | created_user: STRING(50), 37 | remarks: STRING(1000), 38 | update_user: STRING(100), 39 | branch_status: { 40 | type: INTEGER, 41 | defaultValue: 0, 42 | primaryKey: true, 43 | }, 44 | branch_next_status: { 45 | type: INTEGER, 46 | primaryKey: true, 47 | }, 48 | created_at: DATE, 49 | updated_at: DATE, 50 | commit: TEXT('long') 51 | }); 52 | }, 53 | 54 | down: (queryInterface, Sequelize) => { }, 55 | }; 56 | -------------------------------------------------------------------------------- /app/helper/robot/ding.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:59:18 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 11:55:35 6 | * @Description: request 模块 7 | */ 8 | 9 | const crypto = require("crypto"); 10 | 11 | import { DING_SEND_URL, DING_SECRET } from '../../config/default.config' 12 | 13 | 14 | export default (app) => { 15 | return { 16 | /** 17 | * @author: Cookie 18 | * @description: 加签发送消息 19 | */ 20 | async send(content) { 21 | const timestamp = Date.now(); 22 | const str = crypto 23 | .createHmac("sha256", DING_SECRET) 24 | .update(timestamp + "\n" + DING_SECRET) 25 | .digest() 26 | .toString("base64", "UTF-8"); 27 | 28 | try { 29 | const { res, data } = await app.curl( 30 | `${DING_SEND_URL}×tamp=${timestamp}&sign=${encodeURIComponent(str)}`, 31 | { 32 | headers: { 33 | "Content-Type": "application/json; charset=utf-8", 34 | }, 35 | method: "POST", 36 | data: JSON.stringify(content), 37 | } 38 | ); 39 | return res; 40 | } catch (error) { 41 | return error; 42 | } 43 | }, 44 | text({ content = {}, at }) { 45 | at = at || {}; 46 | this.send({ 47 | msgtype: "text", 48 | text: { 49 | content, 50 | }, 51 | at, 52 | }); 53 | }, 54 | }; 55 | }; 56 | -------------------------------------------------------------------------------- /database/migrations/20200807182349-init-projcet.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-05 10:27:10 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 16:04:16 6 | * @Description: 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = { 12 | up: async (queryInterface, Sequelize) => { 13 | const { 14 | DATE, STRING, INTEGER, UUID, UUIDV4 15 | } = Sequelize; 16 | return queryInterface.sequelize.transaction(t => { 17 | return Promise.all([ 18 | queryInterface.addColumn('project', 'project_feat', { 19 | type: INTEGER 20 | }, { transaction: t }), 21 | queryInterface.addColumn('project', 'project_bugfix', { 22 | type: INTEGER 23 | }, { transaction: t }), 24 | queryInterface.addColumn('project', 'project_release', { 25 | type: INTEGER 26 | }, { transaction: t }), 27 | queryInterface.addColumn('project', 'project_version', { 28 | type: STRING(10) 29 | }, { transaction: t }), , 30 | queryInterface.addColumn('project', 'last_activity_at', { 31 | type: DATE 32 | }, { transaction: t }), , 33 | queryInterface.addColumn('project', 'logo', { 34 | type: STRING(100) 35 | }, { transaction: t }), , 36 | queryInterface.addColumn('project', 'name_with_namespace', { 37 | type: STRING(100) 38 | }, { transaction: t }), 39 | ]); 40 | }); 41 | }, 42 | 43 | down: (queryInterface, Sequelize) => { 44 | }, 45 | }; 46 | -------------------------------------------------------------------------------- /app/model/branch.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-21 19:51:36 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 19:30:44 6 | * @Description: 7 | */ 8 | 9 | 10 | module.exports = app => { 11 | const { STRING, INTEGER, UUID, UUIDV4, TEXT } = app.Sequelize; 12 | 13 | const Branch = app.model.define( 14 | 'branch', 15 | { 16 | id: { 17 | type: UUID, 18 | allowNull: false, 19 | defaultValue: UUIDV4, 20 | }, 21 | projectSourceId: { 22 | type: STRING(30), 23 | primaryKey: true, 24 | }, 25 | projectId: { 26 | type: UUID, 27 | }, 28 | processId: { 29 | type: UUID, 30 | }, 31 | branchName: STRING(30), 32 | branchGitName: { type: STRING(30), primaryKey: true }, 33 | createdUser: STRING(50), 34 | remarks: STRING(1000), 35 | updateUser: STRING(100), 36 | branchStatus: { 37 | type: INTEGER, 38 | defaultValue: 0, 39 | }, 40 | branchNextStatus: { 41 | type: INTEGER, 42 | defaultValue: 1, 43 | }, 44 | commit: { 45 | type: TEXT('long'), 46 | primaryKey: true, 47 | set(val, name) { 48 | const vals = val ? JSON.stringify(val) : ''; 49 | (this as any).setDataValue(name, vals); 50 | }, 51 | get(val) { 52 | const value = (this as any).getDataValue(val); 53 | return value ? JSON.parse(value) : {}; 54 | }, 55 | }, 56 | }, 57 | { freezeTableName: true }, 58 | ); 59 | 60 | return Branch; 61 | }; 62 | -------------------------------------------------------------------------------- /app/controller/branch.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:19:22 6 | * @Description: 项目模块 Controller 7 | */ 8 | 9 | import { Post, Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("branch") 13 | export default class BranchController extends BaseController { 14 | /** 15 | * @author: Cookie 16 | * @description: 创建分支 17 | */ 18 | @Post("/create") 19 | public async createBranch({ 20 | request: { 21 | body: { params }, 22 | }, 23 | }) { 24 | const { ctx } = this; 25 | const { access_token } = this.user; 26 | const { name: userName } = this.user; 27 | const { projectSourceId, ref, branch } = params; 28 | const newBranch = await ctx.service.branch.createBranch({ 29 | access_token, 30 | projectSourceId, 31 | ref, 32 | branch, 33 | userName, 34 | }); 35 | this.success(newBranch); 36 | } 37 | 38 | /** 39 | * @author: Cookie 40 | * @description: 获取分支 41 | */ 42 | @Get("/getList") 43 | public async getBranchList({ request: { query } }) { 44 | const { ctx, user } = this; 45 | const { access_token } = user; 46 | const { projectId } = query; 47 | const { projectSourceId } = await ctx.service.project.getProject({ projectId, access_token }); 48 | 49 | const branchList = await ctx.service.branch.getBranchList({ 50 | projectId, 51 | access_token, 52 | projectSourceId: projectSourceId, 53 | }); 54 | this.success(branchList); 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /app/model/testRecord.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-06 10:07:16 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 10:53:46 6 | * @Description: 7 | */ 8 | 9 | export default app => { 10 | const { UUID, STRING, UUIDV4, INTEGER } = app.Sequelize; 11 | 12 | const TestRecord = app.model.define( 13 | 'testRecord', 14 | { 15 | id: { 16 | type: UUID, 17 | allowNull: false, 18 | defaultValue: UUIDV4, 19 | primaryKey: true, 20 | }, 21 | name: STRING(30), 22 | desc: STRING(200), 23 | submitUserId: STRING(300), 24 | testUserId: { 25 | type: STRING(300), 26 | set(val, name) { 27 | const vals = val && val.length > 0 ? val.join(',') : ''; 28 | console.log('vals====>', vals); 29 | (this as any).setDataValue(name, vals); 30 | }, 31 | get(val) { 32 | const value = (this as any).getDataValue(val); 33 | return value ? value.split(',') : []; 34 | }, 35 | }, 36 | processId: { 37 | type: UUID, 38 | }, 39 | testStatus: { 40 | type: INTEGER, 41 | }, 42 | branchIds: { 43 | type: STRING(1000), 44 | set(val, name) { 45 | const vals = val && val.length > 0 ? val.join(',') : ''; 46 | (this as any).setDataValue(name, vals); 47 | }, 48 | get(val) { 49 | const value = (this as any).getDataValue(val); 50 | return value ? value.split(',') : []; 51 | }, 52 | }, 53 | }, 54 | { freezeTableName: true }, 55 | ); 56 | 57 | return TestRecord; 58 | }; 59 | -------------------------------------------------------------------------------- /app/helper/utils/http.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @LastEditors: Cookie 4 | * @LastEditTime: 2021-05-16 11:53:31 5 | * @Description: request 模块 6 | */ 7 | 8 | import { GIT_URL } from "../../config/default.config"; 9 | 10 | const qs = require("qs"); 11 | 12 | interface IMethodV { 13 | url: string 14 | method: string 15 | params?: object 16 | query?: object 17 | } 18 | 19 | export default (app) => { 20 | return { 21 | /** 22 | * @author: Cookie 23 | * @description: 不带 version 的 api 请求 24 | */ 25 | async post({ url, params = {}, query = {} }) { 26 | const sendUrl = `${GIT_URL}${url}?${qs.stringify(query)}`; 27 | try { 28 | const { data, code } = await app.curl(sendUrl, { 29 | dataType: "json", 30 | method: "POST", 31 | data: params, 32 | }); 33 | return { data, code }; 34 | } catch (error) { 35 | return error; 36 | } 37 | }, 38 | 39 | /** 40 | * @author: Cookie 41 | * @description: 带 version 的通用 api 请求 42 | */ 43 | async methodV({ url, method, params = {}, query = {} }: IMethodV) { 44 | let sendUrl = `${GIT_URL}/api/v4${url}` 45 | if (query) { 46 | sendUrl = `${sendUrl}?${qs.stringify(query)}`; 47 | } 48 | 49 | console.warn("sendUrl=====>", sendUrl, query, 'method===>', method); 50 | 51 | try { 52 | const res = await app.curl(sendUrl, { 53 | dataType: "json", 54 | method, 55 | data: params, 56 | }); 57 | const { data, status: code } = res; 58 | return { data, code }; 59 | } catch (error) { 60 | return error; 61 | } 62 | }, 63 | }; 64 | }; 65 | -------------------------------------------------------------------------------- /app/io/controller/nsp.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-07 14:53:43 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:51:22 6 | * @FilePath: /processSystem/app/io/controller/default.ts 7 | * @Description: 8 | */ 9 | 10 | import { Controller } from "egg"; 11 | 12 | export default class NspController extends Controller { 13 | async ping() { 14 | const { ctx, app } = this; 15 | const message = ctx.args[0]; 16 | await ctx.socket.emit("res", `Hi! I've got your message: ${message}`); 17 | } 18 | 19 | public async creatJob() { 20 | const { ctx } = this; 21 | const message = ctx.args[0] || {}; 22 | try { 23 | const { payload } = message; 24 | 25 | const { 26 | projectId, 27 | branchName = "master", 28 | branchGitName, 29 | projectVersion = '0.0.01', 30 | buildPath = './', 31 | type = "h5", 32 | cache, 33 | } = payload; 34 | 35 | // const project = await ctx.service.project.getProject({ projectId, access_token }); 36 | 37 | // let projectGitPath = project.projectUrl.replace( 38 | // "http://", 39 | // `https://oauth2:${access_token}@` 40 | // ); 41 | 42 | // const callBack = await ctx.service.build.buildProject({ 43 | // type, 44 | // projectName: project.projectGitName, 45 | // projectVersion, 46 | // projectGitPath: `${projectGitPath}.git`, 47 | // branchName: branchGitName, 48 | // buildPath, 49 | // cache, 50 | // }); 51 | 52 | setTimeout(() => { 53 | ctx.socket.emit("res", `Hi! I've got your message`); 54 | }, 2000) 55 | 56 | // this.success(callBack); 57 | } catch (e) { 58 | throw e 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /app/controller/build.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:28:18 6 | * @Description: 构建 Controller 7 | */ 8 | 9 | import { Post, Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("build") 13 | export default class BuildController extends BaseController { 14 | /** 15 | * @author: Cookie 16 | * @description: 创建构建任务 17 | */ 18 | @Post("/creatJob") 19 | public async creatJob({ 20 | request: { 21 | body: { params }, 22 | }, 23 | }) { 24 | const { ctx } = this; 25 | const { access_token } = this.user; 26 | console.log(params) 27 | 28 | const { 29 | projectId, 30 | branchName = "master", 31 | branchGitName, 32 | projectVersion = '0.0.01', 33 | buildPath = './', 34 | type = "h5", 35 | cache, 36 | } = params; 37 | 38 | const project = await ctx.service.project.getProject({ projectId, access_token }); 39 | 40 | let projectGitPath = project.projectUrl.replace( 41 | "http://", 42 | `https://oauth2:${access_token}@` 43 | ); 44 | 45 | const callBack = await ctx.service.build.buildProject({ 46 | type, 47 | projectName: project.projectGitName, 48 | projectVersion, 49 | projectGitPath: `${projectGitPath}.git`, 50 | branchName: branchGitName, 51 | buildPath, 52 | cache, 53 | }); 54 | 55 | setTimeout(() => { 56 | // console.log(ctx.socket) 57 | // ctx.socket.emit("res", `Hi! I've got your message`); 58 | ctx.socket.emit('res', { 59 | target: 'Dkn3UXSu8_jHvKBmAAHW', 60 | payload: { 61 | msg: 'test', 62 | }, 63 | }); 64 | }, 2000) 65 | 66 | this.success(callBack); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/model/project.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-19 10:26:57 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-11 21:31:24 6 | * @Description: 7 | */ 8 | 9 | 10 | module.exports = app => { 11 | const { STRING, INTEGER, UUID, UUIDV4, DATE } = app.Sequelize; 12 | 13 | const Project = app.model.define( 14 | 'project', 15 | { 16 | id: { 17 | type: UUID, 18 | allowNull: false, 19 | defaultValue: UUIDV4, 20 | }, 21 | projectSource: { 22 | type: INTEGER, 23 | defaultValue: 0, 24 | primaryKey: true, 25 | }, 26 | projectSourceId: { 27 | type: STRING(30), 28 | primaryKey: true, 29 | }, 30 | projectName: STRING(30), 31 | projectType: { 32 | type: STRING(1000), 33 | primaryKey: true, 34 | set(val, name) { 35 | const vals = val && val.length > 0 ? val.join(',') : ''; 36 | (this as any).setDataValue(name, vals); 37 | }, 38 | get(val) { 39 | const value = (this as any).getDataValue(val); 40 | return value ? value.split(',') : []; 41 | }, 42 | }, 43 | namespace: STRING(30), 44 | projectUrl: STRING(100), 45 | projectGitDesc: STRING(200), 46 | projectDesc: STRING(200), 47 | projectGitName: STRING(30), 48 | projectFeat: { 49 | type: INTEGER, 50 | defaultValue: 0, 51 | }, 52 | projectBugfix: { 53 | type: INTEGER, 54 | defaultValue: 0, 55 | }, 56 | projectRelease: { 57 | type: INTEGER, 58 | defaultValue: 0, 59 | }, 60 | lastActivityAt: DATE, 61 | nameWithNamespace: STRING(100), 62 | logo: STRING(100), 63 | }, 64 | { 65 | freezeTableName: true, 66 | }, 67 | ); 68 | return Project; 69 | }; 70 | -------------------------------------------------------------------------------- /app/controller/process.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-16 10:17:56 6 | * @Description: 项目模块 Controller 7 | */ 8 | 9 | import { Post, Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("process") 13 | export default class ProcessController extends BaseController { 14 | /** 15 | * @author: Cookie 16 | * @description: 创建 devOps 任务流 17 | */ 18 | @Post("/create") 19 | public async createProcess({ 20 | request: { 21 | body: { params }, 22 | }, 23 | }) { 24 | const { ctx } = this; 25 | const { username } = this.userInfo; 26 | const { name, branchIds, workflowTplId, desc } = params; 27 | const branchStatus = await ctx.service.branch.checkProcess({ branchIds }); 28 | if (!branchStatus) 29 | this.error({ 30 | msg: "已有分支在流程中", 31 | }); 32 | const status = await ctx.service.process.createProcess({ 33 | desc, 34 | name, 35 | branchIds, 36 | workflowTplId, 37 | createdUser: username, 38 | updateUser: username, 39 | }); 40 | await ctx.service.branch.updateBranch({ 41 | branchIds, 42 | opt: { 43 | processId: status.id, 44 | }, 45 | }); 46 | this.success(status); 47 | } 48 | 49 | /** 50 | * @author: Cookie 51 | * @description: 查询 devOps 任务流 52 | */ 53 | @Get("/getList") 54 | public async getProcessList({ request: { query } }) { 55 | const { ctx } = this; 56 | const { pageSize = 10, pageNum = 1 } = query; 57 | const processList = await ctx.service.process.getProcessList({ 58 | pageNum: parseInt(pageNum), 59 | pageSize: parseInt(pageSize), 60 | }); 61 | // 联表查询分支信息 62 | for (let process of processList.rows) { 63 | const { branchIds } = process; 64 | process.branches = await ctx.service.branch.getSelfBranchList({ 65 | branchIds, 66 | }); 67 | } 68 | this.success(processList); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /config/config.default.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-08-04 16:11:52 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 11:01:55 6 | * @FilePath: /processSystem/config/config.default.ts 7 | * @Description: 8 | */ 9 | import { EggAppConfig, EggAppInfo, PowerPartial } from "egg"; 10 | 11 | export default (appInfo: EggAppInfo) => { 12 | const config = {} as PowerPartial; 13 | 14 | // override config from framework / plugin 15 | // use for cookie sign key, should change to your own and keep security 16 | config.keys = appInfo.name + "_1596002405659_3047"; 17 | 18 | // add your egg config in here 19 | config.middleware = ["jwtAuth", "errorHandler"]; 20 | 21 | config.jwtAuth = {}; 22 | config.errorHandler = {}; 23 | 24 | // add your egg config in here 25 | config.security = { 26 | csrf: { 27 | enable: false, 28 | }, 29 | // 白名单 30 | // domainWhiteList: '*' 31 | }; 32 | 33 | config.cors = { 34 | origin: (ctx) => ctx.get("origin"), 35 | credentials: true, 36 | allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH", 37 | }; 38 | 39 | // add your special config in here 40 | const bizConfig = { 41 | sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`, 42 | }; 43 | 44 | // 数据库配置 45 | config.sequelize = { 46 | database: "devops_dev", 47 | delegate: "model", // load all models to app.model and ctx.model 48 | baseDir: "model", // load models from `app/model/*.js` 49 | dialect: "mysql", 50 | host: "localhost", 51 | port: 3306, 52 | username: "root", 53 | password: "root123456", 54 | }; 55 | 56 | config.jwt = { 57 | // jwt配置项 58 | secret: "123456", 59 | }; 60 | 61 | // socketio 配置 62 | config.io = { 63 | init: {}, // passed to engine.io 64 | namespace: { 65 | "/": { 66 | connectionMiddleware: [], 67 | packetMiddleware: [], 68 | }, 69 | "/example": { 70 | connectionMiddleware: [], 71 | packetMiddleware: [], 72 | }, 73 | }, 74 | }; 75 | 76 | // the return config will combines to EggAppConfig 77 | return { 78 | ...config, 79 | ...bizConfig, 80 | }; 81 | }; 82 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | es6: true, 4 | node: true, 5 | }, 6 | extends: [ 7 | 'airbnb-base', 8 | ], 9 | globals: { 10 | Atomics: 'readonly', 11 | SharedArrayBuffer: 'readonly', 12 | }, 13 | parser: '@typescript-eslint/parser', 14 | parserOptions: { 15 | ecmaVersion: 2018, 16 | sourceType: 'module', 17 | }, 18 | plugins: [ 19 | '@typescript-eslint', 20 | ], 21 | rules: { 22 | 'linebreak-style': ['off', 'windows' 23 | ], 24 | strict: 'off', 25 | 'valid-jsdoc': 'off', 26 | 'no-empty-pattern': 'off', 27 | indent: ['error', 28 | 2 29 | ], // 缩进风格 30 | semi: [ 31 | 2, 'always' 32 | ], // 句尾强制分号 33 | eqeqeq: 2, // 要求使用 === 和 !== 34 | 'no-alert': [ 35 | 2 36 | ], // 禁止使用alert confirm prompt 37 | quotes: [ 38 | 2, 'single' 39 | ], // 单引号 40 | 'arrow-parens': 2, // 箭头函数用小括号括起来 41 | 'comma-spacing': ['error', 42 | { before: false, after: true 43 | } 44 | ], // 逗号前后的空格 45 | 'no-trailing-spaces': [ 46 | 2 47 | ], // 语句前后空格 48 | 'key-spacing': [ 49 | 2, 50 | { beforeColon: false, afterColon: true 51 | } 52 | ], // 对象字面量中冒号的前后空格 53 | 'space-before-function-paren': [ 54 | 2, 'never' 55 | ], // 函数定义时括号前面要不要有空格 56 | 'no-unreachable': 2, // 不能有无法执行的代码 57 | 'spaced-comment': 2, // 注释风格,//后加空格 58 | 'padded-blocks': ['error', 59 | { classes: 'always' 60 | } 61 | ], 62 | 'import/no-unresolved': 'off', 63 | camelcase: 'off', 64 | 'consistent-return': 'off', 65 | 'prefer-destructuring': 'off', 66 | 'no-plusplus': 'off', 67 | 'no-unused-vars': 'off', 68 | 'class-methods-use-this': 'off', 69 | 'import/prefer-default-export': 'off', 70 | 'no-useless-escape': 'off', 71 | 'no-underscore-dangle': 'off', 72 | 'no-param-reassign': 'off', 73 | 'import/extensions': 'off', 74 | 'no-useless-return': 'off', 75 | 'no-else-return': 'off', 76 | 'no-await-in-loop': 'off', 77 | 'comma-dangle': ['error', 78 | { // 数组、对象逗号风格 79 | arrays: 'always-multiline', 80 | objects: 'always-multiline', 81 | imports: 'always-multiline', 82 | exports: 'always-multiline', 83 | functions: 'ignore', 84 | } 85 | ], 86 | 'max-len': ['error', 87 | { code: 200 88 | } 89 | ], 90 | 'no-return-await': 'off', 91 | 'arrow-body-style': 'off', 92 | 'no-new': 'off', 93 | }, 94 | }; 95 | -------------------------------------------------------------------------------- /app/service/project.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 23:30:02 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 20:18:49 6 | * @Description: 项目 7 | */ 8 | import { Service } from "egg"; 9 | 10 | export default class Project extends Service { 11 | /** 12 | * @author: Cookie 13 | * @description: 根据 gitLab api 获取项目 list,且数据落库 14 | */ 15 | public async getProjectList({ pageSize = 100, pageNum = 1, access_token, userId }) { 16 | const { ctx } = this; 17 | const { projectList } = await ctx.helper.api.gitLab.project.getProjectByUser({ 18 | pageSize, 19 | pageNum, 20 | access_token, 21 | userId 22 | }); 23 | const selfProjectList: any = []; 24 | const opt: number[] = []; 25 | 26 | if (!projectList) return [] 27 | 28 | projectList.forEach((project) => { 29 | if (project) { 30 | selfProjectList.push({ 31 | projectSourceId: project.id, 32 | namespace: project.namespace.name, 33 | projectUrl: project.web_url, 34 | projectGitName: project.name, 35 | projectGitDesc: project.description, 36 | projectDesc: project.description, 37 | logo: project.logo, 38 | lastActivityAt: new Date(project.last_activity_at), 39 | nameWithNamespace: project.name_with_namespace, 40 | }); 41 | opt.push(project.id); 42 | } 43 | }); 44 | 45 | // 数据落库,批量更新 46 | if (selfProjectList.length > 0) { 47 | console.log(ctx.model.Project) 48 | await ctx.model.Project.bulkCreate(selfProjectList, { 49 | updateOnDuplicate: [ 50 | "projectGitDesc", 51 | "namespace", 52 | "projectUrl", 53 | "projectGitName", 54 | "lastActivityAt", 55 | "logo", 56 | "nameWithNamespace", 57 | ], 58 | }); 59 | } 60 | 61 | const local: any = await ctx.model.Project.findAll({ 62 | where: { 63 | projectSourceId: opt, 64 | }, 65 | }); 66 | 67 | return local; 68 | } 69 | 70 | /** 71 | * @author: Cookie 72 | * @description: 查询单个项目详情 73 | */ 74 | public async getProject({ projectId, access_token }) { 75 | const { ctx } = this; 76 | const self_project = await ctx.model.Project.findOne({ 77 | where: { 78 | id: projectId 79 | }, 80 | raw: true, 81 | }); 82 | const project = await ctx.helper.api.gitLab.project.getProject({ 83 | id: self_project.projectSourceId, 84 | access_token 85 | }); 86 | 87 | return { ...self_project, ...project }; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "devops-demo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "private": true, 6 | "egg": { 7 | "typescript": true, 8 | "declarations": true 9 | }, 10 | "scripts": { 11 | "startTest": "ets && tsc -p tsconfig.json && EGG_SERVER_ENV=test egg-scripts start --port=8001 --title=egg-server-devops-demo --daemon --sticky", 12 | "stopTest": "egg-scripts stop --title=egg-server-devops-demo", 13 | "startPre": "ets && tsc -p tsconfig.json && EGG_SERVER_ENV=prod egg-scripts start --port=9001 --title=egg-server-pre-devops-demo --daemon", 14 | "stopPre": "egg-scripts stop --title=egg-server-pre-devops-demo", 15 | "startProd": "ets && tsc -p tsconfig.json && EGG_SERVER_ENV=prod egg-scripts start --port=1001 --title=egg-server-prod-devops-demo --daemon", 16 | "stopProd": "egg-scripts stop --title=egg-server-prod-devops-demo", 17 | "stop": "egg-scripts stop --title=egg-server-process", 18 | "dev": "egg-bin dev --sticky", 19 | "debug": "egg-bin debug", 20 | "test-local": "egg-bin test", 21 | "test": "npm run lint -- --fix && npm run test-local", 22 | "cov": "egg-bin cov", 23 | "tsc": "ets && tsc -p tsconfig.json", 24 | "ci": "npm run lint && npm run cov && npm run tsc", 25 | "autod": "autod", 26 | "lint": "eslint . --ext .ts", 27 | "clean": "ets clean" 28 | }, 29 | "dependencies": { 30 | "@types/jenkins": "^0.23.1", 31 | "crypto": "^1.0.1", 32 | "egg": "^2.6.1", 33 | "egg-helper": "^1.1.5", 34 | "egg-jwt": "^3.1.7", 35 | "egg-scripts": "^2.6.0", 36 | "egg-sequelize": "^5.2.2", 37 | "egg-shell-decorators": "^1.5.0", 38 | "egg-socket.io": "^4.1.6", 39 | "jenkins": "^0.28.0", 40 | "jenkins-api": "^0.3.1", 41 | "marked": "^1.1.1", 42 | "mysql2": "^2.1.0", 43 | "nodemailer": "^6.4.11", 44 | "nunjucks": "^3.2.2", 45 | "qs": "^6.9.4" 46 | }, 47 | "devDependencies": { 48 | "@types/mocha": "^2.2.40", 49 | "@types/node": "^7.0.12", 50 | "@types/supertest": "^2.0.0", 51 | "autod": "^3.0.1", 52 | "autod-egg": "^1.1.0", 53 | "egg-bin": "^4.11.0", 54 | "egg-ci": "^1.8.0", 55 | "egg-cors": "^2.2.3", 56 | "egg-mock": "^3.16.0", 57 | "eslint": "^6.8.0", 58 | "eslint-config-airbnb-base": "^14.2.0", 59 | "eslint-config-egg": "^8.0.0", 60 | "sequelize-cli": "^6.2.0", 61 | "tslib": "^1.9.0", 62 | "typescript": "^3.0.0" 63 | }, 64 | "engines": { 65 | "node": ">=8.9.0" 66 | }, 67 | "ci": { 68 | "version": "8" 69 | }, 70 | "repository": { 71 | "type": "git", 72 | "url": "" 73 | }, 74 | "eslintIgnore": [ 75 | "coverage" 76 | ], 77 | "author": "cookieboty", 78 | "license": "MIT" 79 | } -------------------------------------------------------------------------------- /app/service/user.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 20:14:37 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 15:45:40 6 | * @Description: 用户模块 7 | */ 8 | import { Service } from "egg"; 9 | import { CLIENT_ID, CLIENT_SECRET } from '../config/default.config'; 10 | 11 | export default class User extends Service { 12 | 13 | /** 14 | * @author: Cookie 15 | * @description: 使用 gitLab api 获取 access_token 16 | */ 17 | public async getTokenByApplications({ code }) { 18 | const { data: token } = await this.ctx.helper.utils.http.post({ 19 | url: '/oauth/token', 20 | params: { 21 | grant_type: 'authorization_code', 22 | client_id: CLIENT_ID, 23 | client_secret: CLIENT_SECRET, 24 | code, 25 | redirect_uri: 'http://devops.cookieboty.com/user/getTokenByApp', 26 | }, 27 | }); 28 | 29 | if (token && token.access_token) { 30 | return token; 31 | } 32 | return false; 33 | } 34 | 35 | /** 36 | * @author: Cookie 37 | * @description: 使用 gitLab api 获取 access_token 38 | */ 39 | public async getUserToken({ username, password }) { 40 | const { data: token } = await this.ctx.helper.utils.http.post({ 41 | url: '/oauth/token', 42 | params: { 43 | grant_type: 'password', 44 | username, 45 | password, 46 | }, 47 | }); 48 | 49 | if (token && token.access_token) { 50 | return token; 51 | } 52 | return false; 53 | } 54 | 55 | /** 56 | * @author: Cookie 57 | * @description: 使用 gitLab api 获取 gitLab 用户信息 58 | */ 59 | public async getUserInfo({ access_token }) { 60 | const userInfo = await this.ctx.helper.api.gitLab.user.getUserInfo({ 61 | access_token, 62 | }); 63 | return userInfo; 64 | } 65 | 66 | /** 67 | * @author: Cookie 68 | * @description: 用户信息本地落库 69 | */ 70 | public async saveUser({ userInfo }) { 71 | const { ctx } = this; 72 | const { 73 | id, 74 | name, 75 | username, 76 | email, 77 | avatar_url: avatarUrl, 78 | web_url: webUrl, 79 | } = userInfo; 80 | 81 | // 查询用户是否已经落库 82 | ctx.model.User.findOrCreate({ 83 | where: { 84 | id, 85 | }, 86 | defaults: { 87 | id, 88 | name, 89 | username, 90 | email, 91 | avatarUrl, 92 | webUrl, 93 | } 94 | }).then(([user, created]) => { 95 | if (!created) { 96 | ctx.model.User.update({ 97 | name, 98 | username, 99 | email, 100 | avatarUrl, 101 | webUrl, 102 | }, { 103 | where: { 104 | id, 105 | } 106 | }) 107 | } 108 | }); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/helper/api/gitLab/project.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 21:23:05 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-05 17:23:13 6 | * @Description: gitLab 项目模块 api 7 | */ 8 | import AJAX from "../../utils/http"; 9 | 10 | module.exports = (app) => { 11 | /** 12 | * @author: Cookie 13 | * @description: 获取工程列表 14 | */ 15 | const getProjectList = async ({ pageSize, pageNum, access_token }) => { 16 | const { data: projectList } = await AJAX(app).methodV({ 17 | url: "/projects", 18 | method: "GET", 19 | query: { 20 | per_page: pageSize, 21 | page: pageNum, 22 | access_token 23 | }, 24 | }); 25 | return { projectList }; 26 | }; 27 | 28 | /** 29 | * @author: Cookie 30 | * @description: 获取用户所属工程 31 | */ 32 | const getProjectByUser = async ({ pageSize, pageNum, access_token, userId }) => { 33 | const { data: projectList } = await AJAX(app).methodV({ 34 | url: `/users/${userId}/projects`, 35 | method: "GET", 36 | query: { 37 | per_page: pageSize, 38 | page: pageNum, 39 | access_token 40 | }, 41 | }); 42 | return { projectList }; 43 | }; 44 | 45 | /** 46 | * @author: Cookie 47 | * @description: 获取工程 48 | */ 49 | const getProject = async ({ id, access_token }) => { 50 | const project = await AJAX(app).methodV({ 51 | url: `/projects/${id}`, 52 | method: "GET", 53 | query: { access_token } 54 | }); 55 | return project; 56 | }; 57 | 58 | /** 59 | * @author: Cookie 60 | * @description: 创建 gitLab 工程 61 | */ 62 | const createProjects = async ({ gitParams }) => { 63 | const status = await AJAX(app).methodV({ 64 | url: "/projects", 65 | method: "POST", 66 | params: { 67 | ...gitParams, 68 | }, 69 | }); 70 | return status; 71 | }; 72 | 73 | /** 74 | * @author: Cookie 75 | * @description: 删除 gitLab 工程保护分支 76 | */ 77 | const deleteProtectedBranches = async (projectId: number) => { 78 | const url = `/projects/${projectId}/protected_branches/master`; 79 | const status = await AJAX(app).methodV({ 80 | url, 81 | method: "DELETE", 82 | }); 83 | return status; 84 | }; 85 | 86 | /** 87 | * @author: Cookie 88 | * @description: 设置 gitLab 工程保护分支 89 | */ 90 | const protectedBranches = async (projectId: number) => { 91 | const url = `/projects/${projectId}/protected_branches`; 92 | const status = await AJAX(app).methodV({ 93 | url, 94 | method: "POST", 95 | params: { 96 | name: "master", 97 | push_access_level: 0, 98 | merge_access_level: 40, 99 | }, 100 | }); 101 | return status; 102 | }; 103 | 104 | return { 105 | getProjectList, 106 | getProject, 107 | getProjectByUser, 108 | createProjects, 109 | deleteProtectedBranches, 110 | protectedBranches, 111 | }; 112 | }; 113 | -------------------------------------------------------------------------------- /app/helper/api/jenkins/index.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-09-12 15:47:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-19 20:28:42 6 | * @Description: 7 | */ 8 | 9 | import * as jenkins from "jenkins"; 10 | 11 | /** 12 | * Jenkins连接 13 | * @param type 14 | */ 15 | const getJenkins = function ( 16 | type: "h5" | "node" | "nodeProduct" | "android" | "java" 17 | ) { 18 | const jenkinsConfig = { 19 | h5: { 20 | baseUrl: "http://cookieboty:qq123456@jenkins.cookieboty.com/", 21 | crumbIssuer: true, 22 | }, 23 | }; 24 | return jenkins(jenkinsConfig[type]); 25 | }; 26 | 27 | /** 28 | * @author: Cookie 29 | * @description: 触发jenkins流水线 30 | */ 31 | const buildJenkins = async ({ type, job, params }) => { 32 | const jenkinsCallback: any = await new Promise((resolve) => { 33 | getJenkins(type).job.build( 34 | { name: job, parameters: params }, 35 | (err: any, data: any) => { 36 | if (err) { 37 | console.log("err: ", err); 38 | throw err; 39 | } 40 | resolve({ queueId: data }); 41 | } 42 | ); 43 | }); 44 | return { data: jenkinsCallback }; 45 | }; 46 | 47 | /** 48 | * @author: Cookie 49 | * @description: 获取当前节点信息 50 | */ 51 | const getQueuedInfo = async ({ type, queueId }) => { 52 | const jenkinsCallback: any = await new Promise((resolve) => { 53 | getJenkins(type).queue.item(queueId, (err: any, data: any) => { 54 | if (err) { 55 | console.log("err---->", err); 56 | throw err; 57 | } 58 | resolve(data); 59 | }); 60 | }); 61 | return { data: jenkinsCallback }; 62 | }; 63 | 64 | /** 65 | * @author: Cookie 66 | * @description: 获取当前构建信息 67 | */ 68 | const getJenkinsInfo = async ({ type, job, buildNumber }) => { 69 | console.log(type, job, buildNumber); 70 | const jenkinsCallback: any = await new Promise((resolve) => { 71 | getJenkins(type).build.get(job, buildNumber, (err: any, data: any) => { 72 | console.log("data: ", data); 73 | console.log("err: ", err); 74 | if (err) { 75 | console.log("err---->", err); 76 | throw err; 77 | } 78 | resolve(data); 79 | }); 80 | }); 81 | const { statusCode } = jenkinsCallback; 82 | if (jenkinsCallback && statusCode !== 404) { 83 | return { data: jenkinsCallback }; 84 | } else { 85 | return { data: jenkinsCallback }; 86 | } 87 | }; 88 | 89 | /** 90 | * @author: Cookie 91 | * @description: 获取jenkins打印 92 | */ 93 | const getJenkinsConsole = async ({ type, job, buildId }) => { 94 | const jenkinsCallback: any = await new Promise((resolve) => { 95 | getJenkins(type).build.log(job, buildId, (err: any, data: any) => { 96 | if (err) { 97 | return console.log("err---->", err); 98 | } 99 | resolve(data); 100 | }); 101 | }); 102 | return { data: jenkinsCallback }; 103 | }; 104 | 105 | export default { 106 | buildJenkins, 107 | getQueuedInfo, 108 | getJenkinsInfo, 109 | getJenkinsConsole, 110 | }; 111 | -------------------------------------------------------------------------------- /app/helper/api/gitLab/branch.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-29 14:17:50 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-05-16 11:59:00 6 | * @Description: gitLab 分支模块 api 7 | */ 8 | 9 | import AJAX from "../../utils/http"; 10 | 11 | module.exports = (app) => { 12 | /** 13 | * @author: Cookie 14 | * @description: 获取分支列表 15 | */ 16 | const getBranchList = async ({ 17 | pageSize, 18 | pageNum, 19 | projectId, 20 | access_token, 21 | }) => { 22 | try { 23 | const { data, code } = await AJAX(app).methodV({ 24 | url: `/projects/${projectId}/repository/branches`, 25 | method: "GET", 26 | query: { 27 | per_page: pageSize, 28 | page: pageNum, 29 | access_token, 30 | }, 31 | }); 32 | switch (code) { 33 | case 200: { 34 | return data; 35 | } 36 | default: { 37 | return { msg: data }; 38 | } 39 | } 40 | } catch (e) { 41 | return { msg: e }; 42 | } 43 | }; 44 | 45 | /** 46 | * @author: Cookie 47 | * @description: 获取单分支 48 | */ 49 | const getBranch = async ({ projectId, branch: branchName }) => { 50 | const branch = await AJAX(app).methodV({ 51 | url: `/projects/${projectId}/repository/branches/${branchName}`, 52 | method: "GET", 53 | }); 54 | return branch; 55 | }; 56 | 57 | /** 58 | * @author: Cookie 59 | * @description: 创建分支 60 | */ 61 | const createBranch = async ({ ref, projectId, branch, access_token }) => { 62 | const { code, data } = await AJAX(app).methodV({ 63 | url: `/projects/${projectId}/repository/branches`, 64 | params: { 65 | ref, 66 | branch, 67 | }, 68 | query: { access_token }, 69 | method: "POST", 70 | }); 71 | return data; 72 | }; 73 | 74 | const setProtectedBranch = async ({ branch }) => { 75 | const { projectId } = branch; 76 | const status = await AJAX(app).methodV({ 77 | url: `/projects/${projectId}/protected_branches`, 78 | params: { 79 | name: "zeus/*", 80 | merge_access_level: 30, 81 | push_access_level: 0, 82 | }, 83 | method: "POST", 84 | }); 85 | return status; 86 | }; 87 | 88 | const delProtectedBranch = async ({ branch }) => { 89 | const { projectId } = branch; 90 | const status = await AJAX(app).methodV({ 91 | url: `/projects/${projectId}/protected_branches/zeus%2F*`, 92 | method: "DELETE", 93 | }); 94 | return status; 95 | }; 96 | 97 | const delBranch = async (projectId, branchGitName) => { 98 | const status = await AJAX(app).methodV({ 99 | url: `/projects/${projectId}/repository/branches/${encodeURIComponent( 100 | branchGitName 101 | )}`, 102 | method: "DELETE", 103 | }); 104 | return status; 105 | }; 106 | return { 107 | getBranchList, 108 | createBranch, 109 | getBranch, 110 | setProtectedBranch, 111 | delProtectedBranch, 112 | delBranch, 113 | }; 114 | }; 115 | -------------------------------------------------------------------------------- /app/controller/user.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 15:49:49 6 | * @Description: 用户模块 Controller 7 | */ 8 | 9 | import { Post, Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("user") 13 | export default class UserController extends BaseController { 14 | 15 | /** 16 | * @author: Cookie 17 | * @description: 根据 Applications access_token 18 | */ 19 | 20 | @Get("/getTokenByApp") 21 | public async getTokenByApplications({ 22 | request: { 23 | query: { code }, 24 | }, 25 | }) { 26 | const { ctx, app } = this; 27 | // gitLab 获取 access_token 28 | const userToken = await ctx.service.user.getTokenByApplications({ code }); 29 | 30 | // gitLab 获取用户信息 31 | const userInfo = await ctx.service.user.getUserInfo({ 32 | access_token: userToken.access_token, 33 | }); 34 | 35 | // 用户数据本地落库 36 | ctx.service.user.saveUser({ 37 | userInfo, 38 | }); 39 | 40 | // 将用户信息及 token 使用 jwt 注册 41 | const token = app.jwt.sign( 42 | { 43 | userToken, 44 | userInfo, 45 | }, 46 | app.config.jwt.secret 47 | ); 48 | 49 | ctx.set({ "Access-Control-Expose-Headers": "authorization" }); 50 | ctx.set({ authorization: token }); // 设置 headers 51 | ctx.cookies.set('authorization', token, { 52 | maxAge: 1000 * 3600 * 24, // cookie存储一天 设置过期时间后关闭浏览器重新打开cookie还存在 53 | httpOnly: true, // 仅允许服务获取,不允许js获取 54 | domain: '.cookieboty.com' // 设置cookie跨域 55 | }); 56 | ctx.redirect(`http://fe.cookieboty.com`); 57 | } 58 | 59 | /** 60 | * @author: Cookie 61 | * @description: 根据 gitLab 用户密码获取 access_token 62 | */ 63 | @Post("/getUserToken") 64 | public async getUserToken({ 65 | request: { 66 | body: { params }, 67 | }, 68 | }) { 69 | const { ctx, app } = this; 70 | const { username, password } = params; 71 | 72 | // gitLab 获取 access_token 73 | const userToken = await ctx.service.user.getUserToken({ 74 | username, 75 | password, 76 | }); 77 | 78 | // gitLab 获取用户信息 79 | const userInfo = await ctx.service.user.getUserInfo({ 80 | access_token: userToken.access_token, 81 | }); 82 | 83 | // 用户数据本地落库 84 | ctx.service.user.saveUser({ 85 | userInfo, 86 | }); 87 | 88 | // 将用户信息及 token 使用 jwt 注册 89 | const token = app.jwt.sign( 90 | { 91 | userToken, 92 | userInfo, 93 | }, 94 | app.config.jwt.secret 95 | ); 96 | 97 | ctx.set({ "Access-Control-Expose-Headers": "authorization" }); 98 | ctx.set({ authorization: token }); // 设置headers 99 | this.success(userInfo); 100 | } 101 | 102 | /** 103 | * @author: Cookie 104 | * @description: 根据 gitLab 用户密码获取 access_token 105 | */ 106 | @Get("/getUserInfo") 107 | public async getUserInfo({ 108 | request: { 109 | body: { params }, 110 | }, 111 | }) { 112 | const { ctx } = this; 113 | const { access_token } = this.user; 114 | // gitLab 获取用户信息 115 | const userInfo = await ctx.service.user.getUserInfo({ 116 | access_token, 117 | }); 118 | 119 | this.success(userInfo); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/service/branch.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-07-29 23:30:02 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2021-06-26 19:29:19 6 | * @Description: 分支 7 | */ 8 | import { Service } from "egg"; 9 | 10 | export default class Branch extends Service { 11 | /** 12 | * @author: Cookie 13 | * @description: 根据 gitLab api 获取分支 list,且数据落库 14 | */ 15 | public async getBranchList({ 16 | projectId, 17 | projectSourceId, 18 | access_token, 19 | pageSize = 100, 20 | pageNum = 1, 21 | }) { 22 | 23 | const { ctx } = this; 24 | const branchList = await ctx.helper.api.gitLab.branch.getBranchList({ 25 | projectId: projectSourceId, 26 | access_token, 27 | pageSize, 28 | pageNum, 29 | }); 30 | 31 | branchList.forEach((branch) => { 32 | branch.branchGitName = branch.name; 33 | branch.projectId = projectId; 34 | branch.projectSourceId = projectSourceId; 35 | delete branch.name; 36 | }); 37 | 38 | branchList.length > 0 && 39 | (await ctx.model.Branch.bulkCreate(branchList, { 40 | ignoreDuplicates: true, 41 | updateOnDuplicate: ["commit"], 42 | })); 43 | 44 | const local = await ctx.model.Branch.findAll({ 45 | where: { 46 | projectId, 47 | }, 48 | }); 49 | return local; 50 | } 51 | 52 | /** 53 | * @author: Cookie 54 | * @description: 根据 gitLab api 获取分支 list,且数据落库 55 | */ 56 | public async getSelfBranchList({ branchIds }) { 57 | const { ctx } = this; 58 | const local = await ctx.model.Branch.findAll({ 59 | where: { 60 | id: branchIds, 61 | }, 62 | }); 63 | return local; 64 | } 65 | 66 | /** 67 | * @author: Cookie 68 | * @description: 根据 gitLab api 创建分支,且数据落库 69 | */ 70 | public async createBranch({ 71 | projectSourceId, 72 | access_token, 73 | ref, 74 | branch, 75 | userName, 76 | }) { 77 | const { ctx } = this; 78 | const newBranch = await ctx.helper.api.gitLab.branch.createBranch({ 79 | projectId: projectSourceId, 80 | access_token, 81 | ref, 82 | branch, 83 | userName, 84 | }); 85 | 86 | if (!newBranch) return null; 87 | 88 | const branchCallBack = await ctx.model.Branch.create({ 89 | projectId: projectSourceId, 90 | branchName: branch, 91 | branchType: branch.branchType, 92 | branchGitName: branch.branchGitName, 93 | branchFrom: branch.ref, 94 | processStatus: "dev", 95 | remarks: branch.remarks, 96 | createdUser: userName, 97 | }); 98 | return branchCallBack; 99 | } 100 | 101 | /** 102 | * @author: Cookie 103 | * @description: 更新分支信息 104 | */ 105 | public async updateBranch({ branchIds, opt }) { 106 | const { ctx } = this; 107 | const branchCallBack = await ctx.model.Branch.update( 108 | { 109 | ...opt, 110 | }, 111 | { 112 | where: { 113 | id: branchIds, 114 | }, 115 | } 116 | ); 117 | return branchCallBack; 118 | } 119 | 120 | /** 121 | * @author: Cookie 122 | * @description: 查询分支流程信息 123 | */ 124 | public async checkProcess({ branchIds, status = "some" }) { 125 | const { ctx } = this; 126 | const branchCallBack = await ctx.model.Branch.findAll({ 127 | where: { id: branchIds }, 128 | }); 129 | if (branchCallBack[status]((branch) => branch.processId)) { 130 | return false; 131 | } 132 | return true; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/helper/api/gitLab/merge.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2020-04-21 16:06:27 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-05 17:22:17 6 | * @FilePath: /FE-DEPLOY-SYSTEM/app/helper/api/gitLab/merge.ts 7 | * @Description: gitLab 合并模块 api 8 | */ 9 | 10 | import AJAX from "../../utils/http"; 11 | 12 | module.exports = (app) => { 13 | /** 14 | * @description: 获取合并项目列表 15 | */ 16 | const getMergeList = async ({ params }) => { 17 | const { data } = await AJAX(app).methodV({ 18 | url: "/merge_requests", 19 | params, 20 | method: "GET", 21 | }); 22 | return JSON.parse(data); 23 | }; 24 | 25 | /** 26 | * @description: 处理合并请求 27 | */ 28 | const acceptMerge = async ({ projectId, mergeRequestId }) => { 29 | const { data, code } = await AJAX(app).methodV({ 30 | url: `/projects/${projectId}/merge_requests/${mergeRequestId}/merge`, 31 | method: "PUT", 32 | }); 33 | switch (code) { 34 | case 200: { 35 | return { data: JSON.parse(data) }; 36 | } 37 | case 201: { 38 | return { data: JSON.parse(data) }; 39 | } 40 | case 404: { 41 | return { msg: "无效合并请求,请去 gitLab 检查" }; 42 | } 43 | case 405: { 44 | return { msg: "无效合并请求,请去 gitLab 检查" }; 45 | } 46 | default: 47 | return { msg: data.message }; 48 | } 49 | }; 50 | 51 | /** 52 | * @description: 创建合并请求 53 | */ 54 | const createMerge = async ({ 55 | projectId, 56 | sourceBranch, 57 | targetBranch, 58 | title, 59 | description, 60 | assigneeId, 61 | }) => { 62 | try { 63 | const { data, code } = await AJAX(app).methodV({ 64 | url: `/projects/${projectId}/merge_requests`, 65 | params: { 66 | source_branch: sourceBranch, 67 | target_branch: targetBranch, 68 | title, 69 | description, 70 | assignee_id: assigneeId, 71 | }, 72 | method: "POST", 73 | }); 74 | switch (code) { 75 | case 200: { 76 | return { data: JSON.parse(data) }; 77 | } 78 | case 201: { 79 | return { data }; 80 | } 81 | case 404: { 82 | return { msg: "无效合并请求,请去gitlab检查" }; 83 | } 84 | case 405: { 85 | return { msg: "无效合并请求,请去gitlab检查" }; 86 | } 87 | default: 88 | return { msg: data.message }; 89 | } 90 | } catch (e) { 91 | console.log(e); 92 | } 93 | }; 94 | 95 | /** 96 | * @description: 更新合并请求 97 | * @param {type} 98 | * @return: 99 | */ 100 | const updateMerge = async ({ projectId, mergeRequestId, stateEvent }) => { 101 | const { data, code } = await AJAX(app).methodV({ 102 | url: `/projects/${projectId}/merge_requests/${mergeRequestId}`, 103 | params: { 104 | state_event: stateEvent, 105 | }, 106 | method: "PUT", 107 | }); 108 | switch (code) { 109 | case 200: { 110 | return { data: JSON.parse(data) }; 111 | } 112 | case 201: { 113 | return { data }; 114 | } 115 | case 404: { 116 | return { msg: "无效更新请求,请去gitlab检查" }; 117 | } 118 | case 405: { 119 | return { msg: "无效更新请求,请去gitlab检查" }; 120 | } 121 | default: 122 | return { msg: data.message }; 123 | } 124 | }; 125 | 126 | return { 127 | getMergeList, 128 | acceptMerge, 129 | createMerge, 130 | updateMerge, 131 | }; 132 | }; 133 | -------------------------------------------------------------------------------- /app/controller/notices.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-07-17 14:04:15 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-06 22:19:00 6 | * @Description: 项目模块 Controller 7 | */ 8 | 9 | import { Post, Prefix, Get } from "egg-shell-decorators"; 10 | import BaseController from "./base"; 11 | 12 | @Prefix("notices") 13 | export default class NoticesController extends BaseController { 14 | /** 15 | * @author: Cookie 16 | * @description: 获取消息记录 17 | */ 18 | @Get("/get") 19 | public async getNotices({ 20 | request: { 21 | body: { params }, 22 | }, 23 | }) { 24 | const { ctx } = this; 25 | const { name: userName } = this.user; 26 | this.success([ 27 | { 28 | id: "000000001", 29 | avatar: 30 | "https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png", 31 | title: "你收到了 14 份新周报", 32 | datetime: "2017-08-09", 33 | type: "notification", 34 | }, 35 | { 36 | id: "000000002", 37 | avatar: 38 | "https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png", 39 | title: "你推荐的 曲妮妮 已通过第三轮面试", 40 | datetime: "2017-08-08", 41 | type: "notification", 42 | }, 43 | { 44 | id: "000000003", 45 | avatar: 46 | "https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png", 47 | title: "这种模板可以区分多种通知类型", 48 | datetime: "2017-08-07", 49 | read: true, 50 | type: "notification", 51 | }, 52 | { 53 | id: "000000004", 54 | avatar: 55 | "https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png", 56 | title: "左侧图标用于区分不同的类型", 57 | datetime: "2017-08-07", 58 | type: "notification", 59 | }, 60 | { 61 | id: "000000005", 62 | avatar: 63 | "https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png", 64 | title: "内容不要超过两行字,超出时自动截断", 65 | datetime: "2017-08-07", 66 | type: "notification", 67 | }, 68 | { 69 | id: "000000006", 70 | avatar: 71 | "https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg", 72 | title: "曲丽丽 评论了你", 73 | description: "描述信息描述信息描述信息", 74 | datetime: "2017-08-07", 75 | type: "message", 76 | clickClose: true, 77 | }, 78 | { 79 | id: "000000007", 80 | avatar: 81 | "https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg", 82 | title: "朱偏右 回复了你", 83 | description: "这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像", 84 | datetime: "2017-08-07", 85 | type: "message", 86 | clickClose: true, 87 | }, 88 | { 89 | id: "000000008", 90 | avatar: 91 | "https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg", 92 | title: "标题", 93 | description: "这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像", 94 | datetime: "2017-08-07", 95 | type: "message", 96 | clickClose: true, 97 | }, 98 | { 99 | id: "000000009", 100 | title: "任务名称", 101 | description: "任务需要在 2017-01-12 20:00 前启动", 102 | extra: "未开始", 103 | status: "todo", 104 | type: "event", 105 | }, 106 | { 107 | id: "000000010", 108 | title: "第三方紧急代码变更", 109 | description: 110 | "冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务", 111 | extra: "马上到期", 112 | status: "urgent", 113 | type: "event", 114 | }, 115 | { 116 | id: "000000011", 117 | title: "信息安全考试", 118 | description: "指派竹尔于 2017-01-09 前完成更新并发布", 119 | extra: "已耗时 8 天", 120 | status: "doing", 121 | type: "event", 122 | }, 123 | { 124 | id: "000000012", 125 | title: "ABCD 版本发布", 126 | description: 127 | "冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务", 128 | extra: "进行中", 129 | status: "processing", 130 | type: "event", 131 | }, 132 | ]); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/helper/utils/mail.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Cookie 3 | * @Date: 2019-08-19 10:26:57 4 | * @LastEditors: Cookie 5 | * @LastEditTime: 2020-08-16 11:36:40 6 | * @Description: 7 | */ 8 | import { MAIL_CONFIG } from "../../config/default.config"; 9 | 10 | const marked = require("marked"); // marked 转换 11 | const nodemailer = require("nodemailer"); // 发送邮件 12 | const nunjucks = require("nunjucks"); // 模板引擎 13 | const path = require("path"); 14 | 15 | // 邮箱配置初始化 16 | const transporter = nodemailer.createTransport({ 17 | host: MAIL_CONFIG.service, 18 | secureConnection: true, // 使用SSL方式(安全方式,防止被窃取信息) 19 | port: MAIL_CONFIG.port, 20 | auth: { 21 | user: MAIL_CONFIG.user_email, // 账号 22 | pass: MAIL_CONFIG.auth_code, // 授权码 23 | }, 24 | }); 25 | 26 | const htmlModel = (params) => { 27 | const { storyMail, exitInfo, summitUser, iterationMail } = params 28 | const html = nunjucks.render(path.join(__dirname, "./emailTpl/email.njk"), { 29 | storyMail, 30 | exitInfo, 31 | summitUser, 32 | iterationMail, 33 | }); 34 | return html; 35 | }; 36 | 37 | const htmlPassModel = ({ storyMail, testInfo, testUser, iterationMail }) => { 38 | const html = nunjucks.render(path.join(__dirname, "./emailTpl/pass.njk"), { 39 | storyMail, 40 | testInfo, 41 | testUser, 42 | iterationMail, 43 | }); 44 | return html; 45 | }; 46 | 47 | const htmlRepulseModel = ({ 48 | storyMail, 49 | repulseUser, 50 | repulseInfo, 51 | iterationMail, 52 | }) => { 53 | const html = nunjucks.render(path.join(__dirname, "./emailTpl/repulse.njk"), { 54 | storyMail, 55 | repulseUser, 56 | repulseInfo, 57 | iterationMail, 58 | }); 59 | return html; 60 | }; 61 | 62 | /* 63 | * toEmail: String 接收者,可以同时发送多个,以逗号隔开 64 | * subject: String 标题 65 | * cc: String 抄送 66 | * text: String 文本 67 | * html: Object titleList表头 conterFontList内容 68 | * attachments: any 附件 69 | * [ 70 | * { 71 | filename: 'img1.png', // 改成你的附件名 72 | path: 'public/images/img1.png', // 改成你的附件路径 73 | cid : '00000001' // cid可被邮件使用 74 | } 75 | * ] 76 | */ 77 | 78 | interface mailInterface { 79 | toEmail: string; 80 | subject: string; 81 | cc?: string; 82 | text?: string; 83 | html?: any; 84 | attachments?: any; 85 | storyMail?: any; 86 | exitInfo?: any; 87 | summitUser?: String; 88 | iterationMail?: any; 89 | } 90 | 91 | const sendMail = async (mailOptions: mailInterface) => { 92 | const { 93 | toEmail, 94 | subject, 95 | cc, 96 | text, 97 | attachments, 98 | storyMail, 99 | exitInfo, 100 | summitUser, 101 | iterationMail, 102 | } = mailOptions; 103 | Object.keys(exitInfo).forEach((key) => { 104 | exitInfo[key] = marked(exitInfo[key]); 105 | }); 106 | const html = htmlModel({ storyMail, exitInfo, summitUser, iterationMail }); 107 | const mailOpts = { 108 | from: MAIL_CONFIG.user_email, // 发送者,与上面的user一致 109 | to: toEmail, 110 | subject, 111 | cc, 112 | text, 113 | html, 114 | attachments, 115 | }; 116 | try { 117 | transporter.sendMail(mailOpts); 118 | return true; 119 | } catch (err) { 120 | console.log(err); 121 | return false; 122 | } 123 | }; 124 | 125 | const sendPassMail = async ({ 126 | toEmail, 127 | subject, 128 | cc, 129 | text, 130 | attachments, 131 | storyMail, 132 | testInfo, 133 | testUser, 134 | iterationMail, 135 | }) => { 136 | testInfo = marked(testInfo); 137 | const html = htmlPassModel({ storyMail, testInfo, testUser, iterationMail }); 138 | const mailOpts = { 139 | from: MAIL_CONFIG.user_email, // 发送者,与上面的user一致 140 | to: toEmail, 141 | subject, 142 | cc, 143 | text, 144 | html, 145 | attachments, 146 | }; 147 | try { 148 | transporter.sendMail(mailOpts); 149 | return true; 150 | } catch (err) { 151 | console.log(err); 152 | return false; 153 | } 154 | }; 155 | 156 | const sendRepulseMail = async ({ 157 | toEmail, 158 | subject, 159 | cc, 160 | text, 161 | attachments, 162 | storyMail, 163 | repulseUser, 164 | repulseInfo, 165 | iterationMail, 166 | }) => { 167 | repulseInfo = marked(repulseInfo); 168 | const html = htmlRepulseModel({ 169 | storyMail, 170 | repulseUser, 171 | repulseInfo, 172 | iterationMail, 173 | }); 174 | const mailOpts = { 175 | from: MAIL_CONFIG.user_email, // 发送者,与上面的user一致 176 | to: toEmail, 177 | subject, 178 | cc, 179 | text, 180 | html, 181 | attachments, 182 | }; 183 | try { 184 | transporter.sendMail(mailOpts); 185 | return true; 186 | } catch (err) { 187 | console.log(err); 188 | return false; 189 | } 190 | }; 191 | 192 | export default { sendMail, sendPassMail, sendRepulseMail }; 193 | -------------------------------------------------------------------------------- /app/helper/emailTpl/pass.njk: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | 8 | 9 | 21 | 22 | 23 | 35 | 50 | 51 | 52 | 64 | 80 | 81 | 82 | 94 | 95 | 96 | 108 | 116 | 117 | 118 | 130 | 138 | 139 | 140 |
10 |

11 | 12 | 13 | 测试通过 14 | 15 | 16 | 17 | 18 | 19 |

20 |
24 |

25 | 26 | 27 | 提测版本 28 | 29 | 30 | 31 | 32 | 33 |

34 |
36 |

37 | {% for iteration in iterationMail %} 38 |

39 | 40 | {{loop.index}}.{{ iteration.name }} 41 | 42 |

43 | {% else %} 44 | {% endfor %} 45 | 46 | 47 | 48 |

49 |
53 |

54 | 55 | 56 | 提测功能模块 57 | 58 | 59 | 60 | 61 | 62 |

63 |
65 |

66 | {% for story in storyMail %} 67 |

68 | 69 | {{loop.index}}.{{ story.name }} 70 | 71 |

72 | {% else %} 73 | {% endfor %} 74 |

{{exitInfo.title|safe}}

75 | 76 | 77 | 78 |

79 |
83 |

84 | 85 | 86 | 测试内容 87 | 88 | 89 | 90 | 91 | 92 |

93 |
97 |

98 | 99 | 100 | 测试内容详情 101 | 102 | 103 | 104 | 105 | 106 |

107 |
109 |

110 | {{testInfo|safe}} 111 | 112 | 113 | 114 |

115 |
119 |

120 | 121 | 122 | 测试人 123 | 124 | 125 | 126 | 127 | 128 |

129 |
131 |

132 | {{testUser}} 133 | 134 | 135 | 136 |

137 |
141 |
142 | 143 |
144 |
145 | 146 | -------------------------------------------------------------------------------- /app/helper/emailTpl/repulse.njk: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | 8 | 9 | 21 | 22 | 23 | 35 | 50 | 51 | 52 | 64 | 80 | 81 | 82 | 94 | 95 | 96 | 108 | 116 | 117 | 118 | 130 | 138 | 139 | 140 |
10 |

11 | 12 | 13 | 测试不通过 14 | 15 | 16 | 17 | 18 | 19 |

20 |
24 |

25 | 26 | 27 | 提测版本 28 | 29 | 30 | 31 | 32 | 33 |

34 |
36 |

37 | {% for iteration in iterationMail %} 38 |

39 | 40 | {{loop.index}}.{{ iteration.name }} 41 | 42 |

43 | {% else %} 44 | {% endfor %} 45 | 46 | 47 | 48 |

49 |
53 |

54 | 55 | 56 | 提测功能模块 57 | 58 | 59 | 60 | 61 | 62 |

63 |
65 |

66 | {% for story in storyMail %} 67 |

68 | 69 | {{loop.index}}.{{ story.name }} 70 | 71 |

72 | {% else %} 73 | {% endfor %} 74 |

{{exitInfo.title|safe}}

75 | 76 | 77 | 78 |

79 |
83 |

84 | 85 | 86 | 测试打回内容 87 | 88 | 89 | 90 | 91 | 92 |

93 |
97 |

98 | 99 | 100 | 测试打回原因 101 | 102 | 103 | 104 | 105 | 106 |

107 |
109 |

110 | {{repulseInfo|safe}} 111 | 112 | 113 | 114 |

115 |
119 |

120 | 121 | 122 | 测试人 123 | 124 | 125 | 126 | 127 | 128 |

129 |
131 |

132 | {{repulseUser}} 133 | 134 | 135 | 136 |

137 |
141 |
142 | 143 |
144 |
145 | 146 | -------------------------------------------------------------------------------- /app/helper/emailTpl/email.njk: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | 8 | 9 | 21 | 22 | 23 | 35 | 50 | 51 | 52 | 64 | 81 | 82 | 83 | 95 | 96 | 97 | 109 | 117 | 118 | 119 | 131 | 139 | 140 | 141 | 153 | 161 | 162 | 163 | 179 | 187 | 188 | 189 | 201 | 209 | 210 | 211 | 223 | 233 | 234 | 235 | 251 | 259 | 260 | 261 | 273 | 281 | 282 | 283 | 295 | 303 | 304 | 305 |
10 |

11 | 12 | 13 | 提测申请 14 | 15 | 16 | 17 | 18 | 19 |

20 |
24 |

25 | 26 | 27 | 提测版本 28 | 29 | 30 | 31 | 32 | 33 |

34 |
36 |

37 | {% for iteration in iterationMail %} 38 |

39 | 40 | {{loop.index}}.{{ iteration.name }} 41 | 42 |

43 | {% else %} 44 | {% endfor %} 45 | 46 | 47 | 48 |

49 |
53 |

54 | 55 | 56 | 提测功能模块 57 | 58 | 59 | 60 | 61 | 62 |

63 |
65 |

66 | {% for story in storyMail %} 67 |

68 | 69 | {{loop.index}}.{{ story.name }} 70 | 71 |

72 | {% else %} 73 | {% endfor %} 74 | 75 |

{{exitInfo.title|safe}}

76 | 77 | 78 | 79 |

80 |
84 |

85 | 86 | 87 | 需求信息 88 | 89 | 90 | 91 | 92 | 93 |

94 |
98 |

99 | 100 | 101 | 需求描述及影响范围 102 | 103 | 104 | 105 | 106 | 107 |

108 |
110 |

111 | {{exitInfo.affect|safe}} 112 | 113 | 114 | 115 |

116 |
120 |

121 | 122 | 123 | 数据库变更 124 | 125 | 126 | 127 | 128 | 129 |

130 |
132 |

133 | {{exitInfo.database|safe}} 134 | 135 | 136 | 137 |

138 |
142 |

143 | 144 | 145 | 短信模板或推送内容变更 146 | 147 | 148 | 149 | 150 | 151 |

152 |
154 |

155 | {{exitInfo.desc|safe}} 156 | 157 | 158 | 159 |

160 |
164 |

165 | 166 | 167 | 修复的 168 | 169 | bug 170 | 171 | 列表 172 | 173 | 174 | 175 | 176 | 177 |

178 |
180 |

181 | {{exitInfo.bugs|safe}} 182 | 183 | 184 | 185 |

186 |
190 |

191 | 192 | 193 | 提测建议 194 | 195 | 196 | 197 | 198 | 199 |

200 |
202 |

203 | {{exitInfo.suggest|safe}} 204 | 205 | 206 | 207 |

208 |
212 |

213 | 214 | 215 | 开发自测描述 216 | 217 | 218 | 219 | 220 | 221 |

222 |
224 |

225 | {{exitInfo.selfDesc|safe}} 226 | 227 | 228 | 229 | 230 | 231 |

232 |
236 |

237 | 238 | 239 | 测试分支号 240 | 241 | & 242 | 243 | 分支链接地址 244 | 245 | 246 | 247 | 248 | 249 |

250 |
252 |

253 | {{exitInfo.exitInfo|safe}} 254 | 255 | 256 | 257 |

258 |
262 |

263 | 264 | 265 | 备注 266 | 267 | 268 | 269 | 270 | 271 |

272 |
274 |

275 | {{exitInfo.remark|safe}} 276 | 277 | 278 | 279 |

280 |
284 |

285 | 286 | 287 | 提测人 288 | 289 | 290 | 291 | 292 | 293 |

294 |
296 |

297 | {{summitUser}} 298 | 299 | 300 | 301 |

302 |
306 |
307 | 308 |
309 |
310 | 311 | --------------------------------------------------------------------------------