mirror of
https://github.com/certd/certd.git
synced 2026-08-02 11:04:49 +08:00
chore: 模版创建流水线
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user