This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathgithubService.ts
More file actions
446 lines (390 loc) · 16.8 KB
/
Copy pathgithubService.ts
File metadata and controls
446 lines (390 loc) · 16.8 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { Endpoints } from "@octokit/types";
import { createServiceIdentifier } from '../../../util/common/services';
import { decodeBase64 } from '../../../util/vs/base/common/buffer';
import { vArray } from '../../configuration/common/validator';
import { ICAPIClientService } from '../../endpoint/common/capiClient';
import { ILogService } from '../../log/common/logService';
import { IFetcherService } from '../../networking/common/fetcherService';
import { ITelemetryService } from '../../telemetry/common/telemetry';
import { addPullRequestCommentGraphQLRequest, closePullRequest, getPullRequestFromGlobalId, makeGitHubAPIRequest, makeGitHubAPIRequestWithPagination, makeSearchGraphQLRequest, PullRequestComment, PullRequestSearchItem, SessionInfo } from './githubAPI';
import { vFileContentResponse, vPullRequestFile, vIOctoKitUser, vRemoteAgentJobOrError, vGetCustomAgentsResponse, vSessionInfo, vJobInfo } from './githubAPIValidators';
export type IGetRepositoryInfoResponseData = Endpoints["GET /repos/{owner}/{repo}"]["response"]["data"];
export const IGithubRepositoryService = createServiceIdentifier<IGithubRepositoryService>('IGithubRepositoryService');
export const IOctoKitService = createServiceIdentifier<IOctoKitService>('IOctoKitService');
export const VSCodeTeamId = 1682102;
export type GithubRepositoryItem = {
name: string;
path: string;
html_url: string;
type: 'file' | 'dir';
};
export interface JobInfo {
job_id: string;
session_id: string;
problem_statement: string;
content_filter_mode?: string;
status: string;
result?: string;
actor: {
id: number;
login: string;
};
created_at: string;
updated_at: string;
pull_request: {
id: number;
number: number;
};
workflow_run?: {
id: number;
};
error?: {
message: string;
};
event_type?: string;
event_url?: string;
event_identifiers?: string[];
}
export interface IGithubRepositoryService {
_serviceBrand: undefined;
/**
* Returns whether the given repository is available via GitHub APIs.
* @param org The GitHub organization
* @param repo The GitHub repository
*/
isAvailable(org: string, repo: string): Promise<boolean>;
getRepositoryInfo(owner: string, repo: string): Promise<IGetRepositoryInfoResponseData>;
getRepositoryItems(org: string, repo: string, path: string): Promise<GithubRepositoryItem[]>;
getRepositoryItemContent(org: string, repo: string, path: string): Promise<Uint8Array | undefined>;
}
export interface IOctoKitUser {
login: string;
name: string | null;
avatar_url: string;
}
export interface IOctoKitSessionInfo {
name: string;
owner_id: number;
premium_requests: number;
repo_id: number;
resource_global_id: string;
resource_id: number;
resource_state: string;
resource_type: string;
state: string;
user_id: number;
workflow_run_id: number;
last_updated_at: string;
created_at: string;
}
export interface RemoteAgentJobResponse {
job_id: string;
session_id: string;
actor: {
id: number;
login: string;
};
created_at: string;
updated_at: string;
}
export interface ErrorResponseWithStatusCode {
status: number;
}
export interface RemoteAgentJobPayload {
problem_statement: string;
event_type: string;
pull_request?: {
title?: string;
body_placeholder?: string;
body_suffix?: string;
base_ref?: string;
head_ref?: string;
};
run_name?: string;
custom_agent?: string;
}
interface GetCustomAgentsResponse {
agents: CustomAgentListItem[];
}
export interface CustomAgentListItem {
name: string;
repo_owner_id: number;
repo_owner: string;
repo_id: number;
repo_name: string;
display_name: string;
description: string;
tools: string[];
version: string;
}
export interface CustomAgentDetails extends CustomAgentListItem {
prompt: string;
'mcp-servers'?: {
[serverName: string]: {
type: string;
command?: string;
args?: string[];
tools?: string[];
env?: { [key: string]: string };
headers?: { [key: string]: string };
};
};
}
export interface PullRequestFile {
filename: string;
status: 'added' | 'removed' | 'modified' | 'renamed' | 'copied' | 'changed' | 'unchanged';
additions: number;
deletions: number;
changes: number;
patch?: string;
previous_filename?: string;
}
export interface IOctoKitService {
_serviceBrand: undefined;
/**
* @returns The currently authenticated user or undefined if there isn't one
*/
getCurrentAuthedUser(): Promise<IOctoKitUser | undefined>;
/**
* Returns the list of Copilot pull requests for a given user on a specific repo.
*/
getCopilotPullRequestsForUser(owner: string, repo: string): Promise<PullRequestSearchItem[]>;
/**
* Returns the list of Copilot sessions for a given pull request.
*/
getCopilotSessionsForPR(prId: string): Promise<SessionInfo[]>;
/**
* Returns the logs for a specific Copilot session.
*/
getSessionLogs(sessionId: string): Promise<string>;
/**
* Returns the information for a specific Copilot session.
*/
getSessionInfo(sessionId: string): Promise<SessionInfo>;
/**
* Posts a new Copilot agent job.
*/
postCopilotAgentJob(
owner: string,
name: string,
apiVersion: string,
payload: RemoteAgentJobPayload,
): Promise<RemoteAgentJobResponse | ErrorResponseWithStatusCode>;
/**
* Gets a job by its job ID.
*/
getJobByJobId(owner: string, repo: string, jobId: string, userAgent: string): Promise<JobInfo>;
/**
* Gets a job by session ID
*/
getJobBySessionId(owner: string, repo: string, sessionId: string, userAgent: string): Promise<JobInfo>;
/**
* Adds a comment to a pull request.
*/
addPullRequestComment(pullRequestId: string, commentBody: string): Promise<PullRequestComment | null>;
/**
* Gets all open Copilot sessions.
*/
getAllOpenSessions(nwo: string): Promise<SessionInfo[]>;
/**
* Gets pull request from global id.
*/
getPullRequestFromGlobalId(globalId: string): Promise<PullRequestSearchItem | null>;
/**
* Gets the list of custom agents available for a repository.
* This includes both repo-level and org/enterprise-level custom agents.
* @param owner The repository owner
* @param repo The repository name
* @returns An array of custom agent list items with basic metadata
*/
getCustomAgents(owner: string, repo: string): Promise<CustomAgentListItem[]>;
/**
* Gets the list of files changed in a pull request.
* @param owner The repository owner
* @param repo The repository name
* @param pullNumber The pull request number
* @returns An array of changed files with their metadata
*/
getPullRequestFiles(owner: string, repo: string, pullNumber: number): Promise<PullRequestFile[]>;
/**
* Closes a pull request.
* @param owner The repository owner
* @param repo The repository name
* @param pullNumber The pull request number
* @returns A promise that resolves to true if the PR was successfully closed
*/
closePullRequest(owner: string, repo: string, pullNumber: number): Promise<boolean>;
/**
* Get file content from a specific commit.
* @param owner The repository owner
* @param repo The repository name
* @param ref The commit SHA, branch name, or tag
* @param path The file path within the repository
* @returns The file content as a string
*/
getFileContent(owner: string, repo: string, ref: string, path: string): Promise<string>;
}
/**
* The same as {@link OctoKitService} but doesn't require the AuthService.
* This is because we want to call certain Octokit method inside the Authservice and must
* avoid a circular dependency.
* Note: Only OctoKitService is exposed on the accessor to avoid confusion.
*/
export class BaseOctoKitService {
constructor(
private readonly _capiClientService: ICAPIClientService,
private readonly _fetcherService: IFetcherService,
private readonly _logService: ILogService,
private readonly _telemetryService: ITelemetryService
) { }
async getCurrentAuthedUserWithToken(token: string): Promise<IOctoKitUser | undefined> {
const result = await this._makeGHAPIRequest('user', 'GET', token);
if (!result) {
return undefined;
}
const validationResult = vIOctoKitUser.validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate authenticated user response: ${validationResult.error.message}`);
return undefined;
}
return validationResult.content;
}
async getTeamMembershipWithToken(teamId: number, token: string, username: string): Promise<any | undefined> {
return this._makeGHAPIRequest(`teams/${teamId}/memberships/${username}`, 'GET', token);
}
protected async _makeGHAPIRequest(routeSlug: string, method: 'GET' | 'POST', token: string, body?: { [key: string]: any }) {
return makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, routeSlug, method, token, body, '2022-11-28');
}
protected async getCopilotPullRequestForUserWithToken(owner: string, repo: string, user: string, token: string) {
const query = `repo:${owner}/${repo} is:open author:copilot-swe-agent[bot] involves:${user}`;
return makeSearchGraphQLRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, query);
}
protected async getCopilotSessionsForPRWithToken(prId: string, token: string) {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/resource/pull/${prId}`, 'GET', token);
if (!result) {
return [];
}
const validationResult = vArray(vSessionInfo).validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate sessions for PR response: ${validationResult.error.message}`);
return [];
}
return validationResult.content;
}
protected async getSessionLogsWithToken(sessionId: string, token: string) {
return makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/${sessionId}/logs`, 'GET', token, undefined, undefined, 'text');
}
protected async getSessionInfoWithToken(sessionId: string, token: string) {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/sessions/${sessionId}`, 'GET', token, undefined, undefined, 'text');
if (!result) {
return undefined;
}
// The response is text, so we need to parse it as JSON first
let parsed: unknown;
try {
parsed = typeof result === 'string' ? JSON.parse(result) : result;
} catch (error) {
this._logService.error(`[GitHubAPI] Failed to parse session info response as JSON: ${error}`);
return undefined;
}
const validationResult = vSessionInfo.validate(parsed);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate session info response: ${validationResult.error.message}`);
return undefined;
}
return validationResult.content;
}
protected async postCopilotAgentJobWithToken(owner: string, name: string, apiVersion: string, userAgent: string, payload: RemoteAgentJobPayload, token: string) {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/${apiVersion}/jobs/${owner}/${name}`, 'POST', token, payload, undefined, undefined, userAgent, true);
if (!result) {
return undefined;
}
const validationResult = vRemoteAgentJobOrError.validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate remote agent job response: ${validationResult.error.message}`);
return undefined;
}
return validationResult.content;
}
protected async getJobByJobIdWithToken(owner: string, repo: string, jobId: string, userAgent: string, token: string): Promise<JobInfo> {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/v1/jobs/${owner}/${repo}/${jobId}`, 'GET', token, undefined, undefined, undefined, userAgent);
if (!result) {
throw new Error('Failed to fetch job info: No response received');
}
const validationResult = vJobInfo.validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate job info response: ${validationResult.error.message}`);
throw new Error(`Failed to validate job info: ${validationResult.error.message}`);
}
return validationResult.content;
}
protected async getJobBySessionIdWithToken(owner: string, repo: string, sessionId: string, userAgent: string, token: string): Promise<JobInfo> {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/v1/jobs/${owner}/${repo}/session/${sessionId}`, 'GET', token, undefined, undefined, undefined, userAgent);
if (!result) {
throw new Error('Failed to fetch job info: No response received');
}
const validationResult = vJobInfo.validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate job info response: ${validationResult.error.message}`);
throw new Error(`Failed to validate job info: ${validationResult.error.message}`);
}
return validationResult.content;
}
protected async addPullRequestCommentWithToken(pullRequestId: string, commentBody: string, token: string): Promise<PullRequestComment | null> {
return addPullRequestCommentGraphQLRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, pullRequestId, commentBody);
}
protected async getAllOpenSessionsWithToken(nwo: string, token: string): Promise<SessionInfo[]> {
return makeGitHubAPIRequestWithPagination(this._fetcherService, this._logService, `https://api.githubcopilot.com`, 'agents/sessions', nwo, token);
}
protected async getPullRequestFromSessionWithToken(globalId: string, token: string): Promise<PullRequestSearchItem | null> {
return getPullRequestFromGlobalId(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, globalId);
}
protected async getCustomAgentsWithToken(owner: string, repo: string, token: string): Promise<GetCustomAgentsResponse> {
const queryParams = '?exclude_invalid_config=true';
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/custom-agents/${owner}/${repo}${queryParams}`, 'GET', token, undefined, undefined, 'json', 'vscode-copilot-chat');
if (!result) {
return { agents: [] };
}
const validationResult = vGetCustomAgentsResponse.validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate custom agents response: ${validationResult.error.message}`);
return { agents: [] };
}
return validationResult.content;
}
protected async getPullRequestFilesWithToken(owner: string, repo: string, pullNumber: number, token: string): Promise<PullRequestFile[]> {
const result = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, `repos/${owner}/${repo}/pulls/${pullNumber}/files`, 'GET', token, undefined, '2022-11-28');
if (!result) {
return [];
}
const validationResult = vArray(vPullRequestFile).validate(result);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate pull request files response: ${validationResult.error.message}`);
return [];
}
return validationResult.content;
}
protected async closePullRequestWithToken(owner: string, repo: string, pullNumber: number, token: string): Promise<boolean> {
return closePullRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, owner, repo, pullNumber);
}
protected async getFileContentWithToken(owner: string, repo: string, ref: string, path: string, token: string): Promise<string> {
const response = await makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, `repos/${owner}/${repo}/contents/${path}?ref=${encodeURIComponent(ref)}`, 'GET', token, undefined);
if (!response) {
return '';
}
const validationResult = vFileContentResponse.validate(response);
if (validationResult.error) {
this._logService.error(`[GitHubAPI] Failed to validate file content response: ${validationResult.error.message}`);
return '';
}
if (validationResult.content.encoding === 'base64') {
return decodeBase64(validationResult.content.content.replace(/\n/g, '')).toString();
} else {
return '';
}
}
}