Skip to content

Commit

Permalink
Support opening a pull request by number
Browse files Browse the repository at this point in the history
Fixes #266
  • Loading branch information
alexr00 committed Nov 16, 2021
1 parent cd20396 commit 308611b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@
"title": "Refresh Pull Request Description",
"category": "GitHub Pull Requests"
},
{
"command": "pr.checkoutByNumber",
"title": "Checkout Pull Request by Number",
"category": "GitHub Pull Requests",
"icon": "$(symbol-numeric)"
},
{
"command": "review.openFile",
"title": "Open File",
Expand Down Expand Up @@ -1069,15 +1075,20 @@
"when": "gitHubOpenRepositoryCount != 0 && github:initialized && view == pr:github",
"group": "navigation@1"
},
{
"command": "pr.checkoutByNumber",
"when": "gitHubOpenRepositoryCount != 0 && github:initialized && view == pr:github",
"group": "navigation@2"
},
{
"command": "pr.configurePRViewlet",
"when": "gitHubOpenRepositoryCount != 0 && github:initialized && view =~ /(pr|issues):github/",
"group": "navigation@3"
"group": "navigation@4"
},
{
"command": "pr.refreshList",
"when": "gitHubOpenRepositoryCount != 0 && github:initialized && view == pr:github",
"group": "navigation@2"
"group": "navigation@3"
},
{
"command": "pr.refreshChanges",
Expand Down
32 changes: 32 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { formatError } from './common/utils';
import { EXTENSION_ID } from './constants';
import { CredentialStore } from './github/credentials';
import { FolderRepositoryManager } from './github/folderRepositoryManager';
import { GitHubRepository } from './github/githubRepository';
import { PullRequest } from './github/interface';
import { GHPRComment, TemporaryComment } from './github/prComment';
import { PullRequestModel } from './github/pullRequestModel';
Expand Down Expand Up @@ -845,4 +846,35 @@ export function registerCommands(
vscode.commands.registerCommand('pr.collapseAllComments', () => {
sessionState.commentsExpandState = false;
}));

context.subscriptions.push(
vscode.commands.registerCommand('pr.checkoutByNumber', async () => {

const githubRepositories: { manager: FolderRepositoryManager, repo: GitHubRepository }[] = [];
reposManager.folderManagers.forEach(manager => {
githubRepositories.push(...(manager.gitHubRepositories.map(repo => { return { manager, repo }; })));
});
const githubRepo = await chooseItem<{ manager: FolderRepositoryManager, repo: GitHubRepository }>(
githubRepositories,
itemValue => `${itemValue.repo.remote.owner}/${itemValue.repo.remote.repositoryName}`,
{ placeHolder: 'Which GitHub repository do you want to checkout the pull request from?' }
);
if (!githubRepo) {
return;
}
const prNumber = await vscode.window.showInputBox({
ignoreFocusOut: true, prompt: 'Enter the a pull request number',
validateInput: (input) => {
const asNumber = Number(input);
if (Number.isNaN(asNumber)) {
return 'Value must be a number';
}
return undefined;
}
});
if (prNumber === undefined) {
return;
}
return githubRepo.manager.checkoutById(githubRepo.repo, Number(prNumber));
}));
}
16 changes: 16 additions & 0 deletions src/github/folderRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,22 @@ export class FolderRepositoryManager implements vscode.Disposable {
return this.repository.checkout(branchName);
}

async checkoutById(githubRepo: GitHubRepository, id: number): Promise<void> {
const pullRequest = await githubRepo.getPullRequest(id);
if (pullRequest) {
try {
await this.fetchAndCheckout(pullRequest);
} catch (e) {
Logger.appendLine(e.stderr, 'FolderRepositoryManager');
if ((e.stderr as string).startsWith('fatal: couldn\'t find remote ref')) {
vscode.window.showErrorMessage(`The branch for request number ${id} has been deleted from ${githubRepo.remote.owner}/${githubRepo.remote.owner}`);
}
}
} else {
vscode.window.showErrorMessage(`Pull request number ${id} does not exist in ${githubRepo.remote.owner}/${githubRepo.remote.owner}`);
}
}

public async checkoutDefaultBranch(branch: string): Promise<void> {
try {
const branchObj = await this.repository.getBranch(branch);
Expand Down

0 comments on commit 308611b

Please sign in to comment.