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

379 lines
14 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { Constants, CrudController, SysSettingsService } from "@certd/lib-server";
import { isPlus } from "@certd/plus-core";
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { ApiProperty, ApiTags } from "@midwayjs/swagger";
import { SiteInfoService } from "../../../modules/monitor/index.js";
import { HistoryService } from "../../../modules/pipeline/service/history-service.js";
import { PipelineService } from "../../../modules/pipeline/service/pipeline-service.js";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
import { PipelineEntity } from "../../../modules/pipeline/entity/pipeline.js";
2026-03-15 18:26:49 +08:00
const pipelineExample = `
// 流水线配置示例,实际传送时要去掉注释
{
2026-06-08 16:48:30 +08:00
"title": "handfree.work证书自动化", //标题
2026-03-15 18:26:49 +08:00
"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": 20,
2026-03-15 18:26:49 +08:00
},
"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为使用默认通知
}
],
2026-05-31 01:41:33 +08:00
}`;
2026-03-15 18:26:49 +08:00
export class PipelineSaveDTO {
2026-05-31 01:41:33 +08:00
@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;
2026-03-15 18:26:49 +08:00
}
2023-01-29 13:44:19 +08:00
/**
* 证书
*/
@Provide()
2026-05-31 01:41:33 +08:00
@ApiTags(["pipeline"])
@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;
2023-01-29 13:44:19 +08:00
getService() {
return this.service;
}
2026-05-31 01:41:33 +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-05-31 01:41:33 +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) {
2026-05-31 01:41:33 +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) {
2026-05-31 01:41:33 +08:00
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) {
2026-05-31 01:41:33 +08:00
body.sort = { prop: "order", asc: false };
2024-08-04 02:35:45 +08:00
}
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-05-31 01:41:33 +08:00
@Post("/getSimpleByIds", { description: Constants.per.authOnly, summary: "根据ID列表获取流水线简单信息" })
2025-06-23 18:20:49 +08:00
async getSimpleById(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2026-02-11 00:54:56 +08:00
const ret = await this.getService().getSimplePipelines(body.ids, userId, projectId);
2025-06-23 18:20:49 +08:00
return this.ok(ret);
}
2026-05-31 01:41:33 +08:00
@Post("/add", { description: Constants.per.authOnly })
async add(@Body(ALL) bean: PipelineEntity) {
return await this.save(bean as any);
}
2026-05-31 01:41:33 +08:00
@Post("/update", { description: Constants.per.authOnly })
async update(@Body(ALL) bean: PipelineEntity) {
await this.checkOwner(this.getService(), bean.id, "write", true);
await this.service.update(bean as any);
return this.ok({});
}
2023-01-29 13:44:19 +08:00
2026-05-31 01:41:33 +08:00
@Post("/save", { description: Constants.per.authOnly, summary: "新增/更新流水线" })
2026-03-15 18:26:49 +08:00
async save(@Body() bean: PipelineSaveDTO) {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.getProjectUserIdWrite();
2023-01-29 13:44:19 +08:00
if (bean.id > 0) {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.checkOwner(this.getService(), bean.id, "write", true);
2026-02-14 00:08:13 +08:00
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
// 非管理员用户 不允许设置流水线有效期
2026-05-31 01:41:33 +08:00
delete bean.validTime;
2025-10-24 23:48:32 +08:00
}
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-05-31 01:41:33 +08:00
@Post("/delete", { description: Constants.per.authOnly, summary: "删除流水线" })
async delete(@Query("id") id: number) {
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-05-31 01:41:33 +08:00
@Post("/disabled", { description: Constants.per.authOnly, summary: "禁用流水线" })
async disabled(@Body(ALL) bean) {
2026-05-31 01:41:33 +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-05-31 01:41:33 +08:00
@Post("/detail", { description: Constants.per.authOnly, summary: "查询流水线详情" })
async detail(@Query("id") id: number) {
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-05-31 01:41:33 +08:00
@Post("/trigger", { description: Constants.per.authOnly, summary: "触发流水线执行" })
async trigger(@Query("id") id: number, @Query("stepId") stepId?: string) {
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-05-31 01:41:33 +08:00
@Post("/cancel", { description: Constants.per.authOnly, summary: "取消流水线执行" })
async cancel(@Query("historyId") historyId: number) {
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-05-31 01:41:33 +08:00
@Post("/count", { description: Constants.per.authOnly, summary: "查询流水线数量" })
2024-10-31 22:35:05 +08:00
async count() {
2026-05-31 01:41:33 +08:00
const { userId } = await this.getProjectUserIdRead();
2026-02-11 00:54:56 +08:00
const count = await this.service.count({ userId: userId });
2024-10-31 22:35:05 +08:00
return this.ok({ count });
}
2026-05-31 01:41:33 +08:00
private async checkPermissionCall(callback: any) {
2026-05-31 02:11:00 +08:00
const { projectId, userId: uid } = await this.getProjectUserIdWrite();
let userId = uid;
2026-05-31 01:41:33 +08:00
if (projectId) {
return await callback({ userId, projectId });
2026-02-11 00:54:56 +08:00
}
const isAdmin = await this.authService.isAdmin(this.ctx);
userId = isAdmin ? undefined : userId;
2026-05-31 01:41:33 +08:00
return await callback({ userId });
2026-02-11 00:54:56 +08:00
}
2026-05-31 01:41:33 +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({});
2026-05-31 01:41:33 +08:00
await this.checkPermissionCall(async ({ userId, projectId }) => {
await this.service.batchDelete(ids, userId, projectId);
});
return this.ok({});
}
2026-05-31 01:41:33 +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);
2026-05-31 01:41:33 +08:00
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
2026-05-31 01:41:33 +08:00
@Post("/batchUpdateTrigger", { description: Constants.per.authOnly, summary: "批量更新流水线触发器" })
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);
2026-05-31 01:41:33 +08:00
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-05-31 01:41:33 +08:00
@Post("/batchUpdateNotification", { description: Constants.per.authOnly, summary: "批量更新流水线通知" })
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);
2026-05-31 01:41:33 +08:00
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-05-31 01:41:33 +08:00
@Post("/batchUpdateCertApplyOptions", { description: Constants.per.authOnly, summary: "批量更新证书申请任务配置" })
async batchUpdateCertApplyOptions(@Body("ids") ids: number[], @Body("options") options: any) {
await this.checkPermissionCall(async ({ userId, projectId }) => {
await this.service.batchUpdateCertApplyOptions(ids, options, userId, projectId);
});
return this.ok({});
}
2026-05-31 01:41:33 +08:00
@Post("/batchRerun", { description: Constants.per.authOnly, summary: "批量重新运行流水线" })
async batchRerun(@Body("ids") ids: number[], @Body("force") force: boolean) {
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-05-31 01:41:33 +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);
2026-05-31 01:41:33 +08:00
});
return this.ok({});
}
2026-05-31 01:41:33 +08:00
@Post("/refreshWebhookKey", { description: Constants.per.authOnly, summary: "刷新Webhook密钥" })
async refreshWebhookKey(@Body("id") id: number) {
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
}