完成流水线控制器重构和相关功能改进
This commit is contained in:
@@ -3,4 +3,5 @@ export { ProjectController } from './project/index.ts';
|
||||
export { UserController } from './user/index.ts';
|
||||
export { AuthController } from './auth/index.ts';
|
||||
export { DeploymentController } from './deployment/index.ts';
|
||||
export { PipelineController } from './pipeline/index.ts'
|
||||
export { PipelineController } from './pipeline/index.ts';
|
||||
export { StepController } from './step/index.ts'
|
||||
|
||||
@@ -1,22 +1,175 @@
|
||||
import type { Context } from 'koa';
|
||||
import { Controller, Get, Post } from '../../decorators/route.ts';
|
||||
import { Controller, Get, Post, Put, Delete } from '../../decorators/route.ts';
|
||||
import prisma from '../../libs/db.ts';
|
||||
import { log } from '../../libs/logger.ts';
|
||||
import { BusinessError } from '../../middlewares/exception.ts';
|
||||
import {
|
||||
createPipelineSchema,
|
||||
updatePipelineSchema,
|
||||
pipelineIdSchema,
|
||||
listPipelinesQuerySchema
|
||||
} from './schema.ts';
|
||||
|
||||
@Controller('/pipelines')
|
||||
export class PipelineController {
|
||||
@Get('/:id')
|
||||
async get(ctx: Context) {
|
||||
const id = ctx.params.id;
|
||||
const pipeline = await prisma.pipeline.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
// GET /api/pipelines - 获取流水线列表
|
||||
@Get('')
|
||||
async list(ctx: Context) {
|
||||
const query = listPipelinesQuerySchema.parse(ctx.query);
|
||||
|
||||
const whereCondition: any = {
|
||||
valid: 1,
|
||||
};
|
||||
|
||||
// 如果提供了项目ID参数
|
||||
if (query?.projectId) {
|
||||
whereCondition.projectId = query.projectId;
|
||||
}
|
||||
|
||||
const pipelines = await prisma.pipeline.findMany({
|
||||
where: whereCondition,
|
||||
include: {
|
||||
steps: {
|
||||
where: {
|
||||
valid: 1,
|
||||
},
|
||||
orderBy: {
|
||||
order: 'asc',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
// GET /api/pipelines/:id - 获取单个流水线
|
||||
@Get('/:id')
|
||||
async get(ctx: Context) {
|
||||
const { id } = pipelineIdSchema.parse(ctx.params);
|
||||
|
||||
const pipeline = await prisma.pipeline.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
include: {
|
||||
steps: {
|
||||
where: {
|
||||
valid: 1,
|
||||
},
|
||||
orderBy: {
|
||||
order: 'asc',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!pipeline) {
|
||||
throw new BusinessError('流水线不存在', 3001, 404);
|
||||
}
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
// POST /api/pipelines - 创建流水线
|
||||
@Post('')
|
||||
async create(ctx: Context) {
|
||||
const validatedData = createPipelineSchema.parse(ctx.request.body);
|
||||
|
||||
const pipeline = await prisma.pipeline.create({
|
||||
data: {
|
||||
name: validatedData.name,
|
||||
description: validatedData.description || '',
|
||||
projectId: validatedData.projectId,
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
log.info('pipeline', 'Created new pipeline: %s', pipeline.name);
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
// PUT /api/pipelines/:id - 更新流水线
|
||||
@Put('/:id')
|
||||
async update(ctx: Context) {
|
||||
const { id } = pipelineIdSchema.parse(ctx.params);
|
||||
const validatedData = updatePipelineSchema.parse(ctx.request.body);
|
||||
|
||||
// 检查流水线是否存在
|
||||
const existingPipeline = await prisma.pipeline.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingPipeline) {
|
||||
throw new BusinessError('流水线不存在', 3001, 404);
|
||||
}
|
||||
|
||||
// 只更新提供的字段
|
||||
const updateData: any = {
|
||||
updatedBy: 'system',
|
||||
};
|
||||
|
||||
if (validatedData.name !== undefined) {
|
||||
updateData.name = validatedData.name;
|
||||
}
|
||||
if (validatedData.description !== undefined) {
|
||||
updateData.description = validatedData.description;
|
||||
}
|
||||
|
||||
const pipeline = await prisma.pipeline.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
log.info('pipeline', 'Updated pipeline: %s', pipeline.name);
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
// DELETE /api/pipelines/:id - 删除流水线(软删除)
|
||||
@Delete('/:id')
|
||||
async destroy(ctx: Context) {
|
||||
const { id } = pipelineIdSchema.parse(ctx.params);
|
||||
|
||||
// 检查流水线是否存在
|
||||
const existingPipeline = await prisma.pipeline.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingPipeline) {
|
||||
throw new BusinessError('流水线不存在', 3001, 404);
|
||||
}
|
||||
|
||||
// 软删除:将 valid 设置为 0
|
||||
await prisma.pipeline.update({
|
||||
where: { id },
|
||||
data: {
|
||||
valid: 0,
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
|
||||
// 同时软删除关联的步骤
|
||||
await prisma.step.updateMany({
|
||||
where: { pipelineId: id },
|
||||
data: {
|
||||
valid: 0,
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
|
||||
log.info('pipeline', 'Deleted pipeline: %s', existingPipeline.name);
|
||||
|
||||
// RESTful 删除成功返回 204 No Content
|
||||
ctx.status = 204;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
34
apps/server/controllers/pipeline/schema.ts
Normal file
34
apps/server/controllers/pipeline/schema.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
// 定义验证架构
|
||||
export const createPipelineSchema = z.object({
|
||||
name: z.string({
|
||||
message: '流水线名称必须是字符串',
|
||||
}).min(1, { message: '流水线名称不能为空' }).max(100, { message: '流水线名称不能超过100个字符' }),
|
||||
|
||||
description: z.string({
|
||||
message: '流水线描述必须是字符串',
|
||||
}).max(500, { message: '流水线描述不能超过500个字符' }).optional(),
|
||||
|
||||
projectId: z.number({
|
||||
message: '项目ID必须是数字',
|
||||
}).int().positive({ message: '项目ID必须是正整数' }).optional(),
|
||||
});
|
||||
|
||||
export const updatePipelineSchema = z.object({
|
||||
name: z.string({
|
||||
message: '流水线名称必须是字符串',
|
||||
}).min(1, { message: '流水线名称不能为空' }).max(100, { message: '流水线名称不能超过100个字符' }).optional(),
|
||||
|
||||
description: z.string({
|
||||
message: '流水线描述必须是字符串',
|
||||
}).max(500, { message: '流水线描述不能超过500个字符' }).optional(),
|
||||
});
|
||||
|
||||
export const pipelineIdSchema = z.object({
|
||||
id: z.coerce.number().int().positive({ message: '流水线 ID 必须是正整数' }),
|
||||
});
|
||||
|
||||
export const listPipelinesQuerySchema = z.object({
|
||||
projectId: z.coerce.number().int().positive({ message: '项目ID必须是正整数' }).optional(),
|
||||
}).optional();
|
||||
13
apps/server/controllers/pipeline/types.ts
Normal file
13
apps/server/controllers/pipeline/types.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { z } from 'zod';
|
||||
import type {
|
||||
createPipelineSchema,
|
||||
updatePipelineSchema,
|
||||
pipelineIdSchema,
|
||||
listPipelinesQuerySchema
|
||||
} from './schema.js';
|
||||
|
||||
// TypeScript 类型
|
||||
export type CreatePipelineInput = z.infer<typeof createPipelineSchema>;
|
||||
export type UpdatePipelineInput = z.infer<typeof updatePipelineSchema>;
|
||||
export type PipelineIdParams = z.infer<typeof pipelineIdSchema>;
|
||||
export type ListPipelinesQuery = z.infer<typeof listPipelinesQuerySchema>;
|
||||
234
apps/server/controllers/step/index.ts
Normal file
234
apps/server/controllers/step/index.ts
Normal file
@@ -0,0 +1,234 @@
|
||||
import type { Context } from 'koa';
|
||||
import prisma from '../../libs/db.ts';
|
||||
import { log } from '../../libs/logger.ts';
|
||||
import { BusinessError } from '../../middlewares/exception.ts';
|
||||
import { Controller, Get, Post, Put, Delete } from '../../decorators/route.ts';
|
||||
import { z } from 'zod';
|
||||
|
||||
// 定义验证架构
|
||||
const createStepSchema = z.object({
|
||||
name: z.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
}).min(1, { message: '步骤名称不能为空' }).max(100, { message: '步骤名称不能超过100个字符' }),
|
||||
|
||||
description: z.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
}).max(500, { message: '步骤描述不能超过500个字符' }).optional(),
|
||||
|
||||
order: z.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
}).int().min(0, { message: '步骤顺序必须是非负整数' }),
|
||||
|
||||
script: z.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
}).min(1, { message: '脚本命令不能为空' }),
|
||||
|
||||
pipelineId: z.number({
|
||||
message: '流水线ID必须是数字',
|
||||
}).int().positive({ message: '流水线ID必须是正整数' }),
|
||||
});
|
||||
|
||||
const updateStepSchema = z.object({
|
||||
name: z.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
}).min(1, { message: '步骤名称不能为空' }).max(100, { message: '步骤名称不能超过100个字符' }).optional(),
|
||||
|
||||
description: z.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
}).max(500, { message: '步骤描述不能超过500个字符' }).optional(),
|
||||
|
||||
order: z.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
}).int().min(0, { message: '步骤顺序必须是非负整数' }).optional(),
|
||||
|
||||
script: z.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
}).min(1, { message: '脚本命令不能为空' }).optional(),
|
||||
});
|
||||
|
||||
const stepIdSchema = z.object({
|
||||
id: z.coerce.number().int().positive({ message: '步骤 ID 必须是正整数' }),
|
||||
});
|
||||
|
||||
const listStepsQuerySchema = z.object({
|
||||
pipelineId: z.coerce.number().int().positive({ message: '流水线ID必须是正整数' }).optional(),
|
||||
page: z.coerce.number().int().min(1, { message: '页码必须大于0' }).optional().default(1),
|
||||
limit: z.coerce.number().int().min(1, { message: '每页数量必须大于0' }).max(100, { message: '每页数量不能超过100' }).optional().default(10),
|
||||
}).optional();
|
||||
|
||||
// TypeScript 类型
|
||||
type CreateStepInput = z.infer<typeof createStepSchema>;
|
||||
type UpdateStepInput = z.infer<typeof updateStepSchema>;
|
||||
type StepIdParams = z.infer<typeof stepIdSchema>;
|
||||
type ListStepsQuery = z.infer<typeof listStepsQuerySchema>;
|
||||
|
||||
@Controller('/steps')
|
||||
export class StepController {
|
||||
// GET /api/steps - 获取步骤列表
|
||||
@Get('')
|
||||
async list(ctx: Context) {
|
||||
const query = listStepsQuerySchema.parse(ctx.query);
|
||||
|
||||
const whereCondition: any = {
|
||||
valid: 1,
|
||||
};
|
||||
|
||||
// 如果提供了流水线ID参数
|
||||
if (query?.pipelineId) {
|
||||
whereCondition.pipelineId = query.pipelineId;
|
||||
}
|
||||
|
||||
const [total, steps] = await Promise.all([
|
||||
prisma.step.count({ where: whereCondition }),
|
||||
prisma.step.findMany({
|
||||
where: whereCondition,
|
||||
skip: query ? (query.page - 1) * query.limit : 0,
|
||||
take: query?.limit,
|
||||
orderBy: {
|
||||
order: 'asc',
|
||||
},
|
||||
})
|
||||
]);
|
||||
|
||||
return {
|
||||
data: steps,
|
||||
pagination: {
|
||||
page: query?.page || 1,
|
||||
limit: query?.limit || 10,
|
||||
total,
|
||||
totalPages: Math.ceil(total / (query?.limit || 10)),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// GET /api/steps/:id - 获取单个步骤
|
||||
@Get(':id')
|
||||
async show(ctx: Context) {
|
||||
const { id } = stepIdSchema.parse(ctx.params);
|
||||
|
||||
const step = await prisma.step.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!step) {
|
||||
throw new BusinessError('步骤不存在', 2001, 404);
|
||||
}
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
// POST /api/steps - 创建步骤
|
||||
@Post('')
|
||||
async create(ctx: Context) {
|
||||
const validatedData = createStepSchema.parse(ctx.request.body);
|
||||
|
||||
// 检查关联的流水线是否存在
|
||||
const pipeline = await prisma.pipeline.findFirst({
|
||||
where: {
|
||||
id: validatedData.pipelineId,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!pipeline) {
|
||||
throw new BusinessError('关联的流水线不存在', 2002, 404);
|
||||
}
|
||||
|
||||
const step = await prisma.step.create({
|
||||
data: {
|
||||
name: validatedData.name,
|
||||
description: validatedData.description || '',
|
||||
order: validatedData.order,
|
||||
script: validatedData.script,
|
||||
pipelineId: validatedData.pipelineId,
|
||||
createdBy: 'system',
|
||||
updatedBy: 'system',
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
log.info('step', 'Created new step: %s', step.name);
|
||||
return step;
|
||||
}
|
||||
|
||||
// PUT /api/steps/:id - 更新步骤
|
||||
@Put(':id')
|
||||
async update(ctx: Context) {
|
||||
const { id } = stepIdSchema.parse(ctx.params);
|
||||
const validatedData = updateStepSchema.parse(ctx.request.body);
|
||||
|
||||
// 检查步骤是否存在
|
||||
const existingStep = await prisma.step.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingStep) {
|
||||
throw new BusinessError('步骤不存在', 2001, 404);
|
||||
}
|
||||
|
||||
// 只更新提供的字段
|
||||
const updateData: any = {
|
||||
updatedBy: 'system',
|
||||
};
|
||||
|
||||
if (validatedData.name !== undefined) {
|
||||
updateData.name = validatedData.name;
|
||||
}
|
||||
if (validatedData.description !== undefined) {
|
||||
updateData.description = validatedData.description;
|
||||
}
|
||||
if (validatedData.order !== undefined) {
|
||||
updateData.order = validatedData.order;
|
||||
}
|
||||
if (validatedData.script !== undefined) {
|
||||
updateData.script = validatedData.script;
|
||||
}
|
||||
|
||||
const step = await prisma.step.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
log.info('step', 'Updated step: %s', step.name);
|
||||
return step;
|
||||
}
|
||||
|
||||
// DELETE /api/steps/:id - 删除步骤(软删除)
|
||||
@Delete(':id')
|
||||
async destroy(ctx: Context) {
|
||||
const { id } = stepIdSchema.parse(ctx.params);
|
||||
|
||||
// 检查步骤是否存在
|
||||
const existingStep = await prisma.step.findFirst({
|
||||
where: {
|
||||
id,
|
||||
valid: 1,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingStep) {
|
||||
throw new BusinessError('步骤不存在', 2001, 404);
|
||||
}
|
||||
|
||||
// 软删除:将 valid 设置为 0
|
||||
await prisma.step.update({
|
||||
where: { id },
|
||||
data: {
|
||||
valid: 0,
|
||||
updatedBy: 'system',
|
||||
},
|
||||
});
|
||||
|
||||
log.info('step', 'Deleted step: %s', existingStep.name);
|
||||
|
||||
// RESTful 删除成功返回 204 No Content
|
||||
ctx.status = 204;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user