@zereight/mcp-gitlab 2.3.29-next.248 → 2.3.29-next.250
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +17 -178
- package/build/masking/engine.js +389 -3
- package/build/tools/webhooks.js +205 -0
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -107,7 +107,7 @@ import { isInitializationRequestBody, isUnauthenticatedDiscoveryRequestBody, par
|
|
|
107
107
|
export { readMcpSessionIdHeader } from "./server/request-helpers.js";
|
|
108
108
|
import { authBySession, BASE_HEADERS, bindSessionProjectScope, buildAuthHeaders, configureGitLabRequestContext, getEffectiveAllowedProjectIds, getEffectiveApiUrl, getFetchConfig, SESSION_PROJECT_SCOPE_MISMATCH, sessionAuthStore, updateSessionPublicBaseUrl, usesJobTokenHeader, withPublicBaseUrl, } from "./server/gitlab-request-context.js";
|
|
109
109
|
import { apiUrlUserinfoWarning, commaSplitHint, configuredApiUrlErrors, encodeGitLabPath, encodeGitLabPathSegment, invalidListEntryError, isCleartextRemoteUrl, normalizeGitLabApiUrl, parseConfiguredGitLabApiUrls, uploadFilenameForms, } from "./utils/url.js";
|
|
110
|
-
import { estimateMergeCommitCount, filterDiffsByPatterns, openSafeOutputWriteStream, resolveDownloadRoot, readSafeExistingFile,
|
|
110
|
+
import { estimateMergeCommitCount, filterDiffsByPatterns, openSafeOutputWriteStream, resolveDownloadRoot, readSafeExistingFile, } from "./utils/helpers.js";
|
|
111
111
|
import { graphqlQueryContainsWriteOperation, graphqlQueryContainsDeleteOperation, } from "./utils/graphql-query.js";
|
|
112
112
|
import { encodeRepoFilePayloadContent, fileOperationsIncludeDeleteOrMove, toGitLabCommitActions, } from "./utils/gitlab-commit-actions.js";
|
|
113
113
|
import { redactSensitiveGitLabFields } from "./utils/redact-sensitive.js";
|
|
@@ -137,6 +137,7 @@ import { configureMilestoneTools, handleMilestoneToolCall } from "./tools/milest
|
|
|
137
137
|
import { configureLabelTools, handleLabelToolCall } from "./tools/labels.js";
|
|
138
138
|
import { configureTagTools, handleTagToolCall } from "./tools/tags.js";
|
|
139
139
|
import { configureVariableTools, handleVariableToolCall } from "./tools/variables.js";
|
|
140
|
+
import { configureWebhookTools, handleWebhookToolCall } from "./tools/webhooks.js";
|
|
140
141
|
import { buildUnavailableToolMessage, isSkippedByDiscoverActivation, } from "./tools/discover-activation.js";
|
|
141
142
|
import { selectCallableHiddenToolNames, selectExposedTools } from "./tools/exposed-tools.js";
|
|
142
143
|
import { createListedToolsCache, decorateToolsForList, } from "./tools/listed-tools.js";
|
|
@@ -148,7 +149,7 @@ GitLabDiscussionNoteSchema, // Added
|
|
|
148
149
|
GitLabDiscussionSchema,
|
|
149
150
|
// Draft Notes Schemas
|
|
150
151
|
GitLabDraftNoteSchema, GitLabForkSchema, GitLabBranchSchema, GitLabProtectedBranchSchema, GitLabGroupSchema, GitLabIssueLinkSchema, GitLabIssueSchema, GitLabIssueWithLinkDetailsSchema, GitLabResourceLabelEventSchema, GitLabResourceStateEventSchema, GitLabMarkdownUploadSchema, GitLabMergeRequestPipelineSchema, GitLabMergeRequestSchema, GitLabNamespaceExistsResponseSchema, GitLabNamespaceSchema, GitLabPipelineJobSchema, GitLabDeploymentSchema, GitLabEnvironmentSchema, GitLabPipelineSchema, GitLabPipelineScheduleSchema, GitLabPipelineScheduleVariableSchema, GitLabPipelineSchedulePlayResultSchema, ListPipelineSchedulesSchema, GetPipelineScheduleSchema, ListPipelineSchedulePipelinesSchema, CreatePipelineScheduleSchema, UpdatePipelineScheduleSchema, DeletePipelineScheduleSchema, PlayPipelineScheduleSchema, TakeOwnershipPipelineScheduleSchema, GetPipelineScheduleVariableSchema, CreatePipelineScheduleVariableSchema, UpdatePipelineScheduleVariableSchema, DeletePipelineScheduleVariableSchema, GitLabPipelineTriggerJobSchema, GitLabProjectMemberSchema, GitLabProjectSchema, GitLabTodoSchema, GitLabReferenceSchema, GitLabRepositorySchema, GitLabSearchBlobResultSchema, GitLabSearchResponseSchema, GitLabTreeItemSchema, GitLabUserSchema, GitLabUsersResponseSchema, GroupIteration, ListCommitStatusesSchema, ListBranchesSchema, ListCommitsSchema, ListDraftNotesSchema, ListGroupIterationsSchema, GetGroupIterationSchema, UpdateGroupIterationSchema, CreateGroupIterationSchema, GroupIterationCadence, ListGroupIterationCadencesSchema, DeleteGroupIterationSchema, ListProjectIterationsSchema, CreateGroupIterationCadenceSchema, UpdateGroupIterationCadenceSchema, DeleteGroupIterationCadenceSchema, ListGroupProjectsSchema, GitLabDependencyProxySchema, GitLabDependencyProxyBlobSchema, GetDependencyProxySettingsSchema, UpdateDependencyProxySettingsSchema, ListDependencyProxyBlobsSchema, PurgeDependencyProxyCacheSchema, ListProjectVulnerabilitiesSchema, GetVulnerabilitySchema, DismissVulnerabilitySchema, ConfirmVulnerabilitySchema, ListIssueDiscussionsSchema, ListIssueLinksSchema, ListIssueLabelEventsSchema, ListIssueStateEventsSchema, ListIssuesSchema, ListTodosSchema, ListMergeRequestDiffsSchema, // Added
|
|
151
|
-
GetMergeRequestFileDiffSchema, ListMergeRequestChangedFilesSchema, ListMergeRequestDiscussionsSchema, ListMergeRequestPipelinesSchema, ListMergeRequestsSchema, ListGroupMergeRequestsSchema, ListMergeRequestVersionsSchema, GetMergeRequestVersionSchema, GitLabMergeRequestVersionSchema, GitLabMergeRequestVersionDetailSchema, ListNamespacesSchema, ListPipelineJobsSchema, ListPipelinesSchema, ListDeploymentsSchema, ListEnvironmentsSchema, ListProjectRunnersSchema, GetRunnerSchema, ListPipelineTriggerJobsSchema, ValidateCiLintSchema, ValidateProjectCiLintSchema, ListCiCatalogResourcesSchema, GetCiCatalogResourceSchema, ListProjectMembersSchema, ListGroupMembersSchema, ListProjectsSchema, MarkdownUploadSchema, MarkdownUploadRemoteSchema, DownloadAttachmentSchema, DownloadJobArtifactsSchema, GetJobArtifactFileSchema, GitLabArtifactEntrySchema, ListJobArtifactsSchema, MergeMergeRequestSchema, ApproveMergeRequestSchema, UnapproveMergeRequestSchema, RebaseMergeRequestSchema, GetMergeRequestApprovalStateSchema, GetMergeRequestConflictsSchema, GitLabMergeRequestApprovalStateSchema, MyIssuesSchema, OrbitQuerySchema, OrbitSchemaSchema, OrbitStatusSchema, OrbitToolsSchema, MarkAllTodosDoneSchema, MarkTodoDoneSchema, PaginatedDiscussionsResponseSchema, PublishDraftNoteSchema, PlayPipelineJobSchema, PushFilesSchema, RetryPipelineJobSchema, RetryPipelineSchema, SearchCodeSchema, SearchGroupCodeSchema, SearchProjectCodeSchema, SearchRepositoriesSchema, UpdateDraftNoteSchema, UpdateIssueNoteSchema, UpdateIssueSchema, UpdateIssueDescriptionPatchSchema, ReorderIssueSchema, UpdateProjectSchema, UpdateMergeRequestNoteSchema, UpdateMergeRequestDiscussionNoteSchema, UpdateMergeRequestSchema, VerifyNamespaceSchema, GitLabEventSchema, ListEventsSchema, GetProjectEventsSchema, ExecuteGraphQLSchema, GitLabReleaseSchema, ListReleasesSchema, GetReleaseSchema, CreateReleaseSchema, UpdateReleaseSchema, DeleteReleaseSchema, CreateReleaseEvidenceSchema, DownloadReleaseAssetSchema, ListSnippetsSchema, GetSnippetSchema, CreateSnippetSchema, UpdateSnippetSchema, DeleteSnippetSchema, GitLabSnippetSchema, GetMergeRequestNotesSchema, GetMergeRequestNoteSchema, GetMergeRequestDiscussionSchema, DeleteMergeRequestDiscussionNoteSchema, ResolveMergeRequestThreadSchema, GetWorkItemSchema, ListWorkItemsSchema, CreateWorkItemSchema, UpdateWorkItemSchema, ConvertWorkItemTypeSchema, ListWorkItemStatusesSchema, ListWorkItemNotesSchema, CreateWorkItemNoteSchema, CreateWorkItemEmojiReactionSchema, CreateWorkItemNoteEmojiReactionSchema, ListWorkItemEmojiReactionsSchema, ListWorkItemNoteEmojiReactionsSchema, DeleteWorkItemEmojiReactionSchema, DeleteWorkItemNoteEmojiReactionSchema, MoveWorkItemSchema, ListCustomFieldDefinitionsSchema, GetTimelineEventsSchema, CreateTimelineEventSchema,
|
|
152
|
+
GetMergeRequestFileDiffSchema, ListMergeRequestChangedFilesSchema, ListMergeRequestDiscussionsSchema, ListMergeRequestPipelinesSchema, ListMergeRequestsSchema, ListGroupMergeRequestsSchema, ListMergeRequestVersionsSchema, GetMergeRequestVersionSchema, GitLabMergeRequestVersionSchema, GitLabMergeRequestVersionDetailSchema, ListNamespacesSchema, ListPipelineJobsSchema, ListPipelinesSchema, ListDeploymentsSchema, ListEnvironmentsSchema, ListProjectRunnersSchema, GetRunnerSchema, ListPipelineTriggerJobsSchema, ValidateCiLintSchema, ValidateProjectCiLintSchema, ListCiCatalogResourcesSchema, GetCiCatalogResourceSchema, ListProjectMembersSchema, ListGroupMembersSchema, ListProjectsSchema, MarkdownUploadSchema, MarkdownUploadRemoteSchema, DownloadAttachmentSchema, DownloadJobArtifactsSchema, GetJobArtifactFileSchema, GitLabArtifactEntrySchema, ListJobArtifactsSchema, MergeMergeRequestSchema, ApproveMergeRequestSchema, UnapproveMergeRequestSchema, RebaseMergeRequestSchema, GetMergeRequestApprovalStateSchema, GetMergeRequestConflictsSchema, GitLabMergeRequestApprovalStateSchema, MyIssuesSchema, OrbitQuerySchema, OrbitSchemaSchema, OrbitStatusSchema, OrbitToolsSchema, MarkAllTodosDoneSchema, MarkTodoDoneSchema, PaginatedDiscussionsResponseSchema, PublishDraftNoteSchema, PlayPipelineJobSchema, PushFilesSchema, RetryPipelineJobSchema, RetryPipelineSchema, SearchCodeSchema, SearchGroupCodeSchema, SearchProjectCodeSchema, SearchRepositoriesSchema, UpdateDraftNoteSchema, UpdateIssueNoteSchema, UpdateIssueSchema, UpdateIssueDescriptionPatchSchema, ReorderIssueSchema, UpdateProjectSchema, UpdateMergeRequestNoteSchema, UpdateMergeRequestDiscussionNoteSchema, UpdateMergeRequestSchema, VerifyNamespaceSchema, GitLabEventSchema, ListEventsSchema, GetProjectEventsSchema, ExecuteGraphQLSchema, GitLabReleaseSchema, ListReleasesSchema, GetReleaseSchema, CreateReleaseSchema, UpdateReleaseSchema, DeleteReleaseSchema, CreateReleaseEvidenceSchema, DownloadReleaseAssetSchema, ListSnippetsSchema, GetSnippetSchema, CreateSnippetSchema, UpdateSnippetSchema, DeleteSnippetSchema, GitLabSnippetSchema, GetMergeRequestNotesSchema, GetMergeRequestNoteSchema, GetMergeRequestDiscussionSchema, DeleteMergeRequestDiscussionNoteSchema, ResolveMergeRequestThreadSchema, GetWorkItemSchema, ListWorkItemsSchema, CreateWorkItemSchema, UpdateWorkItemSchema, ConvertWorkItemTypeSchema, ListWorkItemStatusesSchema, ListWorkItemNotesSchema, CreateWorkItemNoteSchema, CreateWorkItemEmojiReactionSchema, CreateWorkItemNoteEmojiReactionSchema, ListWorkItemEmojiReactionsSchema, ListWorkItemNoteEmojiReactionsSchema, DeleteWorkItemEmojiReactionSchema, DeleteWorkItemNoteEmojiReactionSchema, MoveWorkItemSchema, ListCustomFieldDefinitionsSchema, GetTimelineEventsSchema, CreateTimelineEventSchema, HealthCheckSchema, } from "./schemas.js";
|
|
152
153
|
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
153
154
|
import { createGraphQLAuditFailure, createToolCallAuditor, graphqlPayloadHasErrors, markToolResultAsAuditFailure, readKnownGitLabUserId, readKnownGitLabUsername, } from "./utils/audit-log.js";
|
|
154
155
|
import { createLogger, flushBufferedLogs, flushStartupLogs } from "./utils/logger.js";
|
|
@@ -1005,7 +1006,7 @@ configureGitLabRequestContext({
|
|
|
1005
1006
|
getDispatcherForUrl: apiUrl => clientPool.getDispatcherForUrl(apiUrl),
|
|
1006
1007
|
getOAuthAccessToken: () => OAUTH_ACCESS_TOKEN,
|
|
1007
1008
|
});
|
|
1008
|
-
// The wiki, milestone, label, tag, and
|
|
1009
|
+
// The wiki, milestone, label, tag, variable, and webhook handlers call back into the fetch, error
|
|
1009
1010
|
// handling, and project scope helpers below; fetch is read at call time because the cookie
|
|
1010
1011
|
// reload replaces it.
|
|
1011
1012
|
configureWikiTools({
|
|
@@ -1036,6 +1037,12 @@ configureVariableTools({
|
|
|
1036
1037
|
getEffectiveProjectId,
|
|
1037
1038
|
rejectIfProjectScopedDeployment,
|
|
1038
1039
|
});
|
|
1040
|
+
configureWebhookTools({
|
|
1041
|
+
getFetch: () => fetch,
|
|
1042
|
+
handleGitLabError,
|
|
1043
|
+
getEffectiveProjectId,
|
|
1044
|
+
rejectIfStrictProjectScope,
|
|
1045
|
+
});
|
|
1039
1046
|
// Create cookie jar with clean Netscape file parsing
|
|
1040
1047
|
// Resolve cookie path once using os.homedir() for cross-platform support
|
|
1041
1048
|
const resolvedCookiePath = GITLAB_AUTH_COOKIE_PATH
|
|
@@ -5155,126 +5162,6 @@ async function listGroupProjects(options) {
|
|
|
5155
5162
|
const projects = await response.json();
|
|
5156
5163
|
return filterProjectsByAllowlist(GitLabProjectSchema.array().parse(projects));
|
|
5157
5164
|
}
|
|
5158
|
-
// Webhook API helper functions
|
|
5159
|
-
/**
|
|
5160
|
-
* Build the base URL for webhooks (project or group)
|
|
5161
|
-
*/
|
|
5162
|
-
function buildWebhookBaseUrl(projectId, groupId) {
|
|
5163
|
-
if (projectId) {
|
|
5164
|
-
projectId = decodeURIComponent(projectId);
|
|
5165
|
-
return `${getEffectiveApiUrl()}/projects/${encodeGitLabPathSegment(getEffectiveProjectId(projectId))}/hooks`;
|
|
5166
|
-
}
|
|
5167
|
-
rejectIfStrictProjectScope("group webhooks");
|
|
5168
|
-
const decodedGroupId = decodeURIComponent(groupId);
|
|
5169
|
-
return `${getEffectiveApiUrl()}/groups/${encodeGitLabPathSegment(decodedGroupId)}/hooks`;
|
|
5170
|
-
}
|
|
5171
|
-
/**
|
|
5172
|
-
* List webhooks for a project or group
|
|
5173
|
-
*/
|
|
5174
|
-
async function listWebhooks(options) {
|
|
5175
|
-
const url = new URL(buildWebhookBaseUrl(options.project_id, options.group_id));
|
|
5176
|
-
if (options.page)
|
|
5177
|
-
url.searchParams.append("page", options.page.toString());
|
|
5178
|
-
if (options.per_page)
|
|
5179
|
-
url.searchParams.append("per_page", options.per_page.toString());
|
|
5180
|
-
const response = await fetch(url.toString(), { ...getFetchConfig() });
|
|
5181
|
-
await handleGitLabError(response);
|
|
5182
|
-
return (await response.json());
|
|
5183
|
-
}
|
|
5184
|
-
function buildWebhookMutationBody(options) {
|
|
5185
|
-
const body = {};
|
|
5186
|
-
for (const [key, value] of Object.entries(options)) {
|
|
5187
|
-
if (key === "project_id" || key === "group_id" || key === "hook_id")
|
|
5188
|
-
continue;
|
|
5189
|
-
if (value !== undefined)
|
|
5190
|
-
body[key] = value;
|
|
5191
|
-
}
|
|
5192
|
-
return body;
|
|
5193
|
-
}
|
|
5194
|
-
/**
|
|
5195
|
-
* Create a webhook on a project or group
|
|
5196
|
-
*/
|
|
5197
|
-
async function createWebhook(options) {
|
|
5198
|
-
const url = buildWebhookBaseUrl(options.project_id, options.group_id);
|
|
5199
|
-
const response = await fetch(url, {
|
|
5200
|
-
...getFetchConfig(),
|
|
5201
|
-
method: "POST",
|
|
5202
|
-
body: JSON.stringify(buildWebhookMutationBody(options)),
|
|
5203
|
-
});
|
|
5204
|
-
await handleGitLabError(response);
|
|
5205
|
-
return await response.json();
|
|
5206
|
-
}
|
|
5207
|
-
/**
|
|
5208
|
-
* Update a webhook on a project or group
|
|
5209
|
-
*/
|
|
5210
|
-
async function updateWebhook(options) {
|
|
5211
|
-
const url = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}`;
|
|
5212
|
-
const response = await fetch(url, {
|
|
5213
|
-
...getFetchConfig(),
|
|
5214
|
-
method: "PUT",
|
|
5215
|
-
body: JSON.stringify(buildWebhookMutationBody(options)),
|
|
5216
|
-
});
|
|
5217
|
-
await handleGitLabError(response);
|
|
5218
|
-
return await response.json();
|
|
5219
|
-
}
|
|
5220
|
-
/**
|
|
5221
|
-
* Delete a webhook from a project or group
|
|
5222
|
-
*/
|
|
5223
|
-
async function deleteWebhook(options) {
|
|
5224
|
-
const url = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}`;
|
|
5225
|
-
const response = await fetch(url, {
|
|
5226
|
-
...getFetchConfig(),
|
|
5227
|
-
method: "DELETE",
|
|
5228
|
-
});
|
|
5229
|
-
await handleGitLabError(response);
|
|
5230
|
-
return { status: "deleted", hook_id: options.hook_id };
|
|
5231
|
-
}
|
|
5232
|
-
/**
|
|
5233
|
-
* Fetch a single page of webhook events
|
|
5234
|
-
*/
|
|
5235
|
-
async function fetchWebhookEventsPage(baseUrl, page, perPage, status) {
|
|
5236
|
-
const url = new URL(baseUrl);
|
|
5237
|
-
url.searchParams.set("page", page.toString());
|
|
5238
|
-
url.searchParams.set("per_page", perPage.toString());
|
|
5239
|
-
if (status !== undefined)
|
|
5240
|
-
url.searchParams.append("status", String(status));
|
|
5241
|
-
const response = await fetch(url.toString(), { ...getFetchConfig() });
|
|
5242
|
-
await handleGitLabError(response);
|
|
5243
|
-
return (await response.json());
|
|
5244
|
-
}
|
|
5245
|
-
/**
|
|
5246
|
-
* List webhook events for a project or group webhook
|
|
5247
|
-
*/
|
|
5248
|
-
async function listWebhookEvents(options) {
|
|
5249
|
-
const eventsUrl = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}/events`;
|
|
5250
|
-
const events = await fetchWebhookEventsPage(eventsUrl, options.page ?? 1, options.per_page ?? 20, options.status);
|
|
5251
|
-
return options.summary ? summarizeWebhookEvents(events) : events;
|
|
5252
|
-
}
|
|
5253
|
-
/**
|
|
5254
|
-
* Get a specific webhook event by ID (searches up to 500 recent events)
|
|
5255
|
-
*/
|
|
5256
|
-
async function getWebhookEvent(options) {
|
|
5257
|
-
const eventsUrl = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}/events`;
|
|
5258
|
-
// GitLab enforces max per_page=20 for webhook events
|
|
5259
|
-
const perPage = 20;
|
|
5260
|
-
if (options.page) {
|
|
5261
|
-
// Direct page lookup — single API call
|
|
5262
|
-
const events = await fetchWebhookEventsPage(eventsUrl, options.page, perPage);
|
|
5263
|
-
const match = events.find(e => e.id === options.event_id);
|
|
5264
|
-
return match ?? null;
|
|
5265
|
-
}
|
|
5266
|
-
// Auto-paginate up to 500 events
|
|
5267
|
-
const maxPages = 25;
|
|
5268
|
-
for (let page = 1; page <= maxPages; page++) {
|
|
5269
|
-
const events = await fetchWebhookEventsPage(eventsUrl, page, perPage);
|
|
5270
|
-
const match = events.find(e => e.id === options.event_id);
|
|
5271
|
-
if (match)
|
|
5272
|
-
return match;
|
|
5273
|
-
if (events.length < perPage)
|
|
5274
|
-
break;
|
|
5275
|
-
}
|
|
5276
|
-
return null;
|
|
5277
|
-
}
|
|
5278
5165
|
/**
|
|
5279
5166
|
* List pipelines in a GitLab project
|
|
5280
5167
|
*
|
|
@@ -10482,61 +10369,13 @@ async function handleToolCall(params) {
|
|
|
10482
10369
|
],
|
|
10483
10370
|
};
|
|
10484
10371
|
}
|
|
10485
|
-
case "list_webhooks":
|
|
10486
|
-
|
|
10487
|
-
|
|
10488
|
-
|
|
10489
|
-
|
|
10490
|
-
|
|
10491
|
-
|
|
10492
|
-
case "create_webhook": {
|
|
10493
|
-
const args = CreateWebhookSchema.parse(params.arguments);
|
|
10494
|
-
const webhook = await createWebhook(args);
|
|
10495
|
-
return {
|
|
10496
|
-
content: [{ type: "text", text: JSON.stringify(webhook) }],
|
|
10497
|
-
};
|
|
10498
|
-
}
|
|
10499
|
-
case "update_webhook": {
|
|
10500
|
-
const args = UpdateWebhookSchema.parse(params.arguments);
|
|
10501
|
-
const webhook = await updateWebhook(args);
|
|
10502
|
-
return {
|
|
10503
|
-
content: [{ type: "text", text: JSON.stringify(webhook) }],
|
|
10504
|
-
};
|
|
10505
|
-
}
|
|
10506
|
-
case "delete_webhook": {
|
|
10507
|
-
const args = DeleteWebhookSchema.parse(params.arguments);
|
|
10508
|
-
const result = await deleteWebhook(args);
|
|
10509
|
-
return {
|
|
10510
|
-
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
10511
|
-
};
|
|
10512
|
-
}
|
|
10513
|
-
case "list_webhook_events": {
|
|
10514
|
-
const args = ListWebhookEventsSchema.parse(params.arguments);
|
|
10515
|
-
const events = await listWebhookEvents(args);
|
|
10516
|
-
return {
|
|
10517
|
-
content: [{ type: "text", text: JSON.stringify(events) }],
|
|
10518
|
-
};
|
|
10519
|
-
}
|
|
10520
|
-
case "get_webhook_event": {
|
|
10521
|
-
const args = GetWebhookEventSchema.parse(params.arguments);
|
|
10522
|
-
const event = await getWebhookEvent(args);
|
|
10523
|
-
if (!event) {
|
|
10524
|
-
const searchScope = args.page ? `on page ${args.page}` : "in the 500 most recent events";
|
|
10525
|
-
return {
|
|
10526
|
-
content: [
|
|
10527
|
-
{
|
|
10528
|
-
type: "text",
|
|
10529
|
-
text: JSON.stringify({
|
|
10530
|
-
error: `Webhook event ${args.event_id} not found ${searchScope}`,
|
|
10531
|
-
}),
|
|
10532
|
-
},
|
|
10533
|
-
],
|
|
10534
|
-
};
|
|
10535
|
-
}
|
|
10536
|
-
return {
|
|
10537
|
-
content: [{ type: "text", text: JSON.stringify(event) }],
|
|
10538
|
-
};
|
|
10539
|
-
}
|
|
10372
|
+
case "list_webhooks":
|
|
10373
|
+
case "create_webhook":
|
|
10374
|
+
case "update_webhook":
|
|
10375
|
+
case "delete_webhook":
|
|
10376
|
+
case "list_webhook_events":
|
|
10377
|
+
case "get_webhook_event":
|
|
10378
|
+
return await handleWebhookToolCall(params);
|
|
10540
10379
|
case "health_check": {
|
|
10541
10380
|
HealthCheckSchema.parse(params.arguments ?? {});
|
|
10542
10381
|
const url = new URL(`${getEffectiveApiUrl()}/user`);
|
package/build/masking/engine.js
CHANGED
|
@@ -242,6 +242,21 @@ function readJsonStringToken(token) {
|
|
|
242
242
|
}
|
|
243
243
|
return parsed;
|
|
244
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* GitLab marks CI/CD variables as `masked` or `hidden` (masked and hidden) so
|
|
247
|
+
* their values stay out of job logs and the UI. The variables API still returns
|
|
248
|
+
* the value, so with masking enabled the whole `value` of such a variable
|
|
249
|
+
* object is replaced, whatever it looks like.
|
|
250
|
+
*/
|
|
251
|
+
export const FLAGGED_CI_VARIABLE_REPLACEMENT = "[Masked CI variable value]";
|
|
252
|
+
function isFlaggedGitlabCiVariable(value) {
|
|
253
|
+
const record = value;
|
|
254
|
+
return (typeof record.key === "string" &&
|
|
255
|
+
typeof record.value === "string" &&
|
|
256
|
+
(record.masked === true || record.hidden === true));
|
|
257
|
+
}
|
|
258
|
+
// The literal after a JSON property name, up to the next `,` or `}`.
|
|
259
|
+
const JSON_TRUE_VALUE_GAP = /^\s*:\s*true\s*$/;
|
|
245
260
|
function readGitlabCiAwsSecretKey(value) {
|
|
246
261
|
if (!("key" in value))
|
|
247
262
|
return undefined;
|
|
@@ -265,8 +280,13 @@ function maskStrings(value, mask, parentKey) {
|
|
|
265
280
|
return value.map(item => maskStrings(item, mask, parentKey));
|
|
266
281
|
if (value && typeof value === "object") {
|
|
267
282
|
const ciKey = readGitlabCiAwsSecretKey(value);
|
|
283
|
+
const flaggedCiVariable = isFlaggedGitlabCiVariable(value);
|
|
268
284
|
const output = {};
|
|
269
285
|
for (const [key, item] of Object.entries(value)) {
|
|
286
|
+
if (flaggedCiVariable && key === "value") {
|
|
287
|
+
output[key] = FLAGGED_CI_VARIABLE_REPLACEMENT;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
270
290
|
const fieldKey = key === "value" && ciKey !== undefined ? ciKey : key;
|
|
271
291
|
output[key] = maskStrings(item, mask, fieldKey);
|
|
272
292
|
}
|
|
@@ -295,13 +315,27 @@ function maskJsonText(text, mask) {
|
|
|
295
315
|
const scopes = [];
|
|
296
316
|
const stringValues = [];
|
|
297
317
|
const objectCiKeys = new Map();
|
|
318
|
+
// Objects shaped like a GitLab CI variable: a string `key`, and `masked` or
|
|
319
|
+
// `hidden` set to the literal true. Both are collected before any rewrite.
|
|
320
|
+
const objectsWithStringKey = new Set();
|
|
321
|
+
const objectsFlaggedMasked = new Set();
|
|
298
322
|
let nextObjectId = 0;
|
|
323
|
+
let previousTokenEnd = 0;
|
|
299
324
|
for (const match of text.matchAll(JSON_STRING_OR_STRUCTURE_PATTERN)) {
|
|
300
325
|
const token = match[0];
|
|
301
326
|
const index = match.index;
|
|
302
327
|
if (index === undefined)
|
|
303
328
|
continue;
|
|
329
|
+
const gap = text.slice(previousTokenEnd, index);
|
|
330
|
+
previousTokenEnd = index + token.length;
|
|
304
331
|
const scope = scopes[scopes.length - 1];
|
|
332
|
+
if ((token === "," || token === "}") &&
|
|
333
|
+
scope?.isObject &&
|
|
334
|
+
!scope.expectingKey &&
|
|
335
|
+
(scope.fieldName === "masked" || scope.fieldName === "hidden") &&
|
|
336
|
+
JSON_TRUE_VALUE_GAP.test(gap)) {
|
|
337
|
+
objectsFlaggedMasked.add(scope.objectId);
|
|
338
|
+
}
|
|
305
339
|
if (token === "{") {
|
|
306
340
|
scopes.push({
|
|
307
341
|
isObject: true,
|
|
@@ -336,8 +370,10 @@ function maskJsonText(text, mask) {
|
|
|
336
370
|
scope.expectingKey = false;
|
|
337
371
|
continue;
|
|
338
372
|
}
|
|
339
|
-
if (scope?.isObject && scope.fieldName === "key"
|
|
340
|
-
|
|
373
|
+
if (scope?.isObject && scope.fieldName === "key") {
|
|
374
|
+
objectsWithStringKey.add(scope.objectId);
|
|
375
|
+
if (AWS_SECRET_FIELD_NAMES.has(decoded))
|
|
376
|
+
objectCiKeys.set(scope.objectId, decoded);
|
|
341
377
|
}
|
|
342
378
|
stringValues.push({
|
|
343
379
|
index,
|
|
@@ -345,12 +381,21 @@ function maskJsonText(text, mask) {
|
|
|
345
381
|
decoded,
|
|
346
382
|
fieldName: scope?.fieldName,
|
|
347
383
|
objectId: scope?.objectId ?? -1,
|
|
384
|
+
isObjectProperty: scope?.isObject === true,
|
|
348
385
|
});
|
|
349
386
|
}
|
|
350
387
|
let output = "";
|
|
351
388
|
let cursor = 0;
|
|
352
389
|
for (const item of stringValues) {
|
|
353
390
|
output += text.slice(cursor, item.index);
|
|
391
|
+
if (item.isObjectProperty &&
|
|
392
|
+
item.fieldName === "value" &&
|
|
393
|
+
objectsWithStringKey.has(item.objectId) &&
|
|
394
|
+
objectsFlaggedMasked.has(item.objectId)) {
|
|
395
|
+
output += JSON.stringify(FLAGGED_CI_VARIABLE_REPLACEMENT);
|
|
396
|
+
cursor = item.index + item.token.length;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
354
399
|
const ciKey = objectCiKeys.get(item.objectId);
|
|
355
400
|
const parentKey = item.fieldName === "value" && ciKey !== undefined ? ciKey : item.fieldName;
|
|
356
401
|
const masked = maskFieldValue(item.decoded, parentKey, mask);
|
|
@@ -359,11 +404,352 @@ function maskJsonText(text, mask) {
|
|
|
359
404
|
}
|
|
360
405
|
return output + text.slice(cursor);
|
|
361
406
|
}
|
|
407
|
+
/**
|
|
408
|
+
* Regex source for a JSON property name in raw text. Each letter may also be
|
|
409
|
+
* written as a JSON Unicode escape (`va\u006cue`), with one backslash in plain
|
|
410
|
+
* JSON or two in JSON printed inside a string, so escaped names still match.
|
|
411
|
+
*/
|
|
412
|
+
function jsonNamePattern(name) {
|
|
413
|
+
return Array.from(name, char => {
|
|
414
|
+
const hex = Array.from(char.charCodeAt(0).toString(16).padStart(4, "0"), digit => /[a-f]/.test(digit) ? `[${digit}${digit.toUpperCase()}]` : digit).join("");
|
|
415
|
+
return `(?:${char}|\\\\{1,2}u${hex})`;
|
|
416
|
+
}).join("");
|
|
417
|
+
}
|
|
418
|
+
const VALUE_NAME_PATTERN = jsonNamePattern("value");
|
|
419
|
+
// A flag literal in raw text. The escaped form covers JSON that is itself a
|
|
420
|
+
// string value inside prose (`\"masked\":true`).
|
|
421
|
+
const FLAG_LITERAL_PATTERN = new RegExp(`\\\\?"(?:${jsonNamePattern("masked")}|${jsonNamePattern("hidden")})\\\\?"\\s*:\\s*true(?![A-Za-z0-9_])`);
|
|
422
|
+
// A `"value"` property with a JSON string value, used only in uncovered text.
|
|
423
|
+
// A value cut off before its closing quote runs to the end of the text.
|
|
424
|
+
const UNCOVERED_VALUE_PATTERN = new RegExp(`("${VALUE_NAME_PATTERN}"\\s*:\\s*)"(?:\\\\.|[^"\\\\])*(?:"|\\\\?$)`, "g");
|
|
425
|
+
// The start of the same property in escaped JSON (`\"value\":\"..."`), e.g. a
|
|
426
|
+
// log line that prints a JSON string. The closing quote is found by
|
|
427
|
+
// findEndOfEscapedJsonString so an embedded `\\\"` cannot end the value.
|
|
428
|
+
const UNCOVERED_ESCAPED_VALUE_START_PATTERN = new RegExp(`\\\\"${VALUE_NAME_PATTERN}\\\\"\\s*:\\s*\\\\"`, "g");
|
|
429
|
+
// A JSON Unicode escape such as `\u0073` in `ma\u0073ked`. Used to skip the
|
|
430
|
+
// text fast path when a flag name may be escaped this way.
|
|
431
|
+
const JSON_UNICODE_ESCAPE_PATTERN = /\\u[0-9a-fA-F]{4}/;
|
|
432
|
+
const FLAGGED_TEXT_SCAN_FACTOR = 8;
|
|
433
|
+
const FLAGGED_TEXT_SCAN_SLACK = 65536;
|
|
434
|
+
/** Index of the `}` closing the `{` at `open`, or -1. Charges the characters it walks. */
|
|
435
|
+
function findClosingBrace(text, open, budget) {
|
|
436
|
+
const limit = Math.min(text.length, open + budget.remaining);
|
|
437
|
+
let depth = 0;
|
|
438
|
+
let inString = false;
|
|
439
|
+
let index = open;
|
|
440
|
+
let close = -1;
|
|
441
|
+
for (; index < limit; index++) {
|
|
442
|
+
const char = text[index];
|
|
443
|
+
if (inString) {
|
|
444
|
+
if (char === "\\")
|
|
445
|
+
index++;
|
|
446
|
+
else if (char === '"')
|
|
447
|
+
inString = false;
|
|
448
|
+
}
|
|
449
|
+
else if (char === '"') {
|
|
450
|
+
inString = true;
|
|
451
|
+
}
|
|
452
|
+
else if (char === "{" || char === "[") {
|
|
453
|
+
depth++;
|
|
454
|
+
}
|
|
455
|
+
else if (char === "}" || char === "]") {
|
|
456
|
+
depth--;
|
|
457
|
+
if (depth === 0) {
|
|
458
|
+
if (char === "}")
|
|
459
|
+
close = index;
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
budget.remaining -= Math.min(index + 1, limit) - open;
|
|
465
|
+
return close;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Index just past the `"` that closes a JSON string whose content starts at
|
|
469
|
+
* `from`, or the end of the text when it never closes. Charges the
|
|
470
|
+
* characters it walks.
|
|
471
|
+
*/
|
|
472
|
+
function findEndOfJsonString(text, from, budget) {
|
|
473
|
+
let index = from;
|
|
474
|
+
for (; index < text.length; index++) {
|
|
475
|
+
const char = text[index];
|
|
476
|
+
if (char === "\\")
|
|
477
|
+
index++;
|
|
478
|
+
else if (char === '"')
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
budget.remaining -= Math.min(index + 1, text.length) - from;
|
|
482
|
+
return index < text.length ? index + 1 : text.length;
|
|
483
|
+
}
|
|
484
|
+
function parsesAsJsonObject(span) {
|
|
485
|
+
try {
|
|
486
|
+
const parsed = JSON.parse(span);
|
|
487
|
+
return typeof parsed === "object" && parsed !== null;
|
|
488
|
+
}
|
|
489
|
+
catch {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Index just past the `\"` that closes an escaped JSON string starting at
|
|
495
|
+
* `from` (the first character after the opening `\"`), or -1 if it never
|
|
496
|
+
* closes. `\\` delivers a backslash to the inner string and may start an
|
|
497
|
+
* inner escape; `\"` ends the string unless the inner string is escaping.
|
|
498
|
+
*/
|
|
499
|
+
function findEndOfEscapedJsonString(text, from) {
|
|
500
|
+
let innerEscaped = false;
|
|
501
|
+
for (let index = from; index < text.length; index++) {
|
|
502
|
+
const char = text[index];
|
|
503
|
+
if (char === "\\" && index + 1 < text.length) {
|
|
504
|
+
const next = text[index + 1];
|
|
505
|
+
if (next === "\\") {
|
|
506
|
+
innerEscaped = !innerEscaped;
|
|
507
|
+
index++;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (next === '"') {
|
|
511
|
+
if (!innerEscaped)
|
|
512
|
+
return index + 2;
|
|
513
|
+
innerEscaped = false;
|
|
514
|
+
index++;
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
innerEscaped = false;
|
|
519
|
+
}
|
|
520
|
+
return -1;
|
|
521
|
+
}
|
|
522
|
+
function replaceUncoveredEscapedValues(gap) {
|
|
523
|
+
let output = "";
|
|
524
|
+
let cursor = 0;
|
|
525
|
+
UNCOVERED_ESCAPED_VALUE_START_PATTERN.lastIndex = 0;
|
|
526
|
+
for (const match of gap.matchAll(UNCOVERED_ESCAPED_VALUE_START_PATTERN)) {
|
|
527
|
+
const start = match.index;
|
|
528
|
+
if (start === undefined || start < cursor)
|
|
529
|
+
continue;
|
|
530
|
+
const close = findEndOfEscapedJsonString(gap, start + match[0].length);
|
|
531
|
+
output +=
|
|
532
|
+
gap.slice(cursor, start) + match[0].slice(0, -2) + `\\"${FLAGGED_CI_VARIABLE_REPLACEMENT}\\"`;
|
|
533
|
+
// A value cut off before its closing quote runs to the end of the gap.
|
|
534
|
+
cursor = close === -1 ? gap.length : close;
|
|
535
|
+
}
|
|
536
|
+
return output + gap.slice(cursor);
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Fallback for text outside any valid JSON object. Only applied when that text
|
|
540
|
+
* also holds a flag literal, so `value` fields in unflagged text are untouched.
|
|
541
|
+
* Over-masks any `value` string in a broken gap that carries a flag.
|
|
542
|
+
*/
|
|
543
|
+
function replaceUncoveredValues(gap) {
|
|
544
|
+
if (!FLAG_LITERAL_PATTERN.test(gap))
|
|
545
|
+
return gap;
|
|
546
|
+
return replaceUncoveredEscapedValues(gap.replace(UNCOVERED_VALUE_PATTERN, (_match, prefix) => prefix + JSON.stringify(FLAGGED_CI_VARIABLE_REPLACEMENT)));
|
|
547
|
+
}
|
|
548
|
+
function newGapStructure(start) {
|
|
549
|
+
return {
|
|
550
|
+
start,
|
|
551
|
+
scanned: start,
|
|
552
|
+
inString: false,
|
|
553
|
+
stringStart: -1,
|
|
554
|
+
escaped: false,
|
|
555
|
+
open: [],
|
|
556
|
+
firstObject: -1,
|
|
557
|
+
last: undefined,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
/** Reads `[structure.scanned, end)` into `structure`. Charges the characters it reads. */
|
|
561
|
+
function advanceGapStructure(structure, text, end, budget) {
|
|
562
|
+
if (end <= structure.scanned)
|
|
563
|
+
return;
|
|
564
|
+
budget.remaining -= end - structure.scanned;
|
|
565
|
+
for (let index = structure.scanned; index < end; index++) {
|
|
566
|
+
const char = text[index];
|
|
567
|
+
if (structure.inString) {
|
|
568
|
+
if (structure.escaped)
|
|
569
|
+
structure.escaped = false;
|
|
570
|
+
else if (char === "\\")
|
|
571
|
+
structure.escaped = true;
|
|
572
|
+
else if (char === '"') {
|
|
573
|
+
structure.inString = false;
|
|
574
|
+
structure.last = '"';
|
|
575
|
+
}
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
if (/\s/.test(char))
|
|
579
|
+
continue;
|
|
580
|
+
structure.last = char;
|
|
581
|
+
if (char === '"') {
|
|
582
|
+
structure.inString = true;
|
|
583
|
+
structure.stringStart = index;
|
|
584
|
+
}
|
|
585
|
+
else if (char === "{" || char === "[") {
|
|
586
|
+
if (char === "{" && structure.firstObject === -1)
|
|
587
|
+
structure.firstObject = structure.open.length;
|
|
588
|
+
structure.open.push({ char, index });
|
|
589
|
+
}
|
|
590
|
+
else if ((char === "}" || char === "]") && structure.open.length > 0) {
|
|
591
|
+
structure.open.pop();
|
|
592
|
+
if (structure.open.length <= structure.firstObject)
|
|
593
|
+
structure.firstObject = -1;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
structure.scanned = end;
|
|
597
|
+
}
|
|
598
|
+
/** Start and end of each flag literal in `text`, in order. */
|
|
599
|
+
function flagLiteralRanges(text) {
|
|
600
|
+
const pattern = new RegExp(FLAG_LITERAL_PATTERN.source, "g");
|
|
601
|
+
return Array.from(text.matchAll(pattern), match => [
|
|
602
|
+
match.index ?? 0,
|
|
603
|
+
(match.index ?? 0) + match[0].length,
|
|
604
|
+
]);
|
|
605
|
+
}
|
|
606
|
+
/** True when some range lies inside `[start, end)`. `ranges` is sorted. */
|
|
607
|
+
function hasRangeWithin(ranges, start, end) {
|
|
608
|
+
let low = 0;
|
|
609
|
+
let high = ranges.length;
|
|
610
|
+
while (low < high) {
|
|
611
|
+
const middle = (low + high) >> 1;
|
|
612
|
+
if (ranges[middle][0] < start)
|
|
613
|
+
low = middle + 1;
|
|
614
|
+
else
|
|
615
|
+
high = middle;
|
|
616
|
+
}
|
|
617
|
+
return low < ranges.length && ranges[low][1] <= end;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* True when the object at `open` is nested inside an incomplete flagged
|
|
621
|
+
* object in the current gap: it is a property value, an array element, or
|
|
622
|
+
* text inside an open string there, and that parent holds a flag before or
|
|
623
|
+
* after `open`. Also true when `open` is text inside a string at gap level
|
|
624
|
+
* (for example an escaped record in a quoted log argument) and the gap holds
|
|
625
|
+
* a flag before that string ends. Such a span must not split the gap, or a
|
|
626
|
+
* value and its flag could land in different gaps. `parentEnd` gives the end
|
|
627
|
+
* of the parent at an index, and `stringEnd` the end of the string opened at
|
|
628
|
+
* an index (the end of the text when either never closes).
|
|
629
|
+
*/
|
|
630
|
+
function isNestedInIncompleteFlaggedObject(structure, flagRanges, open, parentEnd, stringEnd) {
|
|
631
|
+
if (structure.inString &&
|
|
632
|
+
hasRangeWithin(flagRanges, structure.start, stringEnd(structure.stringStart)))
|
|
633
|
+
return true;
|
|
634
|
+
if (structure.firstObject === -1)
|
|
635
|
+
return false;
|
|
636
|
+
const parent = structure.open[structure.firstObject];
|
|
637
|
+
if (!hasRangeWithin(flagRanges, parent.index, parentEnd(parent.index)))
|
|
638
|
+
return false;
|
|
639
|
+
// A brace inside an open string is string content, not a nested object.
|
|
640
|
+
if (structure.inString)
|
|
641
|
+
return true;
|
|
642
|
+
const innermost = structure.open[structure.open.length - 1];
|
|
643
|
+
if (innermost.char === "[")
|
|
644
|
+
return true;
|
|
645
|
+
return structure.last === ":";
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Replaces the value of each GitLab CI/CD variable flagged masked or hidden in
|
|
649
|
+
* text that is not pure JSON: JSON in prose, job logs, error messages, and JSON
|
|
650
|
+
* inside a JSON string. Each `{` is tried as the start of a complete object.
|
|
651
|
+
* A span that parses as an object goes through the JSON scanner in flagged-only
|
|
652
|
+
* mode, so every other byte is kept. Text outside those spans goes through
|
|
653
|
+
* replaceUncoveredValues. Limits:
|
|
654
|
+
* - A truncated object cut before its flag cannot be detected, because the
|
|
655
|
+
* flag is not in the text yet.
|
|
656
|
+
* - Text after the scan budget runs out is handled as uncovered text.
|
|
657
|
+
* - In a broken span that carries a flag, the fallback masks any `"value"`
|
|
658
|
+
* string, including values of complete objects nested in that span. Those
|
|
659
|
+
* nested objects also go through maskJsonText first.
|
|
660
|
+
*/
|
|
661
|
+
function maskFlaggedCiVariablesInText(text, budget = {
|
|
662
|
+
remaining: FLAGGED_TEXT_SCAN_FACTOR * text.length + FLAGGED_TEXT_SCAN_SLACK,
|
|
663
|
+
}) {
|
|
664
|
+
if (!FLAG_LITERAL_PATTERN.test(text) && !JSON_UNICODE_ESCAPE_PATTERN.test(text))
|
|
665
|
+
return text;
|
|
666
|
+
const pieces = [];
|
|
667
|
+
let gapStart = 0;
|
|
668
|
+
let structure = newGapStructure(0);
|
|
669
|
+
let flagRanges;
|
|
670
|
+
// Parents start at increasing indexes, so one cached entry scans each once.
|
|
671
|
+
let cachedParent = -1;
|
|
672
|
+
let cachedParentEnd = text.length;
|
|
673
|
+
const parentEnd = (index) => {
|
|
674
|
+
if (index !== cachedParent) {
|
|
675
|
+
const close = findClosingBrace(text, index, budget);
|
|
676
|
+
cachedParent = index;
|
|
677
|
+
cachedParentEnd = close === -1 ? text.length : close + 1;
|
|
678
|
+
}
|
|
679
|
+
return cachedParentEnd;
|
|
680
|
+
};
|
|
681
|
+
// Strings also start at increasing indexes within and across gaps.
|
|
682
|
+
let cachedString = -1;
|
|
683
|
+
let cachedStringEnd = text.length;
|
|
684
|
+
const stringEnd = (index) => {
|
|
685
|
+
if (index !== cachedString) {
|
|
686
|
+
cachedString = index;
|
|
687
|
+
cachedStringEnd = findEndOfJsonString(text, index + 1, budget);
|
|
688
|
+
}
|
|
689
|
+
return cachedStringEnd;
|
|
690
|
+
};
|
|
691
|
+
const maskSpan = (span) => maskJsonText(span, (value) => maskFlaggedCiVariablesInText(value, budget));
|
|
692
|
+
// Complete objects kept inside the current gap, outermost only, in order.
|
|
693
|
+
// They still go through maskJsonText, which decodes escaped property names
|
|
694
|
+
// that the uncovered-text fallback cannot match.
|
|
695
|
+
let retained = [];
|
|
696
|
+
const maskGap = (end) => {
|
|
697
|
+
let gap = "";
|
|
698
|
+
let cursor = gapStart;
|
|
699
|
+
for (const [spanStart, spanEnd] of retained) {
|
|
700
|
+
if (spanEnd > end)
|
|
701
|
+
break;
|
|
702
|
+
budget.remaining -= spanEnd - spanStart;
|
|
703
|
+
gap += text.slice(cursor, spanStart) + maskSpan(text.slice(spanStart, spanEnd));
|
|
704
|
+
cursor = spanEnd;
|
|
705
|
+
}
|
|
706
|
+
retained = [];
|
|
707
|
+
return replaceUncoveredValues(gap + text.slice(cursor, end));
|
|
708
|
+
};
|
|
709
|
+
let open = text.indexOf("{");
|
|
710
|
+
while (open !== -1 && budget.remaining > 0) {
|
|
711
|
+
const close = findClosingBrace(text, open, budget);
|
|
712
|
+
if (close !== -1) {
|
|
713
|
+
const spanLength = close - open + 1;
|
|
714
|
+
if (budget.remaining < spanLength)
|
|
715
|
+
break;
|
|
716
|
+
budget.remaining -= spanLength;
|
|
717
|
+
const span = text.slice(open, close + 1);
|
|
718
|
+
if (parsesAsJsonObject(span)) {
|
|
719
|
+
advanceGapStructure(structure, text, open, budget);
|
|
720
|
+
flagRanges ??= flagLiteralRanges(text);
|
|
721
|
+
if (isNestedInIncompleteFlaggedObject(structure, flagRanges, open, parentEnd, stringEnd)) {
|
|
722
|
+
const previous = retained[retained.length - 1];
|
|
723
|
+
if (previous === undefined || open >= previous[1])
|
|
724
|
+
retained.push([open, close + 1]);
|
|
725
|
+
open = text.indexOf("{", open + 1);
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
pieces.push(maskGap(open));
|
|
729
|
+
// maskJsonText parses the span again.
|
|
730
|
+
budget.remaining -= spanLength;
|
|
731
|
+
pieces.push(maskSpan(span));
|
|
732
|
+
gapStart = close + 1;
|
|
733
|
+
structure = newGapStructure(gapStart);
|
|
734
|
+
open = text.indexOf("{", close + 1);
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
open = text.indexOf("{", open + 1);
|
|
739
|
+
}
|
|
740
|
+
pieces.push(maskGap(text.length));
|
|
741
|
+
return pieces.join("");
|
|
742
|
+
}
|
|
362
743
|
function createMaskingEngineFromFile(file, configPath) {
|
|
363
744
|
const builtinRules = mergeBuiltinRules(file);
|
|
364
745
|
const rules = [...(file.rules ?? []), ...builtinRules].map(compileRule);
|
|
365
746
|
const maskedErrors = new WeakSet();
|
|
366
|
-
const maskText = (
|
|
747
|
+
const maskText = (source) => {
|
|
748
|
+
// Flagged variable values are replaced first, so the regex rules below see
|
|
749
|
+
// the replacement text. On the text path that replacement is therefore
|
|
750
|
+
// subject to user keyword rules, while structured and JSON paths emit it
|
|
751
|
+
// verbatim.
|
|
752
|
+
const text = maskFlaggedCiVariablesInText(source);
|
|
367
753
|
const matches = [];
|
|
368
754
|
for (const rule of rules) {
|
|
369
755
|
rule.expression.lastIndex = 0;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { CreateWebhookSchema, DeleteWebhookSchema, GetWebhookEventSchema, ListWebhookEventsSchema, ListWebhooksSchema, UpdateWebhookSchema, } from "../schemas.js";
|
|
2
|
+
import { getEffectiveApiUrl, getFetchConfig } from "../server/gitlab-request-context.js";
|
|
3
|
+
import { encodeGitLabPathSegment } from "../utils/url.js";
|
|
4
|
+
import { summarizeWebhookEvents } from "../utils/helpers.js";
|
|
5
|
+
let deps;
|
|
6
|
+
export function configureWebhookTools(next) {
|
|
7
|
+
deps = next;
|
|
8
|
+
}
|
|
9
|
+
function requireDeps() {
|
|
10
|
+
if (!deps) {
|
|
11
|
+
throw new Error("Webhook tools used before configureWebhookTools()");
|
|
12
|
+
}
|
|
13
|
+
return deps;
|
|
14
|
+
}
|
|
15
|
+
const fetch = (input, init) => requireDeps().getFetch()(input, init);
|
|
16
|
+
function handleGitLabError(response) {
|
|
17
|
+
return requireDeps().handleGitLabError(response);
|
|
18
|
+
}
|
|
19
|
+
function getEffectiveProjectId(projectId) {
|
|
20
|
+
return requireDeps().getEffectiveProjectId(projectId);
|
|
21
|
+
}
|
|
22
|
+
function rejectIfStrictProjectScope(toolName) {
|
|
23
|
+
requireDeps().rejectIfStrictProjectScope(toolName);
|
|
24
|
+
}
|
|
25
|
+
// Webhook API helper functions
|
|
26
|
+
/**
|
|
27
|
+
* Build the base URL for webhooks (project or group)
|
|
28
|
+
*/
|
|
29
|
+
function buildWebhookBaseUrl(projectId, groupId) {
|
|
30
|
+
if (projectId) {
|
|
31
|
+
projectId = decodeURIComponent(projectId);
|
|
32
|
+
return `${getEffectiveApiUrl()}/projects/${encodeGitLabPathSegment(getEffectiveProjectId(projectId))}/hooks`;
|
|
33
|
+
}
|
|
34
|
+
rejectIfStrictProjectScope("group webhooks");
|
|
35
|
+
const decodedGroupId = decodeURIComponent(groupId);
|
|
36
|
+
return `${getEffectiveApiUrl()}/groups/${encodeGitLabPathSegment(decodedGroupId)}/hooks`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* List webhooks for a project or group
|
|
40
|
+
*/
|
|
41
|
+
async function listWebhooks(options) {
|
|
42
|
+
const url = new URL(buildWebhookBaseUrl(options.project_id, options.group_id));
|
|
43
|
+
if (options.page)
|
|
44
|
+
url.searchParams.append("page", options.page.toString());
|
|
45
|
+
if (options.per_page)
|
|
46
|
+
url.searchParams.append("per_page", options.per_page.toString());
|
|
47
|
+
const response = await fetch(url.toString(), { ...getFetchConfig() });
|
|
48
|
+
await handleGitLabError(response);
|
|
49
|
+
return (await response.json());
|
|
50
|
+
}
|
|
51
|
+
function buildWebhookMutationBody(options) {
|
|
52
|
+
const body = {};
|
|
53
|
+
for (const [key, value] of Object.entries(options)) {
|
|
54
|
+
if (key === "project_id" || key === "group_id" || key === "hook_id")
|
|
55
|
+
continue;
|
|
56
|
+
if (value !== undefined)
|
|
57
|
+
body[key] = value;
|
|
58
|
+
}
|
|
59
|
+
return body;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create a webhook on a project or group
|
|
63
|
+
*/
|
|
64
|
+
async function createWebhook(options) {
|
|
65
|
+
const url = buildWebhookBaseUrl(options.project_id, options.group_id);
|
|
66
|
+
const response = await fetch(url, {
|
|
67
|
+
...getFetchConfig(),
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: JSON.stringify(buildWebhookMutationBody(options)),
|
|
70
|
+
});
|
|
71
|
+
await handleGitLabError(response);
|
|
72
|
+
return await response.json();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Update a webhook on a project or group
|
|
76
|
+
*/
|
|
77
|
+
async function updateWebhook(options) {
|
|
78
|
+
const url = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}`;
|
|
79
|
+
const response = await fetch(url, {
|
|
80
|
+
...getFetchConfig(),
|
|
81
|
+
method: "PUT",
|
|
82
|
+
body: JSON.stringify(buildWebhookMutationBody(options)),
|
|
83
|
+
});
|
|
84
|
+
await handleGitLabError(response);
|
|
85
|
+
return await response.json();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Delete a webhook from a project or group
|
|
89
|
+
*/
|
|
90
|
+
async function deleteWebhook(options) {
|
|
91
|
+
const url = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}`;
|
|
92
|
+
const response = await fetch(url, {
|
|
93
|
+
...getFetchConfig(),
|
|
94
|
+
method: "DELETE",
|
|
95
|
+
});
|
|
96
|
+
await handleGitLabError(response);
|
|
97
|
+
return { status: "deleted", hook_id: options.hook_id };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Fetch a single page of webhook events
|
|
101
|
+
*/
|
|
102
|
+
async function fetchWebhookEventsPage(baseUrl, page, perPage, status) {
|
|
103
|
+
const url = new URL(baseUrl);
|
|
104
|
+
url.searchParams.set("page", page.toString());
|
|
105
|
+
url.searchParams.set("per_page", perPage.toString());
|
|
106
|
+
if (status !== undefined)
|
|
107
|
+
url.searchParams.append("status", String(status));
|
|
108
|
+
const response = await fetch(url.toString(), { ...getFetchConfig() });
|
|
109
|
+
await handleGitLabError(response);
|
|
110
|
+
return (await response.json());
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* List webhook events for a project or group webhook
|
|
114
|
+
*/
|
|
115
|
+
async function listWebhookEvents(options) {
|
|
116
|
+
const eventsUrl = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}/events`;
|
|
117
|
+
const events = await fetchWebhookEventsPage(eventsUrl, options.page ?? 1, options.per_page ?? 20, options.status);
|
|
118
|
+
return options.summary ? summarizeWebhookEvents(events) : events;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get a specific webhook event by ID (searches up to 500 recent events)
|
|
122
|
+
*/
|
|
123
|
+
async function getWebhookEvent(options) {
|
|
124
|
+
const eventsUrl = `${buildWebhookBaseUrl(options.project_id, options.group_id)}/${options.hook_id}/events`;
|
|
125
|
+
// GitLab enforces max per_page=20 for webhook events
|
|
126
|
+
const perPage = 20;
|
|
127
|
+
if (options.page) {
|
|
128
|
+
// Direct page lookup — single API call
|
|
129
|
+
const events = await fetchWebhookEventsPage(eventsUrl, options.page, perPage);
|
|
130
|
+
const match = events.find(e => e.id === options.event_id);
|
|
131
|
+
return match ?? null;
|
|
132
|
+
}
|
|
133
|
+
// Auto-paginate up to 500 events
|
|
134
|
+
const maxPages = 25;
|
|
135
|
+
for (let page = 1; page <= maxPages; page++) {
|
|
136
|
+
const events = await fetchWebhookEventsPage(eventsUrl, page, perPage);
|
|
137
|
+
const match = events.find(e => e.id === options.event_id);
|
|
138
|
+
if (match)
|
|
139
|
+
return match;
|
|
140
|
+
if (events.length < perPage)
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
export async function handleWebhookToolCall(params) {
|
|
146
|
+
switch (params.name) {
|
|
147
|
+
case "list_webhooks": {
|
|
148
|
+
const args = ListWebhooksSchema.parse(params.arguments);
|
|
149
|
+
const webhooks = await listWebhooks(args);
|
|
150
|
+
return {
|
|
151
|
+
content: [{ type: "text", text: JSON.stringify(webhooks) }],
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
case "create_webhook": {
|
|
155
|
+
const args = CreateWebhookSchema.parse(params.arguments);
|
|
156
|
+
const webhook = await createWebhook(args);
|
|
157
|
+
return {
|
|
158
|
+
content: [{ type: "text", text: JSON.stringify(webhook) }],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
case "update_webhook": {
|
|
162
|
+
const args = UpdateWebhookSchema.parse(params.arguments);
|
|
163
|
+
const webhook = await updateWebhook(args);
|
|
164
|
+
return {
|
|
165
|
+
content: [{ type: "text", text: JSON.stringify(webhook) }],
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
case "delete_webhook": {
|
|
169
|
+
const args = DeleteWebhookSchema.parse(params.arguments);
|
|
170
|
+
const result = await deleteWebhook(args);
|
|
171
|
+
return {
|
|
172
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
case "list_webhook_events": {
|
|
176
|
+
const args = ListWebhookEventsSchema.parse(params.arguments);
|
|
177
|
+
const events = await listWebhookEvents(args);
|
|
178
|
+
return {
|
|
179
|
+
content: [{ type: "text", text: JSON.stringify(events) }],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
case "get_webhook_event": {
|
|
183
|
+
const args = GetWebhookEventSchema.parse(params.arguments);
|
|
184
|
+
const event = await getWebhookEvent(args);
|
|
185
|
+
if (!event) {
|
|
186
|
+
const searchScope = args.page ? `on page ${args.page}` : "in the 500 most recent events";
|
|
187
|
+
return {
|
|
188
|
+
content: [
|
|
189
|
+
{
|
|
190
|
+
type: "text",
|
|
191
|
+
text: JSON.stringify({
|
|
192
|
+
error: `Webhook event ${args.event_id} not found ${searchScope}`,
|
|
193
|
+
}),
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
content: [{ type: "text", text: JSON.stringify(event) }],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
throw new Error(`Unknown tool: ${params.name}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zereight/mcp-gitlab",
|
|
3
|
-
"version": "2.3.29-next.
|
|
3
|
+
"version": "2.3.29-next.250",
|
|
4
4
|
"mcpName": "io.github.zereight/gitlab-mcp",
|
|
5
5
|
"description": "GitLab MCP server for projects, merge requests, issues, pipelines, wiki, releases, and more",
|
|
6
6
|
"keywords": [
|