Skip to content

Commit a48718b

Browse files
authored
Fix for adding comment to PR (#1383)
1 parent 0a35be3 commit a48718b

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

src/extension/chatSessions/vscode-node/copilotChatSessionsProvider.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -606,19 +606,14 @@ export class CopilotChatSessionsProvider extends Disposable implements vscode.Ch
606606
}
607607
// Add a comment tagging @copilot with the user's prompt
608608
const commentBody = `${this.COPILOT} ${userPrompt} \n\n --- \n\n ${summary ?? ''}`;
609-
const commentResult = {
610-
body: commentBody,
611-
user: {
612-
login: pr.author?.login
613-
},
614-
createdAt: new Date().toISOString()
615-
};
609+
610+
const commentResult = await this._octoKitService.addPullRequestComment(pr.id, commentBody);
616611
if (!commentResult) {
617612
this.logService.error(`Failed to add comment to PR #${pullRequestNumber}`);
618613
return;
619614
}
620615
// allow-any-unicode-next-line
621-
return vscode.l10n.t('🚀 Follow-up comment added to [#{0}]({1})', pullRequestNumber, commentResult.body);
616+
return vscode.l10n.t('🚀 Follow-up comment added to [#{0}]({1})', pullRequestNumber, commentResult.url);
622617
} catch (err) {
623618
this.logService.error(`Failed to add follow-up comment to PR #${pullRequestNumber}: ${err}`);
624619
return;

src/platform/github/common/githubAPI.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ export interface SessionInfo {
6262
error: string | null;
6363
}
6464

65+
export interface PullRequestComment {
66+
id: string;
67+
body: string;
68+
createdAt: string;
69+
author: {
70+
login: string;
71+
};
72+
url: string;
73+
}
74+
6575
export async function makeGitHubAPIRequest(
6676
fetcherService: IFetcherService,
6777
logService: ILogService,
@@ -210,3 +220,42 @@ export async function makeSearchGraphQLRequest(
210220

211221
return result ? result.data.search.nodes : [];
212222
}
223+
224+
export async function addPullRequestCommentGraphQLRequest(
225+
fetcherService: IFetcherService,
226+
logService: ILogService,
227+
telemetry: ITelemetryService,
228+
host: string,
229+
token: string | undefined,
230+
pullRequestId: string,
231+
commentBody: string,
232+
): Promise<PullRequestComment | null> {
233+
const mutation = `
234+
mutation AddPullRequestComment($pullRequestId: ID!, $body: String!) {
235+
addComment(input: {subjectId: $pullRequestId, body: $body}) {
236+
commentEdge {
237+
node {
238+
id
239+
body
240+
createdAt
241+
author {
242+
login
243+
}
244+
url
245+
}
246+
}
247+
}
248+
}
249+
`;
250+
251+
logService.debug(`[GitHubAPI] Adding comment to pull request ${pullRequestId}`);
252+
253+
const variables = {
254+
pullRequestId,
255+
body: commentBody
256+
};
257+
258+
const result = await makeGitHubGraphQLRequest(fetcherService, logService, telemetry, host, mutation, token, variables);
259+
260+
return result?.data?.addComment?.commentEdge?.node || null;
261+
}

src/platform/github/common/githubService.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ICAPIClientService } from '../../endpoint/common/capiClient';
99
import { ILogService } from '../../log/common/logService';
1010
import { IFetcherService } from '../../networking/common/fetcherService';
1111
import { ITelemetryService } from '../../telemetry/common/telemetry';
12-
import { makeGitHubAPIRequest, makeSearchGraphQLRequest, PullRequestSearchItem, SessionInfo } from './githubAPI';
12+
import { addPullRequestCommentGraphQLRequest, makeGitHubAPIRequest, makeSearchGraphQLRequest, PullRequestComment, PullRequestSearchItem, SessionInfo } from './githubAPI';
1313

1414
export type IGetRepositoryInfoResponseData = Endpoints["GET /repos/{owner}/{repo}"]["response"]["data"];
1515

@@ -177,6 +177,11 @@ export interface IOctoKitService {
177177
* Gets a job by its job ID.
178178
*/
179179
getJobByJobId(owner: string, repo: string, jobId: string, userAgent: string): Promise<JobInfo>;
180+
181+
/**
182+
* Adds a comment to a pull request.
183+
*/
184+
addPullRequestComment(pullRequestId: string, commentBody: string): Promise<PullRequestComment | null>;
180185
}
181186

182187
/**
@@ -229,4 +234,8 @@ export class BaseOctoKitService {
229234
protected async getJobByJobIdWithToken(owner: string, repo: string, jobId: string, userAgent: string, token: string): Promise<JobInfo> {
230235
return makeGitHubAPIRequest(this._fetcherService, this._logService, this._telemetryService, 'https://api.githubcopilot.com', `agents/swe/v1/jobs/${owner}/${repo}/${jobId}`, 'GET', token, undefined, undefined, undefined, userAgent);
231236
}
237+
238+
protected async addPullRequestCommentWithToken(pullRequestId: string, commentBody: string, token: string): Promise<PullRequestComment | null> {
239+
return addPullRequestCommentGraphQLRequest(this._fetcherService, this._logService, this._telemetryService, this._capiClientService.dotcomAPIURL, token, pullRequestId, commentBody);
240+
}
232241
}

src/platform/github/common/octoKitServiceImpl.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ICAPIClientService } from '../../endpoint/common/capiClient';
77
import { ILogService } from '../../log/common/logService';
88
import { IFetcherService } from '../../networking/common/fetcherService';
99
import { ITelemetryService } from '../../telemetry/common/telemetry';
10-
import { PullRequestSearchItem, SessionInfo } from './githubAPI';
10+
import { PullRequestComment, PullRequestSearchItem, SessionInfo } from './githubAPI';
1111
import { BaseOctoKitService, IOctoKitService, IOctoKitUser, JobInfo, RemoteAgentJobPayload, RemoteAgentJobResponse } from './githubService';
1212

1313
export class OctoKitService extends BaseOctoKitService implements IOctoKitService {
@@ -107,4 +107,12 @@ export class OctoKitService extends BaseOctoKitService implements IOctoKitServic
107107
}
108108
return this.getJobByJobIdWithToken(owner, repo, jobId, userAgent, authToken);
109109
}
110+
111+
async addPullRequestComment(pullRequestId: string, commentBody: string): Promise<PullRequestComment | null> {
112+
const authToken = (await this._authService.getAnyGitHubSession())?.accessToken;
113+
if (!authToken) {
114+
throw new Error('No authentication token available');
115+
}
116+
return this.addPullRequestCommentWithToken(pullRequestId, commentBody, authToken);
117+
}
110118
}

0 commit comments

Comments
 (0)