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 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 12x 5x 7x 12x 12x 9x 297x 12x 10x 30x 12x 3x 6x 3x 3x 3x 3x | import { z } from "zod";
import { flexibleBoolean, requiredId } from "../utils";
// ============================================================================
// browse_merge_requests - CQRS Query Tool (discriminated union schema)
// Actions: list, get, diffs, compare
// 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");
// --- Shared optional fields for get/diffs actions ---
const includeDivergedCommitsCountField = flexibleBoolean
.optional()
.describe("Include count of commits the source branch is behind target");
const includeRebaseInProgressField = flexibleBoolean
.optional()
.describe("Check if MR is currently being rebased");
// --- Shared not filter schema for list action ---
const NotFilterSchema = z
.object({
labels: z.union([z.string(), z.array(z.string())]).optional(),
milestone: z.string().optional(),
author_id: z.number().optional(),
author_username: z.string().optional(),
assignee_id: z.number().optional(),
assignee_username: z.string().optional(),
my_reaction_emoji: z.string().optional(),
})
.describe("Exclusion filters");
// --- Action: list ---
// Note: .passthrough() preserves unknown fields for superRefine validation
const ListMergeRequestsSchema = z
.object({
action: z.literal("list").describe("List merge requests with filtering"),
project_id: z.coerce
.string()
.optional()
.describe("Project ID or URL-encoded path. Optional for cross-project search."),
state: z
.enum(["opened", "closed", "locked", "merged", "all"])
.optional()
.describe("MR state filter"),
order_by: z
.enum(["created_at", "updated_at", "title", "priority"])
.optional()
.describe("Sort field"),
sort: z.enum(["asc", "desc"]).optional().describe("Sort direction"),
milestone: z.string().optional().describe('Filter by milestone title. Use "None" or "Any".'),
view: z.enum(["simple", "full"]).optional().describe("Response detail level"),
labels: z
.union([z.string(), z.array(z.string())])
.optional()
.describe("Filter by labels"),
with_labels_details: flexibleBoolean.optional().describe("Return full label objects"),
with_merge_status_recheck: flexibleBoolean
.optional()
.describe("Trigger async recheck of merge status"),
created_after: z.string().optional().describe("Filter MRs created after (ISO 8601)"),
created_before: z.string().optional().describe("Filter MRs created before (ISO 8601)"),
updated_after: z.string().optional().describe("Filter MRs modified after (ISO 8601)"),
updated_before: z.string().optional().describe("Filter MRs modified before (ISO 8601)"),
scope: z.enum(["created_by_me", "assigned_to_me", "all"]).optional().describe("Filter scope"),
author_id: z.number().optional().describe("Filter by author's user ID"),
author_username: z.string().optional().describe("Filter by author's username"),
assignee_id: z.number().optional().describe("Filter by assignee's user ID"),
assignee_username: z.string().optional().describe("Filter by assignee's username"),
my_reaction_emoji: z.string().optional().describe("Filter MRs you've reacted to"),
source_branch: z.string().optional().describe("Filter by source branch"),
target_branch: z.string().optional().describe("Filter by target branch"),
search: z.string().optional().describe("Text search in title/description"),
in: z.enum(["title", "description", "title,description"]).optional().describe("Search scope"),
wip: z.enum(["yes", "no"]).optional().describe("Draft/WIP filter"),
not: NotFilterSchema.optional(),
environment: z.string().optional().describe("Filter by deployment environment"),
deployed_before: z.string().optional().describe("Filter MRs deployed before"),
deployed_after: z.string().optional().describe("Filter MRs deployed after"),
approved_by_ids: z.array(z.string()).optional().describe("Filter MRs approved by user IDs"),
approved_by_usernames: z
.array(z.string())
.optional()
.describe("Filter MRs approved by usernames"),
reviewer_id: z.number().optional().describe("Filter by reviewer user ID"),
reviewer_username: z.string().optional().describe("Filter by reviewer username"),
with_api_entity_associations: flexibleBoolean
.optional()
.describe("Include extra API associations"),
min_access_level: z.number().optional().describe("Minimum access level filter (10-50)"),
per_page: z.number().optional().describe("Number of items per page"),
page: z.number().optional().describe("Page number"),
})
.passthrough();
// --- Action: get ---
// Note: .passthrough() preserves unknown fields for superRefine validation
const GetMergeRequestByIidSchema = z
.object({
action: z.literal("get").describe("Get single MR by IID or branch name"),
project_id: projectIdField,
merge_request_iid: mergeRequestIidField
.optional()
.describe("Internal MR ID. Required unless branch_name provided."),
branch_name: z.string().optional().describe("Find MR by its source branch name"),
include_diverged_commits_count: includeDivergedCommitsCountField,
include_rebase_in_progress: includeRebaseInProgressField,
})
.passthrough();
// --- Action: diffs ---
// Note: .passthrough() preserves unknown fields for superRefine validation
const DiffsMergeRequestSchema = z
.object({
action: z.literal("diffs").describe("Get file changes/diffs for an MR"),
project_id: projectIdField,
merge_request_iid: mergeRequestIidField,
include_diverged_commits_count: includeDivergedCommitsCountField,
include_rebase_in_progress: includeRebaseInProgressField,
per_page: z.number().optional().describe("Number of items per page"),
page: z.number().optional().describe("Page number"),
})
.passthrough();
// --- Action: compare ---
// Note: .passthrough() preserves unknown fields for superRefine validation
const CompareMergeRequestSchema = z
.object({
action: z.literal("compare").describe("Compare two branches or commits"),
project_id: projectIdField,
from: z.string().describe("Source reference: branch name or commit SHA"),
to: z.string().describe("Target reference: branch name or commit SHA"),
straight: flexibleBoolean
.optional()
.describe("true=straight diff, false=three-way diff from common ancestor"),
})
.passthrough();
// --- Discriminated union combining all actions ---
// Note: GetMergeRequestSchema uses .refine() which doesn't work with discriminatedUnion directly,
// so we use a two-step approach: discriminatedUnion for base validation, then refinement
const BrowseMergeRequestsBaseSchema = z.discriminatedUnion("action", [
ListMergeRequestsSchema,
GetMergeRequestByIidSchema,
DiffsMergeRequestSchema,
CompareMergeRequestSchema,
]);
// Action-specific field sets for strict validation
const listOnlyFields = [
"state",
"order_by",
"sort",
"milestone",
"view",
"labels",
"with_labels_details",
"with_merge_status_recheck",
"created_after",
"created_before",
"updated_after",
"updated_before",
"scope",
"author_id",
"author_username",
"assignee_id",
"assignee_username",
"my_reaction_emoji",
"source_branch",
"target_branch",
"search",
"in",
"wip",
"not",
"environment",
"deployed_before",
"deployed_after",
"approved_by_ids",
"approved_by_usernames",
"reviewer_id",
"reviewer_username",
"with_api_entity_associations",
"min_access_level",
];
const compareOnlyFields = ["from", "to", "straight"];
const getOnlyFields = ["merge_request_iid", "branch_name"];
// Apply refinement for 'get' action validation and action-specific field validation
export const BrowseMergeRequestsSchema = BrowseMergeRequestsBaseSchema.refine(
data => {
if (data.action === "get") {
return data.merge_request_iid !== undefined || data.branch_name !== undefined;
}
return true;
},
{
message: "Either merge_request_iid or branch_name must be provided for 'get' action",
path: ["merge_request_iid"],
}
).superRefine((data, ctx) => {
const input = data as Record<string, unknown>;
// Check for list-only fields used in non-list actions
if (data.action !== "list") {
for (const field of listOnlyFields) {
Iif (field in input && input[field] !== undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `'${field}' is only valid for 'list' action`,
path: [field],
});
}
}
}
// Check for compare-only fields used in non-compare actions
if (data.action !== "compare") {
for (const field of compareOnlyFields) {
Iif (field in input && input[field] !== undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `'${field}' is only valid for 'compare' action`,
path: [field],
});
}
}
}
// Check for get-only fields (merge_request_iid, branch_name) used in list action
if (data.action === "list") {
for (const field of getOnlyFields) {
Iif (field in input && input[field] !== undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `'${field}' is only valid for 'get' action`,
path: [field],
});
}
}
}
});
// ============================================================================
// browse_mr_discussions - CQRS Query Tool (discriminated union schema)
// Actions: list, drafts, draft
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
// --- Action: list ---
const ListMrDiscussionsSchema = z.object({
action: z.literal("list").describe("List all discussion threads on an MR"),
project_id: projectIdField,
merge_request_iid: mergeRequestIidField,
per_page: z.number().optional().describe("Number of items per page"),
page: z.number().optional().describe("Page number"),
});
// --- Action: drafts ---
const ListDraftNotesSchema = z.object({
action: z.literal("drafts").describe("List unpublished draft notes on an MR"),
project_id: projectIdField,
merge_request_iid: mergeRequestIidField,
});
// --- Action: draft ---
const GetDraftNoteSchema = z.object({
action: z.literal("draft").describe("Get single draft note details"),
project_id: projectIdField,
merge_request_iid: mergeRequestIidField,
draft_note_id: requiredId.describe("Unique identifier of the draft note"),
});
// --- Discriminated union combining all actions ---
export const BrowseMrDiscussionsSchema = z.discriminatedUnion("action", [
ListMrDiscussionsSchema,
ListDraftNotesSchema,
GetDraftNoteSchema,
]);
// ============================================================================
// Export type definitions
// ============================================================================
export type BrowseMergeRequestsInput = z.infer<typeof BrowseMergeRequestsSchema>;
export type BrowseMrDiscussionsInput = z.infer<typeof BrowseMrDiscussionsSchema>;
|