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 106 107 10816x 16x           16x                   16x                           16x 16x 16x     16x                                 16x             16x             16x                     16x             16x               16x                            
import { z } from 'zod';
import { requiredId } from '../utils';
 
// ============================================================================
// Shared schemas for pipeline variables and inputs
// ============================================================================
 
const PipelineVariableSchema = z.object({
  key: z.string().describe('Variable name'),
  value: z.string().describe('Variable value'),
  variable_type: z
    .enum(['env_var', 'file'])
    .optional()
    .describe('Variable type: env_var (default) or file'),
});
 
// Pipeline input value types (GitLab 15.5+ supports string, number, boolean, array)
const PipelineInputValueSchema = z
  .union([z.string(), z.number(), z.boolean(), z.array(z.string())])
  .describe('Input value: string, number, boolean, or array of strings');
 
// ============================================================================
// manage_pipeline - CQRS Command Tool (discriminated union schema)
// Pipeline-level actions: create, retry, cancel
// Job-level actions: play_job, retry_job, cancel_job (folded in; the _job suffix
// avoids colliding with the pipeline-level 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');
const jobIdField = requiredId.describe('The ID of the job');
 
// --- 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('Legacy variables to pass to the pipeline (key-value pairs with optional type)'),
  inputs: z
    .record(z.string(), PipelineInputValueSchema)
    .optional()
    .describe(
      'Typed pipeline inputs defined in .gitlab-ci.yml spec (GitLab 15.5+). Keys must match input names in pipeline spec.',
    ),
});
 
// --- 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,
});
 
// --- Action: play_job ---
const PlayJobSchema = z.object({
  action: z.literal('play_job').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_job ---
const RetryJobSchema = z.object({
  action: z.literal('retry_job').describe('Re-run a failed/canceled job'),
  project_id: projectIdField,
  job_id: jobIdField,
});
 
// --- Action: cancel_job ---
const CancelJobSchema = z.object({
  action: z.literal('cancel_job').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 pipeline + job actions ---
export const ManagePipelineSchema = z.discriminatedUnion('action', [
  CreatePipelineSchema,
  RetryPipelineSchema,
  CancelPipelineSchema,
  PlayJobSchema,
  RetryJobSchema,
  CancelJobSchema,
]);
 
// ============================================================================
// Type exports
// ============================================================================
 
export type ManagePipelineInput = z.infer<typeof ManagePipelineSchema>;