Skip to content

Add "Apply patch of changes to current branch" action to PR overview additional actions button #7083

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 3 commits 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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4046,5 +4046,8 @@
"vscode-tas-client": "^0.1.84",
"vsls": "^0.3.967"
},
"license": "MIT"
}
"license": "MIT",
"optionalDependencies": {
"@esbuild/linux-x64": "^0.25.5"
}
}
32 changes: 32 additions & 0 deletions src/github/pullRequestOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
return this.openSessionLog(message.args.link);
case 'pr.cancel-coding-agent':
return this.cancelCodingAgent(message);
case 'pr.apply-pr-patch':
return this.applyPRPatch(message);
}
}

Expand Down Expand Up @@ -449,6 +451,36 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
}
}

private async applyPRPatch(message: IRequestMessage<void>): Promise<void> {
try {
this._replyMessage(message, {});
const { octokit, remote } = await this._item.githubRepository.ensure();

// Get the PR diff in patch format using the GitHub API
const response = await octokit.call(octokit.api.pulls.get, {
owner: remote.owner,
repo: remote.repositoryName,
pull_number: this._item.number,
headers: {
accept: 'application/vnd.github.v3.diff'
}
});

const patch = response.data as unknown as string;
const tempUri = vscode.Uri.joinPath(this._folderRepositoryManager.repository.rootUri, '.git', `pr-${this._item.number}.diff`);

const encoder = new TextEncoder();
await vscode.workspace.fs.writeFile(tempUri, encoder.encode(patch));
await this._folderRepositoryManager.repository.apply(tempUri.fsPath);
await vscode.workspace.fs.delete(tempUri);

vscode.window.showInformationMessage(vscode.l10n.t('Pull request changes applied to current branch!'));
} catch (e) {
Logger.error(`Applying PR patch failed: ${e}`, PullRequestOverviewPanel.ID);
vscode.window.showErrorMessage(vscode.l10n.t('Applying pull request patch failed: {0}', formatError(e)));
}
}

private async openDiff(message: IRequestMessage<{ comment: IComment }>): Promise<void> {
try {
const comment = message.args.comment;
Expand Down
40 changes: 40 additions & 0 deletions webviews/common/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,44 @@ button.inlined-dropdown {
margin-right: 5px;
display: inline-block;
text-align: center;
}

.additional-actions-container {
position: relative;
display: inline-block;
}

.additional-actions-dropdown {
position: absolute;
top: 100%;
right: 0;
background-color: var(--vscode-dropdown-background);
border: 1px solid var(--vscode-dropdown-border);
border-radius: 3px;
box-shadow: 0 2px 8px var(--vscode-widget-shadow);
z-index: 1000;
min-width: 200px;
margin-top: 2px;
}

.additional-actions-dropdown button {
display: block;
width: 100%;
padding: 8px 12px;
border: none;
background: none;
color: var(--vscode-dropdown-foreground);
text-align: left;
font-size: 13px;
line-height: 18px;
cursor: pointer;
}

.additional-actions-dropdown button:hover:not(:disabled) {
background-color: var(--vscode-list-hoverBackground);
}

.additional-actions-dropdown button:disabled {
opacity: 0.6;
cursor: default;
}
2 changes: 2 additions & 0 deletions webviews/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class PRContext {

public openChanges = () => this.postMessage({ command: 'pr.open-changes' });

public applyPRPatch = () => this.postMessage({ command: 'pr.apply-pr-patch' });

public copyPrLink = () => this.postMessage({ command: 'pr.copy-prlink' });

public copyVscodeDevLink = () => this.postMessage({ command: 'pr.copy-vscodedevlink' });
Expand Down
62 changes: 62 additions & 0 deletions webviews/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function ButtonGroup({ isCurrentlyCheckedOut, canEdit, isIssue, repositoryDefaul
return (
<div className="button-group">
<CheckoutButtons {...{ isCurrentlyCheckedOut, isIssue, repositoryDefaultBranch }} />
<AdditionalActionsButton {...{ isCurrentlyCheckedOut, isIssue }} />
<button title="Open Changes" onClick={openChanges} className="small-button">
Open Changes
</button>
Expand Down Expand Up @@ -156,6 +157,67 @@ function CancelCodingAgentButton({ canEdit, codingAgentEvent }: { canEdit: boole
: null;
}

function AdditionalActionsButton({ isCurrentlyCheckedOut, isIssue }: { isCurrentlyCheckedOut: boolean, isIssue: boolean }) {
const { applyPRPatch } = useContext(PullRequestContext);
const [isDropdownOpen, setDropdownOpen] = useState(false);
const [isBusy, setBusy] = useState(false);

// Only show additional actions if PR is not checked out and it's not an issue
const hasAdditionalActions = !isCurrentlyCheckedOut && !isIssue;

// Close dropdown when clicking outside
React.useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
const target = event.target as Element;
if (!target.closest('.additional-actions-container')) {
setDropdownOpen(false);
}
};

if (isDropdownOpen) {
document.addEventListener('click', handleClickOutside);
}

return () => {
document.removeEventListener('click', handleClickOutside);
};
}, [isDropdownOpen]);

if (!hasAdditionalActions) {
return null;
}

const onApplyPatch = async () => {
try {
setBusy(true);
setDropdownOpen(false);
await applyPRPatch();
} finally {
setBusy(false);
}
};

return (
<div className="additional-actions-container">
<button
title="Additional Actions"
onClick={() => setDropdownOpen(!isDropdownOpen)}
className="secondary small-button"
disabled={isBusy}
>
</button>
{isDropdownOpen && (
<div className="additional-actions-dropdown">
<button onClick={onApplyPatch} disabled={isBusy}>
Apply patch of changes to current branch
</button>
</div>
)}
</div>
);
}

function Subtitle({ state, isDraft, isIssue, author, base, head }) {
const { text, color, icon } = getStatus(state, isDraft, isIssue);

Expand Down
Loading