From 25e5c430eb54f0b6850af54fd843d917bcf5629a Mon Sep 17 00:00:00 2001 From: Bosn Date: Wed, 2 May 2018 17:04:02 +0800 Subject: [PATCH] add importer --- package.json | 4 ++++ src/routes/repository.ts | 22 +++++++++++++++++++++- src/service/migrate.ts | 21 +++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 396238d..0855d40 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,8 @@ "path-to-regexp": "^2.1.0", "redis": "^2.8.0", "reflect-metadata": "^0.1.10", + "request": "^2.85.0", + "request-promise": "^4.2.2", "sequelize": "^4.28.6", "sequelize-typescript": "^0.6.4", "svg-captcha": "^1.3.11", @@ -70,6 +72,8 @@ "@types/mockjs": "^1.0.0", "@types/node": "^8.5.2", "@types/redis": "^2.8.6", + "@types/request": "^2.47.0", + "@types/request-promise": "^4.1.41", "@types/sequelize": "^4.27.12", "@types/underscore": "^1.8.8", "babel-eslint": "^7.2.3", diff --git a/src/routes/repository.ts b/src/routes/repository.ts index e9d1b4b..7372af8 100644 --- a/src/routes/repository.ts +++ b/src/routes/repository.ts @@ -5,9 +5,10 @@ import Pagination from './utils/pagination' import { User, Organization, Repository, Module, Interface, Property, QueryInclude, Logger } from '../models' import { Sequelize } from 'sequelize-typescript' import Tree from './utils/tree' -import { AccessUtils, ACCESS_TYPE } from './utils/access'; +import { AccessUtils, ACCESS_TYPE } from './utils/access' import * as Consts from './utils/const' import RedisService, { CACHE_KEY } from '../service/redis' +import MigrateService from '../service/migrate'; const { initRepository, initModule } = require('./utils/helper') const Op = Sequelize.Op @@ -760,3 +761,22 @@ router.get('/property/remove', async (ctx) => { }), } }) + +router.post('/repository/import', async (ctx) => { + if (!ctx.session || !ctx.session.id) { + ctx.body = { + isOk: false, + message: 'NOT LOGIN' + } + return + } + const { docUrl, orgId } = ctx.request.body + const result = await MigrateService.importRepoFromRAP1DocUrl(orgId, ctx.session.id, docUrl) + ctx.body = { + isOk: result, + message: result ? '导入成功' : '导入失败', + repository: { + id: 1, + } + } +}) diff --git a/src/service/migrate.ts b/src/service/migrate.ts index 237c21b..ed60b37 100644 --- a/src/service/migrate.ts +++ b/src/service/migrate.ts @@ -2,6 +2,8 @@ import { Repository, Module, Interface, Property, User } from "../models"; import { SCOPES, TYPES } from "../models/bo/property"; import * as md5 from 'md5' const isMd5 = require('is-md5') +import * as querystring from 'querystring' +import * as rp from 'request-promise' export default class MigrateService { public static async importRepoFromRAP1ProjectData(orgId: number, curUserId: number, projectData: any): Promise { @@ -46,9 +48,9 @@ export default class MigrateService { async function processParam(p: any, scope: SCOPES, parentId?: number) { const pCreated = await Property.create({ scope, - name: p.name, + name: p.identifier, type: getTypeFromRAP1DataType(p.dataType), - description: `${p.remark}`, + description: `${p.remark}${p.name ? ', ' + p.name : ''}`, priority: pCounter++, interfaceId: itf.id, creatorId: curUserId, @@ -85,6 +87,21 @@ export default class MigrateService { } } } + + public static async importRepoFromRAP1DocUrl(orgId: number, curUserId: number, docUrl: string): Promise { + const { projectId } = querystring.parse(docUrl.substring(docUrl.indexOf('?') + 1)) + let domain = docUrl + if (domain.indexOf('http') === -1) { + domain = 'http://' + domain + } + domain = domain.substring(0, domain.indexOf('/', domain.indexOf('.'))) + const result = await rp(`${domain}/api/queryRAPModel.do?projectId=${projectId}`, { + json: true, + }) + let json = result.modelJSON + const projectData = JSON.parse(json) + return await this.importRepoFromRAP1ProjectData(orgId, curUserId, projectData) + } } function getMethodFromRAP1RequestType(type: number) {