140 lines
3.7 KiB
TypeScript
140 lines
3.7 KiB
TypeScript
import { KyInstance } from "ky";
|
|
import { CorpConfig } from "../../core/config/types.js";
|
|
import * as api from "./api.js";
|
|
import {
|
|
Platform,
|
|
WorkflowSummary,
|
|
WorkflowListAllError,
|
|
WorkflowTask,
|
|
WorkflowWithMatches,
|
|
TaskMatch,
|
|
} from "./types.js";
|
|
|
|
export interface PlatformClients {
|
|
corp: CorpConfig;
|
|
ekbClient: KyInstance;
|
|
easoClient: KyInstance;
|
|
}
|
|
|
|
// ── 服务层:跨应用聚合 ───────────────────────────────────────────────────────
|
|
|
|
export interface ListAllWorkflowsOptions {
|
|
page?: number;
|
|
pageSize?: number;
|
|
}
|
|
|
|
export interface ListAllWorkflowsResult {
|
|
workflows: WorkflowSummary[];
|
|
errors: WorkflowListAllError[];
|
|
}
|
|
|
|
export async function listAllWorkflows(
|
|
clients: PlatformClients,
|
|
options: ListAllWorkflowsOptions = {}
|
|
): Promise<ListAllWorkflowsResult> {
|
|
const { ekbClient, easoClient, corp } = clients;
|
|
const page = options.page ?? 1;
|
|
const pageSize = options.pageSize ?? 20;
|
|
|
|
const platforms = await api.listPlatforms(ekbClient, corp.credentials.corpId);
|
|
const enabledPlatforms = platforms.filter((p) => p.active !== false);
|
|
|
|
const workflows: WorkflowSummary[] = [];
|
|
const errors: WorkflowListAllError[] = [];
|
|
|
|
await Promise.all(
|
|
enabledPlatforms.map(async (platform) => {
|
|
try {
|
|
const pageResult = await api.listWorkflows(
|
|
easoClient,
|
|
platform.id,
|
|
page,
|
|
pageSize
|
|
);
|
|
for (const workflow of pageResult.pipelineList) {
|
|
workflows.push({
|
|
...workflow,
|
|
platformId: platform.id,
|
|
platformName: platform.name,
|
|
});
|
|
}
|
|
} catch (err) {
|
|
errors.push({
|
|
platformId: platform.id,
|
|
platformName: platform.name,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
}
|
|
})
|
|
);
|
|
|
|
return { workflows, errors };
|
|
}
|
|
|
|
function collectTasksByType(
|
|
tasks: WorkflowTask[],
|
|
taskType: string,
|
|
matches: TaskMatch[] = []
|
|
): TaskMatch[] {
|
|
for (const task of tasks) {
|
|
if (task.type === taskType) {
|
|
matches.push({
|
|
taskName: task.name,
|
|
taskType: task.type,
|
|
levelIndex: task.levelIndex,
|
|
});
|
|
}
|
|
if ("tasks" in task && Array.isArray(task.tasks)) {
|
|
collectTasksByType(task.tasks as WorkflowTask[], taskType, matches);
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
|
|
export interface FindWorkflowsByTaskTypeOptions extends ListAllWorkflowsOptions {
|
|
includeDetail?: boolean;
|
|
}
|
|
|
|
export interface FindWorkflowsByTaskTypeResult {
|
|
workflows: WorkflowWithMatches[];
|
|
errors: WorkflowListAllError[];
|
|
}
|
|
|
|
export async function findWorkflowsByTaskType(
|
|
clients: PlatformClients,
|
|
taskType: string,
|
|
options: FindWorkflowsByTaskTypeOptions = {}
|
|
): Promise<FindWorkflowsByTaskTypeResult> {
|
|
const { easoClient } = clients;
|
|
const { includeDetail, ...listOptions } = options;
|
|
|
|
const all = await listAllWorkflows(clients, listOptions);
|
|
const matched: WorkflowWithMatches[] = [];
|
|
const detailErrors: WorkflowListAllError[] = [];
|
|
|
|
for (const workflow of all.workflows) {
|
|
let tasks = workflow.tasks;
|
|
|
|
if (includeDetail) {
|
|
try {
|
|
const detail = await api.getWorkflowDetail(easoClient, workflow.id);
|
|
tasks = detail.tasks;
|
|
} catch (err) {
|
|
detailErrors.push({
|
|
platformId: workflow.platformId,
|
|
platformName: workflow.platformName,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
continue;
|
|
}
|
|
}
|
|
|
|
const matchedTasks = collectTasksByType(tasks, taskType);
|
|
if (matchedTasks.length > 0) {
|
|
matched.push({ ...workflow, matchedTasks });
|
|
}
|
|
}
|
|
|
|
return { workflows: matched, errors: [...all.errors, ...detailErrors] };
|
|
}
|