plugin-git-manager 1.2.27 → 1.2.28

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 (108) hide show
  1. package/README.md +133 -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/900.a48beac7e2ebee7d.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/900.e6155002a9c97e7f.js +10 -0
  13. package/dist/client-v2/928.bba701b5a1ece58e.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 +307 -297
  17. package/dist/locale/vi-VN.json +295 -292
  18. package/dist/locale/zh-CN.json +61 -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.js +20 -10
  27. package/dist/server/plugin.js +6 -4
  28. package/dist/server/poller.js +46 -45
  29. package/dist/server/request-method-policy.js +75 -0
  30. package/dist/server/services/registry-content-service.js +4 -3
  31. package/dist/server/utils/get-repo-account.js +3 -2
  32. package/dist/server/utils/url-security.js +24 -0
  33. package/package.json +61 -43
  34. package/server.d.ts +2 -2
  35. package/server.js +1 -1
  36. package/src/client/__tests__/RunReviewButton.test.tsx +73 -0
  37. package/src/client/ai-context.tsx +114 -114
  38. package/src/client/components/AIEmployeeSelect.tsx +63 -63
  39. package/src/client/components/CommitHistory.tsx +374 -374
  40. package/src/client/components/FileExplorer.tsx +386 -386
  41. package/src/client/components/GitAccounts.tsx +179 -179
  42. package/src/client/components/GitManagerSettings.tsx +160 -160
  43. package/src/client/components/GitOperations.tsx +220 -220
  44. package/src/client/components/LLMModelSelect.tsx +63 -63
  45. package/src/client/components/LLMServiceSelect.tsx +58 -58
  46. package/src/client/components/MarkdownView.tsx +80 -80
  47. package/src/client/components/MergeRequests.tsx +790 -790
  48. package/src/client/components/PollingStatus.tsx +225 -225
  49. package/src/client/components/RepositoryConfig.tsx +335 -335
  50. package/src/client/components/RepositoryPermissions.tsx +123 -123
  51. package/src/client/components/ReviewFlows.tsx +303 -303
  52. package/src/client/components/ReviewHistory.tsx +566 -566
  53. package/src/client/components/RunReviewButton.tsx +63 -55
  54. package/src/client/context/GitManagerContext.tsx +92 -92
  55. package/src/client/index.tsx +91 -82
  56. package/src/client/locale.ts +9 -9
  57. package/src/client/promptTemplates.ts +31 -31
  58. package/src/client-v2/ai-context.tsx +99 -0
  59. package/src/client-v2/components/SubtreeSplits.tsx +547 -547
  60. package/src/client-v2/index.tsx +1 -1
  61. package/src/client-v2/locale.ts +8 -8
  62. package/src/client-v2/plugin.tsx +67 -39
  63. package/src/index.ts +2 -2
  64. package/src/locale/en-US.json +307 -297
  65. package/src/locale/vi-VN.json +295 -292
  66. package/src/locale/zh-CN.json +61 -51
  67. package/src/server/__tests__/git-actions.test.ts +24 -24
  68. package/src/server/__tests__/git-security.test.ts +170 -0
  69. package/src/server/__tests__/plugin-url-security.test.ts +17 -17
  70. package/src/server/__tests__/poller-lifecycle.test.ts +71 -0
  71. package/src/server/__tests__/poller.test.ts +37 -37
  72. package/src/server/__tests__/repository-access.test.ts +752 -752
  73. package/src/server/__tests__/review.test.ts +137 -137
  74. package/src/server/__tests__/role-permissions.test.ts +147 -147
  75. package/src/server/__tests__/smoke.test.ts +139 -17
  76. package/src/server/__tests__/subtree.test.ts +85 -85
  77. package/src/server/actions/git-actions.ts +540 -480
  78. package/src/server/actions/gitlab-api.ts +422 -411
  79. package/src/server/actions/poller.ts +39 -27
  80. package/src/server/actions/review.ts +1388 -1369
  81. package/src/server/actions/role-permissions.ts +321 -321
  82. package/src/server/actions/subtree.ts +508 -508
  83. package/src/server/ai-tools.ts +319 -319
  84. package/src/server/collections/gitAccounts.ts +56 -56
  85. package/src/server/collections/gitCodeReviews.ts +199 -199
  86. package/src/server/collections/gitRepositories.ts +87 -87
  87. package/src/server/collections/gitReviewFlows.ts +108 -108
  88. package/src/server/collections/gitSubtreeConfigs.ts +38 -38
  89. package/src/server/collections/gitSubtreeRuns.ts +30 -30
  90. package/src/server/index.ts +2 -2
  91. package/src/server/migrations/20260508000000-add-auto-review-flow-id.ts +29 -29
  92. package/src/server/migrations/20260712000000-add-git-accounts.ts +198 -198
  93. package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +81 -81
  94. package/src/server/plugin.ts +372 -361
  95. package/src/server/poller.ts +351 -352
  96. package/src/server/repository-access.ts +773 -773
  97. package/src/server/request-method-policy.ts +55 -0
  98. package/src/server/services/__tests__/registry-content-service.test.ts +359 -345
  99. package/src/server/services/registry-content-service.ts +495 -484
  100. package/src/server/utils/__tests__/redact.test.ts +56 -56
  101. package/src/server/utils/__tests__/url-security.test.ts +85 -85
  102. package/src/server/utils/get-repo-account.ts +51 -41
  103. package/src/server/utils/gitlab-url.ts +38 -38
  104. package/src/server/utils/redact.ts +87 -87
  105. package/src/server/utils/url-security.ts +128 -95
  106. package/dist/client/900.acf7d1fa22f95e71.js +0 -10
  107. package/dist/client-v2/434.7a27051db18526c0.js +0 -10
  108. package/dist/client-v2/900.f32b9dc85249fc0a.js +0 -10
@@ -1,345 +1,359 @@
1
- import { execFileSync } from 'child_process';
2
- import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'fs';
3
- import { tmpdir } from 'os';
4
- import { join } from 'path';
5
-
6
- import type { Database } from '@nocobase/database';
7
-
8
- import {
9
- RegistryGitContentService,
10
- SkillHubGitContentService,
11
- type RegistryGitAccessContext,
12
- } from '../registry-content-service';
13
-
14
- const scheduledSyncAccess: RegistryGitAccessContext = { kind: 'system', reason: 'scheduled-sync' };
15
-
16
- describe('RegistryGitContentService', () => {
17
- const originalRepositoryRoot = process.env.GIT_REPOS_BASE_PATH;
18
- let repositoryRoot = '';
19
- let repositoryPath = '';
20
- let commitSha = '';
21
-
22
- beforeEach(() => {
23
- repositoryRoot = mkdtempSync(join(tmpdir(), 'registry-git-content-'));
24
- repositoryPath = join(repositoryRoot, 'skills');
25
- mkdirSync(repositoryPath, { recursive: true });
26
- process.env.GIT_REPOS_BASE_PATH = repositoryRoot;
27
- execFileSync('git', ['init', repositoryPath], { stdio: 'ignore' });
28
- execFileSync('git', ['-C', repositoryPath, 'config', 'user.email', 'registry@example.test'], { stdio: 'ignore' });
29
- execFileSync('git', ['-C', repositoryPath, 'config', 'user.name', 'Registry Test'], { stdio: 'ignore' });
30
- writeFileSync(join(repositoryPath, 'payload.bin'), Buffer.from([0, 255, 128, 17]));
31
- execFileSync('git', ['-C', repositoryPath, 'add', 'payload.bin'], { stdio: 'ignore' });
32
- execFileSync('git', ['-C', repositoryPath, 'commit', '-m', 'binary payload'], { stdio: 'ignore' });
33
- commitSha = execFileSync('git', ['-C', repositoryPath, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
34
- });
35
-
36
- afterEach(() => {
37
- rmSync(repositoryRoot, { recursive: true, force: true });
38
- if (originalRepositoryRoot === undefined) {
39
- delete process.env.GIT_REPOS_BASE_PATH;
40
- } else {
41
- process.env.GIT_REPOS_BASE_PATH = originalRepositoryRoot;
42
- }
43
- });
44
-
45
- it('preserves binary skill files when reading from a pinned commit', async () => {
46
- const database = {
47
- getRepository: () => ({
48
- findOne: vi.fn().mockResolvedValue({
49
- get: (attribute: string) => {
50
- if (attribute === 'localPath') {
51
- return 'skills';
52
- }
53
- if (attribute === 'registryExportEnabled') {
54
- return true;
55
- }
56
- return null;
57
- },
58
- }),
59
- }),
60
- } as unknown as Database;
61
- const service = new RegistryGitContentService(database);
62
-
63
- await expect(
64
- service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
65
- ).resolves.toEqual(Buffer.from([0, 255, 128, 17]));
66
- await expect(
67
- service.readFile({ repositoryId: 1, commitSha, filePath: 'missing.json' }, scheduledSyncAccess),
68
- ).rejects.toMatchObject({
69
- code: 'REGISTRY_GIT_FILE_NOT_FOUND',
70
- });
71
- });
72
-
73
- it('rejects repository reads unless Git Manager explicitly grants registry export', async () => {
74
- const database = {
75
- getRepository: () => ({
76
- findOne: vi.fn().mockResolvedValue({
77
- get: (attribute: string) => (attribute === 'registryExportEnabled' ? false : 'skills'),
78
- }),
79
- }),
80
- } as unknown as Database;
81
- const service = new RegistryGitContentService(database);
82
-
83
- await expect(service.resolveCommit(1, 'HEAD', scheduledSyncAccess)).rejects.toMatchObject({
84
- code: 'REGISTRY_EXPORT_NOT_GRANTED',
85
- });
86
- await expect(
87
- service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
88
- ).rejects.toMatchObject({
89
- code: 'REGISTRY_EXPORT_NOT_GRANTED',
90
- });
91
- });
92
-
93
- it('does not distinguish a missing repository from an ungranted repository', async () => {
94
- const database = {
95
- getRepository: () => ({ findOne: vi.fn().mockResolvedValue(null) }),
96
- } as unknown as Database;
97
- const service = new RegistryGitContentService(database);
98
-
99
- await expect(service.resolveCommit(404, 'HEAD', scheduledSyncAccess)).rejects.toMatchObject({
100
- code: 'REGISTRY_EXPORT_NOT_GRANTED',
101
- });
102
- });
103
-
104
- it('waits for a killed Git process to close before returning an output-limit error', async () => {
105
- // Keep Git writing after the first stdout chunk. This makes the test cover
106
- // the Windows handle race: the repository must be removable immediately
107
- // after `readFile()` rejects, without depending on afterEach timing.
108
- writeFileSync(join(repositoryPath, 'payload.bin'), Buffer.alloc(8 * 1024 * 1024, 255));
109
- execFileSync('git', ['-C', repositoryPath, 'add', 'payload.bin'], { stdio: 'ignore' });
110
- execFileSync('git', ['-C', repositoryPath, 'commit', '--amend', '--no-edit'], { stdio: 'ignore' });
111
- commitSha = execFileSync('git', ['-C', repositoryPath, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
112
-
113
- const database = {
114
- getRepository: () => ({
115
- findOne: vi.fn().mockResolvedValue({
116
- get: (attribute: string) => {
117
- if (attribute === 'localPath') {
118
- return 'skills';
119
- }
120
- if (attribute === 'registryExportEnabled') {
121
- return true;
122
- }
123
- return null;
124
- },
125
- }),
126
- }),
127
- } as unknown as Database;
128
- const service = new RegistryGitContentService(database, {
129
- maxFileBytes: 2,
130
- maxTreeEntries: 10,
131
- maxTreeOutputBytes: 1024,
132
- });
133
-
134
- await expect(
135
- service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
136
- ).rejects.toThrow('Git file exceeds the registry source-file limit.');
137
- expect(() => rmSync(repositoryRoot, { recursive: true, force: true, maxRetries: 0 })).not.toThrow();
138
- });
139
-
140
- it('rejects a Git tree that exceeds the configured entry limit', async () => {
141
- const database = {
142
- getRepository: () => ({
143
- findOne: vi.fn().mockResolvedValue({
144
- get: (attribute: string) => {
145
- if (attribute === 'localPath') {
146
- return 'skills';
147
- }
148
- if (attribute === 'registryExportEnabled') {
149
- return true;
150
- }
151
- return null;
152
- },
153
- }),
154
- }),
155
- } as unknown as Database;
156
- const service = new RegistryGitContentService(database, {
157
- maxFileBytes: 1024,
158
- maxTreeEntries: 0,
159
- maxTreeOutputBytes: 1024,
160
- });
161
-
162
- await expect(
163
- service.listTree({ repositoryId: 1, commitSha, rootPath: '', recursive: true }, scheduledSyncAccess),
164
- ).rejects.toThrow('Git tree contains too many entries for registry export.');
165
- });
166
-
167
- it('requires an explicit actor instead of treating an internal call as a system sync', async () => {
168
- const database = {
169
- getRepository: vi.fn(),
170
- } as unknown as Database;
171
- const service = new RegistryGitContentService(database);
172
-
173
- expect(service.contractVersion).toBe(2);
174
- expect(service.capabilities).toEqual(
175
- expect.arrayContaining(['registry-content-with-actor', 'registry-content-authorize-source']),
176
- );
177
- await expect(service.assertRepositoryAccess(1)).rejects.toMatchObject({
178
- code: 'REGISTRY_REPOSITORY_ACCESS_DENIED',
179
- });
180
- expect(database.getRepository).not.toHaveBeenCalled();
181
- });
182
-
183
- it('allows a user whose Git Manager file-content scope includes the repository', async () => {
184
- const acl = {
185
- can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
186
- };
187
- const database = {
188
- getRepository: () => ({
189
- findOne: vi.fn().mockResolvedValue({
190
- get: (attribute: string) => {
191
- if (attribute === 'localPath') return 'skills';
192
- if (attribute === 'registryExportEnabled') return true;
193
- return null;
194
- },
195
- }),
196
- }),
197
- } as unknown as Database;
198
- const service = new RegistryGitContentService(database, undefined, acl);
199
-
200
- await expect(service.resolveCommit(1, 'HEAD', { kind: 'user', roles: ['developer'] })).resolves.toBe(commitSha);
201
- expect(acl.can).toHaveBeenCalledWith({
202
- roles: ['developer'],
203
- resource: 'gitManager',
204
- action: 'fileContent',
205
- });
206
- });
207
-
208
- it('rejects a user whose Git Manager scope excludes the requested repository', async () => {
209
- const acl = {
210
- can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
211
- };
212
- const repositoryLookup = vi.fn();
213
- const database = {
214
- getRepository: repositoryLookup,
215
- } as unknown as Database;
216
- const service = new RegistryGitContentService(database, undefined, acl);
217
-
218
- await expect(service.assertRepositoryAccess(2, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
219
- code: 'REGISTRY_REPOSITORY_ACCESS_DENIED',
220
- });
221
- expect(repositoryLookup).not.toHaveBeenCalled();
222
- });
223
-
224
- it('allows root without evaluating repository scope', async () => {
225
- const acl = { can: vi.fn() };
226
- const database = {
227
- getRepository: () => ({
228
- findOne: vi.fn().mockResolvedValue({
229
- get: (attribute: string) => {
230
- if (attribute === 'localPath') return 'skills';
231
- if (attribute === 'registryExportEnabled') return true;
232
- return null;
233
- },
234
- }),
235
- }),
236
- } as unknown as Database;
237
- const service = new RegistryGitContentService(database, undefined, acl);
238
-
239
- await expect(service.resolveCommit(1, 'HEAD', { kind: 'user', roles: ['root'] })).resolves.toBe(commitSha);
240
- expect(acl.can).not.toHaveBeenCalled();
241
- });
242
-
243
- it('allows a scheduled system sync but still requires the repository export opt-in', async () => {
244
- const acl = { can: vi.fn() };
245
- const database = {
246
- getRepository: () => ({
247
- findOne: vi.fn().mockResolvedValue({
248
- get: (attribute: string) => {
249
- if (attribute === 'localPath') return 'skills';
250
- if (attribute === 'registryExportEnabled') return true;
251
- return null;
252
- },
253
- }),
254
- }),
255
- } as unknown as Database;
256
- const service = new RegistryGitContentService(database, undefined, acl);
257
-
258
- await expect(service.resolveCommit(1, 'HEAD', scheduledSyncAccess)).resolves.toBe(commitSha);
259
- expect(acl.can).not.toHaveBeenCalled();
260
- });
261
-
262
- it('rejects an otherwise authorized user when repository export is disabled', async () => {
263
- const acl = {
264
- can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
265
- };
266
- const database = {
267
- getRepository: () => ({
268
- findOne: vi.fn().mockResolvedValue({
269
- get: (attribute: string) => (attribute === 'registryExportEnabled' ? false : 'skills'),
270
- }),
271
- }),
272
- } as unknown as Database;
273
- const service = new RegistryGitContentService(database, undefined, acl);
274
-
275
- await expect(service.assertRepositoryAccess(1, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
276
- code: 'REGISTRY_EXPORT_NOT_GRANTED',
277
- });
278
- });
279
-
280
- it('allows Skill Hub reads for a scoped user even when Registry export is disabled', async () => {
281
- const acl = {
282
- can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
283
- };
284
- const database = {
285
- getRepository: () => ({
286
- findOne: vi.fn().mockResolvedValue({
287
- get: (attribute: string) => {
288
- if (attribute === 'localPath') return 'skills';
289
- if (attribute === 'registryExportEnabled') return false;
290
- return null;
291
- },
292
- }),
293
- }),
294
- } as unknown as Database;
295
- const service = new SkillHubGitContentService(database, undefined, acl);
296
-
297
- expect(service.contractVersion).toBe(2);
298
- expect(service.capabilities).toContain('skill-hub-content-with-actor');
299
- await expect(
300
- service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, { kind: 'user', roles: ['developer'] }),
301
- ).resolves.toEqual(Buffer.from([0, 255, 128, 17]));
302
- });
303
-
304
- it('rejects a Skill Hub user outside repository scope before reading the database', async () => {
305
- const acl = {
306
- can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
307
- };
308
- const repositoryLookup = vi.fn();
309
- const database = {
310
- getRepository: repositoryLookup,
311
- } as unknown as Database;
312
- const service = new SkillHubGitContentService(database, undefined, acl);
313
-
314
- await expect(service.assertRepositoryAccess(2, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
315
- code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
316
- });
317
- expect(repositoryLookup).not.toHaveBeenCalled();
318
- });
319
-
320
- it('rejects a Skill Hub call without an actor before reading the database', async () => {
321
- const repositoryLookup = vi.fn();
322
- const database = {
323
- getRepository: repositoryLookup,
324
- } as unknown as Database;
325
- const service = new SkillHubGitContentService(database);
326
-
327
- await expect(service.assertRepositoryAccess(1)).rejects.toMatchObject({
328
- code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
329
- });
330
- expect(repositoryLookup).not.toHaveBeenCalled();
331
- });
332
-
333
- it('rejects a scheduled system actor from Skill Hub before reading the database', async () => {
334
- const repositoryLookup = vi.fn();
335
- const database = {
336
- getRepository: repositoryLookup,
337
- } as unknown as Database;
338
- const service = new SkillHubGitContentService(database);
339
-
340
- await expect(service.assertRepositoryAccess(1, scheduledSyncAccess)).rejects.toMatchObject({
341
- code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
342
- });
343
- expect(repositoryLookup).not.toHaveBeenCalled();
344
- });
345
- });
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 { execFileSync } from 'child_process';
11
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'fs';
12
+ import { tmpdir } from 'os';
13
+ import { join } from 'path';
14
+
15
+ import type { Database } from '@nocobase/database';
16
+
17
+ import {
18
+ RegistryGitContentService,
19
+ SkillHubGitContentService,
20
+ type RegistryGitAccessContext,
21
+ } from '../registry-content-service';
22
+
23
+ const scheduledSyncAccess: RegistryGitAccessContext = { kind: 'system', reason: 'scheduled-sync' };
24
+
25
+ describe('RegistryGitContentService', () => {
26
+ const originalRepositoryRoot = process.env.GIT_REPOS_BASE_PATH;
27
+ let repositoryRoot = '';
28
+ let repositoryPath = '';
29
+ let commitSha = '';
30
+
31
+ beforeEach(() => {
32
+ repositoryRoot = mkdtempSync(join(tmpdir(), 'registry-git-content-'));
33
+ repositoryPath = join(repositoryRoot, 'skills');
34
+ mkdirSync(repositoryPath, { recursive: true });
35
+ process.env.GIT_REPOS_BASE_PATH = repositoryRoot;
36
+ execFileSync('git', ['init', repositoryPath], { stdio: 'ignore' });
37
+ execFileSync('git', ['-C', repositoryPath, 'config', 'user.email', 'registry@example.test'], { stdio: 'ignore' });
38
+ execFileSync('git', ['-C', repositoryPath, 'config', 'user.name', 'Registry Test'], { stdio: 'ignore' });
39
+ writeFileSync(join(repositoryPath, 'payload.bin'), Buffer.from([0, 255, 128, 17]));
40
+ execFileSync('git', ['-C', repositoryPath, 'add', 'payload.bin'], { stdio: 'ignore' });
41
+ execFileSync('git', ['-C', repositoryPath, 'commit', '-m', 'binary payload'], { stdio: 'ignore' });
42
+ commitSha = execFileSync('git', ['-C', repositoryPath, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
43
+ });
44
+
45
+ afterEach(() => {
46
+ rmSync(repositoryRoot, { recursive: true, force: true });
47
+ if (originalRepositoryRoot === undefined) {
48
+ delete process.env.GIT_REPOS_BASE_PATH;
49
+ } else {
50
+ process.env.GIT_REPOS_BASE_PATH = originalRepositoryRoot;
51
+ }
52
+ });
53
+
54
+ it('preserves binary skill files when reading from a pinned commit', async () => {
55
+ const database = {
56
+ getRepository: () => ({
57
+ findOne: vi.fn().mockResolvedValue({
58
+ get: (attribute: string) => {
59
+ if (attribute === 'localPath') {
60
+ return 'skills';
61
+ }
62
+ if (attribute === 'registryExportEnabled') {
63
+ return true;
64
+ }
65
+ return null;
66
+ },
67
+ }),
68
+ }),
69
+ } as unknown as Database;
70
+ const service = new RegistryGitContentService(database);
71
+ const treeRequest = { repositoryId: 1, commitSha, rootPath: '', recursive: true };
72
+ await expect(service.listTree({ ...treeRequest, maxTreeOutputBytes: 1 }, scheduledSyncAccess)).rejects.toThrow(
73
+ /output limit/,
74
+ );
75
+ await expect(service.listTree(treeRequest, scheduledSyncAccess)).resolves.toHaveLength(1);
76
+
77
+ await expect(
78
+ service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
79
+ ).resolves.toEqual(Buffer.from([0, 255, 128, 17]));
80
+ await expect(
81
+ service.readFile({ repositoryId: 1, commitSha, filePath: 'missing.json' }, scheduledSyncAccess),
82
+ ).rejects.toMatchObject({
83
+ code: 'REGISTRY_GIT_FILE_NOT_FOUND',
84
+ });
85
+ });
86
+
87
+ it('rejects repository reads unless Git Manager explicitly grants registry export', async () => {
88
+ const database = {
89
+ getRepository: () => ({
90
+ findOne: vi.fn().mockResolvedValue({
91
+ get: (attribute: string) => (attribute === 'registryExportEnabled' ? false : 'skills'),
92
+ }),
93
+ }),
94
+ } as unknown as Database;
95
+ const service = new RegistryGitContentService(database);
96
+
97
+ await expect(service.resolveCommit(1, 'HEAD', scheduledSyncAccess)).rejects.toMatchObject({
98
+ code: 'REGISTRY_EXPORT_NOT_GRANTED',
99
+ });
100
+ await expect(
101
+ service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
102
+ ).rejects.toMatchObject({
103
+ code: 'REGISTRY_EXPORT_NOT_GRANTED',
104
+ });
105
+ });
106
+
107
+ it('does not distinguish a missing repository from an ungranted repository', async () => {
108
+ const database = {
109
+ getRepository: () => ({ findOne: vi.fn().mockResolvedValue(null) }),
110
+ } as unknown as Database;
111
+ const service = new RegistryGitContentService(database);
112
+
113
+ await expect(service.resolveCommit(404, 'HEAD', scheduledSyncAccess)).rejects.toMatchObject({
114
+ code: 'REGISTRY_EXPORT_NOT_GRANTED',
115
+ });
116
+ });
117
+
118
+ it('waits for a killed Git process to close before returning an output-limit error', async () => {
119
+ // Keep Git writing after the first stdout chunk. This makes the test cover
120
+ // the Windows handle race: the repository must be removable immediately
121
+ // after `readFile()` rejects, without depending on afterEach timing.
122
+ writeFileSync(join(repositoryPath, 'payload.bin'), Buffer.alloc(8 * 1024 * 1024, 255));
123
+ execFileSync('git', ['-C', repositoryPath, 'add', 'payload.bin'], { stdio: 'ignore' });
124
+ execFileSync('git', ['-C', repositoryPath, 'commit', '--amend', '--no-edit'], { stdio: 'ignore' });
125
+ commitSha = execFileSync('git', ['-C', repositoryPath, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
126
+
127
+ const database = {
128
+ getRepository: () => ({
129
+ findOne: vi.fn().mockResolvedValue({
130
+ get: (attribute: string) => {
131
+ if (attribute === 'localPath') {
132
+ return 'skills';
133
+ }
134
+ if (attribute === 'registryExportEnabled') {
135
+ return true;
136
+ }
137
+ return null;
138
+ },
139
+ }),
140
+ }),
141
+ } as unknown as Database;
142
+ const service = new RegistryGitContentService(database, {
143
+ maxFileBytes: 2,
144
+ maxTreeEntries: 10,
145
+ maxTreeOutputBytes: 1024,
146
+ });
147
+
148
+ await expect(
149
+ service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, scheduledSyncAccess),
150
+ ).rejects.toThrow('Git file exceeds the registry source-file limit.');
151
+ expect(() => rmSync(repositoryRoot, { recursive: true, force: true, maxRetries: 0 })).not.toThrow();
152
+ });
153
+
154
+ it('rejects a Git tree that exceeds the configured entry limit', async () => {
155
+ const database = {
156
+ getRepository: () => ({
157
+ findOne: vi.fn().mockResolvedValue({
158
+ get: (attribute: string) => {
159
+ if (attribute === 'localPath') {
160
+ return 'skills';
161
+ }
162
+ if (attribute === 'registryExportEnabled') {
163
+ return true;
164
+ }
165
+ return null;
166
+ },
167
+ }),
168
+ }),
169
+ } as unknown as Database;
170
+ const service = new RegistryGitContentService(database, {
171
+ maxFileBytes: 1024,
172
+ maxTreeEntries: 0,
173
+ maxTreeOutputBytes: 1024,
174
+ });
175
+
176
+ await expect(
177
+ service.listTree({ repositoryId: 1, commitSha, rootPath: '', recursive: true }, scheduledSyncAccess),
178
+ ).rejects.toThrow('Git tree contains too many entries for registry export.');
179
+ });
180
+
181
+ it('requires an explicit actor instead of treating an internal call as a system sync', async () => {
182
+ const database = {
183
+ getRepository: vi.fn(),
184
+ } as unknown as Database;
185
+ const service = new RegistryGitContentService(database);
186
+
187
+ expect(service.contractVersion).toBe(2);
188
+ expect(service.capabilities).toEqual(
189
+ expect.arrayContaining(['registry-content-with-actor', 'registry-content-authorize-source']),
190
+ );
191
+ await expect(service.assertRepositoryAccess(1)).rejects.toMatchObject({
192
+ code: 'REGISTRY_REPOSITORY_ACCESS_DENIED',
193
+ });
194
+ expect(database.getRepository).not.toHaveBeenCalled();
195
+ });
196
+
197
+ it('allows a user whose Git Manager file-content scope includes the repository', async () => {
198
+ const acl = {
199
+ can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
200
+ };
201
+ const database = {
202
+ getRepository: () => ({
203
+ findOne: vi.fn().mockResolvedValue({
204
+ get: (attribute: string) => {
205
+ if (attribute === 'localPath') return 'skills';
206
+ if (attribute === 'registryExportEnabled') return true;
207
+ return null;
208
+ },
209
+ }),
210
+ }),
211
+ } as unknown as Database;
212
+ const service = new RegistryGitContentService(database, undefined, acl);
213
+
214
+ await expect(service.resolveCommit(1, 'HEAD', { kind: 'user', roles: ['developer'] })).resolves.toBe(commitSha);
215
+ expect(acl.can).toHaveBeenCalledWith({
216
+ roles: ['developer'],
217
+ resource: 'gitManager',
218
+ action: 'fileContent',
219
+ });
220
+ });
221
+
222
+ it('rejects a user whose Git Manager scope excludes the requested repository', async () => {
223
+ const acl = {
224
+ can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
225
+ };
226
+ const repositoryLookup = vi.fn();
227
+ const database = {
228
+ getRepository: repositoryLookup,
229
+ } as unknown as Database;
230
+ const service = new RegistryGitContentService(database, undefined, acl);
231
+
232
+ await expect(service.assertRepositoryAccess(2, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
233
+ code: 'REGISTRY_REPOSITORY_ACCESS_DENIED',
234
+ });
235
+ expect(repositoryLookup).not.toHaveBeenCalled();
236
+ });
237
+
238
+ it('allows root without evaluating repository scope', async () => {
239
+ const acl = { can: vi.fn() };
240
+ const database = {
241
+ getRepository: () => ({
242
+ findOne: vi.fn().mockResolvedValue({
243
+ get: (attribute: string) => {
244
+ if (attribute === 'localPath') return 'skills';
245
+ if (attribute === 'registryExportEnabled') return true;
246
+ return null;
247
+ },
248
+ }),
249
+ }),
250
+ } as unknown as Database;
251
+ const service = new RegistryGitContentService(database, undefined, acl);
252
+
253
+ await expect(service.resolveCommit(1, 'HEAD', { kind: 'user', roles: ['root'] })).resolves.toBe(commitSha);
254
+ expect(acl.can).not.toHaveBeenCalled();
255
+ });
256
+
257
+ it('allows a scheduled system sync but still requires the repository export opt-in', async () => {
258
+ const acl = { can: vi.fn() };
259
+ const database = {
260
+ getRepository: () => ({
261
+ findOne: vi.fn().mockResolvedValue({
262
+ get: (attribute: string) => {
263
+ if (attribute === 'localPath') return 'skills';
264
+ if (attribute === 'registryExportEnabled') return true;
265
+ return null;
266
+ },
267
+ }),
268
+ }),
269
+ } as unknown as Database;
270
+ const service = new RegistryGitContentService(database, undefined, acl);
271
+
272
+ await expect(service.resolveCommit(1, 'HEAD', scheduledSyncAccess)).resolves.toBe(commitSha);
273
+ expect(acl.can).not.toHaveBeenCalled();
274
+ });
275
+
276
+ it('rejects an otherwise authorized user when repository export is disabled', async () => {
277
+ const acl = {
278
+ can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
279
+ };
280
+ const database = {
281
+ getRepository: () => ({
282
+ findOne: vi.fn().mockResolvedValue({
283
+ get: (attribute: string) => (attribute === 'registryExportEnabled' ? false : 'skills'),
284
+ }),
285
+ }),
286
+ } as unknown as Database;
287
+ const service = new RegistryGitContentService(database, undefined, acl);
288
+
289
+ await expect(service.assertRepositoryAccess(1, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
290
+ code: 'REGISTRY_EXPORT_NOT_GRANTED',
291
+ });
292
+ });
293
+
294
+ it('allows Skill Hub reads for a scoped user even when Registry export is disabled', async () => {
295
+ const acl = {
296
+ can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
297
+ };
298
+ const database = {
299
+ getRepository: () => ({
300
+ findOne: vi.fn().mockResolvedValue({
301
+ get: (attribute: string) => {
302
+ if (attribute === 'localPath') return 'skills';
303
+ if (attribute === 'registryExportEnabled') return false;
304
+ return null;
305
+ },
306
+ }),
307
+ }),
308
+ } as unknown as Database;
309
+ const service = new SkillHubGitContentService(database, undefined, acl);
310
+
311
+ expect(service.contractVersion).toBe(2);
312
+ expect(service.capabilities).toContain('skill-hub-content-with-actor');
313
+ await expect(
314
+ service.readFile({ repositoryId: 1, commitSha, filePath: 'payload.bin' }, { kind: 'user', roles: ['developer'] }),
315
+ ).resolves.toEqual(Buffer.from([0, 255, 128, 17]));
316
+ });
317
+
318
+ it('rejects a Skill Hub user outside repository scope before reading the database', async () => {
319
+ const acl = {
320
+ can: vi.fn().mockResolvedValue({ params: { filter: { $and: [{ id: { $in: [1] } }] } } }),
321
+ };
322
+ const repositoryLookup = vi.fn();
323
+ const database = {
324
+ getRepository: repositoryLookup,
325
+ } as unknown as Database;
326
+ const service = new SkillHubGitContentService(database, undefined, acl);
327
+
328
+ await expect(service.assertRepositoryAccess(2, { kind: 'user', roles: ['developer'] })).rejects.toMatchObject({
329
+ code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
330
+ });
331
+ expect(repositoryLookup).not.toHaveBeenCalled();
332
+ });
333
+
334
+ it('rejects a Skill Hub call without an actor before reading the database', async () => {
335
+ const repositoryLookup = vi.fn();
336
+ const database = {
337
+ getRepository: repositoryLookup,
338
+ } as unknown as Database;
339
+ const service = new SkillHubGitContentService(database);
340
+
341
+ await expect(service.assertRepositoryAccess(1)).rejects.toMatchObject({
342
+ code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
343
+ });
344
+ expect(repositoryLookup).not.toHaveBeenCalled();
345
+ });
346
+
347
+ it('rejects a scheduled system actor from Skill Hub before reading the database', async () => {
348
+ const repositoryLookup = vi.fn();
349
+ const database = {
350
+ getRepository: repositoryLookup,
351
+ } as unknown as Database;
352
+ const service = new SkillHubGitContentService(database);
353
+
354
+ await expect(service.assertRepositoryAccess(1, scheduledSyncAccess)).rejects.toMatchObject({
355
+ code: 'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
356
+ });
357
+ expect(repositoryLookup).not.toHaveBeenCalled();
358
+ });
359
+ });