import type { TFunction } from 'i18next';
import { Badge } from '@/components/ui/badge';
import { CheckCircle, XCircle, Loader2 } from 'lucide-react';
// 流水线阶段定义 // Logan.Li
export interface PipelineStage {
id: string;
name: string;
status: 'pending' | 'running' | 'success' | 'failed' | 'skipped';
duration: string;
logs: string[];
}
// 流水线模板(新建时快捷选择) // Logan.Li
export function getPipelineTemplates(t: TFunction) {
return [
{
id: 'template-1',
name: t('deploy.template.nodejs'),
description: t('deploy.template.nodejsDesc'),
stages: [
t('deploy.stage.checkout'),
t('deploy.template.dependencyInstall'),
t('deploy.releaseHelpers.pipelineStep.codeCheck'),
t('deploy.stage.buildDesc'),
t('deploy.stage.deploy'),
],
},
{
id: 'template-2',
name: t('deploy.template.go'),
description: t('deploy.template.goDesc'),
stages: [
t('deploy.stage.checkout'),
t('deploy.template.unitTest'),
t('deploy.stage.build'),
t('deploy.releaseHelpers.pipelineStep.buildImage'),
t('deploy.stage.deploy'),
],
},
{
id: 'template-3',
name: t('deploy.template.java'),
description: t('deploy.template.javaDesc'),
stages: [
t('deploy.stage.checkout'),
t('deploy.template.mavenBuild'),
t('deploy.template.unitTest'),
t('deploy.template.package'),
t('deploy.stage.deploy'),
],
},
{
id: 'template-4',
name: t('deploy.template.python'),
description: t('deploy.template.pythonDesc'),
stages: [
t('deploy.stage.checkout'),
t('deploy.template.dependencyInstall'),
t('deploy.releaseHelpers.pipelineStep.codeCheck'),
t('deploy.stage.test'),
t('deploy.stage.deploy'),
],
},
];
}
// 解析 stages 字段(兼容 JSON 数组 / 逗号分隔字符串) // Logan.Li
export function parseStages( stages: string): PipelineStage[] {
if (Array.isArray(stages)) return stages;
if (typeof stages === 'string') {
try {
const parsed = JSON.parse(stages);
if (Array.isArray(parsed)) return parsed;
} catch {
return stages.split(',').map((s, i) => ({ id: `stage-${i + 1}`, name: s.trim(), status: 'success' as const, duration: '-', logs: [] }));
}
}
return [];
}
// 由流水线名称生成 code(kebab-case) // Logan.Li
export function nameToCode(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9]/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '') || `pipeline-${Date.now()}`;
}
// 状态徽章(使用后端 last_status 字段) // Logan.Li
export function getStatusBadge(status: string, t: TFunction) {
switch (status) {
case 'success':
return (
{t('deploy.status.success')}
);
case 'failed':
return (
{t('deploy.status.failed')}
);
case 'running':
return (
{t('deploy.status.running')}
);
default:
return (
{t('deploy.pipeline.neverRun')}
);
}
}
// 阶段状态图标 // Logan.Li
export function getStageStatusIcon(status: PipelineStage['status'], t: TFunction) {
switch (status) {
case 'success':
return ;
case 'failed':
return ;
case 'running':
return ;
case 'skipped':
return {t('deploy.stageStatus.skipped')};
default:
return {t('deploy.status.pending')};
}
}