Skip to content

Commit cc5ddb5

Browse files
authored
Update to support new arg format from chat commands (#8472)
1 parent ea73659 commit cc5ddb5

3 files changed

Lines changed: 82 additions & 51 deletions

File tree

package.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,12 +1426,24 @@
14261426
"category": "%command.pull.request.category%",
14271427
"icon": "$(git-compare)"
14281428
},
1429+
{
1430+
"command": "pr.checkoutFromChat",
1431+
"title": "%command.pr.checkoutFromChat.title%",
1432+
"category": "%command.pull.request.category%",
1433+
"icon": "$(git-compare)"
1434+
},
14291435
{
14301436
"command": "pr.applyChangesFromDescription",
14311437
"title": "%command.pr.applyChangesFromDescription.title%",
14321438
"category": "%command.pull.request.category%",
14331439
"icon": "$(git-stash-apply)"
14341440
},
1441+
{
1442+
"command": "pr.applyChangesFromChat",
1443+
"title": "%command.pr.applyChangesFromChat.title%",
1444+
"category": "%command.pull.request.category%",
1445+
"icon": "$(git-stash-apply)"
1446+
},
14351447
{
14361448
"command": "pr.checkoutOnVscodeDevFromDescription",
14371449
"title": "%command.pr.checkoutOnVscodeDevFromDescription.title%",
@@ -2172,10 +2184,18 @@
21722184
"command": "pr.checkoutFromDescription",
21732185
"when": "false"
21742186
},
2187+
{
2188+
"command": "pr.checkoutFromChat",
2189+
"when": "false"
2190+
},
21752191
{
21762192
"command": "pr.applyChangesFromDescription",
21772193
"when": "false"
21782194
},
2195+
{
2196+
"command": "pr.applyChangesFromChat",
2197+
"when": "false"
2198+
},
21792199
{
21802200
"command": "pr.checkoutChatSessionPullRequest",
21812201
"when": "false"
@@ -3613,12 +3633,12 @@
36133633
],
36143634
"chat/input/editing/sessionToolbar": [
36153635
{
3616-
"command": "pr.checkoutFromDescription",
3636+
"command": "pr.checkoutFromChat",
36173637
"group": "navigation@0",
36183638
"when": "chatSessionType == copilot-cloud-agent && workspaceFolderCount > 0 && github.vscode-pull-request-github.activated && !config.chat.disableAIFeatures"
36193639
},
36203640
{
3621-
"command": "pr.applyChangesFromDescription",
3641+
"command": "pr.applyChangesFromChat",
36223642
"group": "navigation@1",
36233643
"when": "chatSessionType == copilot-cloud-agent && workspaceFolderCount > 0 && github.vscode-pull-request-github.activated && !config.chat.disableAIFeatures"
36243644
}

package.nls.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@
302302
"command.pr.toggleEditorCommentingOn.title": "Toggle Editor Commenting On",
303303
"command.pr.toggleEditorCommentingOff.title": "Toggle Editor Commenting Off",
304304
"command.pr.checkoutFromDescription.title": "Checkout",
305+
"command.pr.checkoutFromChat.title": "Checkout",
305306
"command.pr.applyChangesFromDescription.title": "Apply",
307+
"command.pr.applyChangesFromChat.title": "Apply",
306308
"command.pr.checkoutOnVscodeDevFromDescription.title": "Checkout on vscode.dev",
307309
"command.pr.checkoutOnCodespacesFromDescription.title": "Checkout on Codespaces",
308310
"command.pr.openSessionLogFromDescription.title": "View Session",

src/commands.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -606,75 +606,84 @@ export function registerCommands(
606606
}
607607
}
608608

609-
context.subscriptions.push(vscode.commands.registerCommand('pr.checkoutFromDescription', async (ctx: OverviewContext | { path: string } | undefined, metadata?: SessionMetadata) => {
609+
type ChatCommandArgs = { path: string } | [{ path: string } | undefined, SessionMetadata | undefined] | undefined;
610+
611+
function parseChatCommandArgs(ctxOrArgs: ChatCommandArgs, metadataArg?: SessionMetadata): { ctx: { path: string } | undefined; metadata: SessionMetadata | undefined } {
612+
if (Array.isArray(ctxOrArgs)) {
613+
return { ctx: ctxOrArgs[0], metadata: ctxOrArgs[1] };
614+
}
615+
return { ctx: ctxOrArgs, metadata: metadataArg };
616+
}
617+
618+
async function resolvePrFromChat(ctx: { path: string }, metadata: SessionMetadata | undefined): Promise<{ folderManager: FolderRepositoryManager; pullRequest: PullRequestModel; prNumber: number } | undefined> {
619+
const prNumber = prNumberFromUriPath(ctx.path);
620+
if (!prNumber) {
621+
return undefined;
622+
}
623+
const result = getFolderManagerFromMetadata(metadata);
624+
if (!result) {
625+
return undefined;
626+
}
627+
const { folderManager, githubRepo } = result;
628+
const pullRequest = await folderManager.fetchById(githubRepo, prNumber);
629+
if (!pullRequest) {
630+
return undefined;
631+
}
632+
return { folderManager, pullRequest, prNumber };
633+
}
634+
635+
context.subscriptions.push(vscode.commands.registerCommand('pr.checkoutFromChat', async (ctxOrArgs: ChatCommandArgs, metadataArg?: SessionMetadata) => {
636+
const { ctx, metadata } = parseChatCommandArgs(ctxOrArgs, metadataArg);
610637
if (!ctx) {
611638
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request context provided for checkout.'));
612639
}
613640

614-
if (contextHasPath(ctx)) {
615-
const { path } = ctx;
616-
const prNumber = prNumberFromUriPath(path);
617-
if (!prNumber) {
618-
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request number found in context path.'));
619-
}
620-
// Use metadata to find the correct repository if available
621-
const result = getFolderManagerFromMetadata(metadata);
622-
if (!result) {
623-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find repository manager.'));
624-
}
625-
const { folderManager, githubRepo } = result;
626-
const pullRequest = await folderManager.fetchById(githubRepo, Number(prNumber));
627-
if (!pullRequest) {
628-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find pull request #{0}', prNumber.toString()));
629-
}
641+
const resolved = await resolvePrFromChat(ctx, metadata);
642+
if (!resolved) {
643+
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find pull request from chat context.'));
644+
}
630645

631-
return reviewsManager.switchToPr(folderManager, pullRequest, folderManager.repository, true);
646+
return reviewsManager.switchToPr(resolved.folderManager, resolved.pullRequest, resolved.folderManager.repository, true);
647+
}));
648+
649+
context.subscriptions.push(vscode.commands.registerCommand('pr.checkoutFromDescription', async (ctx: OverviewContext | undefined) => {
650+
if (!ctx) {
651+
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request context provided for checkout.'));
632652
}
633653

634654
const resolved = await resolvePr(ctx);
635655
if (!resolved) {
636656
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to resolve pull request for checkout.'));
637657
}
638658
return reviewsManager.switchToPr(resolved.folderManager, resolved.pr, resolved.folderManager.repository, true);
639-
640659
}));
641660

642-
context.subscriptions.push(vscode.commands.registerCommand('pr.applyChangesFromDescription', async (ctx: OverviewContext | { path: string } | undefined, metadata?: SessionMetadata) => {
661+
context.subscriptions.push(vscode.commands.registerCommand('pr.applyChangesFromChat', async (ctxOrArgs: ChatCommandArgs, metadataArg?: SessionMetadata) => {
662+
const { ctx, metadata } = parseChatCommandArgs(ctxOrArgs, metadataArg);
643663
if (!ctx) {
644664
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request context provided for applying changes.'));
645665
}
646666

647-
if (contextHasPath(ctx)) {
648-
const { path } = ctx;
649-
const prNumber = prNumberFromUriPath(path);
650-
if (!prNumber) {
651-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to parse pull request number.'));
652-
}
653-
654-
await vscode.window.withProgress(
655-
{
656-
location: vscode.ProgressLocation.Notification,
657-
title: vscode.l10n.t('Applying changes from pull request #{0}', prNumber.toString()),
658-
cancellable: false
659-
},
660-
async (task) => {
661-
task.report({ increment: 30 });
662-
663-
// Use metadata to find the correct repository if available
664-
const result = getFolderManagerFromMetadata(metadata);
665-
if (!result) {
666-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find repository manager.'));
667-
}
668-
const { folderManager, githubRepo } = result;
669-
const pullRequest = await folderManager.fetchById(githubRepo, Number(prNumber));
670-
if (!pullRequest) {
671-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find pull request #{0}', prNumber.toString()));
672-
}
667+
const resolved = await resolvePrFromChat(ctx, metadata);
668+
if (!resolved) {
669+
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to find pull request from chat context.'));
670+
}
673671

674-
return applyPullRequestChanges(task, folderManager, pullRequest);
675-
});
672+
await vscode.window.withProgress(
673+
{
674+
location: vscode.ProgressLocation.Notification,
675+
title: vscode.l10n.t('Applying changes from pull request #{0}', resolved.prNumber.toString()),
676+
cancellable: false
677+
},
678+
async (task) => {
679+
task.report({ increment: 30 });
680+
return applyPullRequestChanges(task, resolved.folderManager, resolved.pullRequest);
681+
});
682+
}));
676683

677-
return;
684+
context.subscriptions.push(vscode.commands.registerCommand('pr.applyChangesFromDescription', async (ctx: OverviewContext | undefined) => {
685+
if (!ctx) {
686+
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request context provided for applying changes.'));
678687
}
679688

680689
await vscode.window.withProgress(

0 commit comments

Comments
 (0)