plugin-git-manager 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/client/{156.c27c2392746e7866.js → 156.8e36af3e2cee173e.js} +1 -1
- package/dist/client/{317.17ed0018e3e7db00.js → 317.2d0d33871e24ee24.js} +1 -1
- package/dist/client/{434.b2eeaaef33aae6e0.js → 434.2401f7257495b66e.js} +1 -1
- package/dist/client/{453.d49fac0e20ea08d9.js → 453.58657b9ba0904477.js} +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client-v2/{434.19c7579c06b9077c.js → 434.32b0a07b821b9329.js} +1 -1
- package/dist/client-v2/{453.84c4387ba605dc27.js → 453.f9a0ec6d8e1bd1cc.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +10 -9
- package/dist/locale/en-US.json +27 -1
- package/dist/locale/vi-VN.json +27 -1
- package/dist/locale/zh-CN.json +30 -1
- package/dist/node_modules/simple-git/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/actions/chat-context.js +282 -0
- package/dist/server/actions/git-actions.js +99 -4
- package/dist/server/actions/gitlab-api.js +86 -7
- package/dist/server/chat-context-admission.js +187 -0
- package/dist/server/plugin.js +8 -0
- package/dist/server/request-method-policy.js +2 -0
- package/dist/server/services/review-source.js +7 -4
- package/package.json +2 -2
- package/src/client/__tests__/CommitHistory.test.tsx +1 -8
- package/src/client/__tests__/GitContextPicker.test.tsx +120 -0
- package/src/client/__tests__/ReviewHistory.test.tsx +1 -8
- package/src/client/__tests__/RunReviewButton.test.tsx +1 -8
- package/src/client/__tests__/ai-context.test.tsx +119 -0
- package/src/client/__tests__/workflowRoutes.test.ts +3 -10
- package/src/client/ai-context.tsx +3 -113
- package/src/client/components/CommitHistory.tsx +1 -8
- package/src/client/components/ReviewFlows.tsx +1 -8
- package/src/client/components/ReviewHistory.tsx +1 -8
- package/src/client/components/RunReviewButton.tsx +1 -8
- package/src/client/context/GitManagerContext.tsx +18 -2
- package/src/client/index.tsx +4 -13
- package/src/client/workflowRoutes.ts +3 -18
- package/src/client-v2/ai-context.tsx +135 -72
- package/src/client-v2/components/GitContextPicker.tsx +418 -0
- package/src/client-v2/git-context-api.ts +59 -0
- package/src/client-v2/plugin.tsx +2 -4
- package/src/locale/en-US.json +27 -1
- package/src/locale/vi-VN.json +27 -1
- package/src/locale/zh-CN.json +30 -1
- package/src/server/__tests__/chat-context-admission.test.ts +276 -0
- package/src/server/__tests__/chat-context-options.test.ts +144 -0
- package/src/server/__tests__/git-chat-context.test.ts +138 -0
- package/src/server/__tests__/git-security.test.ts +53 -8
- package/src/server/__tests__/poller-lifecycle.test.ts +1 -8
- package/src/server/__tests__/repository-access.test.ts +1 -8
- package/src/server/__tests__/review-service.integration.test.ts +1 -8
- package/src/server/__tests__/review.test.ts +1 -8
- package/src/server/__tests__/smoke.test.ts +201 -8
- package/src/server/actions/chat-context.ts +302 -0
- package/src/server/actions/git-actions.ts +120 -14
- package/src/server/actions/gitlab-api.ts +98 -12
- package/src/server/actions/poller.ts +1 -8
- package/src/server/actions/review-cutover.ts +1 -8
- package/src/server/actions/review.ts +1 -8
- package/src/server/actions/role-permissions.ts +1 -8
- package/src/server/ai-tools.ts +1 -8
- package/src/server/chat-context-admission.ts +182 -0
- package/src/server/collections/gitCodeReviews.ts +1 -8
- package/src/server/collections/gitReviewCutover.ts +1 -8
- package/src/server/collections/gitReviewFlows.ts +1 -8
- package/src/server/collections/gitReviewInbox.ts +1 -8
- package/src/server/collections/gitReviewProjects.ts +1 -8
- package/src/server/collections/gitReviewRequests.ts +1 -8
- package/src/server/index.ts +1 -8
- package/src/server/plugin.ts +12 -8
- package/src/server/poller.ts +1 -8
- package/src/server/repository-access.ts +1 -8
- package/src/server/request-method-policy.ts +3 -8
- package/src/server/review-control-access.ts +1 -8
- package/src/server/services/__tests__/registry-content-service.test.ts +1 -8
- package/src/server/services/__tests__/review-source.test.ts +33 -8
- package/src/server/services/legacy-post-service.ts +1 -8
- package/src/server/services/registry-content-service.ts +1 -8
- package/src/server/services/review-service.ts +1 -8
- package/src/server/services/review-source.ts +8 -12
- package/src/server/utils/get-repo-account.ts +1 -8
- package/src/server/utils/url-security.ts +1 -8
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import { execFileSync } from 'child_process';
|
|
4
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync, renameSync } from 'fs';
|
|
5
|
+
import { tmpdir } from 'os';
|
|
6
|
+
import { join } from 'path';
|
|
7
|
+
import type { Context } from '@nocobase/actions';
|
|
8
|
+
import { log, readChatCommit } from '../actions/git-actions';
|
|
9
|
+
|
|
10
|
+
describe('Git chat commit context', () => {
|
|
11
|
+
let fixture: string;
|
|
12
|
+
let repoPath: string;
|
|
13
|
+
const previousBasePath = process.env.GIT_REPOS_BASE_PATH;
|
|
14
|
+
|
|
15
|
+
const runGit = (args: string[]) => execFileSync('git', ['-C', repoPath, ...args], { encoding: 'utf8' }).trim();
|
|
16
|
+
|
|
17
|
+
const commit = (message: string) => {
|
|
18
|
+
runGit(['-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'add', '-A']);
|
|
19
|
+
runGit(['-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'commit', '-m', message]);
|
|
20
|
+
return runGit(['rev-parse', 'HEAD']);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const createContext = (params: Record<string, unknown> = {}) => {
|
|
24
|
+
const repository = { get: (field: string) => (field === 'localPath' ? 'repository' : undefined) };
|
|
25
|
+
const context = {
|
|
26
|
+
action: { params: { repositoryId: 1, ...params } },
|
|
27
|
+
db: {
|
|
28
|
+
getRepository: vi.fn(() => ({ findOne: vi.fn().mockResolvedValue(repository) })),
|
|
29
|
+
},
|
|
30
|
+
throw: (status: number, message: string): never => {
|
|
31
|
+
throw Object.assign(new Error(message), { status });
|
|
32
|
+
},
|
|
33
|
+
body: undefined as unknown,
|
|
34
|
+
};
|
|
35
|
+
return context as unknown as Context & { body: unknown };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
fixture = mkdtempSync(join(tmpdir(), 'git-chat-context-'));
|
|
40
|
+
repoPath = join(fixture, 'repository');
|
|
41
|
+
mkdirSync(repoPath);
|
|
42
|
+
process.env.GIT_REPOS_BASE_PATH = fixture;
|
|
43
|
+
execFileSync('git', ['init', '--initial-branch=main', repoPath], { stdio: 'ignore' });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
if (previousBasePath === undefined) delete process.env.GIT_REPOS_BASE_PATH;
|
|
48
|
+
else process.env.GIT_REPOS_BASE_PATH = previousBasePath;
|
|
49
|
+
if (fixture) rmSync(fixture, { recursive: true, force: true });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('reads a root commit and returns only the bounded changed file context', async () => {
|
|
53
|
+
writeFileSync(join(repoPath, 'root.txt'), 'root content\n');
|
|
54
|
+
const rootCommit = commit('Initial commit');
|
|
55
|
+
|
|
56
|
+
const result = await readChatCommit(createContext(), rootCommit, 50, 1024);
|
|
57
|
+
|
|
58
|
+
expect(result.commitSha).toBe(rootCommit);
|
|
59
|
+
expect(result.subject).toBe('Initial commit');
|
|
60
|
+
expect(result.files).toEqual([{ oldPath: 'root.txt', newPath: 'root.txt', status: 'A' }]);
|
|
61
|
+
expect(result.diff).toContain('+root content');
|
|
62
|
+
expect(result.truncated).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('reads history from the selected branch and applies the requested offset', async () => {
|
|
66
|
+
writeFileSync(join(repoPath, 'root.txt'), 'root\n');
|
|
67
|
+
commit('root');
|
|
68
|
+
writeFileSync(join(repoPath, 'main.txt'), 'main\n');
|
|
69
|
+
commit('main change');
|
|
70
|
+
runGit(['checkout', '-b', 'feature/chat-context']);
|
|
71
|
+
writeFileSync(join(repoPath, 'feature-one.txt'), 'first\n');
|
|
72
|
+
const firstFeatureCommit = commit('first feature change');
|
|
73
|
+
writeFileSync(join(repoPath, 'feature-two.txt'), 'second\n');
|
|
74
|
+
commit('second feature change');
|
|
75
|
+
|
|
76
|
+
const context = createContext({ branch: 'feature/chat-context', skip: 1, maxCount: 1 });
|
|
77
|
+
await log(context, vi.fn().mockResolvedValue(undefined));
|
|
78
|
+
|
|
79
|
+
const body = context.body as { data: { all: Array<{ hash: string }> } };
|
|
80
|
+
expect(body.data.all).toHaveLength(1);
|
|
81
|
+
expect(body.data.all[0].hash).toBe(firstFeatureCommit);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('compares merge commits with their first parent', async () => {
|
|
85
|
+
writeFileSync(join(repoPath, 'root.txt'), 'root\n');
|
|
86
|
+
commit('root');
|
|
87
|
+
runGit(['checkout', '-b', 'feature']);
|
|
88
|
+
writeFileSync(join(repoPath, 'feature.txt'), 'feature change\n');
|
|
89
|
+
commit('feature change');
|
|
90
|
+
runGit(['checkout', 'main']);
|
|
91
|
+
writeFileSync(join(repoPath, 'main-only.txt'), 'main change\n');
|
|
92
|
+
commit('main change');
|
|
93
|
+
runGit(['merge', '--no-ff', 'feature', '-m', 'merge feature']);
|
|
94
|
+
const mergeCommit = runGit(['rev-parse', 'HEAD']);
|
|
95
|
+
|
|
96
|
+
const result = await readChatCommit(createContext(), mergeCommit, 50, 2048);
|
|
97
|
+
|
|
98
|
+
expect(result.subject).toBe('merge feature');
|
|
99
|
+
expect(result.files.map((file) => file.newPath)).toEqual(['feature.txt']);
|
|
100
|
+
expect(result.diff).toContain('+feature change');
|
|
101
|
+
expect(result.diff).not.toContain('main-only.txt');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('lists binary changes without adding binary contents to the diff', async () => {
|
|
105
|
+
writeFileSync(join(repoPath, 'binary.bin'), Buffer.from([0, 1, 2, 255]));
|
|
106
|
+
const binaryCommit = commit('Add binary file');
|
|
107
|
+
|
|
108
|
+
const result = await readChatCommit(createContext(), binaryCommit, 50, 2048);
|
|
109
|
+
|
|
110
|
+
expect(result.files.map((file) => file.newPath)).toEqual(['binary.bin']);
|
|
111
|
+
expect(result.diff).not.toContain('GIT binary patch');
|
|
112
|
+
expect(result.limitations).toContain('Binary file contents are not included in the diff.');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('preserves both names for a detected rename', async () => {
|
|
116
|
+
writeFileSync(join(repoPath, 'before.txt'), 'same contents\n');
|
|
117
|
+
commit('Add file');
|
|
118
|
+
renameSync(join(repoPath, 'before.txt'), join(repoPath, 'after.txt'));
|
|
119
|
+
const renameCommit = commit('Rename file');
|
|
120
|
+
|
|
121
|
+
const result = await readChatCommit(createContext(), renameCommit, 50, 2048);
|
|
122
|
+
|
|
123
|
+
expect(result.files).toEqual([{ oldPath: 'before.txt', newPath: 'after.txt', status: 'R100' }]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('truncates UTF-8 diffs at the byte limit without exceeding it', async () => {
|
|
127
|
+
writeFileSync(join(repoPath, 'unicode.txt'), `${'café 🙂\n'.repeat(80)}`);
|
|
128
|
+
const unicodeCommit = commit('Add unicode content');
|
|
129
|
+
const byteLimit = 280;
|
|
130
|
+
|
|
131
|
+
const result = await readChatCommit(createContext(), unicodeCommit, 50, byteLimit);
|
|
132
|
+
|
|
133
|
+
expect(Buffer.byteLength(result.diff, 'utf8')).toBeLessThanOrEqual(byteLimit);
|
|
134
|
+
expect(result.truncated).toBe(true);
|
|
135
|
+
expect(result.limitations).toContain('The diff was truncated to the configured limit.');
|
|
136
|
+
expect(result.diff).not.toMatch(/\uFFFD$/);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { execFileSync } from 'child_process';
|
|
11
4
|
import { mkdtempSync, mkdirSync, readFileSync, rmSync, symlinkSync, unlinkSync } from 'fs';
|
|
@@ -78,6 +71,58 @@ describe('Git credential destination', () => {
|
|
|
78
71
|
await mergeRequests({ db, action: { params: { repositoryId: 1 } } } as unknown as Context, async () => {});
|
|
79
72
|
expect(fetchMock).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ redirect: 'error' }));
|
|
80
73
|
});
|
|
74
|
+
|
|
75
|
+
it('preserves GitHub next-page state when a merged-state page filters to no results', async () => {
|
|
76
|
+
const fetchMock = vi.fn().mockResolvedValue(
|
|
77
|
+
new Response(JSON.stringify([{ number: 42, state: 'closed', merged_at: null }]), {
|
|
78
|
+
status: 200,
|
|
79
|
+
headers: {
|
|
80
|
+
link: '<https://api.github.com/repos/team/r/pulls?page=2>; rel="next", <https://api.github.com/repos/team/r/pulls?page=4>; rel="last"',
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
85
|
+
const { db } = database('https://github.com/team/r', null, 'github');
|
|
86
|
+
const context = {
|
|
87
|
+
db,
|
|
88
|
+
action: { params: { repositoryId: 1, state: 'merged', page: 1, perPage: 20 } },
|
|
89
|
+
body: undefined as unknown,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
await mergeRequests(context as unknown as Context, async () => {});
|
|
93
|
+
|
|
94
|
+
expect(fetchMock.mock.calls[0][0]).toContain('state=closed');
|
|
95
|
+
expect(context.body).toMatchObject({
|
|
96
|
+
success: true,
|
|
97
|
+
data: [],
|
|
98
|
+
pagination: { page: 1, perPage: 20, total: null, totalPages: null, hasMore: true },
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('uses GitLab next-page headers to preserve hasMore', async () => {
|
|
103
|
+
const fetchMock = vi.fn().mockResolvedValue(
|
|
104
|
+
new Response(JSON.stringify([{ id: 42, iid: 7, title: 'MR', state: 'opened' }]), {
|
|
105
|
+
status: 200,
|
|
106
|
+
headers: { 'x-next-page': '2', 'x-total-pages': '5', 'x-total': '81' },
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
110
|
+
const { db } = database();
|
|
111
|
+
const context = {
|
|
112
|
+
db,
|
|
113
|
+
action: { params: { repositoryId: 1, page: 1, perPage: 20 } },
|
|
114
|
+
body: undefined as unknown,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
await mergeRequests(context as unknown as Context, async () => {});
|
|
118
|
+
|
|
119
|
+
expect(fetchMock.mock.calls[0][0]).toContain('page=1');
|
|
120
|
+
expect(context.body).toMatchObject({
|
|
121
|
+
success: true,
|
|
122
|
+
data: [{ id: 42, iid: 7, title: 'MR' }],
|
|
123
|
+
pagination: { page: 1, perPage: 20, total: 81, totalPages: 5, hasMore: true },
|
|
124
|
+
});
|
|
125
|
+
});
|
|
81
126
|
});
|
|
82
127
|
|
|
83
128
|
describe('Git filesystem and process credentials', () => {
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Application } from '@nocobase/server';
|
|
11
4
|
import { getPollerStatus, pollAllRepos, startPoller, stopPoller } from '../poller';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { enforceRepositoryAccess, enforceRepositoryCollectionAccess } from '../repository-access';
|
|
11
4
|
import { vi } from 'vitest';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { execFileSync } from 'child_process';
|
|
11
4
|
import { createHash } from 'crypto';
|
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import type { Application } from '@nocobase/server';
|
|
11
4
|
import { vi } from 'vitest';
|
|
@@ -1,17 +1,12 @@
|
|
|
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
|
-
*/
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
2
|
|
|
10
3
|
import { execFileSync } from 'child_process';
|
|
11
4
|
import { mkdtempSync, mkdirSync, rmSync, symlinkSync, unlinkSync } from 'fs';
|
|
12
5
|
import { tmpdir } from 'os';
|
|
13
6
|
import { join } from 'path';
|
|
14
7
|
import { createMockServer, type MockServer } from '@nocobase/test';
|
|
8
|
+
import { UNION_ROLE_KEY } from '../../../../@nocobase/plugin-acl/src/server/constants';
|
|
9
|
+
import { SystemRoleMode } from '../../../../@nocobase/plugin-acl/src/server/enum';
|
|
15
10
|
import PluginGitManagerServer from '../plugin';
|
|
16
11
|
|
|
17
12
|
describe('Git Manager HTTP integration', () => {
|
|
@@ -20,6 +15,8 @@ describe('Git Manager HTTP integration', () => {
|
|
|
20
15
|
let fixture: string;
|
|
21
16
|
let repoPath: string;
|
|
22
17
|
let repositoryId: number;
|
|
18
|
+
let otherRepositoryId: number;
|
|
19
|
+
let rootCommitHash: string;
|
|
23
20
|
const originalBase = process.env.GIT_REPOS_BASE_PATH;
|
|
24
21
|
|
|
25
22
|
beforeAll(async () => {
|
|
@@ -45,6 +42,7 @@ describe('Git Manager HTTP integration', () => {
|
|
|
45
42
|
{ stdio: 'ignore' },
|
|
46
43
|
);
|
|
47
44
|
execFileSync('git', ['-C', repoPath, 'branch', 'marker'], { stdio: 'ignore' });
|
|
45
|
+
rootCommitHash = execFileSync('git', ['-C', repoPath, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
|
|
48
46
|
app = await createMockServer({
|
|
49
47
|
acl: true,
|
|
50
48
|
registerActions: true,
|
|
@@ -64,6 +62,29 @@ describe('Git Manager HTTP integration', () => {
|
|
|
64
62
|
values: { name: 'fixture', localPath: 'repository', repoUrl: 'https://gitlab.com/team/fixture.git' },
|
|
65
63
|
});
|
|
66
64
|
repositoryId = repository.get('id');
|
|
65
|
+
const otherRepoPath = join(fixture, 'other-repository');
|
|
66
|
+
mkdirSync(otherRepoPath);
|
|
67
|
+
execFileSync('git', ['init', '-b', 'main', otherRepoPath], { stdio: 'ignore' });
|
|
68
|
+
execFileSync(
|
|
69
|
+
'git',
|
|
70
|
+
[
|
|
71
|
+
'-C',
|
|
72
|
+
otherRepoPath,
|
|
73
|
+
'-c',
|
|
74
|
+
'user.name=Test',
|
|
75
|
+
'-c',
|
|
76
|
+
'user.email=test@example.invalid',
|
|
77
|
+
'commit',
|
|
78
|
+
'--allow-empty',
|
|
79
|
+
'-m',
|
|
80
|
+
'other fixture',
|
|
81
|
+
],
|
|
82
|
+
{ stdio: 'ignore' },
|
|
83
|
+
);
|
|
84
|
+
const otherRepository = await app.db.getRepository('gitRepositories').create({
|
|
85
|
+
values: { name: 'other-fixture', localPath: 'other-repository', repoUrl: 'https://gitlab.com/team/other.git' },
|
|
86
|
+
});
|
|
87
|
+
otherRepositoryId = otherRepository.get('id');
|
|
67
88
|
agent = await app.agent().login(await app.db.getRepository('users').findOne());
|
|
68
89
|
});
|
|
69
90
|
|
|
@@ -82,6 +103,178 @@ describe('Git Manager HTTP integration', () => {
|
|
|
82
103
|
expect(response.body.data).toBeTruthy();
|
|
83
104
|
});
|
|
84
105
|
|
|
106
|
+
it('filters chat context options and rechecks content against SQLite ACL scopes', async () => {
|
|
107
|
+
const createReaderAgent = async (
|
|
108
|
+
roleName: string,
|
|
109
|
+
includeCommitActions: boolean,
|
|
110
|
+
fields = ['id', 'name', 'defaultBranch', 'status'],
|
|
111
|
+
) => {
|
|
112
|
+
await app.db.getRepository('roles').create({ values: { name: roleName } });
|
|
113
|
+
const scope = await agent.resource('dataSourcesRolesResourcesScopes').create({
|
|
114
|
+
values: {
|
|
115
|
+
name: `${roleName} repository scope`,
|
|
116
|
+
resourceName: 'gitRepositories',
|
|
117
|
+
scope: { $and: [{ id: { $in: [repositoryId] } }] },
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
const scopeId = scope.body.data.id;
|
|
121
|
+
const repositoryResource = {
|
|
122
|
+
name: 'gitRepositories',
|
|
123
|
+
usingActionsConfig: true,
|
|
124
|
+
actions: [
|
|
125
|
+
{ name: 'list', fields, scopeId },
|
|
126
|
+
{ name: 'get', fields, scopeId },
|
|
127
|
+
],
|
|
128
|
+
};
|
|
129
|
+
await agent.resource('roles.resources', roleName).create({ values: repositoryResource });
|
|
130
|
+
if (includeCommitActions) {
|
|
131
|
+
const commitResource = {
|
|
132
|
+
name: 'gitManager',
|
|
133
|
+
usingActionsConfig: true,
|
|
134
|
+
actions: ['branches', 'log', 'commitDetail', 'diff'].map((name) => ({ name, scopeId })),
|
|
135
|
+
};
|
|
136
|
+
await agent.resource('roles.resources', roleName).create({ values: commitResource });
|
|
137
|
+
}
|
|
138
|
+
const user = await app.db.getRepository('users').create({ values: { roles: [roleName] } });
|
|
139
|
+
return app.agent().login(user);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const repoReader = await createReaderAgent('git-chat-repo-reader', false);
|
|
143
|
+
const repoResponse = await repoReader.get('/gitManager:chatContextOptions').query({ kind: 'repository' });
|
|
144
|
+
expect(repoResponse.status).toBe(200);
|
|
145
|
+
const repoPayload = repoResponse.body.data.data as {
|
|
146
|
+
capabilities: Record<string, boolean>;
|
|
147
|
+
items: Array<Record<string, unknown>>;
|
|
148
|
+
};
|
|
149
|
+
expect(repoPayload.capabilities.repository).toBe(true);
|
|
150
|
+
expect(repoPayload.capabilities.commit).toBe(false);
|
|
151
|
+
expect(repoPayload.capabilities['merge-request']).toBe(false);
|
|
152
|
+
expect(repoPayload.items).toEqual([
|
|
153
|
+
{ id: repositoryId, name: 'fixture', defaultBranch: 'main', status: expect.any(String) },
|
|
154
|
+
]);
|
|
155
|
+
expect(JSON.stringify(repoResponse.body)).not.toContain('localPath');
|
|
156
|
+
expect(JSON.stringify(repoResponse.body)).not.toContain('repoUrl');
|
|
157
|
+
|
|
158
|
+
const commitReader = await createReaderAgent('git-chat-commit-reader', true);
|
|
159
|
+
const commitOptions = await commitReader
|
|
160
|
+
.get('/gitManager:chatContextOptions')
|
|
161
|
+
.query({ kind: 'commit', repositoryId, pageSize: 1 });
|
|
162
|
+
expect(commitOptions.status).toBe(200);
|
|
163
|
+
const commitPayload = commitOptions.body.data.data as {
|
|
164
|
+
capabilities: Record<string, boolean>;
|
|
165
|
+
items: Array<{ id: number }>;
|
|
166
|
+
};
|
|
167
|
+
expect(commitPayload.capabilities.commit).toBe(true);
|
|
168
|
+
expect(commitPayload.items.map((item) => item.id)).toEqual([repositoryId]);
|
|
169
|
+
|
|
170
|
+
const unauthorizedPreselection = await commitReader
|
|
171
|
+
.get('/gitManager:chatContextOptions')
|
|
172
|
+
.query({ kind: 'commit', repositoryId: otherRepositoryId, pageSize: 1 });
|
|
173
|
+
expect(unauthorizedPreselection.status).toBe(200);
|
|
174
|
+
expect(unauthorizedPreselection.body.data.data.items).toEqual([]);
|
|
175
|
+
|
|
176
|
+
const authorizedContent = await commitReader.get('/gitManager:chatContextContent').query({
|
|
177
|
+
kind: 'commit',
|
|
178
|
+
repositoryId,
|
|
179
|
+
commitHash: rootCommitHash,
|
|
180
|
+
});
|
|
181
|
+
expect(authorizedContent.status).toBe(200);
|
|
182
|
+
const content = authorizedContent.body.data.data as Record<string, unknown>;
|
|
183
|
+
expect(content).toMatchObject({
|
|
184
|
+
type: 'git-commit',
|
|
185
|
+
repositoryId: String(repositoryId),
|
|
186
|
+
commitSha: rootCommitHash,
|
|
187
|
+
});
|
|
188
|
+
expect(JSON.stringify(authorizedContent.body)).not.toContain('localPath');
|
|
189
|
+
expect(JSON.stringify(authorizedContent.body)).not.toContain('repoUrl');
|
|
190
|
+
|
|
191
|
+
const revokedContent = await commitReader.get('/gitManager:chatContextContent').query({
|
|
192
|
+
kind: 'commit',
|
|
193
|
+
repositoryId: otherRepositoryId,
|
|
194
|
+
commitHash: rootCommitHash,
|
|
195
|
+
});
|
|
196
|
+
expect(revokedContent.status).toBe(403);
|
|
197
|
+
|
|
198
|
+
const hiddenNameReader = await createReaderAgent('git-chat-hidden-name-reader', false, [
|
|
199
|
+
'id',
|
|
200
|
+
'defaultBranch',
|
|
201
|
+
'status',
|
|
202
|
+
]);
|
|
203
|
+
const visibleFields = await hiddenNameReader.get('/gitManager:chatContextOptions').query({ kind: 'repository' });
|
|
204
|
+
expect(visibleFields.status).toBe(200);
|
|
205
|
+
expect(visibleFields.body.data.data.items).toEqual([
|
|
206
|
+
{ id: repositoryId, defaultBranch: 'main', status: expect.any(String) },
|
|
207
|
+
]);
|
|
208
|
+
const hiddenNameSearch = await hiddenNameReader
|
|
209
|
+
.get('/gitManager:chatContextOptions')
|
|
210
|
+
.query({ kind: 'repository', search: 'fixture' });
|
|
211
|
+
expect(hiddenNameSearch.status).toBe(200);
|
|
212
|
+
expect(hiddenNameSearch.body.data.data.items).toEqual([]);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('combines repository and Git action permissions from current union roles', async () => {
|
|
216
|
+
const repositoryRole = 'git-chat-union-repository-reader';
|
|
217
|
+
const commitRole = 'git-chat-union-commit-reader';
|
|
218
|
+
await app.db.getRepository('roles').create({ values: { name: repositoryRole } });
|
|
219
|
+
await app.db.getRepository('roles').create({ values: { name: commitRole } });
|
|
220
|
+
const scope = await agent.resource('dataSourcesRolesResourcesScopes').create({
|
|
221
|
+
values: {
|
|
222
|
+
name: 'Git chat union repository scope',
|
|
223
|
+
resourceName: 'gitRepositories',
|
|
224
|
+
scope: { $and: [{ id: { $in: [repositoryId] } }] },
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
const scopeId = scope.body.data.id;
|
|
228
|
+
await agent.resource('roles.resources', repositoryRole).create({
|
|
229
|
+
values: {
|
|
230
|
+
name: 'gitRepositories',
|
|
231
|
+
usingActionsConfig: true,
|
|
232
|
+
actions: [
|
|
233
|
+
{ name: 'list', fields: ['id', 'name', 'defaultBranch', 'status'], scopeId },
|
|
234
|
+
{ name: 'get', fields: ['id', 'name', 'defaultBranch', 'status'], scopeId },
|
|
235
|
+
],
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
await agent.resource('roles.resources', commitRole).create({
|
|
239
|
+
values: {
|
|
240
|
+
name: 'gitManager',
|
|
241
|
+
usingActionsConfig: true,
|
|
242
|
+
actions: ['branches', 'log', 'commitDetail', 'diff'].map((name) => ({ name, scopeId })),
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
const unionUser = await app.db.getRepository('users').create({
|
|
246
|
+
values: { roles: [repositoryRole, commitRole] },
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
await agent.resource('roles').setSystemRoleMode({ values: { roleMode: SystemRoleMode.allowUseUnion } });
|
|
250
|
+
try {
|
|
251
|
+
const unionAgent = await app.agent().login(unionUser, UNION_ROLE_KEY);
|
|
252
|
+
const response = await unionAgent
|
|
253
|
+
.get('/gitManager:chatContextOptions')
|
|
254
|
+
.query({ kind: 'commit', repositoryId, pageSize: 1 });
|
|
255
|
+
|
|
256
|
+
expect(response.status).toBe(200);
|
|
257
|
+
expect(response.body.data.data.capabilities.commit).toBe(true);
|
|
258
|
+
expect(response.body.data.data.items).toEqual([
|
|
259
|
+
{ id: repositoryId, name: 'fixture', defaultBranch: 'main', status: expect.any(String) },
|
|
260
|
+
]);
|
|
261
|
+
} finally {
|
|
262
|
+
await agent.resource('roles').setSystemRoleMode({ values: { roleMode: SystemRoleMode.default } });
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('does not expose Git context capabilities to an authenticated user without Git permissions', async () => {
|
|
267
|
+
await app.db.getRepository('roles').create({ values: { name: 'git-chat-no-access' } });
|
|
268
|
+
const user = await app.db.getRepository('users').create({ values: { roles: ['git-chat-no-access'] } });
|
|
269
|
+
const noGitAgent = await app.agent().login(user);
|
|
270
|
+
|
|
271
|
+
const response = await noGitAgent.get('/gitManager:chatContextOptions');
|
|
272
|
+
|
|
273
|
+
expect(response.status).toBe(200);
|
|
274
|
+
expect(response.body.data.data.capabilities).toEqual({ repository: false, 'merge-request': false, commit: false });
|
|
275
|
+
expect(response.body.data.data.items).toEqual([]);
|
|
276
|
+
});
|
|
277
|
+
|
|
85
278
|
it.each([
|
|
86
279
|
'clone',
|
|
87
280
|
'pull',
|