perf: [comm] 支持插件管理

This commit is contained in:
xiaojunnuo
2024-10-14 00:19:55 +08:00
parent 417971d15d
commit e8b617b80c
24 changed files with 270 additions and 179 deletions
@@ -1,7 +1,7 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService, PermissionException, ValidateException } from '@certd/lib-server';
import { BaseService, PageReq, PermissionException, ValidateException } from '@certd/lib-server';
import { AccessEntity } from '../entity/access.js';
import { AccessDefine, accessRegistry, newAccess } from '@certd/pipeline';
import { EncryptService } from './encrypt-service.js';
@@ -23,8 +23,8 @@ export class AccessService extends BaseService<AccessEntity> {
return this.repository;
}
async page(query, page = { offset: 0, limit: 20 }, order, buildQuery) {
const res = await super.page(query, page, order, buildQuery);
async page(pageReq: PageReq<AccessEntity>) {
const res = await super.page(pageReq);
res.records = res.records.map(item => {
delete item.encryptSetting;
return item;
@@ -107,7 +107,7 @@ export class AccessService extends BaseService<AccessEntity> {
if (entity == null) {
throw new Error(`该授权配置不存在,请确认是否已被删除:id=${id}`);
}
if (userId !== entity.userId) {
if (userId !== entity.userId && entity.userId !== 0) {
throw new PermissionException('您对该Access授权无访问权限');
}
// const access = accessRegistry.get(entity.type);
@@ -1,7 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { HistoryEntity } from '../entity/history.js';
import { PipelineEntity } from '../entity/pipeline.js';
import { HistoryDetail } from '../entity/vo/history-detail.js';
@@ -32,8 +32,8 @@ export class HistoryService extends BaseService<HistoryEntity> {
return this.repository;
}
async page(query, page, sort, buildQuery) {
const res = await super.page(query, page, sort, buildQuery);
async page(pageReq: PageReq<HistoryEntity>) {
const res = await super.page(pageReq);
for (const item of res.records) {
item.fillPipelineTitle();
}
@@ -1,7 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { PipelineEntity } from '../entity/pipeline.js';
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
import { Executor, isPlus, Pipeline, ResultType, RunHistory, UserInfo } from '@certd/pipeline';
@@ -68,8 +68,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
return bean;
}
async page(query: any, page: { offset: number; limit: number }, order: any, buildQuery: any) {
const result = await super.page(query, page, order, buildQuery);
async page(pageReq: PageReq<PipelineEntity>) {
const result = await super.page(pageReq);
const pipelineIds: number[] = [];
const recordMap = {};
for (const record of result.records) {
@@ -1,10 +1,11 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { PluginEntity } from '../entity/plugin.js';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { checkComm } from '@certd/pipeline';
import { BuiltInPluginService } from '../../pipeline/service/plugin-service.js';
import { checkComm, isComm } from '@certd/pipeline';
import { BuiltInPluginService } from '../../pipeline/service/builtin-plugin-service.js';
import { merge } from 'lodash-es';
@Provide()
@Scope(ScopeEnum.Singleton)
@@ -21,7 +22,103 @@ export class PluginService extends BaseService<PluginEntity> {
return this.repository;
}
async setDisabled(id: number, disabled: boolean) {
await this.repository.update({ id }, { disabled });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async page(pageReq: PageReq<PluginEntity>) {
const builtInList = await this.getBuiltInEntityList();
return {
records: builtInList,
total: builtInList.length,
offset: 0,
limit: 99999,
};
}
async getEnabledBuildInGroup() {
const groups = this.builtInPluginService.getGroups();
if (!isComm()) {
return groups;
}
const list = await this.list({
query: {
type: 'builtIn',
disabled: true,
},
});
const disabledNames = list.map(it => it.name);
for (const key in groups) {
const group = groups[key];
if (!group.plugins) {
continue;
}
group.plugins = group.plugins.filter(it => !disabledNames.includes(it.name));
}
return groups;
}
async getEnabledBuiltInList(): Promise<any> {
const builtInList = this.builtInPluginService.getList();
if (!isComm()) {
return builtInList;
}
const list = await this.list({
query: {
type: 'builtIn',
disabled: true,
},
});
const disabledNames = list.map(it => it.name);
return builtInList.filter(it => !disabledNames.includes(it.name));
}
async getBuiltInEntityList() {
const builtInList = this.builtInPluginService.getList();
const list = await this.list({
query: {
type: 'builtIn',
},
});
const records: PluginEntity[] = [];
for (const item of builtInList) {
let record = list.find(it => it.name === item.name);
if (!record) {
record = new PluginEntity();
record.disabled = false;
}
merge(record, {
name: item.name,
title: item.title,
type: 'builtIn',
icon: item.icon,
desc: item.desc,
group: item.group,
});
records.push(record);
}
return records;
}
async setDisabled(opts: { id: number; name: string; type: string; disabled: boolean }) {
const { id, name, type, disabled } = opts;
if (!type) {
throw new Error('参数错误: type 不能为空');
}
if (id > 0) {
//update
await this.repository.update({ id }, { disabled });
return;
}
if (name && type === 'builtIn') {
const pluginEntity = new PluginEntity();
pluginEntity.name = name;
pluginEntity.type = type;
pluginEntity.disabled = disabled;
await this.repository.save(pluginEntity);
return;
}
throw new Error('参数错误: id 和 name 必须有一个');
}
}