All files / src/entities/pipelines schema.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 0/0
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 1063x 3x           3x                           3x 3x     3x                     3x             3x             3x                           3x     3x                     3x             3x               3x                        
import { z } from "zod";
import { requiredId } from "../utils";
 
// ============================================================================
// Shared schemas for pipeline variables
// ============================================================================
 
const PipelineVariableSchema = z.object({
  key: z.string(),
  value: z.string(),
  variable_type: z.enum(["env_var", "file"]).optional(),
});
 
// ============================================================================
// manage_pipeline - CQRS Command Tool (discriminated union schema)
// Actions: create, retry, cancel
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
 
// --- Shared fields ---
const projectIdField = requiredId.describe("Project ID or URL-encoded path");
const pipelineIdField = requiredId.describe("The ID of the pipeline");
 
// --- Action: create ---
const CreatePipelineSchema = z.object({
  action: z.literal("create").describe("Trigger a new pipeline on branch/tag"),
  project_id: projectIdField,
  ref: z.string().describe("The branch or tag to run the pipeline on"),
  variables: z
    .array(PipelineVariableSchema)
    .optional()
    .describe("Variables to pass to the pipeline"),
});
 
// --- Action: retry ---
const RetryPipelineSchema = z.object({
  action: z.literal("retry").describe("Re-run a failed/canceled pipeline"),
  project_id: projectIdField,
  pipeline_id: pipelineIdField,
});
 
// --- Action: cancel ---
const CancelPipelineSchema = z.object({
  action: z.literal("cancel").describe("Stop a running pipeline"),
  project_id: projectIdField,
  pipeline_id: pipelineIdField,
});
 
// --- Discriminated union combining all actions ---
export const ManagePipelineSchema = z.discriminatedUnion("action", [
  CreatePipelineSchema,
  RetryPipelineSchema,
  CancelPipelineSchema,
]);
 
// ============================================================================
// manage_pipeline_job - CQRS Command Tool (discriminated union schema)
// Actions: play, retry, cancel
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
 
// --- Shared fields ---
const jobIdField = requiredId.describe("The ID of the job");
 
// --- Action: play ---
const PlayJobSchema = z.object({
  action: z.literal("play").describe("Trigger a manual job"),
  project_id: projectIdField,
  job_id: jobIdField,
  job_variables_attributes: z
    .array(PipelineVariableSchema)
    .optional()
    .describe("Variables to pass to the job"),
});
 
// --- Action: retry ---
const RetryJobSchema = z.object({
  action: z.literal("retry").describe("Re-run a failed/canceled job"),
  project_id: projectIdField,
  job_id: jobIdField,
});
 
// --- Action: cancel ---
const CancelJobSchema = z.object({
  action: z.literal("cancel").describe("Stop a running job"),
  project_id: projectIdField,
  job_id: jobIdField,
  force: z.boolean().optional().describe("Force cancellation of the job"),
});
 
// --- Discriminated union combining all actions ---
export const ManagePipelineJobSchema = z.discriminatedUnion("action", [
  PlayJobSchema,
  RetryJobSchema,
  CancelJobSchema,
]);
 
// ============================================================================
// Type exports
// ============================================================================
 
export type ManagePipelineInput = z.infer<typeof ManagePipelineSchema>;
export type ManagePipelineJobInput = z.infer<typeof ManagePipelineJobSchema>;