Skip to content

Commit 32386b2

Browse files
Copilotalexr00
andcommitted
Refactor: extract common logic from pr.pickOnVscodeDev and pr.pickOnCodespaces
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent a486961 commit 32386b2

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/commands.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -694,48 +694,38 @@ export function registerCommands(
694694
}
695695
}));
696696

697-
context.subscriptions.push(
698-
vscode.commands.registerCommand('pr.pickOnVscodeDev', async (pr: PRNode | RepositoryChangesNode | PullRequestModel) => {
699-
if (pr === undefined) {
700-
// This is unexpected, but has happened a few times.
701-
Logger.error('Unexpectedly received undefined when picking a PR.', logId);
702-
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request was selected to checkout, please try again.'));
703-
}
704-
705-
let pullRequestModel: PullRequestModel;
706-
707-
if (pr instanceof PRNode || pr instanceof RepositoryChangesNode) {
708-
pullRequestModel = pr.pullRequestModel;
709-
} else {
710-
pullRequestModel = pr;
711-
}
697+
const pickPullRequest = async (pr: PRNode | RepositoryChangesNode | PullRequestModel, linkGenerator: (pr: PullRequestModel) => string, requiresHead: boolean = false) => {
698+
if (pr === undefined) {
699+
// This is unexpected, but has happened a few times.
700+
Logger.error('Unexpectedly received undefined when picking a PR.', logId);
701+
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request was selected to checkout, please try again.'));
702+
}
712703

713-
return vscode.env.openExternal(vscode.Uri.parse(vscodeDevPrLink(pullRequestModel)));
714-
}),
715-
);
704+
let pullRequestModel: PullRequestModel;
716705

717-
context.subscriptions.push(
718-
vscode.commands.registerCommand('pr.pickOnCodespaces', async (pr: PRNode | RepositoryChangesNode | PullRequestModel) => {
719-
if (pr === undefined) {
720-
// This is unexpected, but has happened a few times.
721-
Logger.error('Unexpectedly received undefined when picking a PR.', logId);
722-
return vscode.window.showErrorMessage(vscode.l10n.t('No pull request was selected to checkout, please try again.'));
723-
}
706+
if (pr instanceof PRNode || pr instanceof RepositoryChangesNode) {
707+
pullRequestModel = pr.pullRequestModel;
708+
} else {
709+
pullRequestModel = pr;
710+
}
724711

725-
let pullRequestModel: PullRequestModel;
712+
if (requiresHead && !pullRequestModel.head) {
713+
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to checkout pull request: missing head branch information.'));
714+
}
726715

727-
if (pr instanceof PRNode || pr instanceof RepositoryChangesNode) {
728-
pullRequestModel = pr.pullRequestModel;
729-
} else {
730-
pullRequestModel = pr;
731-
}
716+
return vscode.env.openExternal(vscode.Uri.parse(linkGenerator(pullRequestModel)));
717+
};
732718

733-
if (!pullRequestModel.head) {
734-
return vscode.window.showErrorMessage(vscode.l10n.t('Unable to checkout pull request: missing head branch information.'));
735-
}
719+
context.subscriptions.push(
720+
vscode.commands.registerCommand('pr.pickOnVscodeDev', (pr: PRNode | RepositoryChangesNode | PullRequestModel) =>
721+
pickPullRequest(pr, vscodeDevPrLink)
722+
),
723+
);
736724

737-
return vscode.env.openExternal(vscode.Uri.parse(codespacesPrLink(pullRequestModel)));
738-
}),
725+
context.subscriptions.push(
726+
vscode.commands.registerCommand('pr.pickOnCodespaces', (pr: PRNode | RepositoryChangesNode | PullRequestModel) =>
727+
pickPullRequest(pr, codespacesPrLink, true)
728+
),
739729
);
740730

741731
context.subscriptions.push(vscode.commands.registerCommand('pr.checkoutOnVscodeDevFromDescription', async (context: OverviewContext | undefined) => {

0 commit comments

Comments
 (0)