plugin-git-manager 1.2.27 → 1.3.1

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.
Files changed (138) hide show
  1. package/README.md +151 -108
  2. package/client-v2.d.ts +2 -2
  3. package/client-v2.js +1 -1
  4. package/client.d.ts +3 -3
  5. package/client.js +1 -1
  6. package/dist/client/{326.90c276058682b536.js → 313.d38fc1d40c4589b9.js} +1 -1
  7. package/dist/client/941.1793e67472872992.js +10 -0
  8. package/dist/client/index.js +1 -1
  9. package/dist/client-v2/156.445366026c1cc9a8.js +10 -0
  10. package/dist/client-v2/{326.42b7b53651ffab3a.js → 313.3c867179c3a1e817.js} +1 -1
  11. package/dist/client-v2/434.19c7579c06b9077c.js +10 -0
  12. package/dist/client-v2/928.bba701b5a1ece58e.js +10 -0
  13. package/dist/client-v2/941.b0d55429cd053f32.js +10 -0
  14. package/dist/client-v2/index.js +1 -1
  15. package/dist/externalVersion.js +9 -8
  16. package/dist/locale/en-US.json +396 -297
  17. package/dist/locale/vi-VN.json +384 -292
  18. package/dist/locale/zh-CN.json +150 -51
  19. package/dist/node_modules/simple-git/dist/cjs/index.js +25 -25
  20. package/dist/node_modules/simple-git/package.json +1 -1
  21. package/dist/node_modules/zod/index.cjs +1 -1
  22. package/dist/node_modules/zod/package.json +1 -1
  23. package/dist/server/actions/git-actions.js +90 -36
  24. package/dist/server/actions/gitlab-api.js +4 -2
  25. package/dist/server/actions/poller.js +5 -2
  26. package/dist/server/actions/review-cutover.js +137 -0
  27. package/dist/server/actions/review.js +150 -51
  28. package/dist/server/actions/role-permissions.js +1 -0
  29. package/dist/server/ai-tools.js +8 -9
  30. package/dist/server/collections/gitCodeReviews.js +10 -0
  31. package/dist/server/collections/gitReviewCutover.js +44 -0
  32. package/dist/server/collections/gitReviewFlows.js +3 -0
  33. package/dist/server/collections/gitReviewInbox.js +51 -0
  34. package/dist/server/collections/gitReviewProjects.js +43 -0
  35. package/dist/server/collections/gitReviewRequests.js +56 -0
  36. package/dist/server/index.js +8 -2
  37. package/dist/server/plugin.js +57 -20
  38. package/dist/server/poller.js +95 -74
  39. package/dist/server/repository-access.js +23 -5
  40. package/dist/server/request-method-policy.js +76 -0
  41. package/dist/server/review-control-access.js +79 -0
  42. package/dist/server/services/legacy-post-service.js +412 -0
  43. package/dist/server/services/registry-content-service.js +4 -3
  44. package/dist/server/services/review-service.js +1451 -0
  45. package/dist/server/services/review-source.js +325 -0
  46. package/dist/server/utils/get-repo-account.js +3 -2
  47. package/dist/server/utils/url-security.js +24 -0
  48. package/package.json +61 -43
  49. package/server.d.ts +2 -2
  50. package/server.js +1 -1
  51. package/src/client/__tests__/CommitHistory.test.tsx +79 -0
  52. package/src/client/__tests__/ReviewHistory.test.tsx +126 -0
  53. package/src/client/__tests__/RunReviewButton.test.tsx +84 -0
  54. package/src/client/__tests__/workflowRoutes.test.ts +65 -0
  55. package/src/client/ai-context.tsx +114 -114
  56. package/src/client/components/AIEmployeeSelect.tsx +63 -63
  57. package/src/client/components/CommitHistory.tsx +429 -374
  58. package/src/client/components/FileExplorer.tsx +386 -386
  59. package/src/client/components/GitAccounts.tsx +179 -179
  60. package/src/client/components/GitManagerSettings.tsx +160 -160
  61. package/src/client/components/GitOperations.tsx +220 -220
  62. package/src/client/components/LLMModelSelect.tsx +63 -63
  63. package/src/client/components/LLMServiceSelect.tsx +58 -58
  64. package/src/client/components/MarkdownView.tsx +80 -80
  65. package/src/client/components/MergeRequests.tsx +790 -790
  66. package/src/client/components/PollingStatus.tsx +225 -225
  67. package/src/client/components/RepositoryConfig.tsx +335 -335
  68. package/src/client/components/RepositoryPermissions.tsx +123 -123
  69. package/src/client/components/ReviewFlows.tsx +352 -303
  70. package/src/client/components/ReviewHistory.tsx +796 -566
  71. package/src/client/components/RunReviewButton.tsx +57 -173
  72. package/src/client/context/GitManagerContext.tsx +92 -92
  73. package/src/client/index.tsx +91 -82
  74. package/src/client/locale.ts +9 -9
  75. package/src/client/promptTemplates.ts +31 -31
  76. package/src/client/workflowRoutes.ts +42 -0
  77. package/src/client-v2/ai-context.tsx +99 -0
  78. package/src/client-v2/components/SubtreeSplits.tsx +547 -547
  79. package/src/client-v2/index.tsx +1 -1
  80. package/src/client-v2/locale.ts +8 -8
  81. package/src/client-v2/plugin.tsx +67 -39
  82. package/src/index.ts +2 -2
  83. package/src/locale/en-US.json +396 -297
  84. package/src/locale/vi-VN.json +384 -292
  85. package/src/locale/zh-CN.json +150 -51
  86. package/src/server/__tests__/git-actions.test.ts +24 -24
  87. package/src/server/__tests__/git-security.test.ts +170 -0
  88. package/src/server/__tests__/plugin-url-security.test.ts +17 -17
  89. package/src/server/__tests__/poller-lifecycle.test.ts +71 -0
  90. package/src/server/__tests__/poller.test.ts +37 -37
  91. package/src/server/__tests__/repository-access.test.ts +760 -752
  92. package/src/server/__tests__/review-service.integration.test.ts +1263 -0
  93. package/src/server/__tests__/review.test.ts +76 -137
  94. package/src/server/__tests__/role-permissions.test.ts +147 -147
  95. package/src/server/__tests__/smoke.test.ts +139 -17
  96. package/src/server/__tests__/subtree.test.ts +85 -85
  97. package/src/server/actions/git-actions.ts +540 -480
  98. package/src/server/actions/gitlab-api.ts +422 -411
  99. package/src/server/actions/poller.ts +39 -27
  100. package/src/server/actions/review-cutover.ts +121 -0
  101. package/src/server/actions/review.ts +1498 -1369
  102. package/src/server/actions/role-permissions.ts +331 -321
  103. package/src/server/actions/subtree.ts +508 -508
  104. package/src/server/ai-tools.ts +334 -319
  105. package/src/server/collections/gitAccounts.ts +56 -56
  106. package/src/server/collections/gitCodeReviews.ts +218 -199
  107. package/src/server/collections/gitRepositories.ts +87 -87
  108. package/src/server/collections/gitReviewCutover.ts +23 -0
  109. package/src/server/collections/gitReviewFlows.ts +120 -108
  110. package/src/server/collections/gitReviewInbox.ts +30 -0
  111. package/src/server/collections/gitReviewProjects.ts +22 -0
  112. package/src/server/collections/gitReviewRequests.ts +35 -0
  113. package/src/server/collections/gitSubtreeConfigs.ts +38 -38
  114. package/src/server/collections/gitSubtreeRuns.ts +30 -30
  115. package/src/server/index.ts +14 -2
  116. package/src/server/migrations/20260508000000-add-auto-review-flow-id.ts +29 -29
  117. package/src/server/migrations/20260712000000-add-git-accounts.ts +198 -198
  118. package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +81 -81
  119. package/src/server/plugin.ts +409 -361
  120. package/src/server/poller.ts +365 -352
  121. package/src/server/repository-access.ts +803 -773
  122. package/src/server/request-method-policy.ts +56 -0
  123. package/src/server/review-control-access.ts +71 -0
  124. package/src/server/services/__tests__/registry-content-service.test.ts +359 -345
  125. package/src/server/services/__tests__/review-source.test.ts +226 -0
  126. package/src/server/services/legacy-post-service.ts +550 -0
  127. package/src/server/services/registry-content-service.ts +495 -484
  128. package/src/server/services/review-service.ts +1899 -0
  129. package/src/server/services/review-source.ts +359 -0
  130. package/src/server/utils/__tests__/redact.test.ts +56 -56
  131. package/src/server/utils/__tests__/url-security.test.ts +85 -85
  132. package/src/server/utils/get-repo-account.ts +51 -41
  133. package/src/server/utils/gitlab-url.ts +38 -38
  134. package/src/server/utils/redact.ts +87 -87
  135. package/src/server/utils/url-security.ts +128 -95
  136. package/dist/client/900.acf7d1fa22f95e71.js +0 -10
  137. package/dist/client-v2/434.7a27051db18526c0.js +0 -10
  138. package/dist/client-v2/900.f32b9dc85249fc0a.js +0 -10
@@ -1,319 +1,334 @@
1
- import { z } from 'zod';
2
- import type { Application } from '@nocobase/server';
3
- import * as gitlabApi from './actions/gitlab-api';
4
- import * as gitActions from './actions/git-actions';
5
-
6
- /**
7
- * Register AI tools that allow an AI employee to inspect git repositories,
8
- * merge requests, commits, and file content during a review session.
9
- *
10
- * Each tool is invoked by the AI runtime; the `invoke` callback constructs a
11
- * synthetic `ctx.action.params` payload then delegates to the existing
12
- * resource action implementation.
13
- */
14
- export function registerGitReviewAiTools(app: Application) {
15
- const aiManager = (app as any).aiManager;
16
- if (!aiManager?.toolsManager?.registerTools) {
17
- app.log?.warn?.('plugin-git-manager: AIManager.toolsManager not available; skipping AI tool registration');
18
- return;
19
- }
20
-
21
- /**
22
- * Enforce that the AI session has an authenticated user and that this user
23
- * is permitted to invoke the underlying resource:action. Without this check,
24
- * a prompt-injected AI could pass an arbitrary `repositoryId` to the tools
25
- * and read any repository's MRs / commits / file content regardless of the
26
- * caller's ACL.
27
- */
28
- const enforceAcl = (ctx: any, resource: string, action: string, params: any) => {
29
- if (ctx.isBackgroundReview) {
30
- if (params.repositoryId && params.repositoryId !== ctx.reviewTargetRepositoryId) {
31
- const err: any = new Error('Permission denied: AI cannot access other repositories');
32
- err.status = 403;
33
- throw err;
34
- }
35
- return; // Authorized for this specific repository in background context
36
- }
37
- const user = ctx?.state?.currentUser;
38
- if (!user?.id) {
39
- const err: any = new Error('AI tool requires an authenticated user context');
40
- err.status = 401;
41
- throw err;
42
- }
43
- const acl = ctx.app?.acl;
44
- if (!acl?.can) return; // ACL not available — fail-open is acceptable for legacy contexts
45
- const role = ctx?.state?.currentRole || (Array.isArray(user.roles) && user.roles[0]?.name) || null;
46
- if (!role) {
47
- const err: any = new Error('AI tool requires a resolvable role on the current user');
48
- err.status = 403;
49
- throw err;
50
- }
51
- const allowed = acl.can({ role, resource, action });
52
- if (!allowed) {
53
- const err: any = new Error(`Permission denied: ${resource}:${action}`);
54
- err.status = 403;
55
- throw err;
56
- }
57
- };
58
-
59
- const sanitizeForDb = (obj: any): any => {
60
- if (typeof obj === 'string') {
61
- // Remove null bytes \u0000 which crash Postgres jsonb/text fields
62
- // eslint-disable-next-line no-control-regex
63
- return obj.replace(/\u0000/g, '');
64
- }
65
- if (Array.isArray(obj)) {
66
- return obj.map(sanitizeForDb);
67
- }
68
- if (obj && typeof obj === 'object') {
69
- const sanitized: any = {};
70
- for (const [key, value] of Object.entries(obj)) {
71
- sanitized[key] = sanitizeForDb(value);
72
- }
73
- return sanitized;
74
- }
75
- return obj;
76
- };
77
-
78
- const runResourceAction = async (
79
- ctx: any,
80
- handler: (ctx: any, next: () => Promise<void>) => Promise<void>,
81
- params: Record<string, any>,
82
- gate: { resource: string; action: string },
83
- ) => {
84
- enforceAcl(ctx, gate.resource, gate.action, params);
85
- const synthCtx: any = {
86
- ...ctx,
87
- app: ctx.app,
88
- db: ctx.db,
89
- action: { params },
90
- request: { ...(ctx.request || {}), body: ctx.request?.body || {} },
91
- throw: (status: number, message: string) => {
92
- const err: any = new Error(message);
93
- err.status = status;
94
- throw err;
95
- },
96
- };
97
- await handler(synthCtx, async () => undefined);
98
- return sanitizeForDb(synthCtx.body);
99
- };
100
-
101
- aiManager.toolsManager.registerTools([
102
- {
103
- scope: 'GENERAL',
104
- execution: 'backend',
105
- defaultPermission: 'ALLOW',
106
- silence: false,
107
- introduction: { title: 'Git: Get merge request' },
108
- definition: {
109
- name: 'git_get_merge_request',
110
- description:
111
- 'Get full details of a GitLab merge request including its file-level diffs. Use this to inspect the code changes that need review.',
112
- schema: z.object({
113
- repositoryId: z.number().describe('The numeric ID of the gitRepositories record'),
114
- mrIid: z.number().describe('The GitLab merge request internal ID (iid)'),
115
- }),
116
- },
117
- invoke: async (ctx: any, args: any) => {
118
- const body = await runResourceAction(ctx, gitlabApi.mergeRequestDetail, args, {
119
- resource: 'gitManager',
120
- action: 'mergeRequestDetail',
121
- });
122
- return body?.data ?? body;
123
- },
124
- },
125
- {
126
- scope: 'GENERAL',
127
- execution: 'backend',
128
- defaultPermission: 'ALLOW',
129
- silence: false,
130
- introduction: { title: 'Git: List merge requests' },
131
- definition: {
132
- name: 'git_list_merge_requests',
133
- description: 'List GitLab merge requests for a repository, with optional state and search filters.',
134
- schema: z.object({
135
- repositoryId: z.number(),
136
- state: z.enum(['opened', 'closed', 'merged', 'all']).optional(),
137
- search: z.string().optional(),
138
- page: z.number().optional(),
139
- perPage: z.number().optional(),
140
- }),
141
- },
142
- invoke: async (ctx: any, args: any) => {
143
- const body = await runResourceAction(ctx, gitlabApi.mergeRequests, args, {
144
- resource: 'gitManager',
145
- action: 'mergeRequests',
146
- });
147
- return body?.data ?? body;
148
- },
149
- },
150
- {
151
- scope: 'GENERAL',
152
- execution: 'backend',
153
- defaultPermission: 'ALLOW',
154
- silence: false,
155
- introduction: { title: 'Git: Get merge request notes' },
156
- definition: {
157
- name: 'git_get_merge_request_notes',
158
- description: 'Get existing comments / notes on a merge request to avoid duplicating feedback.',
159
- schema: z.object({
160
- repositoryId: z.number(),
161
- mrIid: z.number(),
162
- }),
163
- },
164
- invoke: async (ctx: any, args: any) => {
165
- const body = await runResourceAction(ctx, gitlabApi.mergeRequestNotes, args, {
166
- resource: 'gitManager',
167
- action: 'mergeRequestNotes',
168
- });
169
- return body?.data ?? body;
170
- },
171
- },
172
- {
173
- scope: 'GENERAL',
174
- execution: 'backend',
175
- defaultPermission: 'ALLOW',
176
- silence: false,
177
- introduction: { title: 'Git: Get commit detail' },
178
- definition: {
179
- name: 'git_get_commit',
180
- description: 'Get details and diff of a specific commit in a cloned repository.',
181
- schema: z.object({
182
- repositoryId: z.number(),
183
- commitHash: z.string().describe('Full or short commit SHA'),
184
- }),
185
- },
186
- invoke: async (ctx: any, args: any) => {
187
- const body = await runResourceAction(ctx, gitActions.commitDetail, args, {
188
- resource: 'gitManager',
189
- action: 'commitDetail',
190
- });
191
- return body?.data ?? body;
192
- },
193
- },
194
- {
195
- scope: 'GENERAL',
196
- execution: 'backend',
197
- defaultPermission: 'ALLOW',
198
- silence: false,
199
- introduction: { title: 'Git: Get diff' },
200
- definition: {
201
- name: 'git_get_diff',
202
- description:
203
- 'Get the diff between two refs (branches/commits) in a cloned repository, or the diff of a single commit when only commitHash is given.',
204
- schema: z.object({
205
- repositoryId: z.number(),
206
- commitHash: z.string().optional(),
207
- compareHash: z.string().optional(),
208
- file: z.string().optional().describe('Optional file path to scope the diff'),
209
- }),
210
- },
211
- invoke: async (ctx: any, args: any) => {
212
- const body = await runResourceAction(ctx, gitActions.diff, args, {
213
- resource: 'gitManager',
214
- action: 'diff',
215
- });
216
- return body?.data ?? body;
217
- },
218
- },
219
- {
220
- scope: 'GENERAL',
221
- execution: 'backend',
222
- defaultPermission: 'ALLOW',
223
- silence: false,
224
- introduction: { title: 'Git: Get file content' },
225
- definition: {
226
- name: 'git_get_file_content',
227
- description:
228
- 'Read the content of a single file in a cloned repository at a given ref. Useful when reviewing changes in surrounding context.',
229
- schema: z.object({
230
- repositoryId: z.number(),
231
- filePath: z.string(),
232
- ref: z.string().optional(),
233
- }),
234
- },
235
- invoke: async (ctx: any, args: any) => {
236
- const body = await runResourceAction(ctx, gitActions.fileContent, args, {
237
- resource: 'gitManager',
238
- action: 'fileContent',
239
- });
240
- return body?.data ?? body;
241
- },
242
- },
243
- {
244
- scope: 'GENERAL',
245
- execution: 'backend',
246
- defaultPermission: 'ALLOW',
247
- silence: false,
248
- introduction: { title: 'Git: List files' },
249
- definition: {
250
- name: 'git_list_files',
251
- description:
252
- 'List files and folders of a cloned repository at a given ref. Set recursive=true with a treePath to enumerate every file under a folder, e.g. when reviewing the full code of a folder.',
253
- schema: z.object({
254
- repositoryId: z.number(),
255
- treePath: z.string().optional().describe('Folder path relative to repo root; omit for the root'),
256
- ref: z.string().optional().describe('Branch, tag or commit SHA; defaults to HEAD'),
257
- recursive: z.boolean().optional().describe('true to list all files under treePath recursively'),
258
- }),
259
- },
260
- invoke: async (ctx: any, args: any) => {
261
- const body = await runResourceAction(ctx, gitActions.fileTree, args, {
262
- resource: 'gitManager',
263
- action: 'fileTree',
264
- });
265
- const files = (body?.data ?? body ?? []) as unknown[];
266
- const list = Array.isArray(files) ? files : [];
267
- const MAX_ENTRIES = 500;
268
- if (list.length > MAX_ENTRIES) {
269
- return { files: list.slice(0, MAX_ENTRIES), totalCount: list.length, truncated: true };
270
- }
271
- return { files: list, totalCount: list.length, truncated: false };
272
- },
273
- },
274
- {
275
- scope: 'GENERAL',
276
- execution: 'backend',
277
- defaultPermission: 'ALLOW',
278
- silence: false,
279
- introduction: { title: 'Git: List branches' },
280
- definition: {
281
- name: 'git_list_branches',
282
- description: 'List branches available in a cloned repository.',
283
- schema: z.object({ repositoryId: z.number() }),
284
- },
285
- invoke: async (ctx: any, args: any) => {
286
- const body = await runResourceAction(ctx, gitActions.branches, args, {
287
- resource: 'gitManager',
288
- action: 'branches',
289
- });
290
- return body?.data ?? body;
291
- },
292
- },
293
- {
294
- scope: 'GENERAL',
295
- execution: 'backend',
296
- defaultPermission: 'ALLOW',
297
- silence: false,
298
- introduction: { title: 'Git: List recent commits' },
299
- definition: {
300
- name: 'git_list_commits',
301
- description: 'List recent commits in a cloned repository, optionally scoped to a single file.',
302
- schema: z.object({
303
- repositoryId: z.number(),
304
- maxCount: z.number().optional(),
305
- file: z.string().optional(),
306
- }),
307
- },
308
- invoke: async (ctx: any, args: any) => {
309
- const body = await runResourceAction(ctx, gitActions.log, args, {
310
- resource: 'gitManager',
311
- action: 'log',
312
- });
313
- return body?.data ?? body;
314
- },
315
- },
316
- ]);
317
-
318
- app.log?.info?.('plugin-git-manager: registered git_* AI tools for code review');
319
- }
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { z } from 'zod';
11
+ import type { Application } from '@nocobase/server';
12
+ import * as gitlabApi from './actions/gitlab-api';
13
+ import * as gitActions from './actions/git-actions';
14
+ import { hasRepositoryAccess } from './repository-access';
15
+
16
+ /**
17
+ * Register AI tools that allow an AI employee to inspect git repositories,
18
+ * merge requests, commits, and file content during a review session.
19
+ *
20
+ * Each tool is invoked by the AI runtime; the `invoke` callback constructs a
21
+ * synthetic `ctx.action.params` payload then delegates to the existing
22
+ * resource action implementation.
23
+ */
24
+ export function registerGitReviewAiTools(app: Application) {
25
+ const aiManager = (app as any).aiManager;
26
+ if (!aiManager?.toolsManager?.registerTools) {
27
+ app.log?.warn?.('plugin-git-manager: AIManager.toolsManager not available; skipping AI tool registration');
28
+ return;
29
+ }
30
+
31
+ /**
32
+ * Enforce that the AI session has an authenticated user and that this user
33
+ * is permitted to invoke the underlying resource:action. Without this check,
34
+ * a prompt-injected AI could pass an arbitrary `repositoryId` to the tools
35
+ * and read any repository's MRs / commits / file content regardless of the
36
+ * caller's ACL.
37
+ */
38
+ const enforceAcl = async (ctx: any, resource: string, action: string, params: any) => {
39
+ if (ctx.isBackgroundReview) {
40
+ if (String(params.repositoryId) !== String(ctx.reviewTargetRepositoryId)) {
41
+ const err: any = new Error('Permission denied: AI cannot access other repositories');
42
+ err.status = 403;
43
+ throw err;
44
+ }
45
+ return; // Authorized for this specific repository in background context
46
+ }
47
+ const user = ctx?.state?.currentUser;
48
+ if (!user?.id) {
49
+ const err: any = new Error('AI tool requires an authenticated user context');
50
+ err.status = 401;
51
+ throw err;
52
+ }
53
+ const acl = ctx.app?.acl;
54
+ const roles = Array.isArray(ctx?.state?.currentRoles)
55
+ ? ctx.state.currentRoles.filter((role: unknown): role is string => typeof role === 'string')
56
+ : typeof ctx?.state?.currentRole === 'string'
57
+ ? [ctx.state.currentRole]
58
+ : [];
59
+ if (!roles.length || !acl?.can) {
60
+ const err: any = new Error('AI tool requires a resolvable role on the current user');
61
+ err.status = 403;
62
+ throw err;
63
+ }
64
+ const allowed =
65
+ resource === 'gitManager' &&
66
+ (await hasRepositoryAccess({ acl, roles, repositoryId: params.repositoryId, action }));
67
+ if (!allowed) {
68
+ const err: any = new Error(`Permission denied: ${resource}:${action}`);
69
+ err.status = 403;
70
+ throw err;
71
+ }
72
+ };
73
+
74
+ const sanitizeForDb = (obj: any): any => {
75
+ if (typeof obj === 'string') {
76
+ // Remove null bytes \u0000 which crash Postgres jsonb/text fields
77
+ // eslint-disable-next-line no-control-regex
78
+ return obj.replace(/\u0000/g, '');
79
+ }
80
+ if (Array.isArray(obj)) {
81
+ return obj.map(sanitizeForDb);
82
+ }
83
+ if (obj && typeof obj === 'object') {
84
+ const sanitized: any = {};
85
+ for (const [key, value] of Object.entries(obj)) {
86
+ sanitized[key] = sanitizeForDb(value);
87
+ }
88
+ return sanitized;
89
+ }
90
+ return obj;
91
+ };
92
+
93
+ const runResourceAction = async (
94
+ ctx: any,
95
+ handler: (ctx: any, next: () => Promise<void>) => Promise<void>,
96
+ params: Record<string, any>,
97
+ gate: { resource: string; action: string },
98
+ ) => {
99
+ await enforceAcl(ctx, gate.resource, gate.action, params);
100
+ const synthCtx: any = {
101
+ ...ctx,
102
+ app: ctx.app,
103
+ db: ctx.db,
104
+ action: { params },
105
+ request: { ...(ctx.request || {}), body: {} },
106
+ throw: (status: number, message: string) => {
107
+ const err: any = new Error(message);
108
+ err.status = status;
109
+ throw err;
110
+ },
111
+ };
112
+ await handler(synthCtx, async () => undefined);
113
+ return sanitizeForDb(synthCtx.body);
114
+ };
115
+
116
+ aiManager.toolsManager.registerTools([
117
+ {
118
+ scope: 'GENERAL',
119
+ execution: 'backend',
120
+ defaultPermission: 'ALLOW',
121
+ silence: false,
122
+ introduction: { title: 'Git: Get merge request' },
123
+ definition: {
124
+ name: 'git_get_merge_request',
125
+ description:
126
+ 'Get full details of a GitLab merge request including its file-level diffs. Use this to inspect the code changes that need review.',
127
+ schema: z.object({
128
+ repositoryId: z.number().describe('The numeric ID of the gitRepositories record'),
129
+ mrIid: z.number().describe('The GitLab merge request internal ID (iid)'),
130
+ }),
131
+ },
132
+ invoke: async (ctx: any, args: any) => {
133
+ const body = await runResourceAction(ctx, gitlabApi.mergeRequestDetail, args, {
134
+ resource: 'gitManager',
135
+ action: 'mergeRequestDetail',
136
+ });
137
+ return body?.data ?? body;
138
+ },
139
+ },
140
+ {
141
+ scope: 'GENERAL',
142
+ execution: 'backend',
143
+ defaultPermission: 'ALLOW',
144
+ silence: false,
145
+ introduction: { title: 'Git: List merge requests' },
146
+ definition: {
147
+ name: 'git_list_merge_requests',
148
+ description: 'List GitLab merge requests for a repository, with optional state and search filters.',
149
+ schema: z.object({
150
+ repositoryId: z.number(),
151
+ state: z.enum(['opened', 'closed', 'merged', 'all']).optional(),
152
+ search: z.string().optional(),
153
+ page: z.number().optional(),
154
+ perPage: z.number().optional(),
155
+ }),
156
+ },
157
+ invoke: async (ctx: any, args: any) => {
158
+ const body = await runResourceAction(ctx, gitlabApi.mergeRequests, args, {
159
+ resource: 'gitManager',
160
+ action: 'mergeRequests',
161
+ });
162
+ return body?.data ?? body;
163
+ },
164
+ },
165
+ {
166
+ scope: 'GENERAL',
167
+ execution: 'backend',
168
+ defaultPermission: 'ALLOW',
169
+ silence: false,
170
+ introduction: { title: 'Git: Get merge request notes' },
171
+ definition: {
172
+ name: 'git_get_merge_request_notes',
173
+ description: 'Get existing comments / notes on a merge request to avoid duplicating feedback.',
174
+ schema: z.object({
175
+ repositoryId: z.number(),
176
+ mrIid: z.number(),
177
+ }),
178
+ },
179
+ invoke: async (ctx: any, args: any) => {
180
+ const body = await runResourceAction(ctx, gitlabApi.mergeRequestNotes, args, {
181
+ resource: 'gitManager',
182
+ action: 'mergeRequestNotes',
183
+ });
184
+ return body?.data ?? body;
185
+ },
186
+ },
187
+ {
188
+ scope: 'GENERAL',
189
+ execution: 'backend',
190
+ defaultPermission: 'ALLOW',
191
+ silence: false,
192
+ introduction: { title: 'Git: Get commit detail' },
193
+ definition: {
194
+ name: 'git_get_commit',
195
+ description: 'Get details and diff of a specific commit in a cloned repository.',
196
+ schema: z.object({
197
+ repositoryId: z.number(),
198
+ commitHash: z.string().describe('Full or short commit SHA'),
199
+ }),
200
+ },
201
+ invoke: async (ctx: any, args: any) => {
202
+ const body = await runResourceAction(ctx, gitActions.commitDetail, args, {
203
+ resource: 'gitManager',
204
+ action: 'commitDetail',
205
+ });
206
+ return body?.data ?? body;
207
+ },
208
+ },
209
+ {
210
+ scope: 'GENERAL',
211
+ execution: 'backend',
212
+ defaultPermission: 'ALLOW',
213
+ silence: false,
214
+ introduction: { title: 'Git: Get diff' },
215
+ definition: {
216
+ name: 'git_get_diff',
217
+ description:
218
+ 'Get the diff between two refs (branches/commits) in a cloned repository, or the diff of a single commit when only commitHash is given.',
219
+ schema: z.object({
220
+ repositoryId: z.number(),
221
+ commitHash: z.string().optional(),
222
+ compareHash: z.string().optional(),
223
+ file: z.string().optional().describe('Optional file path to scope the diff'),
224
+ }),
225
+ },
226
+ invoke: async (ctx: any, args: any) => {
227
+ const body = await runResourceAction(ctx, gitActions.diff, args, {
228
+ resource: 'gitManager',
229
+ action: 'diff',
230
+ });
231
+ return body?.data ?? body;
232
+ },
233
+ },
234
+ {
235
+ scope: 'GENERAL',
236
+ execution: 'backend',
237
+ defaultPermission: 'ALLOW',
238
+ silence: false,
239
+ introduction: { title: 'Git: Get file content' },
240
+ definition: {
241
+ name: 'git_get_file_content',
242
+ description:
243
+ 'Read the content of a single file in a cloned repository at a given ref. Useful when reviewing changes in surrounding context.',
244
+ schema: z.object({
245
+ repositoryId: z.number(),
246
+ filePath: z.string(),
247
+ ref: z.string().optional(),
248
+ }),
249
+ },
250
+ invoke: async (ctx: any, args: any) => {
251
+ const body = await runResourceAction(ctx, gitActions.fileContent, args, {
252
+ resource: 'gitManager',
253
+ action: 'fileContent',
254
+ });
255
+ return body?.data ?? body;
256
+ },
257
+ },
258
+ {
259
+ scope: 'GENERAL',
260
+ execution: 'backend',
261
+ defaultPermission: 'ALLOW',
262
+ silence: false,
263
+ introduction: { title: 'Git: List files' },
264
+ definition: {
265
+ name: 'git_list_files',
266
+ description:
267
+ 'List files and folders of a cloned repository at a given ref. Set recursive=true with a treePath to enumerate every file under a folder, e.g. when reviewing the full code of a folder.',
268
+ schema: z.object({
269
+ repositoryId: z.number(),
270
+ treePath: z.string().optional().describe('Folder path relative to repo root; omit for the root'),
271
+ ref: z.string().optional().describe('Branch, tag or commit SHA; defaults to HEAD'),
272
+ recursive: z.boolean().optional().describe('true to list all files under treePath recursively'),
273
+ }),
274
+ },
275
+ invoke: async (ctx: any, args: any) => {
276
+ const body = await runResourceAction(ctx, gitActions.fileTree, args, {
277
+ resource: 'gitManager',
278
+ action: 'fileTree',
279
+ });
280
+ const files = (body?.data ?? body ?? []) as unknown[];
281
+ const list = Array.isArray(files) ? files : [];
282
+ const MAX_ENTRIES = 500;
283
+ if (list.length > MAX_ENTRIES) {
284
+ return { files: list.slice(0, MAX_ENTRIES), totalCount: list.length, truncated: true };
285
+ }
286
+ return { files: list, totalCount: list.length, truncated: false };
287
+ },
288
+ },
289
+ {
290
+ scope: 'GENERAL',
291
+ execution: 'backend',
292
+ defaultPermission: 'ALLOW',
293
+ silence: false,
294
+ introduction: { title: 'Git: List branches' },
295
+ definition: {
296
+ name: 'git_list_branches',
297
+ description: 'List branches available in a cloned repository.',
298
+ schema: z.object({ repositoryId: z.number() }),
299
+ },
300
+ invoke: async (ctx: any, args: any) => {
301
+ const body = await runResourceAction(ctx, gitActions.branches, args, {
302
+ resource: 'gitManager',
303
+ action: 'branches',
304
+ });
305
+ return body?.data ?? body;
306
+ },
307
+ },
308
+ {
309
+ scope: 'GENERAL',
310
+ execution: 'backend',
311
+ defaultPermission: 'ALLOW',
312
+ silence: false,
313
+ introduction: { title: 'Git: List recent commits' },
314
+ definition: {
315
+ name: 'git_list_commits',
316
+ description: 'List recent commits in a cloned repository, optionally scoped to a single file.',
317
+ schema: z.object({
318
+ repositoryId: z.number(),
319
+ maxCount: z.number().optional(),
320
+ file: z.string().optional(),
321
+ }),
322
+ },
323
+ invoke: async (ctx: any, args: any) => {
324
+ const body = await runResourceAction(ctx, gitActions.log, args, {
325
+ resource: 'gitManager',
326
+ action: 'log',
327
+ });
328
+ return body?.data ?? body;
329
+ },
330
+ },
331
+ ]);
332
+
333
+ app.log?.info?.('plugin-git-manager: registered git_* AI tools for code review');
334
+ }