chore: 模版创建流水线

This commit is contained in:
xiaojunnuo
2025-06-25 14:41:27 +08:00
parent 821c6d807d
commit 9296ba7492
15 changed files with 290 additions and 23 deletions
@@ -59,8 +59,8 @@ export class TemplateController extends CrudController<TemplateService> {
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
return super.delete(id);
await this.service.batchDelete([id], this.getUserId());
return this.ok({});
}
@Post('/batchDelete', { summary: Constants.per.authOnly })
@@ -52,6 +52,7 @@ export class CnameProviderService extends BaseService<CnameProviderEntity> {
}
}
//@ts-ignore
async delete(ids: any) {
if (!ids) {
return;
@@ -37,6 +37,9 @@ export class PipelineEntity {
@Column({ comment: '来源', nullable: true, default: '' })
from: string;
@Column({ name:"template_id", comment: '是否模版', nullable: true, default: '' })
templateId: number;
@Column({
name: 'last_history_time',
comment: '最后一次执行时间',
@@ -46,6 +46,7 @@ import {UserSuiteEntity, UserSuiteService} from "@certd/commercial-core";
import {CertInfoService} from "../../monitor/service/cert-info-service.js";
import {TaskServiceBuilder} from "./task-service-getter.js";
import {nanoid} from "nanoid";
import {set} from "lodash-es";
const runningTasks: Map<string | number, Executor> = new Map();
@@ -117,6 +118,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
}
async page(pageReq: PageReq<PipelineEntity>) {
//模版流水线不要被查询出来
set(pageReq,"query.templateId",0)
const result = await super.page(pageReq);
await this.fillLastVars(result.records);
@@ -281,6 +284,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
},
where: {
disabled: false,
templateId: 0,
},
});
const ids = idEntityList.map(item => {
@@ -385,7 +389,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
}
}
async delete(id: any) {
//@ts-ignore
async delete(id:any) {
await this.clearTriggers(id);
//TODO 删除storage
// const storage = new DbStorage(pipeline.userId, this.storageService);
@@ -1,12 +1,15 @@
import {Inject, Provide, Scope, ScopeEnum} from '@midwayjs/core';
import {BaseService, SysSettingsService} from '@certd/lib-server';
import {InjectEntityModel} from '@midwayjs/typeorm';
import {Repository} from 'typeorm';
import { TemplateEntity } from '../entity/template.js';
import { PipelineService } from './pipeline-service.js';
import {In, Repository} from 'typeorm';
import {TemplateEntity} from '../entity/template.js';
import {PipelineService} from './pipeline-service.js';
import {cloneDeep} from "lodash-es";
import {PipelineEntity} from "../entity/pipeline.js";
import {Pipeline} from "@certd/pipeline";
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
@Scope(ScopeEnum.Request, {allowDowngrade: true})
export class TemplateService extends BaseService<TemplateEntity> {
@InjectEntityModel(TemplateEntity)
repository: Repository<TemplateEntity>;
@@ -22,6 +25,47 @@ export class TemplateService extends BaseService<TemplateEntity> {
return this.repository;
}
async add(param: any) {
const pipelineId = param.pipelineId;
delete param.pipelineId;
const pipelineEntity = await this.pipelineService.info(pipelineId);
if (!pipelineEntity) {
throw new Error('pipeline not found');
}
if (pipelineEntity.userId !== param.userId) {
throw new Error('permission denied');
}
let template = null
await this.transaction(async (tx: any) => {
template = await tx.getRepository(TemplateEntity).save(param);
let newPipeline = cloneDeep(pipelineEntity)
//创建pipeline模版
newPipeline.id = undefined;
newPipeline.title = template.title+"模版流水线"
newPipeline.templateId = template.id
const pipelineJson:Pipeline = JSON.parse(newPipeline.content)
delete pipelineJson.triggers
pipelineJson.id = template.id
pipelineJson.userId = template.userId
pipelineJson.title = newPipeline.title
newPipeline.content = JSON.stringify(pipelineJson)
newPipeline = await tx.getRepository(PipelineEntity).save(newPipeline)
const update :any= {}
update.id = template.id
update.pipelineId = newPipeline.id
await tx.getRepository(TemplateEntity).save(update)
})
return template
}
async detail(id: number, userId: number) {
const info = await this.info(id)
if (!info) {
@@ -30,12 +74,31 @@ export class TemplateService extends BaseService<TemplateEntity> {
if (info.userId !== userId) {
throw new Error('无权限');
}
const pipeline = await this.pipelineService.info(info.pipelineId);
let pipeline = null
if (info.pipelineId) {
const pipelineEntity = await this.pipelineService.info(info.pipelineId);
pipeline = JSON.parse(pipelineEntity.content)
}
return {
template:info,
pipeline: JSON.parse(pipeline.content),
template: info,
pipeline,
}
}
async batchDelete(ids: number[], userId: number) {
const where :any= {
id: In(ids),
}
if (userId > 0) {
where.userId = userId
}
const list = await this.getRepository().find({where })
ids = list.map(item => item.id)
const pipelineIds = list.map(item => item.pipelineId)
await this.delete(ids);
await this.pipelineService.batchDelete(pipelineIds,userId)
}
}
@@ -129,6 +129,7 @@ export class RoleService extends BaseService<RoleEntity> {
return permissionSet;
}
//@ts-ignore
async delete(id: any) {
const idArr = this.resolveIdArr(id);
//@ts-ignore
@@ -253,6 +253,7 @@ export class UserService extends BaseService<UserEntity> {
await this.update(param);
}
//@ts-ignore
async delete(ids: any) {
if (typeof ids === 'string') {
ids = ids.split(',');
@@ -7,6 +7,7 @@ import {
createCertDomainGetterInputDefine,
createRemoteSelectInputDefine
} from "@certd/plugin-lib";
import {PageReq} from "@certd/lib-server";
@IsTaskPlugin({
name: 'AliyunDeployCertToWaf',
@@ -168,7 +169,7 @@ export class AliyunDeployCertToWaf extends AbstractTaskPlugin {
}
}
async onGetCnameList(data: any) {
async onGetCnameList(data: PageReq) {
if (!this.accessId) {
throw new Error('请选择Access授权');
}