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,137 +1,137 @@
1
- import type { Application } from '@nocobase/server';
2
- import { vi } from 'vitest';
3
-
4
- import { resolveBackgroundReviewRoles, resolveImmutableReviewActorId, triggerReviewInternal } from '../actions/review';
5
-
6
- function record(values: Record<string, unknown>) {
7
- return {
8
- get(attribute: string) {
9
- return values[attribute];
10
- },
11
- };
12
- }
13
-
14
- function createReviewApp(flowRepositoryId: number | null, existingReview: Record<string, unknown> | null = null) {
15
- const flow = record({
16
- id: 8,
17
- enabled: true,
18
- repositoryId: flowRepositoryId,
19
- aiEmployeeUsername: 'reviewer',
20
- name: 'Review flow',
21
- postMode: 'manual',
22
- llmService: null,
23
- model: null,
24
- instructions: null,
25
- branchFilter: null,
26
- });
27
- const flowsRepository = {
28
- findOne: vi.fn().mockResolvedValue(flow),
29
- };
30
- const repositoriesRepository = {
31
- findOne: vi.fn().mockResolvedValue(record({ id: 20 })),
32
- };
33
- const reviewsRepository = {
34
- findOne: vi.fn().mockResolvedValue(existingReview ? record(existingReview) : null),
35
- create: vi.fn().mockResolvedValue(record({ id: 99 })),
36
- update: vi.fn().mockResolvedValue(undefined),
37
- };
38
- const getRepository = vi.fn((name: string) => {
39
- if (name === 'gitReviewFlows') return flowsRepository;
40
- if (name === 'gitRepositories') return repositoriesRepository;
41
- if (name === 'gitCodeReviews') return reviewsRepository;
42
- throw new Error(`Unexpected repository ${name}`);
43
- });
44
- const app = {
45
- db: { getRepository },
46
- lockManager: {
47
- runExclusive: vi.fn(async (_key: string, callback: () => Promise<unknown>) => callback()),
48
- },
49
- serving: vi.fn().mockReturnValue(false),
50
- eventQueue: {
51
- publish: vi.fn().mockResolvedValue(undefined),
52
- },
53
- } as unknown as Application;
54
-
55
- return { app, repositoriesRepository, reviewsRepository };
56
- }
57
-
58
- describe('Git Manager review flows', () => {
59
- it('rejects an explicitly selected flow from another repository', async () => {
60
- const { app, repositoriesRepository } = createReviewApp(10);
61
-
62
- await expect(
63
- triggerReviewInternal(app, {
64
- flowId: 8,
65
- repositoryId: 20,
66
- targetType: 'branch',
67
- branch: 'main',
68
- }),
69
- ).rejects.toMatchObject({ status: 400, message: 'Review flow does not belong to this repository' });
70
-
71
- expect(repositoriesRepository.findOne).not.toHaveBeenCalled();
72
- });
73
-
74
- it('allows an explicitly selected global flow', async () => {
75
- const { app, repositoriesRepository, reviewsRepository } = createReviewApp(null);
76
-
77
- const reviewId = await triggerReviewInternal(app, {
78
- flowId: 8,
79
- repositoryId: 20,
80
- targetType: 'branch',
81
- branch: 'main',
82
- userId: 7,
83
- });
84
-
85
- expect(reviewId).toBe(99);
86
- expect(repositoriesRepository.findOne).toHaveBeenCalledWith({ filterByTk: 20 });
87
- expect(reviewsRepository.create).toHaveBeenCalledWith({
88
- values: expect.objectContaining({ flowId: 8, repositoryId: 20, createdById: 7 }),
89
- });
90
- });
91
-
92
- it('uses the immutable creator field instead of review metadata for worker identity', () => {
93
- expect(resolveImmutableReviewActorId(record({ createdById: 7, metadata: { userId: 1 } }))).toBe(7);
94
- expect(resolveImmutableReviewActorId(record({ createdById: null, metadata: { userId: 1 } }))).toBeNull();
95
- });
96
-
97
- it('fails closed when a manual review actor has no resolvable roles', async () => {
98
- const db = {
99
- getRepository: vi.fn().mockReturnValue({ find: vi.fn().mockResolvedValue([]) }),
100
- };
101
-
102
- await expect(resolveBackgroundReviewRoles(db, 7)).rejects.toThrow('Unable to resolve roles for review actor');
103
- await expect(resolveBackgroundReviewRoles(db, null)).resolves.toEqual(['admin']);
104
- });
105
-
106
- it.each(['completed', 'failed'])(
107
- 'refreshes the immutable actor when a different user requeues a %s review',
108
- async (status) => {
109
- const { app, reviewsRepository } = createReviewApp(null, {
110
- id: 99,
111
- status,
112
- latestSha: null,
113
- createdById: 1,
114
- });
115
-
116
- await triggerReviewInternal(app, {
117
- flowId: 8,
118
- repositoryId: 20,
119
- targetType: 'branch',
120
- branch: 'main',
121
- triggeredBy: 'manual',
122
- userId: 7,
123
- });
124
-
125
- expect(reviewsRepository.update).toHaveBeenCalledWith({
126
- filterByTk: 99,
127
- values: expect.objectContaining({
128
- createdById: 7,
129
- triggeredBy: 'manual',
130
- status: 'pending',
131
- metadata: expect.objectContaining({ userId: 7 }),
132
- }),
133
- });
134
- expect(reviewsRepository.create).not.toHaveBeenCalled();
135
- },
136
- );
137
- });
1
+ import type { Application } from '@nocobase/server';
2
+ import { vi } from 'vitest';
3
+
4
+ import { resolveBackgroundReviewRoles, resolveImmutableReviewActorId, triggerReviewInternal } from '../actions/review';
5
+
6
+ function record(values: Record<string, unknown>) {
7
+ return {
8
+ get(attribute: string) {
9
+ return values[attribute];
10
+ },
11
+ };
12
+ }
13
+
14
+ function createReviewApp(flowRepositoryId: number | null, existingReview: Record<string, unknown> | null = null) {
15
+ const flow = record({
16
+ id: 8,
17
+ enabled: true,
18
+ repositoryId: flowRepositoryId,
19
+ aiEmployeeUsername: 'reviewer',
20
+ name: 'Review flow',
21
+ postMode: 'manual',
22
+ llmService: null,
23
+ model: null,
24
+ instructions: null,
25
+ branchFilter: null,
26
+ });
27
+ const flowsRepository = {
28
+ findOne: vi.fn().mockResolvedValue(flow),
29
+ };
30
+ const repositoriesRepository = {
31
+ findOne: vi.fn().mockResolvedValue(record({ id: 20 })),
32
+ };
33
+ const reviewsRepository = {
34
+ findOne: vi.fn().mockResolvedValue(existingReview ? record(existingReview) : null),
35
+ create: vi.fn().mockResolvedValue(record({ id: 99 })),
36
+ update: vi.fn().mockResolvedValue(undefined),
37
+ };
38
+ const getRepository = vi.fn((name: string) => {
39
+ if (name === 'gitReviewFlows') return flowsRepository;
40
+ if (name === 'gitRepositories') return repositoriesRepository;
41
+ if (name === 'gitCodeReviews') return reviewsRepository;
42
+ throw new Error(`Unexpected repository ${name}`);
43
+ });
44
+ const app = {
45
+ db: { getRepository },
46
+ lockManager: {
47
+ runExclusive: vi.fn(async (_key: string, callback: () => Promise<unknown>) => callback()),
48
+ },
49
+ serving: vi.fn().mockReturnValue(false),
50
+ eventQueue: {
51
+ publish: vi.fn().mockResolvedValue(undefined),
52
+ },
53
+ } as unknown as Application;
54
+
55
+ return { app, repositoriesRepository, reviewsRepository };
56
+ }
57
+
58
+ describe('Git Manager review flows', () => {
59
+ it('rejects an explicitly selected flow from another repository', async () => {
60
+ const { app, repositoriesRepository } = createReviewApp(10);
61
+
62
+ await expect(
63
+ triggerReviewInternal(app, {
64
+ flowId: 8,
65
+ repositoryId: 20,
66
+ targetType: 'branch',
67
+ branch: 'main',
68
+ }),
69
+ ).rejects.toMatchObject({ status: 400, message: 'Review flow does not belong to this repository' });
70
+
71
+ expect(repositoriesRepository.findOne).not.toHaveBeenCalled();
72
+ });
73
+
74
+ it('allows an explicitly selected global flow', async () => {
75
+ const { app, repositoriesRepository, reviewsRepository } = createReviewApp(null);
76
+
77
+ const reviewId = await triggerReviewInternal(app, {
78
+ flowId: 8,
79
+ repositoryId: 20,
80
+ targetType: 'branch',
81
+ branch: 'main',
82
+ userId: 7,
83
+ });
84
+
85
+ expect(reviewId).toBe(99);
86
+ expect(repositoriesRepository.findOne).toHaveBeenCalledWith({ filterByTk: 20 });
87
+ expect(reviewsRepository.create).toHaveBeenCalledWith({
88
+ values: expect.objectContaining({ flowId: 8, repositoryId: 20, createdById: 7 }),
89
+ });
90
+ });
91
+
92
+ it('uses the immutable creator field instead of review metadata for worker identity', () => {
93
+ expect(resolveImmutableReviewActorId(record({ createdById: 7, metadata: { userId: 1 } }))).toBe(7);
94
+ expect(resolveImmutableReviewActorId(record({ createdById: null, metadata: { userId: 1 } }))).toBeNull();
95
+ });
96
+
97
+ it('fails closed when a manual review actor has no resolvable roles', async () => {
98
+ const db = {
99
+ getRepository: vi.fn().mockReturnValue({ find: vi.fn().mockResolvedValue([]) }),
100
+ };
101
+
102
+ await expect(resolveBackgroundReviewRoles(db, 7)).rejects.toThrow('Unable to resolve roles for review actor');
103
+ await expect(resolveBackgroundReviewRoles(db, null)).resolves.toEqual(['admin']);
104
+ });
105
+
106
+ it.each(['completed', 'failed'])(
107
+ 'refreshes the immutable actor when a different user requeues a %s review',
108
+ async (status) => {
109
+ const { app, reviewsRepository } = createReviewApp(null, {
110
+ id: 99,
111
+ status,
112
+ latestSha: null,
113
+ createdById: 1,
114
+ });
115
+
116
+ await triggerReviewInternal(app, {
117
+ flowId: 8,
118
+ repositoryId: 20,
119
+ targetType: 'branch',
120
+ branch: 'main',
121
+ triggeredBy: 'manual',
122
+ userId: 7,
123
+ });
124
+
125
+ expect(reviewsRepository.update).toHaveBeenCalledWith({
126
+ filterByTk: 99,
127
+ values: expect.objectContaining({
128
+ createdById: 7,
129
+ triggeredBy: 'manual',
130
+ status: 'pending',
131
+ metadata: expect.objectContaining({ userId: 7 }),
132
+ }),
133
+ });
134
+ expect(reviewsRepository.create).not.toHaveBeenCalled();
135
+ },
136
+ );
137
+ });
@@ -1,147 +1,147 @@
1
- import type { Database, Model } from '@nocobase/database';
2
- import { vi } from 'vitest';
3
-
4
- import {
5
- getRepositoryPermissionIds,
6
- getRepositoryPermissionScope,
7
- repositoryPermissionIdsForDisplay,
8
- syncScopedActions,
9
- } from '../actions/role-permissions';
10
-
11
- function permissionAction(name: string, repositoryIds: Array<number | string>) {
12
- return {
13
- toJSON: () => ({
14
- name,
15
- scope: { scope: { $and: [{ id: { $in: repositoryIds } }] } },
16
- }),
17
- } as unknown as Model;
18
- }
19
-
20
- describe('Git Manager role permissions', () => {
21
- it('reads repository IDs from legacy scoped actions', () => {
22
- const actions = [permissionAction('read', [1]), permissionAction('write', [1, 2])];
23
-
24
- expect(getRepositoryPermissionIds(actions, ['read', 'write'])).toEqual([1, 2]);
25
- });
26
-
27
- it('does not include repository IDs from unrelated actions', () => {
28
- const actions = [permissionAction('destroy', [3]), permissionAction('write', [1])];
29
-
30
- expect(getRepositoryPermissionIds(actions, ['write'])).toEqual([1]);
31
- });
32
-
33
- it('preserves direct and equality repository scopes from legacy roles', () => {
34
- const directScope = {
35
- toJSON: () => ({
36
- name: 'read',
37
- scope: { scope: { $and: [{ id: 42 }] } },
38
- }),
39
- } as unknown as Model;
40
- const equalityScope = {
41
- toJSON: () => ({
42
- name: 'write',
43
- scope: { scope: { $or: [{ id: { $eq: '43' } }, { id: { $in: [44] } }] } },
44
- }),
45
- } as unknown as Model;
46
-
47
- expect(getRepositoryPermissionIds([directScope, equalityScope], ['read', 'write'])).toEqual([42, '43', 44]);
48
- });
49
-
50
- it('recognizes an unscoped legacy permission as access to every repository', () => {
51
- const unscopedAction = {
52
- toJSON: () => ({ name: 'read' }),
53
- } as unknown as Model;
54
-
55
- expect(getRepositoryPermissionScope([unscopedAction], ['read'])).toEqual({
56
- ids: [],
57
- unrestricted: true,
58
- supported: true,
59
- });
60
- });
61
-
62
- it('expands an unrestricted scope to the repository IDs shown by the role editor', () => {
63
- expect(repositoryPermissionIdsForDisplay({ ids: [], unrestricted: true, supported: true }, [1, 2])).toEqual([1, 2]);
64
- expect(repositoryPermissionIdsForDisplay({ ids: [], unrestricted: false, supported: true }, [1, 2])).toEqual([]);
65
- });
66
-
67
- it('does not widen a legacy scope whose repository conditions intersect', () => {
68
- const intersectingScope = {
69
- toJSON: () => ({
70
- name: 'read',
71
- scope: { scope: { $and: [{ id: { $in: [1, 2] } }, { id: { $in: [2, 3] } }] } },
72
- }),
73
- } as unknown as Model;
74
-
75
- expect(getRepositoryPermissionScope([intersectingScope], ['read'])).toEqual({
76
- ids: [2],
77
- unrestricted: false,
78
- supported: true,
79
- });
80
- });
81
-
82
- it('marks unsupported legacy scopes as unsafe to migrate', () => {
83
- const unsupportedScope = {
84
- toJSON: () => ({
85
- name: 'read',
86
- scope: { scope: { $and: [{ id: { $in: [1] } }, { status: 'active' }] } },
87
- }),
88
- } as unknown as Model;
89
-
90
- expect(getRepositoryPermissionScope([unsupportedScope], ['read'])).toEqual({
91
- ids: [],
92
- unrestricted: false,
93
- supported: false,
94
- });
95
- });
96
-
97
- it('creates unscoped v2 actions when migrating an unrestricted legacy permission', async () => {
98
- const actionsRepository = {
99
- findOne: vi.fn().mockResolvedValue(null),
100
- create: vi.fn().mockResolvedValue(undefined),
101
- };
102
- const db = {
103
- getRepository: vi.fn((name: string) => {
104
- if (name === 'rolesResourcesActions') return actionsRepository;
105
- throw new Error(`Unexpected repository ${name}`);
106
- }),
107
- } as unknown as Database;
108
- const resource = {
109
- get: (attribute: string) => (attribute === 'id' ? 1 : undefined),
110
- } as unknown as Model;
111
-
112
- await syncScopedActions(db, resource, ['fileContent'], [], { unrestricted: true });
113
-
114
- expect(actionsRepository.create).toHaveBeenCalledWith({
115
- values: { rolesResourceId: 1, name: 'fileContent' },
116
- });
117
- });
118
-
119
- it('persists a deny-all scope instead of deleting a zero-selection action', async () => {
120
- const actionsRepository = {
121
- findOne: vi.fn().mockResolvedValue(null),
122
- create: vi.fn().mockResolvedValue(undefined),
123
- };
124
- const scopesRepository = {
125
- create: vi.fn().mockResolvedValue({ get: (attribute: string) => (attribute === 'id' ? 99 : undefined) }),
126
- };
127
- const db = {
128
- getRepository: vi.fn((name: string) => {
129
- if (name === 'rolesResourcesActions') return actionsRepository;
130
- if (name === 'rolesResourcesScopes') return scopesRepository;
131
- throw new Error(`Unexpected repository ${name}`);
132
- }),
133
- } as unknown as Database;
134
- const resource = {
135
- get: (attribute: string) => (attribute === 'id' ? 1 : undefined),
136
- } as unknown as Model;
137
-
138
- await syncScopedActions(db, resource, ['fileContent'], []);
139
-
140
- expect(scopesRepository.create).toHaveBeenCalledWith({
141
- values: { scope: { $and: [{ id: { $in: [] } }] } },
142
- });
143
- expect(actionsRepository.create).toHaveBeenCalledWith({
144
- values: { rolesResourceId: 1, name: 'fileContent', scopeId: 99 },
145
- });
146
- });
147
- });
1
+ import type { Database, Model } from '@nocobase/database';
2
+ import { vi } from 'vitest';
3
+
4
+ import {
5
+ getRepositoryPermissionIds,
6
+ getRepositoryPermissionScope,
7
+ repositoryPermissionIdsForDisplay,
8
+ syncScopedActions,
9
+ } from '../actions/role-permissions';
10
+
11
+ function permissionAction(name: string, repositoryIds: Array<number | string>) {
12
+ return {
13
+ toJSON: () => ({
14
+ name,
15
+ scope: { scope: { $and: [{ id: { $in: repositoryIds } }] } },
16
+ }),
17
+ } as unknown as Model;
18
+ }
19
+
20
+ describe('Git Manager role permissions', () => {
21
+ it('reads repository IDs from legacy scoped actions', () => {
22
+ const actions = [permissionAction('read', [1]), permissionAction('write', [1, 2])];
23
+
24
+ expect(getRepositoryPermissionIds(actions, ['read', 'write'])).toEqual([1, 2]);
25
+ });
26
+
27
+ it('does not include repository IDs from unrelated actions', () => {
28
+ const actions = [permissionAction('destroy', [3]), permissionAction('write', [1])];
29
+
30
+ expect(getRepositoryPermissionIds(actions, ['write'])).toEqual([1]);
31
+ });
32
+
33
+ it('preserves direct and equality repository scopes from legacy roles', () => {
34
+ const directScope = {
35
+ toJSON: () => ({
36
+ name: 'read',
37
+ scope: { scope: { $and: [{ id: 42 }] } },
38
+ }),
39
+ } as unknown as Model;
40
+ const equalityScope = {
41
+ toJSON: () => ({
42
+ name: 'write',
43
+ scope: { scope: { $or: [{ id: { $eq: '43' } }, { id: { $in: [44] } }] } },
44
+ }),
45
+ } as unknown as Model;
46
+
47
+ expect(getRepositoryPermissionIds([directScope, equalityScope], ['read', 'write'])).toEqual([42, '43', 44]);
48
+ });
49
+
50
+ it('recognizes an unscoped legacy permission as access to every repository', () => {
51
+ const unscopedAction = {
52
+ toJSON: () => ({ name: 'read' }),
53
+ } as unknown as Model;
54
+
55
+ expect(getRepositoryPermissionScope([unscopedAction], ['read'])).toEqual({
56
+ ids: [],
57
+ unrestricted: true,
58
+ supported: true,
59
+ });
60
+ });
61
+
62
+ it('expands an unrestricted scope to the repository IDs shown by the role editor', () => {
63
+ expect(repositoryPermissionIdsForDisplay({ ids: [], unrestricted: true, supported: true }, [1, 2])).toEqual([1, 2]);
64
+ expect(repositoryPermissionIdsForDisplay({ ids: [], unrestricted: false, supported: true }, [1, 2])).toEqual([]);
65
+ });
66
+
67
+ it('does not widen a legacy scope whose repository conditions intersect', () => {
68
+ const intersectingScope = {
69
+ toJSON: () => ({
70
+ name: 'read',
71
+ scope: { scope: { $and: [{ id: { $in: [1, 2] } }, { id: { $in: [2, 3] } }] } },
72
+ }),
73
+ } as unknown as Model;
74
+
75
+ expect(getRepositoryPermissionScope([intersectingScope], ['read'])).toEqual({
76
+ ids: [2],
77
+ unrestricted: false,
78
+ supported: true,
79
+ });
80
+ });
81
+
82
+ it('marks unsupported legacy scopes as unsafe to migrate', () => {
83
+ const unsupportedScope = {
84
+ toJSON: () => ({
85
+ name: 'read',
86
+ scope: { scope: { $and: [{ id: { $in: [1] } }, { status: 'active' }] } },
87
+ }),
88
+ } as unknown as Model;
89
+
90
+ expect(getRepositoryPermissionScope([unsupportedScope], ['read'])).toEqual({
91
+ ids: [],
92
+ unrestricted: false,
93
+ supported: false,
94
+ });
95
+ });
96
+
97
+ it('creates unscoped v2 actions when migrating an unrestricted legacy permission', async () => {
98
+ const actionsRepository = {
99
+ findOne: vi.fn().mockResolvedValue(null),
100
+ create: vi.fn().mockResolvedValue(undefined),
101
+ };
102
+ const db = {
103
+ getRepository: vi.fn((name: string) => {
104
+ if (name === 'rolesResourcesActions') return actionsRepository;
105
+ throw new Error(`Unexpected repository ${name}`);
106
+ }),
107
+ } as unknown as Database;
108
+ const resource = {
109
+ get: (attribute: string) => (attribute === 'id' ? 1 : undefined),
110
+ } as unknown as Model;
111
+
112
+ await syncScopedActions(db, resource, ['fileContent'], [], { unrestricted: true });
113
+
114
+ expect(actionsRepository.create).toHaveBeenCalledWith({
115
+ values: { rolesResourceId: 1, name: 'fileContent' },
116
+ });
117
+ });
118
+
119
+ it('persists a deny-all scope instead of deleting a zero-selection action', async () => {
120
+ const actionsRepository = {
121
+ findOne: vi.fn().mockResolvedValue(null),
122
+ create: vi.fn().mockResolvedValue(undefined),
123
+ };
124
+ const scopesRepository = {
125
+ create: vi.fn().mockResolvedValue({ get: (attribute: string) => (attribute === 'id' ? 99 : undefined) }),
126
+ };
127
+ const db = {
128
+ getRepository: vi.fn((name: string) => {
129
+ if (name === 'rolesResourcesActions') return actionsRepository;
130
+ if (name === 'rolesResourcesScopes') return scopesRepository;
131
+ throw new Error(`Unexpected repository ${name}`);
132
+ }),
133
+ } as unknown as Database;
134
+ const resource = {
135
+ get: (attribute: string) => (attribute === 'id' ? 1 : undefined),
136
+ } as unknown as Model;
137
+
138
+ await syncScopedActions(db, resource, ['fileContent'], []);
139
+
140
+ expect(scopesRepository.create).toHaveBeenCalledWith({
141
+ values: { scope: { $and: [{ id: { $in: [] } }] } },
142
+ });
143
+ expect(actionsRepository.create).toHaveBeenCalledWith({
144
+ values: { rolesResourceId: 1, name: 'fileContent', scopeId: 99 },
145
+ });
146
+ });
147
+ });