From 0c0be4ad930b92d6e069eb4d8dead0e7143b2c76 Mon Sep 17 00:00:00 2001 From: "huoyong.msb" Date: Wed, 5 Aug 2020 11:19:26 +0800 Subject: [PATCH] feat: fix #692 --- src/routes/repository.ts | 22 ++++++++++++++++------ src/routes/utils/access.ts | 12 ------------ src/scripts/dev.ts | 2 +- src/service/mail.ts | 7 ++----- src/service/migrate.ts | 28 +++++++++++++++++++++------- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/routes/repository.ts b/src/routes/repository.ts index 470d48b..e175c9b 100644 --- a/src/routes/repository.ts +++ b/src/routes/repository.ts @@ -18,6 +18,7 @@ import nanoid = require('nanoid') import { LOG_SEPERATOR, LOG_SUB_SEPERATOR } from '../models/bo/historyLog' import { ENTITY_TYPE } from './utils/const' import { IPager } from '../types' +import * as JSON5 from 'json5' router.get('/app/get', async (ctx, next) => { let data: any = {} @@ -1115,13 +1116,22 @@ router.post('/repository/import', isLoggedIn, async (ctx) => { ctx.body = Consts.COMMON_ERROR_RES.ACCESS_DENY return } - const result = await MigrateService.importRepoFromRAP1DocUrl(orgId, ctx.session.id, docUrl, +version, projectData) - ctx.body = { - isOk: result, - message: result ? '导入成功' : '导入失败', - repository: { - id: 1, + let success = false + let message = '' + try { + if (+version === 3) { + await MigrateService.importRepoFromJSON(JSON5.parse(projectData).data, ctx.session.id, true, orgId) + success = true + } else { + success = await MigrateService.importRepoFromRAP1DocUrl(orgId, ctx.session.id, docUrl, +version, projectData) } + } catch (ex) { + success = false + message = ex.message + } + ctx.body = { + isOk: success, + message: success ? '导入成功' : `导入失败:${message}`, } }) diff --git a/src/routes/utils/access.ts b/src/routes/utils/access.ts index 7eaa3e4..ffccc8e 100644 --- a/src/routes/utils/access.ts +++ b/src/routes/utils/access.ts @@ -29,18 +29,6 @@ export class AccessUtils { if (inTestMode) { return true } - // 内网全可读 - const { - ORGANIZATION_GET, - REPOSITORY_GET, - MODULE_GET, - INTERFACE_GET, - PROPERTY_GET, - } = ACCESS_TYPE - const GET_TYPES = [ORGANIZATION_GET, REPOSITORY_GET, MODULE_GET, INTERFACE_GET, PROPERTY_GET] - if (GET_TYPES.includes(accessType)) { - return true - } // 无 session 且无 toeken 时拒绝访问 if (!curUserId && !token) { diff --git a/src/scripts/dev.ts b/src/scripts/dev.ts index 002621e..029c54e 100644 --- a/src/scripts/dev.ts +++ b/src/scripts/dev.ts @@ -8,7 +8,7 @@ const start = () => { let open = false console.log('----------------------------------------') app.listen(port, () => { - console.log(`rap2-dolores is running as ${url}`) + console.log(`rap2-delos is running as ${url}`) if (!open) return try { execSync(`osascript openChrome.applescript ${url}`, { cwd: __dirname, stdio: 'ignore' }) diff --git a/src/service/mail.ts b/src/service/mail.ts index 537c95e..85d3e5e 100644 --- a/src/service/mail.ts +++ b/src/service/mail.ts @@ -2,9 +2,7 @@ import * as nodemailer from 'nodemailer' import config from '../config' export default class MailService { - - public static async sendMail(mailOptions: any) { - + public static async sendMail(mailOptions: nodemailer.SendMailOptions) { const transporter = nodemailer.createTransport(config.mail) return new Promise((resolve, reject) => { @@ -25,7 +23,6 @@ export default class MailService { } public static send(to: string | string[], subject: string, html: string) { - const transporter = nodemailer.createTransport(config.mail) const mailOptions = { @@ -266,4 +263,4 @@ public static mailFindpwdTemp = ` } ` -} \ No newline at end of file +} diff --git a/src/service/migrate.ts b/src/service/migrate.ts index 3891196..7116eda 100644 --- a/src/service/migrate.ts +++ b/src/service/migrate.ts @@ -1,4 +1,4 @@ -import { Repository, Module, Interface, Property, QueryInclude, User } from '../models' +import { Repository, Module, Interface, Property, User, QueryInclude } from '../models' import { SCOPES } from '../models/bo/property' import Tree from '../routes/utils/tree' import * as JSON5 from 'json5' @@ -8,10 +8,8 @@ import { Op } from 'sequelize' import RedisService, { CACHE_KEY } from './redis' import MailService from './mail' import * as md5 from 'md5' -// import DingPushService from './ding.push' -// import sequelize from '../models/sequelize' -import * as _ from 'lodash' const isMd5 = require('is-md5') +import * as _ from 'lodash' const safeEval = require('notevil') @@ -429,7 +427,6 @@ export default class MigrateService { let description = [] if (p.name) description.push(p.name) if (p.remark && remarkWithoutMock) description.push(remarkWithoutMock) - const pCreated = await Property.create({ scope, name, @@ -739,7 +736,6 @@ export default class MigrateService { } else { mod = repository.modules[findIndex] } - for (const action in paths) { const apiObj = paths[action][Object.keys(paths[action])[0]] const method = Object.keys(paths[action])[0] @@ -1176,7 +1172,7 @@ export default class MigrateService { } /** 可以直接让用户把自己本地的 data 数据导入到 RAP 中 */ - public static async importRepoFromJSON(data: JsonData, curUserId: number) { + public static async importRepoFromJSON(data: JsonData, curUserId: number, createRepo: boolean = false, orgId?: number) { function parseJSON(str: string) { try { const data = JSON5.parse(str) @@ -1186,6 +1182,21 @@ export default class MigrateService { } } + if (createRepo) { + if (orgId === undefined) { + throw new Error("orgId is essential while createRepo = true") + } + const repo = await Repository.create({ + name: data.name, + description: data.description, + visibility: true, + ownerId: curUserId, + creatorId: curUserId, + organizationId: orgId, + }) + data.id = repo.id + } + const repositoryId = data.id await Promise.all( data.modules.map(async (modData, index) => { @@ -1248,6 +1259,7 @@ export default class MigrateService { value: pData.value, type: pData.type, description: pData.description, + pos: pData.pos, priority: index + 1, interfaceId: itf.id, creatorId: curUserId, @@ -1304,6 +1316,8 @@ interface JsonData { * 要导入的目标 repo id 名 */ id: number + name?: string + description?: string modules: { name: string description?: string