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';
|
|
|
|
|
import { SiteInfoService } from '../../../modules/monitor/index.js';
|
2025-01-15 01:26:39 +08:00
|
|
|
import { PipelineEntity } from '../../../modules/pipeline/entity/pipeline.js';
|
|
|
|
|
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';
|
2023-01-29 13:44:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 证书
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@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;
|
2024-08-05 12:49:44 +08:00
|
|
|
@Inject()
|
|
|
|
|
authService: AuthService;
|
|
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
2023-01-29 13:44:19 +08:00
|
|
|
|
2025-10-24 23:10:20 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/page', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async page(@Body(ALL) body) {
|
2024-08-05 12:49:44 +08:00
|
|
|
const isAdmin = await this.authService.isAdmin(this.ctx);
|
|
|
|
|
const publicSettings = await this.sysSettingsService.getPublicSettings();
|
2025-12-16 22:52:07 +08:00
|
|
|
|
2026-02-11 00:54:56 +08:00
|
|
|
const { projectId, userId } = await this.getProjectUserIdRead()
|
|
|
|
|
body.query.projectId = projectId
|
2025-12-16 22:52:07 +08:00
|
|
|
let onlyOther = false
|
2026-01-09 01:10:43 +08:00
|
|
|
if (isAdmin) {
|
|
|
|
|
if (publicSettings.managerOtherUserPipeline) {
|
2025-12-16 22:52:07 +08:00
|
|
|
//管理员管理 其他用户
|
2026-01-09 01:10:43 +08:00
|
|
|
if (body.query.userId === -1) {
|
2025-12-16 22:52:07 +08:00
|
|
|
//如果只查询其他用户
|
|
|
|
|
onlyOther = true;
|
|
|
|
|
delete body.query.userId;
|
|
|
|
|
}
|
2026-01-09 01:10:43 +08:00
|
|
|
} else {
|
2026-02-11 00:07:29 +08:00
|
|
|
body.query.userId = userId;
|
2025-12-16 22:52:07 +08:00
|
|
|
}
|
2026-01-09 01:10:43 +08:00
|
|
|
} else {
|
2026-02-11 00:07:29 +08:00
|
|
|
body.query.userId = userId;
|
2024-08-05 12:49:44 +08:00
|
|
|
}
|
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
|
|
|
}
|
2026-01-09 01:10:43 +08:00
|
|
|
if (onlyOther) {
|
2025-12-16 22:52:07 +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) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-06-23 18:20:49 +08:00
|
|
|
@Post('/getSimpleByIds', { summary: Constants.per.authOnly })
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/add', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async add(@Body(ALL) bean: PipelineEntity) {
|
2026-02-11 00:54:56 +08:00
|
|
|
const { projectId, userId } = await this.getProjectUserIdWrite()
|
2026-02-11 00:07:29 +08:00
|
|
|
bean.userId = userId
|
|
|
|
|
bean.projectId = projectId
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.add(bean);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/update', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async update(@Body(ALL) bean) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), bean.id,"write");
|
2024-12-01 02:10:40 +08:00
|
|
|
delete bean.userId;
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.update(bean);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/save', { summary: Constants.per.authOnly })
|
2026-01-09 01:10:43 +08:00
|
|
|
async save(@Body(ALL) bean: { addToMonitorEnabled: boolean, addToMonitorDomains: string } & PipelineEntity) {
|
2026-02-11 18:11:33 +08:00
|
|
|
const { userId } = await this.getProjectUserIdWrite()
|
2023-01-29 13:44:19 +08:00
|
|
|
if (bean.id > 0) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), bean.id,"write");
|
2024-10-10 14:57:26 +08:00
|
|
|
} else {
|
2026-02-11 00:54:56 +08:00
|
|
|
bean.userId = userId;
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|
2025-10-24 23:48:32 +08:00
|
|
|
|
2026-01-09 01:10:43 +08:00
|
|
|
if (!this.isAdmin()) {
|
2025-10-24 23:48:32 +08:00
|
|
|
// 非管理员用户 不允许设置流水线有效期
|
|
|
|
|
delete bean.validTime
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 01:10:43 +08:00
|
|
|
const { version } = await this.service.save(bean);
|
2025-10-24 23:10:20 +08:00
|
|
|
//是否增加证书监控
|
|
|
|
|
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,
|
2025-10-24 23:10:20 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-09 01:10:43 +08:00
|
|
|
return this.ok({ id: bean.id, version: version });
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/delete', { summary: Constants.per.authOnly })
|
2024-08-30 18:50:53 +08:00
|
|
|
async delete(@Query('id') id: number) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), id,"write");
|
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-01-06 13:38:10 +08:00
|
|
|
@Post('/disabled', { summary: Constants.per.authOnly })
|
|
|
|
|
async disabled(@Body(ALL) bean) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), bean.id,"write");
|
2026-01-06 13:38:10 +08:00
|
|
|
delete bean.userId;
|
2026-01-07 10:58:17 +08:00
|
|
|
await this.service.disabled(bean.id, bean.disabled);
|
|
|
|
|
return this.ok({});
|
2026-01-06 13:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/detail', { summary: Constants.per.authOnly })
|
2024-08-30 18:50:53 +08:00
|
|
|
async detail(@Query('id') id: number) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), id,"read");
|
2023-01-29 13:44:19 +08:00
|
|
|
const detail = await this.service.detail(id);
|
|
|
|
|
return this.ok(detail);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/trigger', { summary: Constants.per.authOnly })
|
2024-09-02 18:36:12 +08:00
|
|
|
async trigger(@Query('id') id: number, @Query('stepId') stepId?: string) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.getService(), id,"write");
|
2026-01-09 01:10:43 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
@Post('/cancel', { summary: Constants.per.authOnly })
|
2024-08-30 18:50:53 +08:00
|
|
|
async cancel(@Query('historyId') historyId: number) {
|
2026-02-11 18:11:33 +08:00
|
|
|
await this.checkEntityOwner(this.historyService, historyId,"write");
|
2023-07-03 11:45:32 +08:00
|
|
|
await this.service.cancel(historyId);
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2024-10-31 22:35:05 +08:00
|
|
|
|
|
|
|
|
@Post('/count', { summary: Constants.per.authOnly })
|
|
|
|
|
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 });
|
|
|
|
|
}
|
2024-12-01 02:10:40 +08:00
|
|
|
|
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});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 02:10:40 +08:00
|
|
|
@Post('/batchDelete', { summary: Constants.per.authOnly })
|
|
|
|
|
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({})
|
2024-12-01 02:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 22:31:06 +08:00
|
|
|
|
|
|
|
|
|
2024-12-01 02:10:40 +08:00
|
|
|
@Post('/batchUpdateGroup', { summary: Constants.per.authOnly })
|
|
|
|
|
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);
|
|
|
|
|
})
|
2024-12-01 02:10:40 +08:00
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2025-05-27 11:08:08 +08:00
|
|
|
|
2025-06-18 12:29:43 +08:00
|
|
|
|
|
|
|
|
@Post('/batchUpdateTrigger', { summary: Constants.per.authOnly })
|
|
|
|
|
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({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('/batchUpdateNotification', { summary: Constants.per.authOnly })
|
|
|
|
|
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({});
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 11:08:08 +08:00
|
|
|
@Post('/batchRerun', { summary: Constants.per.authOnly })
|
2025-12-29 15:31:33 +08:00
|
|
|
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-01-09 01:10:43 +08:00
|
|
|
|
|
|
|
|
@Post('/refreshWebhookKey', { summary: Constants.per.authOnly })
|
|
|
|
|
async refreshWebhookKey(@Body('id') id: number) {
|
2026-02-11 00:54:56 +08:00
|
|
|
|
|
|
|
|
const { projectId } = await this.getProjectUserIdWrite()
|
|
|
|
|
if (projectId) {
|
|
|
|
|
await this.authService.checkEntityProjectId(this.getService(), projectId, id);
|
|
|
|
|
} else {
|
|
|
|
|
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
|
|
|
|
|
}
|
2026-01-09 01:10:43 +08:00
|
|
|
const res = await this.service.refreshWebhookKey(id);
|
|
|
|
|
return this.ok({
|
|
|
|
|
webhookKey: res,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|