Files
certd/packages/ui/certd-server/src/controller/user/pipeline/pipeline-controller.ts
T

381 lines
14 KiB
TypeScript
Raw Normal View History

2024-10-09 02:34:28 +08:00
import { Constants, CrudController, SysSettingsService } from '@certd/lib-server';
2026-02-11 00:07:29 +08:00
import { isPlus } from '@certd/plus-core';
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
2026-03-15 18:26:49 +08:00
import { ApiProperty, ApiTags } from '@midwayjs/swagger';
2026-02-11 00:07:29 +08:00
import { SiteInfoService } from '../../../modules/monitor/index.js';
2025-01-15 01:26:39 +08:00
import { HistoryService } from '../../../modules/pipeline/service/history-service.js';
2026-02-11 00:07:29 +08:00
import { PipelineService } from '../../../modules/pipeline/service/pipeline-service.js';
2025-01-15 01:26:39 +08:00
import { AuthService } from '../../../modules/sys/authority/service/auth-service.js';
2026-03-15 18:26:49 +08:00
const pipelineExample = `
// 流水线配置示例,实际传送时要去掉注释
{
"title": "handsfree.work证书自动化", //标题
"runnableType": "pipeline", //类型,固定为pipeline
"projectId": 1, // 项目ID 未开启企业模式,无需传递
"type": "cert", // 流水线类型,cert:证书自动化, custom :自定义流水线
"from": "", // 来源,cert:证书自动化, upload :证书托管
"stages": [ //流水线阶段,多个阶段串行执行
{
"id": "edKopnpp2wvOCQ2vkV8Ii" // 阶段ID, 由客户端生成,流水线内部全局唯一即可
"title": "证书申请阶段", //阶段标题
"runnableType": "stage", // 类型标识
"tasks": [ // 任务列表,多个任务并行执行
{
"id": "Lb8I7Dj10cGh6gqIIkmKv" // 任务ID, 由客户端生成,流水线内部全局唯一即可
"title": "证书申请任务", // 任务标题
"runnableType": "task", // 类型标识
"steps": [ // 步骤列表,同一个任务下的多个步骤串行执行
{
"id": "zc8X1L2f0N0KgbrqFU3gz" // 步骤ID, 由客户端生成,流水线内部全局唯一即可
"type": "CertApply", // 插件类型
"title": "申请证书", // 步骤标题
"runnableType": "step", // 类型标识
"input": { //输入参数 ,根据插件的配置有不同的参数,具体参数建议通过浏览器F12进行查看
"renewDays": 15,
},
"strategy": { // 策略
"runStrategy": 0 // 运行策略,0:正常执行,1:成功后跳过
},
}
],
}
],
}
],
"triggers": [ // 触发器配置
{
"id": "pt3865qfIAkULBS5sOQf7", // 触发器ID, 由客户端生成,流水线内部全局唯一即可
"title": "定时触发",
"type": "timer", // 触发器类型,timer:定时触发
"props": {
"cron": "0 34 5 * * *" // 定时表达式
},
}
],
"notifications": [ // 通知配置
{
"id": "5pb1gZTnDEjdHkR5tDd6g" // 通知ID, 由客户端生成,流水线内部全局唯一即可
"title": "使用默认通知",// 通知标题
"type": "custom", // 通知类型,固定为custom
"when": [ // 触发条件,error:错误时触发,turnToSuccess:失败转成功后触发,success: 成功时触发
"error",
"turnToSuccess"
],
"notificationId": 0, // 通知ID 0为使用默认通知
}
],
}`
export class PipelineSaveDTO {
@ApiProperty({ description: 'Id,修改时必传' })
id: number;
userId: number;
@ApiProperty({ description: '标题' })
title: string;
@ApiProperty({ description: '流水线详细配置,json格式的字符串', example: pipelineExample })
content: string;
@ApiProperty({ description: '保留历史版本数量' })
keepHistoryCount: number;
@ApiProperty({ description: '分组ID' })
groupId: number;
@ApiProperty({ description: '备注' })
remark: string;
@ApiProperty({ description: '状态' })
status: string;
@ApiProperty({ description: '是否禁用' })
disabled: boolean;
@ApiProperty({ description: '类型' })
type: string;
webhookKey: string;
@ApiProperty({ description: '来源' })
from: string;
@ApiProperty({ description: '排序' })
order: number;
@ApiProperty({ description: '项目ID' })
projectId: number;
@ApiProperty({ description: '流水线有效期,单位秒' })
validTime: number;
@ApiProperty({ description: '是否增加证书监控' })
addToMonitorEnabled: boolean
@ApiProperty({ description: '增加证书监控的域名,逗号分隔' })
addToMonitorDomains: string
}
2023-01-29 13:44:19 +08:00
/**
* 证书
*/
@Provide()
2026-03-15 14:01:34 +08:00
@ApiTags(['pipeline'])
2023-01-29 13:44:19 +08:00
@Controller('/api/pi/pipeline')
2023-05-23 18:01:20 +08:00
export class PipelineController extends CrudController<PipelineService> {
2023-01-29 13:44:19 +08:00
@Inject()
service: PipelineService;
2023-07-03 11:45:32 +08:00
@Inject()
historyService: HistoryService;
@Inject()
authService: AuthService;
@Inject()
sysSettingsService: SysSettingsService;
2023-01-29 13:44:19 +08:00
@Inject()
siteInfoService: SiteInfoService;
2026-02-11 00:07:29 +08:00
2023-01-29 13:44:19 +08:00
getService() {
return this.service;
}
2026-03-15 18:26:49 +08:00
@Post('/page', { description: Constants.per.authOnly, summary: "查询流水线分页列表" })
2023-01-29 13:44:19 +08:00
async page(@Body(ALL) body) {
const isAdmin = await this.authService.isAdmin(this.ctx);
const publicSettings = await this.sysSettingsService.getPublicSettings();
2026-02-11 00:54:56 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
body.query.projectId = projectId
let onlyOther = false
if (isAdmin) {
if (publicSettings.managerOtherUserPipeline) {
//管理员管理 其他用户
if (body.query.userId === -1) {
//如果只查询其他用户
onlyOther = true;
delete body.query.userId;
}
} else {
2026-02-11 00:07:29 +08:00
body.query.userId = userId;
}
} else {
2026-02-11 00:07:29 +08:00
body.query.userId = userId;
}
2024-08-04 02:35:45 +08:00
const title = body.query.title;
delete body.query.title;
2023-01-29 13:44:19 +08:00
const buildQuery = qb => {
2024-08-04 02:35:45 +08:00
if (title) {
2024-11-20 10:46:05 +08:00
qb.andWhere('(title like :title or content like :content)', { title: `%${title}%`, content: `%${title}%` });
2024-08-04 02:35:45 +08:00
}
if (onlyOther) {
qb.andWhere('user_id != :userId', { userId: this.getUserId() });
}
2023-01-29 13:44:19 +08:00
};
2024-08-04 02:35:45 +08:00
if (!body.sort || !body.sort?.prop) {
body.sort = { prop: 'order', asc: false };
}
2024-10-14 00:19:55 +08:00
const pageRet = await this.getService().page({
query: body.query,
page: body.page,
2024-10-14 14:00:24 +08:00
sort: body.sort,
2024-10-14 00:19:55 +08:00
buildQuery,
});
2024-08-06 10:12:02 +08:00
return this.ok(pageRet);
2023-01-29 13:44:19 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/getSimpleByIds', { description: Constants.per.authOnly, summary: "根据ID列表获取流水线简单信息" })
2025-06-23 18:20:49 +08:00
async getSimpleById(@Body(ALL) body) {
2026-02-11 00:54:56 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
const ret = await this.getService().getSimplePipelines(body.ids, userId, projectId);
2025-06-23 18:20:49 +08:00
return this.ok(ret);
}
// @Post('/add', { description: Constants.per.authOnly })
2026-03-15 14:01:34 +08:00
// async add(@Body(ALL) bean: PipelineEntity) {
// const { projectId, userId } = await this.getProjectUserIdWrite()
// bean.userId = userId
// bean.projectId = projectId
// return super.add(bean);
// }
2023-01-29 13:44:19 +08:00
// @Post('/update', { description: Constants.per.authOnly })
2026-03-15 14:01:34 +08:00
// async update(@Body(ALL) bean) {
// await this.checkOwner(this.getService(), bean.id,"write",true);
// delete bean.userId;
// delete bean.projectId;
// return super.update(bean);
// }
2023-01-29 13:44:19 +08:00
@Post('/save', { description: Constants.per.authOnly, summary: '新增/更新流水线' })
2026-03-15 18:26:49 +08:00
async save(@Body() bean: PipelineSaveDTO) {
2026-02-13 22:24:04 +08:00
const { userId ,projectId} = await this.getProjectUserIdWrite()
2023-01-29 13:44:19 +08:00
if (bean.id > 0) {
2026-02-14 00:08:13 +08:00
const {userId,projectId} = await this.checkOwner(this.getService(), bean.id,"write",true);
bean.userId = userId;
bean.projectId = projectId;
} else {
2026-02-11 00:54:56 +08:00
bean.userId = userId;
2026-02-13 22:24:04 +08:00
bean.projectId = projectId;
2023-01-29 13:44:19 +08:00
}
2025-10-24 23:48:32 +08:00
if (!this.isAdmin()) {
2025-10-24 23:48:32 +08:00
// 非管理员用户 不允许设置流水线有效期
delete bean.validTime
}
2026-03-15 18:26:49 +08:00
const { version } = await this.service.save(bean as any);
//是否增加证书监控
if (bean.addToMonitorEnabled && bean.addToMonitorDomains) {
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
if (isPlus() && sysPublicSettings.certDomainAddToMonitorEnabled) {
//增加证书监控
await this.siteInfoService.doImport({
text: bean.addToMonitorDomains,
2026-02-11 00:54:56 +08:00
userId: userId,
2026-02-13 22:24:04 +08:00
projectId: projectId,
});
}
}
return this.ok({ id: bean.id, version: version });
2023-01-29 13:44:19 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/delete', { description: Constants.per.authOnly, summary: "删除流水线" })
async delete(@Query('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.getService(), id,"write",true);
2023-06-28 12:46:29 +08:00
await this.service.delete(id);
return this.ok({});
2023-01-29 13:44:19 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/disabled', { description: Constants.per.authOnly, summary: "禁用流水线" })
async disabled(@Body(ALL) bean) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.getService(), bean.id,"write",true);
delete bean.userId;
2026-02-13 21:28:17 +08:00
delete bean.projectId;
2026-01-07 10:58:17 +08:00
await this.service.disabled(bean.id, bean.disabled);
return this.ok({});
}
2026-03-15 18:26:49 +08:00
@Post('/detail', { description: Constants.per.authOnly, summary: "查询流水线详情" })
async detail(@Query('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.getService(), id,"read",true);
2023-01-29 13:44:19 +08:00
const detail = await this.service.detail(id);
return this.ok(detail);
}
2026-03-15 18:26:49 +08:00
@Post('/trigger', { description: Constants.per.authOnly, summary: "触发流水线执行" })
2024-09-02 18:36:12 +08:00
async trigger(@Query('id') id: number, @Query('stepId') stepId?: string) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.getService(), id,"write",true);
await this.service.trigger(id, stepId, true);
2023-01-29 13:44:19 +08:00
return this.ok({});
}
2023-07-03 11:45:32 +08:00
2026-03-15 18:26:49 +08:00
@Post('/cancel', { description: Constants.per.authOnly, summary: "取消流水线执行" })
async cancel(@Query('historyId') historyId: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.historyService, historyId,"write",true);
2023-07-03 11:45:32 +08:00
await this.service.cancel(historyId);
return this.ok({});
}
2024-10-31 22:35:05 +08:00
2026-03-15 18:26:49 +08:00
@Post('/count', { description: Constants.per.authOnly, summary: "查询流水线数量" })
2024-10-31 22:35:05 +08:00
async count() {
2026-02-11 00:54:56 +08:00
const { userId } = await this.getProjectUserIdRead()
const count = await this.service.count({ userId: userId });
2024-10-31 22:35:05 +08:00
return this.ok({ count });
}
2026-02-11 00:54:56 +08:00
private async checkPermissionCall(callback:any){
let { projectId ,userId} = await this.getProjectUserIdWrite()
if(projectId){
return await callback({userId,projectId});
}
const isAdmin = await this.authService.isAdmin(this.ctx);
userId = isAdmin ? undefined : userId;
return await callback({userId});
}
2026-03-15 18:26:49 +08:00
@Post('/batchDelete', { description: Constants.per.authOnly, summary: "批量删除流水线" })
async batchDelete(@Body('ids') ids: number[]) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchDelete(ids, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : userId;
// await this.service.batchDelete(ids, userId);
// return this.ok({});
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchDelete(ids, userId,projectId);
})
return this.ok({})
}
2025-12-16 22:31:06 +08:00
2026-03-15 18:26:49 +08:00
@Post('/batchUpdateGroup', { description: Constants.per.authOnly, summary: "批量更新流水线分组" })
async batchUpdateGroup(@Body('ids') ids: number[], @Body('groupId') groupId: number) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchUpdateGroup(ids, groupId, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateGroup(ids, groupId, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateGroup(ids, groupId, userId,projectId);
})
return this.ok({});
}
2025-05-27 11:08:08 +08:00
2025-06-18 12:29:43 +08:00
2026-03-15 18:26:49 +08:00
@Post('/batchUpdateTrigger', { description: Constants.per.authOnly, summary: "批量更新流水线触发器" })
2025-06-18 12:29:43 +08:00
async batchUpdateTrigger(@Body('ids') ids: number[], @Body('trigger') trigger: any) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchUpdateTrigger(ids, trigger, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateTrigger(ids, trigger, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateTrigger(ids, trigger, userId,projectId);
})
2025-06-18 12:29:43 +08:00
return this.ok({});
}
2026-03-15 18:26:49 +08:00
@Post('/batchUpdateNotification', { description: Constants.per.authOnly, summary: "批量更新流水线通知" })
2025-06-18 12:29:43 +08:00
async batchUpdateNotification(@Body('ids') ids: number[], @Body('notification') notification: any) {
2026-02-11 00:54:56 +08:00
// const isAdmin = await this.authService.isAdmin(this.ctx);
// const userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateNotifications(ids, notification, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateNotifications(ids, notification, userId,projectId);
})
2025-06-18 12:29:43 +08:00
return this.ok({});
}
2026-03-15 18:26:49 +08:00
@Post('/batchRerun', { description: Constants.per.authOnly, summary: "批量重新运行流水线" })
async batchRerun(@Body('ids') ids: number[], @Body('force') force: boolean) {
2026-02-11 00:54:56 +08:00
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchRerun(ids, force,userId,projectId);
})
2025-05-27 11:08:08 +08:00
return this.ok({});
}
2026-03-15 18:26:49 +08:00
@Post('/batchTransfer', { description: Constants.per.authOnly, summary: "批量迁移流水线" })
async batchTransfer(@Body('ids') ids: number[], @Body('toProjectId') toProjectId: number) {
await this.checkPermissionCall(async ({})=>{
await this.service.batchTransfer(ids, toProjectId);
})
return this.ok({});
}
2026-03-15 18:26:49 +08:00
@Post('/refreshWebhookKey', { description: Constants.per.authOnly, summary: "刷新Webhook密钥" })
async refreshWebhookKey(@Body('id') id: number) {
2026-02-13 00:41:40 +08:00
await this.checkOwner(this.getService(), id,"write",true);
const res = await this.service.refreshWebhookKey(id);
return this.ok({
webhookKey: res,
});
}
2023-01-29 13:44:19 +08:00
}