Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit fc8fed4

Browse files
committed
refactor: further simplifications
1 parent ab24371 commit fc8fed4

File tree

1 file changed

+36
-57
lines changed

1 file changed

+36
-57
lines changed

src/extension.ts

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {join} from 'path';
22
import * as sander from 'sander';
33
import * as vscode from 'vscode';
44
import * as git from './git';
5-
import {GitHubError} from './github';
5+
import {GitHubError, PullRequest} from './github';
66
import {StatusBarManager} from './status-bar-manager';
77
import {GitHubManager} from './github-manager';
88

@@ -21,24 +21,18 @@ export function activate(context: vscode.ExtensionContext): void {
2121
githubManager = new GitHubManager(cwd, channel);
2222
statusBarManager = new StatusBarManager(context, githubManager);
2323

24-
const token = getToken(context);
24+
const token = context.globalState.get<string|undefined>('token');
2525
if (token) {
2626
githubManager.connect(token);
2727
}
2828
checkVersionAndToken(context, token);
2929

3030
context.subscriptions.push(
31-
vscode.commands.registerCommand('extension.setGitHubToken',
32-
createGithubTokenCommand(context)));
33-
context.subscriptions.push(
34-
vscode.commands.registerCommand('extension.createPullRequest',
35-
wrapCommand(createPullRequest)));
36-
context.subscriptions.push(
37-
vscode.commands.registerCommand('extension.checkoutPullRequests',
38-
wrapCommand(checkoutPullRequests)));
39-
context.subscriptions.push(
40-
vscode.commands.registerCommand('extension.browserPullRequest',
41-
wrapCommand(browserPullRequest)));
31+
vscode.commands.registerCommand('extension.setGitHubToken', createGithubTokenCommand(context)),
32+
vscode.commands.registerCommand('extension.createPullRequest', wrapCommand(createPullRequest)),
33+
vscode.commands.registerCommand('extension.checkoutPullRequests', wrapCommand(checkoutPullRequests)),
34+
vscode.commands.registerCommand('extension.browserPullRequest', wrapCommand(browserPullRequest))
35+
);
4236
}
4337

4438
function checkVersionAndToken(context: vscode.ExtensionContext, token: string|undefined): void {
@@ -58,7 +52,11 @@ function checkVersionAndToken(context: vscode.ExtensionContext, token: string|un
5852
function wrapCommand<T>(command: T): T {
5953
const wrap: any = (...args: any[]) => {
6054
if (githubManager.connected && cwd) {
61-
return (command as any).apply(null, args);
55+
try {
56+
return (command as any).apply(null, args);
57+
} catch (e) {
58+
logAndShowError(e);
59+
}
6260
} else {
6361
vscode.window.showWarningMessage('Please setup your Github Personal Access Token '
6462
+ 'and open a GitHub project in your workspace');
@@ -67,10 +65,6 @@ function wrapCommand<T>(command: T): T {
6765
return wrap;
6866
}
6967

70-
function getToken(context: vscode.ExtensionContext): string|undefined {
71-
return context.globalState.get<string|undefined>('token');
72-
}
73-
7468
function logAndShowError(e: Error): void {
7569
channel.appendLine(e.message);
7670
if (e instanceof GitHubError) {
@@ -97,50 +91,35 @@ function createGithubTokenCommand(context: vscode.ExtensionContext): () => Promi
9791
}
9892

9993
async function createPullRequest(): Promise<void> {
100-
try {
101-
const pullRequest = await githubManager.createPullRequest();
102-
if (pullRequest) {
103-
statusBarManager.updatePullRequestStatus(true);
104-
vscode.window.showInformationMessage(`Successfully created #${pullRequest.number}`);
105-
}
106-
} catch (e) {
107-
logAndShowError(e);
94+
const pullRequest = await githubManager.createPullRequest();
95+
if (pullRequest) {
96+
statusBarManager.updatePullRequestStatus(true);
97+
vscode.window.showInformationMessage(`Successfully created #${pullRequest.number}`);
10898
}
10999
}
110100

101+
async function selectPullRequest(doSomething: (pullRequest: PullRequest) => void): Promise<void> {
102+
const pullRequests = await githubManager.listPullRequests();
103+
vscode.window.showQuickPick(pullRequests.map(pullRequest => ({
104+
label: pullRequest.title,
105+
description: `#${pullRequest.number}`,
106+
pullRequest
107+
}))).then(selected => {
108+
if (selected) {
109+
doSomething(selected.pullRequest);
110+
}
111+
});
112+
}
113+
111114
async function checkoutPullRequests(): Promise<void> {
112-
try {
113-
const pullRequests = await githubManager.listPullRequests();
114-
vscode.window.showQuickPick(pullRequests.map(pullRequest => ({
115-
label: pullRequest.title,
116-
description: `#${pullRequest.number}`,
117-
pullRequest
118-
}))).then(selected => {
119-
if (selected) {
120-
git.checkout(cwd, selected.pullRequest.head.ref)
121-
.then(() => {
122-
statusBarManager.updatePullRequestStatus();
123-
});
124-
}
125-
});
126-
} catch (e) {
127-
logAndShowError(e);
128-
}
115+
selectPullRequest(async pullRequest => {
116+
await git.checkout(cwd, pullRequest.head.ref);
117+
statusBarManager.updatePullRequestStatus();
118+
});
129119
}
130120

131121
async function browserPullRequest(): Promise<void> {
132-
try {
133-
const response = await githubManager.listPullRequests();
134-
vscode.window.showQuickPick(response.map(pullRequest => ({
135-
label: pullRequest.title,
136-
description: `#${pullRequest.number}`,
137-
pullRequest
138-
}))).then(selected => {
139-
if (selected) {
140-
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(selected.pullRequest.html_url));
141-
}
142-
});
143-
} catch (e) {
144-
logAndShowError(e);
145-
}
122+
selectPullRequest(pullRequest => {
123+
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(pullRequest.html_url));
124+
});
146125
}

0 commit comments

Comments
 (0)