Skip to content

Review individual file changes #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,20 @@
"enablement": "github.copilot.chat.reviewDiff.enabled && !github.copilot.interactiveSession.disabled",
"category": "GitHub Copilot"
},
{
"command": "github.copilot.chat.review.stagedFileChange",
"title": "%github.copilot.command.reviewFileChange%",
"icon": "$(code-review)",
"enablement": "github.copilot.chat.reviewDiff.enabled && !github.copilot.interactiveSession.disabled",
"category": "GitHub Copilot"
},
{
"command": "github.copilot.chat.review.unstagedFileChange",
"title": "%github.copilot.command.reviewFileChange%",
"icon": "$(code-review)",
"enablement": "github.copilot.chat.reviewDiff.enabled && !github.copilot.interactiveSession.disabled",
"category": "GitHub Copilot"
},
{
"command": "github.copilot.chat.review.changes.cancel",
"title": "%github.copilot.command.reviewChanges.cancel%",
Expand Down Expand Up @@ -3067,6 +3081,14 @@
"command": "github.copilot.chat.review.changes",
"when": "false"
},
{
"command": "github.copilot.chat.review.stagedFileChange",
"when": "false"
},
{
"command": "github.copilot.chat.review.unstagedFileChange",
"when": "false"
},
{
"command": "github.copilot.chat.review.changes.cancel",
"when": "false"
Expand Down Expand Up @@ -3334,6 +3356,18 @@
"group": "inline@-3"
}
],
"scm/resourceState/context": [
{
"command": "github.copilot.chat.review.stagedFileChange",
"group": "3_copilot",
"when": "github.copilot.chat.reviewDiff.enabled && scmProvider == git && scmResourceGroup == index"
},
{
"command": "github.copilot.chat.review.unstagedFileChange",
"group": "3_copilot",
"when": "github.copilot.chat.reviewDiff.enabled && scmProvider == git && scmResourceGroup == workingTree"
}
],
"scm/inputBox": [
{
"command": "github.copilot.git.generateCommitMessage",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"github.copilot.command.reviewUnstagedChanges": "Code Review - Unstaged Changes",
"github.copilot.command.reviewChanges": "Code Review - Uncommitted Changes",
"github.copilot.command.reviewChanges.cancel": "Code Review - Cancel",
"github.copilot.command.reviewFileChange": "Review Changes",
"github.copilot.command.gotoPreviousReviewSuggestion": "Previous Suggestion",
"github.copilot.command.gotoNextReviewSuggestion": "Next Suggestion",
"github.copilot.command.continueReviewInInlineChat": "Discard and Copy to Inline Chat",
Expand Down
8 changes: 8 additions & 0 deletions src/extension/inlineChat/vscode-node/inlineChatCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ ${message}`,
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.stagedChanges', () => doReview(...instaService.invokeFunction(getServicesForReview), 'index', vscode.ProgressLocation.SourceControl)));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.unstagedChanges', () => doReview(...instaService.invokeFunction(getServicesForReview), 'workingTree', vscode.ProgressLocation.SourceControl)));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.changes', () => doReview(...instaService.invokeFunction(getServicesForReview), 'all', vscode.ProgressLocation.SourceControl)));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.stagedFileChange', (resource: vscode.SourceControlResourceState) => {
vscode.window.showTextDocument(resource.resourceUri, { preview: false });
return doReview(...instaService.invokeFunction(getServicesForReview), { group: 'index', file: resource.resourceUri }, vscode.ProgressLocation.SourceControl);
}));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.unstagedFileChange', (resource: vscode.SourceControlResourceState) => {
vscode.window.showTextDocument(resource.resourceUri, { preview: false });
return doReview(...instaService.invokeFunction(getServicesForReview), { group: 'workingTree', file: resource.resourceUri }, vscode.ProgressLocation.SourceControl);
}));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.changes.cancel', () => cancelReview(vscode.ProgressLocation.SourceControl, instaService.invokeFunction(accessor => accessor.get(IRunCommandExecutionService)))));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.apply', doApplyReview));
disposables.add(vscode.commands.registerCommand('github.copilot.chat.review.applyAndNext', (commentThread: vscode.CommentThread) => doApplyReview(commentThread, true)));
Expand Down
6 changes: 3 additions & 3 deletions src/extension/review/node/doReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { TextEditor } from 'vscode';
import type { TextEditor, Uri } from 'vscode';
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
import { IRunCommandExecutionService } from '../../../platform/commands/common/runCommandExecutionService';
import { TextDocumentSnapshot } from '../../../platform/editing/common/textDocumentSnapshot';
Expand Down Expand Up @@ -69,7 +69,7 @@ export async function doReview(
workspaceService: IWorkspaceService,
commandService: IRunCommandExecutionService,
notificationService: INotificationService,
group: 'selection' | 'index' | 'workingTree' | 'all' | { repositoryRoot: string; commitMessages: string[]; patches: { patch: string; fileUri: string; previousFileUri?: string }[] },
group: 'selection' | 'index' | 'workingTree' | 'all' | { group: 'index' | 'workingTree'; file: Uri } | { repositoryRoot: string; commitMessages: string[]; patches: { patch: string; fileUri: string; previousFileUri?: string }[] },
progressLocation: ProgressLocation,
cancellationToken?: CancellationToken
): Promise<FeedbackResult | undefined> {
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function doReview(
try {
const copilotToken = await authService.getCopilotToken();
const canUseGitHubAgent = (group === 'index' || group === 'workingTree' || group === 'all' || typeof group === 'object') && copilotToken.isCopilotCodeReviewEnabled;
result = canUseGitHubAgent ? await githubReview(logService, gitExtensionService, authService, capiClientService, domainService, fetcherService, envService, ignoreService, workspaceService, group, progress, tokenSource.token) : await review(instantiationService, gitExtensionService, workspaceService, group, editor, progress, tokenSource.token);
result = canUseGitHubAgent ? await githubReview(logService, gitExtensionService, authService, capiClientService, domainService, fetcherService, envService, ignoreService, workspaceService, group, progress, tokenSource.token) : await review(instantiationService, gitExtensionService, workspaceService, typeof group === 'object' && 'group' in group ? group.group : group, editor, progress, tokenSource.token);
} catch (err) {
result = { type: 'error', reason: err.message, severity: err.severity };
} finally {
Expand Down
26 changes: 23 additions & 3 deletions src/extension/review/node/githubReviewAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function githubReview(
envService: IEnvService,
ignoreService: IIgnoreService,
workspaceService: IWorkspaceService,
group: 'index' | 'workingTree' | 'all' | { repositoryRoot: string; commitMessages: string[]; patches: { patch: string; fileUri: string; previousFileUri?: string }[] },
group: 'index' | 'workingTree' | 'all' | { group: 'index' | 'workingTree'; file: Uri } | { repositoryRoot: string; commitMessages: string[]; patches: { patch: string; fileUri: string; previousFileUri?: string }[] },
progress: Progress<ReviewComment[]>,
cancellationToken: CancellationToken
): Promise<FeedbackResult> {
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function githubReview(
}));
return changes;
}))).flat()
: await Promise.all(group.patches.map(async patch => {
: 'repositoryRoot' in group ? await Promise.all(group.patches.map(async patch => {
const uri = Uri.parse(patch.fileUri);
const document = await workspaceService.openTextDocument(uri).then(undefined, () => undefined);
if (!document) {
Expand All @@ -92,7 +92,27 @@ export async function githubReview(
after,
document,
};
}))).filter((change): change is NonNullable<typeof change> => !!change);
}))
: await (async () => {
const { group: g, file } = group;
const repository = git.getRepository(file);
const document = await workspaceService.openTextDocument(file).then(undefined, () => undefined);
if (!repository || !document) {
return [];
}
const before = await (g === 'index' ? repository.show('HEAD', file.fsPath).catch(() => '') : repository.show('', file.fsPath).catch(() => ''));
const after = g === 'index' ? await (repository.show('', file.fsPath).catch(() => '')) : document.getText();
const relativePath = path.relative(repository.rootUri.fsPath, file.fsPath);
return [
{
repository,
relativePath: process.platform === 'win32' ? relativePath.replace(/\\/g, '/') : relativePath,
before,
after,
document,
}
];
})()).filter((change): change is NonNullable<typeof change> => !!change);

if (!changes.length) {
return { type: 'success', comments: [] };
Expand Down