plugin-git-manager 1.3.3 → 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/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.fc3042c9740d8fab.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.5e507c94c59277b1.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/package.json +2 -2
- package/src/client/__tests__/GitContextPicker.test.tsx +120 -0
- package/src/client/__tests__/ai-context.test.tsx +119 -0
- package/src/client/ai-context.tsx +3 -113
- package/src/client/context/GitManagerContext.tsx +18 -2
- package/src/client/index.tsx +3 -5
- 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 +52 -0
- package/src/server/__tests__/smoke.test.ts +200 -0
- package/src/server/actions/chat-context.ts +302 -0
- package/src/server/actions/git-actions.ts +119 -6
- package/src/server/actions/gitlab-api.ts +97 -4
- package/src/server/chat-context-admission.ts +182 -0
- package/src/server/plugin.ts +11 -0
- package/src/server/request-method-policy.ts +2 -0
package/src/client/index.tsx
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { Plugin } from '@nocobase/client';
|
|
4
4
|
import type PluginAIClientV2 from '@nocobase/plugin-ai/client-v2';
|
|
5
5
|
import React from 'react';
|
|
6
|
-
import {
|
|
6
|
+
import { registerGitWorkContexts } from './ai-context';
|
|
7
7
|
|
|
8
8
|
const GitManagerSettings = React.lazy(() =>
|
|
9
9
|
import('./components/GitManagerSettings').then((m) => ({ default: m.GitManagerSettings })),
|
|
@@ -70,10 +70,8 @@ export class PluginGitManagerClient extends Plugin {
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
const aiManager = this.app.pm.get<PluginAIClientV2>('ai')?.aiManager;
|
|
73
|
-
if (aiManager
|
|
74
|
-
|
|
75
|
-
aiManager.registerWorkContext('git-merge-request', GitMergeRequestWorkContext);
|
|
76
|
-
aiManager.registerWorkContext('git-commit', GitCommitWorkContext);
|
|
73
|
+
if (aiManager) {
|
|
74
|
+
await registerGitWorkContexts(this.app, aiManager);
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
const aclPlugin = this.app.pm.get<PluginACLClient>('acl');
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
1
3
|
import { BranchesOutlined, MergeOutlined, NodeIndexOutlined } from '@ant-design/icons';
|
|
2
4
|
import type { AIManager, ContextItem } from '@nocobase/plugin-ai/client-v2';
|
|
3
|
-
import {
|
|
5
|
+
import { Registry } from '@nocobase/utils/client';
|
|
6
|
+
import { message, Space } from 'antd';
|
|
4
7
|
import React from 'react';
|
|
5
8
|
import { useTranslation } from 'react-i18next';
|
|
9
|
+
import { GitContextPicker } from './components/GitContextPicker';
|
|
10
|
+
import { actionData, fetchGitContext, type GitContextApp, type GitContextKind } from './git-context-api';
|
|
6
11
|
|
|
7
12
|
type WorkContextOptions = Parameters<AIManager['registerWorkContext']>[1];
|
|
13
|
+
type ContextCapabilities = Record<GitContextKind, boolean>;
|
|
14
|
+
|
|
15
|
+
const kinds: GitContextKind[] = ['repository', 'merge-request', 'commit'];
|
|
16
|
+
const emptyCapabilities = (): ContextCapabilities => ({ repository: false, 'merge-request': false, commit: false });
|
|
17
|
+
const registrations = new WeakMap<AIManager, Promise<void>>();
|
|
8
18
|
|
|
9
|
-
function ContextLabel({ kind, title }: { kind:
|
|
19
|
+
function ContextLabel({ kind, title }: { kind: GitContextKind; title?: string }) {
|
|
10
20
|
const { t } = useTranslation('plugin-git-manager');
|
|
11
21
|
const labels = { repository: 'Git Repository', 'merge-request': 'Git Merge Request', commit: 'Git Commit' };
|
|
12
22
|
const icons = { repository: <BranchesOutlined />, 'merge-request': <MergeOutlined />, commit: <NodeIndexOutlined /> };
|
|
@@ -18,82 +28,135 @@ function ContextLabel({ kind, title }: { kind: 'repository' | 'merge-request' |
|
|
|
18
28
|
);
|
|
19
29
|
}
|
|
20
30
|
|
|
21
|
-
function
|
|
22
|
-
const data = (response as { data?: { data?: unknown } })?.data?.data;
|
|
23
|
-
return data && typeof data === 'object' ? (data as Record<string, unknown>) : null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function contextComponents(kind: 'repository' | 'merge-request' | 'commit') {
|
|
31
|
+
function contextDefinition(app: GitContextApp, kind: GitContextKind): WorkContextOptions {
|
|
27
32
|
return {
|
|
28
|
-
|
|
33
|
+
name: `git-${kind}`,
|
|
29
34
|
tag: {
|
|
30
35
|
Component: ({ item }: { item: ContextItem }) => <ContextLabel kind={kind} title={item.title || item.uid} />,
|
|
31
36
|
},
|
|
37
|
+
getContent: async (currentApp, { uid }) => {
|
|
38
|
+
try {
|
|
39
|
+
return await fetchGitContext(app, kind, uid);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
message.error(
|
|
42
|
+
currentApp.i18n.t('Git context is unavailable. Check your access and select it again.', {
|
|
43
|
+
ns: 'plugin-git-manager',
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
32
49
|
};
|
|
33
50
|
}
|
|
34
51
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
function contextMenu(
|
|
53
|
+
app: GitContextApp,
|
|
54
|
+
kind: GitContextKind,
|
|
55
|
+
isAllowed: () => boolean,
|
|
56
|
+
): NonNullable<WorkContextOptions['menu']> {
|
|
57
|
+
return {
|
|
58
|
+
Component: () => <ContextLabel kind={kind} />,
|
|
59
|
+
onClick: ({ ctx, onAdd }) => {
|
|
60
|
+
if (!isAllowed()) return;
|
|
61
|
+
let active = true;
|
|
62
|
+
const dialog = ctx.viewer.dialog({
|
|
63
|
+
title: <ContextLabel kind={kind} />,
|
|
64
|
+
width: 720,
|
|
65
|
+
zIndex: 2200,
|
|
66
|
+
onClose: () => {
|
|
67
|
+
active = false;
|
|
68
|
+
},
|
|
69
|
+
content: () => (
|
|
70
|
+
<GitContextPicker
|
|
71
|
+
app={app}
|
|
72
|
+
kind={kind}
|
|
73
|
+
onAdd={onAdd}
|
|
74
|
+
isActive={() => active && isAllowed()}
|
|
75
|
+
onClose={() => {
|
|
76
|
+
dialog.close();
|
|
77
|
+
}}
|
|
78
|
+
/>
|
|
79
|
+
),
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
53
84
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
};
|
|
85
|
+
/** Register readers even when the current role cannot add Git context. */
|
|
86
|
+
export function registerGitWorkContexts(app: GitContextApp, manager: AIManager): Promise<void> {
|
|
87
|
+
let registration = registrations.get(manager);
|
|
88
|
+
if (!registration) {
|
|
89
|
+
registration = initializeGitWorkContexts(app, manager);
|
|
90
|
+
registrations.set(manager, registration);
|
|
91
|
+
}
|
|
92
|
+
return registration;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function initializeGitWorkContexts(app: GitContextApp, manager: AIManager): Promise<void> {
|
|
96
|
+
let capabilities = emptyCapabilities();
|
|
97
|
+
let requestSequence = 0;
|
|
98
|
+
|
|
99
|
+
for (const kind of kinds) {
|
|
100
|
+
const name = `git-${kind}`;
|
|
101
|
+
if (!manager.getWorkContext(name)) manager.registerWorkContext(name, contextDefinition(app, kind));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const updateMenus = () => {
|
|
105
|
+
const registry = new Registry<WorkContextOptions>();
|
|
106
|
+
for (const [name, existing] of manager.workContext.getEntities()) {
|
|
107
|
+
const kind = kinds.find((item) => name === `git-${item}`);
|
|
108
|
+
if (!kind) {
|
|
109
|
+
registry.register(name, existing);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const { menu: _menu, ...definition } = existing;
|
|
113
|
+
registry.register(name, {
|
|
114
|
+
...definition,
|
|
115
|
+
...(capabilities[kind] ? { menu: contextMenu(app, kind, () => capabilities[kind]) } : {}),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
manager.workContext = registry;
|
|
119
|
+
};
|
|
82
120
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
121
|
+
const refresh = async () => {
|
|
122
|
+
const sequence = ++requestSequence;
|
|
123
|
+
capabilities = emptyCapabilities();
|
|
124
|
+
updateMenus();
|
|
125
|
+
if (app.apiClient.auth && !app.apiClient.auth.token) return;
|
|
126
|
+
try {
|
|
127
|
+
const result = actionData(
|
|
128
|
+
await app.apiClient.request({ url: 'gitManager:chatContextOptions', skipNotify: true, timeout: 5000 }),
|
|
129
|
+
) as { capabilities?: Partial<ContextCapabilities> } | undefined;
|
|
130
|
+
if (sequence !== requestSequence) return;
|
|
131
|
+
capabilities = {
|
|
132
|
+
repository: result?.capabilities?.repository === true,
|
|
133
|
+
'merge-request': result?.capabilities?.['merge-request'] === true,
|
|
134
|
+
commit: result?.capabilities?.commit === true,
|
|
135
|
+
};
|
|
136
|
+
updateMenus();
|
|
137
|
+
} catch {
|
|
138
|
+
// A failed capability request keeps all menu entries hidden.
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
app.eventBus.addEventListener('auth:tokenChanged', () => {
|
|
143
|
+
refresh();
|
|
144
|
+
});
|
|
145
|
+
const client = app.apiClient as typeof app.apiClient & {
|
|
146
|
+
axios?: {
|
|
147
|
+
interceptors?: {
|
|
148
|
+
response?: {
|
|
149
|
+
use: (handler: (response: { config?: { url?: string } }) => { config?: { url?: string } }) => unknown;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
client.axios?.interceptors?.response?.use((response) => {
|
|
155
|
+
if (/(^|\/)roles:check(?:\?|$)/.test(response.config?.url || '')) {
|
|
156
|
+
// ACLRolesCheckProvider applies the returned role after this interceptor resolves.
|
|
157
|
+
setTimeout(() => refresh(), 0);
|
|
158
|
+
}
|
|
159
|
+
return response;
|
|
160
|
+
});
|
|
161
|
+
await refresh();
|
|
162
|
+
}
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import { Alert, Button, Form, Input, Select, Space, Spin, Typography } from 'antd';
|
|
4
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import { useTranslation } from 'react-i18next';
|
|
6
|
+
import {
|
|
7
|
+
actionBody,
|
|
8
|
+
actionData,
|
|
9
|
+
fetchGitContext,
|
|
10
|
+
getCurrentGitRepository,
|
|
11
|
+
type GitContextApp,
|
|
12
|
+
type GitContextKind,
|
|
13
|
+
} from '../git-context-api';
|
|
14
|
+
|
|
15
|
+
type Repository = { id: number; name?: string; defaultBranch?: string; status?: string };
|
|
16
|
+
type MergeRequest = {
|
|
17
|
+
iid: number;
|
|
18
|
+
title: string;
|
|
19
|
+
sourceBranch?: string;
|
|
20
|
+
targetBranch?: string;
|
|
21
|
+
author?: { name?: string; username?: string };
|
|
22
|
+
updatedAt?: string;
|
|
23
|
+
};
|
|
24
|
+
type Commit = { hash: string; message?: string; subject?: string; author_name?: string; date?: string };
|
|
25
|
+
type ContextSelection = { uid: string; title?: string };
|
|
26
|
+
|
|
27
|
+
const PAGE_SIZE = 30;
|
|
28
|
+
|
|
29
|
+
function formatDate(value?: string): string {
|
|
30
|
+
if (!value) return '';
|
|
31
|
+
const date = new Date(value);
|
|
32
|
+
return Number.isNaN(date.getTime()) ? value : date.toLocaleString();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function GitContextPicker({
|
|
36
|
+
app,
|
|
37
|
+
kind,
|
|
38
|
+
onAdd,
|
|
39
|
+
isActive,
|
|
40
|
+
onClose,
|
|
41
|
+
}: {
|
|
42
|
+
app: GitContextApp;
|
|
43
|
+
kind: GitContextKind;
|
|
44
|
+
onAdd: (item: ContextSelection) => void;
|
|
45
|
+
isActive?: () => boolean;
|
|
46
|
+
onClose: () => void;
|
|
47
|
+
}) {
|
|
48
|
+
const { t } = useTranslation('plugin-git-manager');
|
|
49
|
+
const [repositoryQuery, setRepositoryQuery] = useState('');
|
|
50
|
+
const [repositoryPage, setRepositoryPage] = useState(1);
|
|
51
|
+
const [repositories, setRepositories] = useState<Repository[]>([]);
|
|
52
|
+
const [repositoryHasMore, setRepositoryHasMore] = useState(false);
|
|
53
|
+
const [repository, setRepository] = useState<Repository>();
|
|
54
|
+
const [repositoryLoading, setRepositoryLoading] = useState(false);
|
|
55
|
+
const [branchLoading, setBranchLoading] = useState(false);
|
|
56
|
+
const [branches, setBranches] = useState<string[]>([]);
|
|
57
|
+
const [branch, setBranch] = useState<string>();
|
|
58
|
+
const [state, setState] = useState('opened');
|
|
59
|
+
const [itemQuery, setItemQuery] = useState('');
|
|
60
|
+
const [itemPage, setItemPage] = useState(1);
|
|
61
|
+
const [mergeRequests, setMergeRequests] = useState<MergeRequest[]>([]);
|
|
62
|
+
const [commits, setCommits] = useState<Commit[]>([]);
|
|
63
|
+
const [itemHasMore, setItemHasMore] = useState(false);
|
|
64
|
+
const [itemLoading, setItemLoading] = useState(false);
|
|
65
|
+
const [selectedItem, setSelectedItem] = useState<string>();
|
|
66
|
+
const [repositoryError, setRepositoryError] = useState('');
|
|
67
|
+
const [branchError, setBranchError] = useState('');
|
|
68
|
+
const [itemError, setItemError] = useState('');
|
|
69
|
+
const [attachmentError, setAttachmentError] = useState('');
|
|
70
|
+
const [submitting, setSubmitting] = useState(false);
|
|
71
|
+
const repositoryTouched = useRef(false);
|
|
72
|
+
const mounted = useRef(true);
|
|
73
|
+
const submittingRef = useRef(false);
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
mounted.current = true;
|
|
77
|
+
return () => {
|
|
78
|
+
mounted.current = false;
|
|
79
|
+
};
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const repositoryId = getCurrentGitRepository(app);
|
|
84
|
+
if (repositoryId == null) return;
|
|
85
|
+
let cancelled = false;
|
|
86
|
+
app.apiClient
|
|
87
|
+
.request({
|
|
88
|
+
url: 'gitManager:chatContextOptions',
|
|
89
|
+
params: { kind, repositoryId, page: 1, pageSize: 1 },
|
|
90
|
+
skipNotify: true,
|
|
91
|
+
})
|
|
92
|
+
.then((response) => {
|
|
93
|
+
const data = actionData(response) as { items?: Repository[] } | undefined;
|
|
94
|
+
if (!cancelled && !repositoryTouched.current && data?.items?.[0]) setRepository(data.items[0]);
|
|
95
|
+
})
|
|
96
|
+
.catch(() => undefined);
|
|
97
|
+
return () => {
|
|
98
|
+
cancelled = true;
|
|
99
|
+
};
|
|
100
|
+
}, [app, kind]);
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
let cancelled = false;
|
|
104
|
+
setRepositoryLoading(true);
|
|
105
|
+
app.apiClient
|
|
106
|
+
.request({
|
|
107
|
+
url: 'gitManager:chatContextOptions',
|
|
108
|
+
params: { kind, search: repositoryQuery, page: repositoryPage, pageSize: PAGE_SIZE },
|
|
109
|
+
skipNotify: true,
|
|
110
|
+
})
|
|
111
|
+
.then((response) => {
|
|
112
|
+
if (cancelled) return;
|
|
113
|
+
const data = actionData(response) as { items?: Repository[]; hasMore?: boolean } | undefined;
|
|
114
|
+
const items = Array.isArray(data?.items) ? data.items : [];
|
|
115
|
+
setRepositories((current) => (repositoryPage === 1 ? items : [...current, ...items]));
|
|
116
|
+
setRepositoryHasMore(data?.hasMore === true);
|
|
117
|
+
setRepositoryError('');
|
|
118
|
+
})
|
|
119
|
+
.finally(() => {
|
|
120
|
+
if (!cancelled) setRepositoryLoading(false);
|
|
121
|
+
})
|
|
122
|
+
.catch(() => {
|
|
123
|
+
if (!cancelled) setRepositoryError(t('Unable to load Git context. Check your access and try again.'));
|
|
124
|
+
});
|
|
125
|
+
return () => {
|
|
126
|
+
cancelled = true;
|
|
127
|
+
};
|
|
128
|
+
}, [app, kind, repositoryPage, repositoryQuery, t]);
|
|
129
|
+
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
setBranches([]);
|
|
132
|
+
setBranch(undefined);
|
|
133
|
+
setBranchError('');
|
|
134
|
+
setBranchLoading(false);
|
|
135
|
+
if (kind !== 'commit' || !repository) return;
|
|
136
|
+
let cancelled = false;
|
|
137
|
+
setBranchLoading(true);
|
|
138
|
+
app.apiClient
|
|
139
|
+
.request({ url: 'gitManager:branches', params: { repositoryId: repository.id }, skipNotify: true })
|
|
140
|
+
.then((response) => {
|
|
141
|
+
if (cancelled) return;
|
|
142
|
+
const data = actionData(response) as { all?: string[]; current?: string; detached?: boolean } | undefined;
|
|
143
|
+
const available = Array.isArray(data?.all) ? data.all : [];
|
|
144
|
+
setBranches(available);
|
|
145
|
+
const current = !data?.detached && data?.current ? data.current : undefined;
|
|
146
|
+
const preferred = current && available.includes(current) ? current : repository.defaultBranch;
|
|
147
|
+
setBranch(preferred && available.includes(preferred) ? preferred : available[0]);
|
|
148
|
+
setBranchError('');
|
|
149
|
+
})
|
|
150
|
+
.finally(() => {
|
|
151
|
+
if (!cancelled) setBranchLoading(false);
|
|
152
|
+
})
|
|
153
|
+
.catch(() => {
|
|
154
|
+
if (!cancelled) setBranchError(t('Unable to load Git context. Check your access and try again.'));
|
|
155
|
+
});
|
|
156
|
+
return () => {
|
|
157
|
+
cancelled = true;
|
|
158
|
+
};
|
|
159
|
+
}, [app, kind, repository, t]);
|
|
160
|
+
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
if (!repository || kind === 'repository' || (kind === 'commit' && !branch)) {
|
|
163
|
+
setItemLoading(false);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
let cancelled = false;
|
|
167
|
+
setItemLoading(true);
|
|
168
|
+
const params =
|
|
169
|
+
kind === 'merge-request'
|
|
170
|
+
? { repositoryId: repository.id, state, page: itemPage, perPage: PAGE_SIZE }
|
|
171
|
+
: { repositoryId: repository.id, branch, skip: (itemPage - 1) * PAGE_SIZE, maxCount: PAGE_SIZE };
|
|
172
|
+
app.apiClient
|
|
173
|
+
.request({
|
|
174
|
+
url: kind === 'merge-request' ? 'gitManager:mergeRequests' : 'gitManager:log',
|
|
175
|
+
params,
|
|
176
|
+
skipNotify: true,
|
|
177
|
+
})
|
|
178
|
+
.then((response) => {
|
|
179
|
+
if (cancelled) return;
|
|
180
|
+
if (kind === 'merge-request') {
|
|
181
|
+
const items = actionData(response);
|
|
182
|
+
const list = Array.isArray(items) ? (items as MergeRequest[]) : [];
|
|
183
|
+
const body = actionBody(response) as
|
|
184
|
+
| { pagination?: { totalPages?: number | null; hasMore?: boolean } }
|
|
185
|
+
| undefined;
|
|
186
|
+
setMergeRequests((current) => (itemPage === 1 ? list : [...current, ...list]));
|
|
187
|
+
setItemHasMore(
|
|
188
|
+
body?.pagination?.hasMore ??
|
|
189
|
+
(body?.pagination?.totalPages != null
|
|
190
|
+
? itemPage < body.pagination.totalPages
|
|
191
|
+
: list.length === PAGE_SIZE),
|
|
192
|
+
);
|
|
193
|
+
} else {
|
|
194
|
+
const data = actionData(response) as { all?: Commit[] } | undefined;
|
|
195
|
+
const list = Array.isArray(data?.all) ? data.all : [];
|
|
196
|
+
setCommits((current) => (itemPage === 1 ? list : [...current, ...list]));
|
|
197
|
+
setItemHasMore(list.length === PAGE_SIZE);
|
|
198
|
+
}
|
|
199
|
+
setItemError('');
|
|
200
|
+
})
|
|
201
|
+
.finally(() => {
|
|
202
|
+
if (!cancelled) setItemLoading(false);
|
|
203
|
+
})
|
|
204
|
+
.catch(() => {
|
|
205
|
+
if (!cancelled) setItemError(t('Unable to load Git context. Check your access and try again.'));
|
|
206
|
+
});
|
|
207
|
+
return () => {
|
|
208
|
+
cancelled = true;
|
|
209
|
+
};
|
|
210
|
+
}, [app, branch, itemPage, kind, repository, state, t]);
|
|
211
|
+
|
|
212
|
+
const filteredMergeRequests = useMemo(() => {
|
|
213
|
+
const query = itemQuery.trim().toLowerCase().replace(/^!/, '');
|
|
214
|
+
return mergeRequests.filter((item) => `${item.iid} ${item.title}`.toLowerCase().includes(query));
|
|
215
|
+
}, [itemQuery, mergeRequests]);
|
|
216
|
+
const filteredCommits = useMemo(() => {
|
|
217
|
+
const query = itemQuery.trim().toLowerCase();
|
|
218
|
+
return commits.filter((item) => `${item.hash} ${item.subject || item.message || ''}`.toLowerCase().includes(query));
|
|
219
|
+
}, [itemQuery, commits]);
|
|
220
|
+
|
|
221
|
+
const changeRepository = (id: number) => {
|
|
222
|
+
repositoryTouched.current = true;
|
|
223
|
+
setRepository(repositories.find((item) => item.id === id));
|
|
224
|
+
setBranch(undefined);
|
|
225
|
+
setBranches([]);
|
|
226
|
+
setSelectedItem(undefined);
|
|
227
|
+
setItemPage(1);
|
|
228
|
+
setMergeRequests([]);
|
|
229
|
+
setCommits([]);
|
|
230
|
+
setItemHasMore(false);
|
|
231
|
+
setItemQuery('');
|
|
232
|
+
setItemError('');
|
|
233
|
+
setAttachmentError('');
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const attach = async () => {
|
|
237
|
+
if (submittingRef.current || !repository || (kind !== 'repository' && !selectedItem)) return;
|
|
238
|
+
const uid = kind === 'repository' ? String(repository.id) : `${repository.id}:${selectedItem}`;
|
|
239
|
+
const selectedMR = mergeRequests.find((item) => String(item.iid) === selectedItem);
|
|
240
|
+
const selectedCommit = commits.find((item) => item.hash === selectedItem);
|
|
241
|
+
const repoName = repository.name || String(repository.id);
|
|
242
|
+
const title =
|
|
243
|
+
kind === 'repository'
|
|
244
|
+
? repoName
|
|
245
|
+
: kind === 'merge-request'
|
|
246
|
+
? `${repoName} · !${selectedItem} · ${selectedMR?.title || ''}`
|
|
247
|
+
: `${repoName} · ${selectedItem?.slice(0, 7)} · ${selectedCommit?.subject || selectedCommit?.message || ''}`;
|
|
248
|
+
submittingRef.current = true;
|
|
249
|
+
setSubmitting(true);
|
|
250
|
+
try {
|
|
251
|
+
await fetchGitContext(app, kind, uid);
|
|
252
|
+
if (!mounted.current || (isActive && !isActive())) return;
|
|
253
|
+
onAdd({ uid, title });
|
|
254
|
+
onClose();
|
|
255
|
+
} catch {
|
|
256
|
+
if (mounted.current) setAttachmentError(t('Git context is unavailable. Check your access and select it again.'));
|
|
257
|
+
} finally {
|
|
258
|
+
submittingRef.current = false;
|
|
259
|
+
if (mounted.current) setSubmitting(false);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<Form layout="vertical" onFinish={attach} disabled={submitting}>
|
|
265
|
+
<Form.Item label={t('Search repositories')}>
|
|
266
|
+
<Input.Search
|
|
267
|
+
allowClear
|
|
268
|
+
onSearch={(value, event) => {
|
|
269
|
+
event?.preventDefault();
|
|
270
|
+
repositoryTouched.current = true;
|
|
271
|
+
setRepositoryPage(1);
|
|
272
|
+
setRepositoryQuery(value);
|
|
273
|
+
}}
|
|
274
|
+
aria-label={t('Search repositories')}
|
|
275
|
+
/>
|
|
276
|
+
</Form.Item>
|
|
277
|
+
<Form.Item label={t('Repository')} required>
|
|
278
|
+
<Select
|
|
279
|
+
aria-label={t('Repository')}
|
|
280
|
+
loading={repositoryLoading}
|
|
281
|
+
value={repository?.id}
|
|
282
|
+
onChange={changeRepository}
|
|
283
|
+
placeholder={t('Select a repository')}
|
|
284
|
+
options={(repository && !repositories.some((item) => item.id === repository.id)
|
|
285
|
+
? [repository, ...repositories]
|
|
286
|
+
: repositories
|
|
287
|
+
).map((item) => ({
|
|
288
|
+
value: item.id,
|
|
289
|
+
label: [item.name || String(item.id), item.defaultBranch, item.status].filter(Boolean).join(' · '),
|
|
290
|
+
}))}
|
|
291
|
+
notFoundContent={repositoryLoading ? <Spin size="small" /> : t('No repositories available')}
|
|
292
|
+
/>
|
|
293
|
+
{repositoryHasMore && (
|
|
294
|
+
<Button type="link" loading={repositoryLoading} onClick={() => setRepositoryPage((page) => page + 1)}>
|
|
295
|
+
{t('Load more repositories')}
|
|
296
|
+
</Button>
|
|
297
|
+
)}
|
|
298
|
+
</Form.Item>
|
|
299
|
+
{kind === 'commit' && repository && (
|
|
300
|
+
<Form.Item label={t('Branch')} required>
|
|
301
|
+
<Select
|
|
302
|
+
aria-label={t('Branch')}
|
|
303
|
+
loading={branchLoading}
|
|
304
|
+
value={branch}
|
|
305
|
+
placeholder={t('Select a branch')}
|
|
306
|
+
options={branches.map((name) => ({ value: name, label: name }))}
|
|
307
|
+
onChange={(value) => {
|
|
308
|
+
setBranch(value);
|
|
309
|
+
setItemPage(1);
|
|
310
|
+
setCommits([]);
|
|
311
|
+
setItemHasMore(false);
|
|
312
|
+
setSelectedItem(undefined);
|
|
313
|
+
setItemError('');
|
|
314
|
+
setAttachmentError('');
|
|
315
|
+
}}
|
|
316
|
+
/>
|
|
317
|
+
</Form.Item>
|
|
318
|
+
)}
|
|
319
|
+
{kind === 'merge-request' && repository && (
|
|
320
|
+
<Form.Item label={t('State')}>
|
|
321
|
+
<Select
|
|
322
|
+
aria-label={t('State')}
|
|
323
|
+
value={state}
|
|
324
|
+
options={['opened', 'merged', 'closed', 'all'].map((value) => ({ value, label: t(value) }))}
|
|
325
|
+
onChange={(value) => {
|
|
326
|
+
setState(value);
|
|
327
|
+
setItemPage(1);
|
|
328
|
+
setMergeRequests([]);
|
|
329
|
+
setItemHasMore(false);
|
|
330
|
+
setSelectedItem(undefined);
|
|
331
|
+
setItemError('');
|
|
332
|
+
setAttachmentError('');
|
|
333
|
+
}}
|
|
334
|
+
/>
|
|
335
|
+
</Form.Item>
|
|
336
|
+
)}
|
|
337
|
+
{kind !== 'repository' && repository && (kind !== 'commit' || branch) && (
|
|
338
|
+
<>
|
|
339
|
+
<Form.Item label={t('Search loaded results')}>
|
|
340
|
+
<Input
|
|
341
|
+
aria-label={t('Search loaded results')}
|
|
342
|
+
value={itemQuery}
|
|
343
|
+
onChange={(event) => {
|
|
344
|
+
setItemQuery(event.target.value);
|
|
345
|
+
setSelectedItem(undefined);
|
|
346
|
+
setAttachmentError('');
|
|
347
|
+
}}
|
|
348
|
+
onPressEnter={(event) => event.preventDefault()}
|
|
349
|
+
allowClear
|
|
350
|
+
/>
|
|
351
|
+
</Form.Item>
|
|
352
|
+
<Form.Item label={t(kind === 'commit' ? 'Commit' : 'Merge Request')} required>
|
|
353
|
+
<Select
|
|
354
|
+
aria-label={t(kind === 'commit' ? 'Commit' : 'Merge Request')}
|
|
355
|
+
loading={itemLoading}
|
|
356
|
+
value={selectedItem}
|
|
357
|
+
onChange={setSelectedItem}
|
|
358
|
+
placeholder={t(kind === 'commit' ? 'Select a commit' : 'Select a merge request')}
|
|
359
|
+
options={
|
|
360
|
+
kind === 'merge-request'
|
|
361
|
+
? filteredMergeRequests.map((item) => ({
|
|
362
|
+
value: String(item.iid),
|
|
363
|
+
label: [
|
|
364
|
+
`!${item.iid} · ${item.title}`,
|
|
365
|
+
item.sourceBranch && item.targetBranch ? `${item.sourceBranch} → ${item.targetBranch}` : '',
|
|
366
|
+
item.author?.name || item.author?.username,
|
|
367
|
+
formatDate(item.updatedAt),
|
|
368
|
+
]
|
|
369
|
+
.filter(Boolean)
|
|
370
|
+
.join(' · '),
|
|
371
|
+
}))
|
|
372
|
+
: filteredCommits.map((item) => ({
|
|
373
|
+
value: item.hash,
|
|
374
|
+
label: [
|
|
375
|
+
item.hash.slice(0, 7),
|
|
376
|
+
item.subject || item.message,
|
|
377
|
+
item.author_name,
|
|
378
|
+
formatDate(item.date),
|
|
379
|
+
]
|
|
380
|
+
.filter(Boolean)
|
|
381
|
+
.join(' · '),
|
|
382
|
+
}))
|
|
383
|
+
}
|
|
384
|
+
notFoundContent={itemLoading ? <Spin size="small" /> : t('No results')}
|
|
385
|
+
/>
|
|
386
|
+
{itemHasMore && (
|
|
387
|
+
<Button type="link" loading={itemLoading} onClick={() => setItemPage((page) => page + 1)}>
|
|
388
|
+
{t('Load more results')}
|
|
389
|
+
</Button>
|
|
390
|
+
)}
|
|
391
|
+
</Form.Item>
|
|
392
|
+
</>
|
|
393
|
+
)}
|
|
394
|
+
{(attachmentError || itemError || branchError || repositoryError) && (
|
|
395
|
+
<Alert
|
|
396
|
+
type="error"
|
|
397
|
+
showIcon
|
|
398
|
+
message={attachmentError || itemError || branchError || repositoryError}
|
|
399
|
+
style={{ marginBottom: 16 }}
|
|
400
|
+
/>
|
|
401
|
+
)}
|
|
402
|
+
<Space style={{ display: 'flex', justifyContent: 'flex-end' }}>
|
|
403
|
+
<Button onClick={onClose}>{t('Cancel')}</Button>
|
|
404
|
+
<Button
|
|
405
|
+
type="primary"
|
|
406
|
+
htmlType="submit"
|
|
407
|
+
loading={submitting}
|
|
408
|
+
disabled={!repository || (kind !== 'repository' && !selectedItem)}
|
|
409
|
+
>
|
|
410
|
+
{t('Attach to chat')}
|
|
411
|
+
</Button>
|
|
412
|
+
</Space>
|
|
413
|
+
<Typography.Text type="secondary" aria-live="polite">
|
|
414
|
+
{itemLoading || repositoryLoading || branchLoading ? t('Loading') : ''}
|
|
415
|
+
</Typography.Text>
|
|
416
|
+
</Form>
|
|
417
|
+
);
|
|
418
|
+
}
|