All files / src/entities/mrs schema.ts

96% Statements 24/25
100% Branches 0/0
50% Functions 1/2
96% Lines 24/25

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 2913x 3x           3x                                                                               3x 3x     3x                                                     3x                                                                                 3x                             3x                           3x         2x                   3x                   3x                   3x                 3x                 3x                 3x                                 3x     3x                     3x                   3x               3x             3x               3x                                
import { z } from "zod";
import { flexibleBoolean, requiredId } from "../utils";
 
// ============================================================================
// Shared position schema for diff notes
// ============================================================================
 
export const MergeRequestThreadPositionSchema = z.object({
  base_sha: z.string().optional().describe("Base commit SHA for three-way diff."),
  start_sha: z.string().optional().describe("Starting commit SHA in target branch."),
  head_sha: z.string().optional().describe("Latest commit SHA in MR source branch."),
  position_type: z.enum(["text", "image"]).optional().describe("Comment type: text or image."),
  old_path: z.string().optional().describe("Original file path before changes."),
  new_path: z.string().optional().describe("Current file path after changes."),
  old_line: z.number().optional().describe("Line number in original file."),
  new_line: z.number().optional().describe("Line number in changed file."),
  line_range: z
    .object({
      start: z.object({
        line_code: z.string(),
        type: z.enum(["new", "old"]).optional(),
        old_line: z.number().optional(),
        new_line: z.number().optional(),
      }),
      end: z.object({
        line_code: z.string(),
        type: z.enum(["new", "old"]).optional(),
        old_line: z.number().optional(),
        new_line: z.number().optional(),
      }),
    })
    .optional()
    .describe("Line range for multi-line comments."),
  width: z.number().optional().describe("Image width in pixels."),
  height: z.number().optional().describe("Image height in pixels."),
  x: z.number().optional().describe("Horizontal pixel position for image comment."),
  y: z.number().optional().describe("Vertical pixel position for image comment."),
});
 
// ============================================================================
// manage_merge_request - CQRS Command Tool (discriminated union schema)
// Actions: create, update, merge
// 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 mergeRequestIidField = requiredId.describe("Internal MR ID unique to project");
 
// --- Action: create ---
const CreateMergeRequestSchema = z.object({
  action: z.literal("create").describe("Create a new merge request"),
  project_id: projectIdField,
  source_branch: z.string().describe("Branch containing changes to merge"),
  target_branch: z.string().describe("Branch to merge into"),
  title: z.string().describe("MR title/summary"),
  assignee_id: z.string().optional().describe("Single assignee user ID"),
  assignee_ids: z.array(z.string()).optional().describe("Multiple assignee IDs"),
  reviewer_ids: z.array(z.string()).optional().describe("User IDs for code reviewers"),
  description: z.string().optional().describe("MR description (Markdown)"),
  target_project_id: z.coerce.string().optional().describe("Target project for cross-project MRs"),
  labels: z
    .union([z.string(), z.array(z.string())])
    .optional()
    .describe("Labels to categorize MR"),
  milestone_id: z.string().optional().describe("Associate MR with milestone"),
  remove_source_branch: flexibleBoolean
    .optional()
    .describe("Auto-delete source branch after merge"),
  allow_collaboration: flexibleBoolean.optional().describe("Let maintainers push to source branch"),
  allow_maintainer_to_push: flexibleBoolean
    .optional()
    .describe("Deprecated - use allow_collaboration"),
  squash: flexibleBoolean.optional().describe("Combine all commits into one when merging"),
});
 
// --- Action: update ---
const UpdateMergeRequestSchema = z.object({
  action: z.literal("update").describe("Update an existing merge request"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  target_branch: z.string().optional().describe("Branch to merge into"),
  title: z.string().optional().describe("MR title/summary"),
  assignee_id: z.string().optional().describe("Single assignee user ID"),
  assignee_ids: z.array(z.string()).optional().describe("Multiple assignee IDs"),
  reviewer_ids: z.array(z.string()).optional().describe("User IDs for code reviewers"),
  description: z.string().optional().describe("MR description (Markdown)"),
  labels: z
    .union([z.string(), z.array(z.string())])
    .optional()
    .describe("Labels to categorize MR"),
  add_labels: z
    .union([z.string(), z.array(z.string())])
    .optional()
    .describe("Labels to add"),
  remove_labels: z
    .union([z.string(), z.array(z.string())])
    .optional()
    .describe("Labels to remove"),
  milestone_id: z.string().optional().describe("Associate MR with milestone"),
  remove_source_branch: flexibleBoolean
    .optional()
    .describe("Auto-delete source branch after merge"),
  allow_collaboration: flexibleBoolean.optional().describe("Let maintainers push to source branch"),
  allow_maintainer_to_push: flexibleBoolean
    .optional()
    .describe("Deprecated - use allow_collaboration"),
  squash: flexibleBoolean.optional().describe("Combine all commits into one when merging"),
  state_event: z
    .string()
    .transform(val => val.toLowerCase())
    .pipe(z.enum(["close", "reopen"]))
    .optional()
    .describe("State event: close or reopen"),
  discussion_locked: flexibleBoolean.optional().describe("Lock discussion thread"),
});
 
// --- Action: merge ---
const MergeMergeRequestSchema = z.object({
  action: z.literal("merge").describe("Merge an approved merge request"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  merge_commit_message: z.string().optional().describe("Custom merge commit message"),
  squash_commit_message: z.string().optional().describe("Custom squash commit message"),
  squash: flexibleBoolean.optional().describe("Combine all commits into one when merging"),
  should_remove_source_branch: flexibleBoolean
    .optional()
    .describe("Remove source branch after merge"),
  merge_when_pipeline_succeeds: flexibleBoolean.optional().describe("Merge when pipeline succeeds"),
  sha: z.string().optional().describe("SHA of the head commit"),
});
 
// --- Discriminated union combining all actions ---
export const ManageMergeRequestSchema = z.discriminatedUnion("action", [
  CreateMergeRequestSchema,
  UpdateMergeRequestSchema,
  MergeMergeRequestSchema,
]);
 
// ============================================================================
// manage_mr_discussion - CQRS Command Tool (discriminated union schema)
// Actions: comment, thread, reply, update, apply_suggestion, apply_suggestions
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
 
// --- Action: comment ---
const CommentOnNoteableSchema = z.object({
  action: z.literal("comment").describe("Add a comment to an issue or merge request"),
  project_id: projectIdField,
  noteable_type: z
    .string()
    .transform(val => val.toLowerCase())
    .pipe(z.enum(["issue", "merge_request"]))
    .describe("Type of noteable: issue or merge_request"),
  noteable_id: requiredId.describe("ID of the noteable object"),
  body: z.string().describe("Content/text of the comment"),
  confidential: flexibleBoolean.optional().describe("Confidential note flag"),
  created_at: z.string().optional().describe("Date time string (ISO 8601)"),
});
 
// --- Action: thread ---
const CreateThreadSchema = z.object({
  action: z.literal("thread").describe("Start a new discussion thread on an MR"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  body: z.string().describe("Content/text of the thread"),
  position: MergeRequestThreadPositionSchema.optional().describe("Position for diff note"),
  commit_id: z.string().optional().describe("SHA of commit to start discussion on"),
});
 
// --- Action: reply ---
const ReplyToThreadSchema = z.object({
  action: z.literal("reply").describe("Reply to an existing discussion thread"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  discussion_id: z.string().describe("ID of the discussion to reply to"),
  body: z.string().describe("Content/text of the reply"),
  created_at: z.string().optional().describe("Date time string (ISO 8601)"),
});
 
// --- Action: update ---
const UpdateNoteSchema = z.object({
  action: z.literal("update").describe("Update an existing note/comment"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  note_id: requiredId.describe("ID of the note to update"),
  body: z.string().describe("New content/text for the note"),
});
 
// --- Action: apply_suggestion ---
const ApplySuggestionSchema = z.object({
  action: z.literal("apply_suggestion").describe("Apply a single code suggestion from a review"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  suggestion_id: z.number().describe("ID of the suggestion to apply"),
  commit_message: z.string().optional().describe("Custom commit message for the apply commit"),
});
 
// --- Action: apply_suggestions ---
const ApplySuggestionsSchema = z.object({
  action: z.literal("apply_suggestions").describe("Batch apply multiple code suggestions"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  suggestion_ids: z.array(z.number()).min(1).describe("Array of suggestion IDs to apply"),
  commit_message: z.string().optional().describe("Custom commit message for the apply commit"),
});
 
// --- Discriminated union combining all actions ---
export const ManageMrDiscussionSchema = z.discriminatedUnion("action", [
  CommentOnNoteableSchema,
  CreateThreadSchema,
  ReplyToThreadSchema,
  UpdateNoteSchema,
  ApplySuggestionSchema,
  ApplySuggestionsSchema,
]);
 
// ============================================================================
// manage_draft_notes - CQRS Command Tool (discriminated union schema)
// Actions: create, update, publish, publish_all, delete
// 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 draftNoteIdField = requiredId.describe("ID of the draft note");
 
// --- Action: create ---
const CreateDraftNoteSchema = z.object({
  action: z.literal("create").describe("Create a new draft note"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  note: z.string().describe("Content of the draft note"),
  position: MergeRequestThreadPositionSchema.optional().describe("Position for diff note"),
  in_reply_to_discussion_id: z.string().optional().describe("Discussion ID to reply to"),
  commit_id: z.string().optional().describe("SHA of commit to start discussion on"),
});
 
// --- Action: update ---
const UpdateDraftNoteSchema = z.object({
  action: z.literal("update").describe("Update an existing draft note"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  draft_note_id: draftNoteIdField,
  note: z.string().describe("New content for the draft note"),
  position: MergeRequestThreadPositionSchema.optional().describe("Position for diff note"),
});
 
// --- Action: publish ---
const PublishDraftNoteSchema = z.object({
  action: z.literal("publish").describe("Publish a single draft note"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  draft_note_id: draftNoteIdField,
});
 
// --- Action: publish_all ---
const PublishAllDraftNotesSchema = z.object({
  action: z.literal("publish_all").describe("Publish all draft notes at once"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
});
 
// --- Action: delete ---
const DeleteDraftNoteSchema = z.object({
  action: z.literal("delete").describe("Delete a draft note"),
  project_id: projectIdField,
  merge_request_iid: mergeRequestIidField,
  draft_note_id: draftNoteIdField,
});
 
// --- Discriminated union combining all actions ---
export const ManageDraftNotesSchema = z.discriminatedUnion("action", [
  CreateDraftNoteSchema,
  UpdateDraftNoteSchema,
  PublishDraftNoteSchema,
  PublishAllDraftNotesSchema,
  DeleteDraftNoteSchema,
]);
 
// ============================================================================
// Export type definitions
// ============================================================================
 
export type ManageMergeRequestInput = z.infer<typeof ManageMergeRequestSchema>;
export type ManageMrDiscussionInput = z.infer<typeof ManageMrDiscussionSchema>;
export type ManageDraftNotesInput = z.infer<typeof ManageDraftNotesSchema>;
export type MergeRequestThreadPosition = z.infer<typeof MergeRequestThreadPositionSchema>;