-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab-client.ts
More file actions
426 lines (372 loc) · 13.5 KB
/
gitlab-client.ts
File metadata and controls
426 lines (372 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { Gitlab } from '@gitbeaker/node';
import { Logger } from '../utils/logger';
import {
IGitlabProject,
IGitlabBranch,
IGitlabMergeRequest,
IGitlabDiscussion,
ICodeSuggestion,
ICommitFile,
} from '../models/gitlab-models';
export class GitlabConnectionError extends Error {
constructor(
message: string,
public readonly cause?: Error
) {
super(message);
this.name = 'GitlabConnectionError';
}
}
export class GitlabAuthenticationError extends Error {
constructor(message: string) {
super(message);
this.name = 'GitlabAuthenticationError';
}
}
export interface IGitlabClientConfig {
serverUrl: string;
token: string;
projectId?: string;
}
export class GitlabClient {
private readonly _api: InstanceType<typeof Gitlab>;
private readonly _logger: Logger;
private readonly _projectId?: string;
constructor(config: IGitlabClientConfig, logger: Logger) {
this._logger = logger;
this._projectId = config.projectId;
// 移除serverUrl末尾的斜杠,避免双斜杠问题
const serverUrl = config.serverUrl.replace(/\/+$/, '');
this._api = new Gitlab({
host: serverUrl,
token: config.token,
});
this._logger.info('GitLab client initialized', { serverUrl });
}
public async testConnection(): Promise<boolean> {
try {
this._logger.debug('Testing GitLab connection...');
const user = await this._api.Users.current();
this._logger.info('GitLab connection test successful', { user: user.username });
return true;
} catch (error) {
this._logger.error('GitLab connection test failed', error);
if (this.isGitlabError(error) && error.response?.status === 401) {
throw new GitlabAuthenticationError('GitLab认证失败,请检查Personal Access Token');
}
throw new GitlabConnectionError(`连接GitLab失败: ${String(error)}`);
}
}
public async getProject(projectId?: string): Promise<IGitlabProject> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Fetching GitLab project', { projectId: id });
const project = await this._api.Projects.show(id);
return {
id: project.id,
name: project.name,
path: project.path,
pathWithNamespace: project.path_with_namespace,
httpUrlToRepo: project.http_url_to_repo,
defaultBranch: project.default_branch || 'main',
};
} catch (error) {
this._logger.error('Failed to fetch GitLab project', error);
throw new GitlabConnectionError(`获取GitLab项目失败: ${String(error)}`);
}
}
public async getBranches(projectId?: string): Promise<IGitlabBranch[]> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Fetching GitLab branches', { projectId: id });
const branches = await this._api.Branches.all(id);
return branches.map((b: any) => ({
name: String(b.name || ''),
commit: {
id: String(b.commit?.id || ''),
shortId: String(b.commit?.short_id || ''),
message: String(b.commit?.message || ''),
},
protected: Boolean(b.protected),
}));
} catch (error) {
this._logger.error('Failed to fetch GitLab branches', error);
throw new GitlabConnectionError(`获取Git分支失败: ${String(error)}`);
}
}
public async createBranch(
branchName: string,
ref: string,
projectId?: string
): Promise<IGitlabBranch> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Creating GitLab branch', { projectId: id, branchName, ref });
const branch = await this._api.Branches.create(id, branchName, ref);
this._logger.info('GitLab branch created successfully', { branchName });
return {
name: String(branch.name || ''),
commit: {
id: String(branch.commit?.id || ''),
shortId: String(branch.commit?.short_id || ''),
message: String(branch.commit?.message || ''),
},
protected: Boolean(branch.protected),
};
} catch (error) {
this._logger.error('Failed to create GitLab branch', error);
throw new GitlabConnectionError(`创建Git分支失败: ${String(error)}`);
}
}
public async commitFiles(
branchName: string,
commitMessage: string,
files: ICommitFile[],
projectId?: string
): Promise<void> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Committing files to GitLab', {
projectId: id,
branchName,
fileCount: files.length,
});
await this._api.Commits.create(id, branchName, commitMessage, files);
this._logger.info('Files committed successfully', { branchName, fileCount: files.length });
} catch (error) {
this._logger.error('Failed to commit files', error);
throw new GitlabConnectionError(`提交文件失败: ${String(error)}`);
}
}
public async createMergeRequest(
sourceBranch: string,
targetBranch: string,
title: string,
description: string,
projectId?: string
): Promise<IGitlabMergeRequest> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Creating GitLab merge request', {
projectId: id,
sourceBranch,
targetBranch,
});
const mr = await this._api.MergeRequests.create(id, sourceBranch, targetBranch, title, {
description,
});
this._logger.info('Merge request created successfully', { mrIid: mr.iid });
return this.transformMergeRequest(mr);
} catch (error) {
this._logger.error('Failed to create merge request', error);
throw new GitlabConnectionError(`创建Merge Request失败: ${String(error)}`);
}
}
public async getMergeRequest(mrIid: number, projectId?: string): Promise<IGitlabMergeRequest> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Fetching GitLab merge request', { projectId: id, mrIid });
const mr = await this._api.MergeRequests.show(id, mrIid);
return this.transformMergeRequest(mr);
} catch (error) {
this._logger.error('Failed to fetch merge request', error);
throw new GitlabConnectionError(`获取Merge Request失败: ${String(error)}`);
}
}
public async getMergeRequestDiscussions(
mrIid: number,
projectId?: string
): Promise<IGitlabDiscussion[]> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
this._logger.debug('Fetching merge request discussions', { projectId: id, mrIid });
const discussions = await this._api.MergeRequestDiscussions.all(id, mrIid);
return discussions.map((d: any) => ({
id: d.id,
resolved: d.resolved,
notes: d.notes.map((n: any) => ({
id: n.id,
body: n.body,
author: {
id: n.author.id,
name: n.author.name,
username: n.author.username,
},
createdAt: n.created_at,
resolvable: n.resolvable,
resolved: n.resolved,
position: n.position
? {
baseSha: n.position.base_sha,
startSha: n.position.start_sha,
headSha: n.position.head_sha,
oldPath: n.position.old_path,
newPath: n.position.new_path,
positionType: n.position.position_type,
oldLine: n.position.old_line,
newLine: n.position.new_line,
}
: undefined,
})),
}));
} catch (error) {
this._logger.error('Failed to fetch merge request discussions', error);
throw new GitlabConnectionError(`获取MR讨论失败: ${String(error)}`);
}
}
public extractCodeSuggestions(discussions: IGitlabDiscussion[]): ICodeSuggestion[] {
const suggestions: ICodeSuggestion[] = [];
for (const discussion of discussions) {
// 如果整个讨论已解决,跳过
if (discussion.resolved) {
continue;
}
for (const note of discussion.notes) {
// 只提取未解决的代码建议(检查note级别的resolved状态)
if (note.position && !note.resolved) {
suggestions.push({
id: `${discussion.id}-${note.id}`,
filePath: note.position.newPath || note.position.oldPath,
oldLine: note.position.oldLine,
newLine: note.position.newLine,
body: note.body,
author: note.author.name,
createdAt: note.createdAt,
resolved: note.resolved,
type: this.classifySuggestionType(note.body),
});
}
}
}
return suggestions;
}
private classifySuggestionType(body: string): 'error' | 'warning' | 'suggestion' {
const lowerBody = body.toLowerCase();
if (lowerBody.includes('error') || lowerBody.includes('错误') || lowerBody.includes('必须')) {
return 'error';
}
if (lowerBody.includes('warning') || lowerBody.includes('警告') || lowerBody.includes('建议')) {
return 'warning';
}
return 'suggestion';
}
/**
* 获取当前用户相关的MR列表
* @param projectId 项目ID
* @returns MR列表
*/
public async getMyMergeRequests(projectId?: string): Promise<IGitlabMergeRequest[]> {
const id = projectId || this._projectId;
if (!id) {
throw new Error('未指定项目ID');
}
try {
// 直接使用项目ID,gitbeaker会自动进行URL编码
this._logger.info(`正在获取MR列表,项目ID: ${id}`);
// 获取当前用户信息
const currentUser = await this._api.Users.current();
this._logger.debug(`当前用户: ${currentUser.username} (ID: ${currentUser.id})`);
// 使用MergeRequests.all获取项目的所有MR
this._logger.debug(`正在获取项目 ${id} 的MR列表`);
const allMRs: any[] = await this._api.MergeRequests.all({
projectId: id,
scope: 'all',
orderBy: 'updated_at',
sort: 'desc',
perPage: 50,
maxPages: 1
} as any);
this._logger.debug(`项目共有${allMRs.length}个MR`);
// 筛选出与当前用户相关的MR(作者、审核人或分配人)
const myMRs: any[] = allMRs.filter((mr: any) => {
const isAuthor = mr.author?.id === currentUser.id;
const isAssignee = mr.assignee?.id === currentUser.id;
const isInAssignees = mr.assignees?.some((a: any) => a.id === currentUser.id);
const isInReviewers = mr.reviewers?.some((r: any) => r.id === currentUser.id);
return isAuthor || isAssignee || isInAssignees || isInReviewers;
});
const mergeRequests = myMRs.map(mr => this.transformMergeRequest(mr));
this._logger.info(`找到${mergeRequests.length}个与当前用户相关的MR`);
return mergeRequests;
} catch (error: any) {
// 详细的错误分类和日志
this._logger.error('获取MR列表失败', {
projectId: id,
error: error.message,
statusCode: error.response?.statusCode || error.statusCode,
url: error.response?.url
});
// 根据错误类型提供详细信息
if (error.response?.statusCode === 404 || error.statusCode === 404) {
throw new GitlabConnectionError(
`GitLab项目不存在或无法访问\n\n` +
`项目ID: ${id}\n\n` +
`可能的原因:\n` +
`1. 项目ID不正确(应为数字ID或"命名空间/项目名")\n` +
`2. 您没有该项目的访问权限\n` +
`3. GitLab服务器地址配置错误\n\n` +
`请在GitLab网页上确认项目是否存在,并检查插件配置中的GitLab URL和Access Token`
);
} else if (error.response?.statusCode === 401 || error.statusCode === 401) {
throw new GitlabConnectionError(
`GitLab认证失败\n\n` +
`您的Access Token可能已过期或无效。\n\n` +
`请重新生成Token并更新配置:\n` +
`命令面板 -> "GitLab: Configure Connection"`
);
} else if (error.response?.statusCode === 403 || error.statusCode === 403) {
throw new GitlabConnectionError(
`GitLab权限不足\n\n` +
`您的Access Token没有足够的权限访问该项目。\n\n` +
`请确保Token具有以下权限:\n` +
`- read_api\n` +
`- read_repository`
);
}
throw new GitlabConnectionError(`获取MR列表失败: ${error.message || String(error)}`);
}
}
private transformMergeRequest(mr: any): IGitlabMergeRequest {
return {
iid: mr.iid,
id: mr.id,
title: mr.title,
description: mr.description || '',
state: mr.state,
sourceBranch: mr.source_branch,
targetBranch: mr.target_branch,
author: {
id: mr.author.id,
name: mr.author.name,
username: mr.author.username,
},
webUrl: mr.web_url,
createdAt: mr.created_at,
updatedAt: mr.updated_at,
};
}
private isGitlabError(error: unknown): error is { response?: { status?: number } } {
return typeof error === 'object' && error !== null && 'response' in error;
}
}