import Base from './base' import TeambitionClient from '../client/teambition' import TesthubClient from '../client/testhub' import { App, DbCollection } from '../../types' import { Context } from 'koa' import { parseSteps } from '../../service/utils' export class TestcaseChecker extends Base { private teambitionClient: TeambitionClient private testhubClient: TesthubClient private caseTypeMap = new Map() private priorityMap = new Map() private maintainerMap = new Map() private groupArray: DbCollection.TesthubGroup[] = null constructor (app: App, ctx: Context) { super(app, ctx) this.teambitionClient = new TeambitionClient(app, ctx) this.testhubClient = new TesthubClient(app, ctx) } async testPlan (_testplanId: string, _projectId: string) { const testPlans: DbCollection.TestPlan[] = await this.teambitionClient.getTestPlansInProject(_projectId) if (!testPlans.some((testplan) => { return testplan._id === _testplanId })) { this.ctx.throw(400, 'ParamError: _testplanId') } } async testhub(_testhubId: string, _organizationId: string) { const testhub: DbCollection.Testhub = await this.testhubClient.getTesthub(_testhubId) if (testhub._organizationId !== _organizationId) { this.ctx.throw(400, 'ParamError: _testhubId') } } steps(desc = '', expected = '', row: number): Array<{ desc: string, expected: string }> { const result = [] const [descArray, expectedArray] = [desc, expected].map(parseSteps) if (descArray.length > 20) { this.pushError('err.excel.steps.desc.invalid', row, 'tc.step.desc', desc) } if (descArray.length > 20) { this.pushError('err.excel.steps.desc.expected.invalid', row, 'tc.step.expected', expected) } for (let i = 0; i < Math.max(descArray.length, expectedArray.length); i++) { result.push({ desc: descArray[i] || '', expected: expectedArray[i] || '' }) } return result } caseType (value: string, row: number): string{ if (!value) { return } if (!this.caseTypeMap.size) { this.caseTypeMap.set(this.__('tc.caseType.function'), 'function')// '功能测试' this.caseTypeMap.set(this.__('tc.caseType.performance'), 'performance')// '性能测试' this.caseTypeMap.set(this.__('tc.caseType.config'), 'config') // '配置相关' this.caseTypeMap.set(this.__('tc.caseType.deployment'), 'deployment') // '安装部署' this.caseTypeMap.set(this.__('tc.caseType.security'), 'security') // '安全相关' this.caseTypeMap.set(this.__('tc.caseType.api'), 'api')// '接口测试' this.caseTypeMap.set(this.__('tc.caseType.auto'), 'auto')// '自动化测试' this.caseTypeMap.set(this.__('tc.caseType.other'), 'other')// '其它' this.caseTypeMap.set(this.__('tc.caseType.empty'), null)// '无' } if (this.caseTypeMap.has(value)) { return this.caseTypeMap.get(value) } else { this.pushError('err.excel.caseType.invalid', row, 'tc.caseType', value) } } priority (value: string, row: number):number { if (!value) { return -1 } value = String(value) if (!this.priorityMap.size) { for (let i = 0; i <= 5; i++) { this.priorityMap.set(`P${i}`, i) } this.priorityMap.set('无', -1) } value = value.toLocaleUpperCase() if (this.priorityMap.has(value)) { return this.priorityMap.get(value) } else { this.pushError('err.excel.priority.invalid', row, 'tc.priority', value) } } async group(str: string, _appId: string, row: number, appType = 'testhub'): Promise { if (!str) return if (!this.groupArray) { const groups = await ( appType === 'testplan' ? this.teambitionClient.getTestPlanGroups(_appId, true) : this.testhubClient.getTesthubGroups(_appId) ) // 保证 同名分组,选取第一个 this.groupArray = (groups || []).sort((pre, cur) => pre.position > cur.position) } const groupNames = String(str).split('|').map((item) => item.trim()) let curGroup while (groupNames.length) { const curGroupName = groupNames.shift() curGroup = this.groupArray.find((group) => { const ancestorIds = group.ancestorIds || [] if (curGroup && `${curGroup._id}` === ancestorIds[0] && group.name === curGroupName) return true if (!curGroup && group.name === curGroupName && ancestorIds.length === 0) return true }) if (!curGroup) return } return curGroup._id } async maintainer(str: string, _organizationId: string, throwErr: boolean, row: number): Promise { if (!str) return str = String(str) if (this.maintainerMap.has(str)) { return this.maintainerMap.get(str) } const maintainer = (await this.teambitionClient.getOrganizationMemberByName(_organizationId, str)).result.shift() if (!maintainer && throwErr) { this.pushError('err.excel.maintainer.invalid', row, 'maintainer', str) return null } else if (!maintainer) { return null } this.maintainerMap.set(str, maintainer._userId) return maintainer._userId } required(item: object, key: string, row: number): string { let val = item[key] if (val != null) { val = typeof val === 'string' ? val.trim() : `${val}` } if (val) return val this.pushError('err.excel.' + key + '.required', row, key, item[key]) return '' } lengthLimit (fieldInfo: {type: string, key: string, val: string, row: number}, maxLength?: number, minLength?: number) { const {type, key, val, row} = fieldInfo if (!val) return if (maxLength != null && val.length > maxLength) { this.pushErrorWithParams({ name: `err.excel.${type}.maxLength`, params: { length: maxLength } }, row, key, 'length') } else if (minLength != null && val.length < minLength) { this.pushErrorWithParams({ name: `err.excel.${type}.mixLength`, params: { length: minLength } }, row, key, 'length') } } }