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

Commit 0cc8bd6

Browse files
committed
feat: add multi token support
1 parent 94d1cf9 commit 0cc8bd6

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
"title": "Set Personal Access Token...",
6161
"category": "GitHub"
6262
},
63+
{
64+
"command": "vscode-github.setGitHubEnterpriseToken",
65+
"title": "Setup GitHub Enterprise Token...",
66+
"category": "GitHub"
67+
},
6368
{
6469
"command": "vscode-github.browseProject",
6570
"title": "Browse project",

src/extension.ts

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {join} from 'path';
1+
import { join } from 'path';
22
import * as sander from 'sander';
33
import * as vscode from 'vscode';
44

55
import * as git from './git';
6-
import {GitHubError, PullRequest, MergeMethod} from './github';
7-
import {GitHubManager} from './github-manager';
8-
import {StatusBarManager} from './status-bar-manager';
6+
import { GitHubError, PullRequest, MergeMethod } from './github';
7+
import { GitHubManager, Tokens } from './github-manager';
8+
import { StatusBarManager } from './status-bar-manager';
99

1010
export function activate(context: vscode.ExtensionContext): void {
1111
context.subscriptions.push(new Extension(context));
@@ -22,22 +22,26 @@ class Extension {
2222
private statusBarManager: StatusBarManager;
2323

2424
constructor(context: vscode.ExtensionContext) {
25+
this.migrateToken(context);
26+
2527
this.channel = vscode.window.createOutputChannel('github');
2628
context.subscriptions.push(this.channel);
2729
this.channel.appendLine('Visual Studio Code GitHub Extension');
2830

2931
this.githubManager = new GitHubManager(this.cwd, this.channel);
3032
this.statusBarManager = new StatusBarManager(context, this.cwd, this.githubManager, this.channel);
3133

32-
const token = context.globalState.get<string|undefined>('token');
33-
if (token) {
34-
this.githubManager.connect(token);
34+
const tokens = context.globalState.get<Tokens>('tokens');
35+
if (tokens) {
36+
this.githubManager.connect(tokens);
3537
}
36-
this.checkVersionAndToken(context, token);
38+
this.checkVersionAndToken(context, tokens);
3739

3840
context.subscriptions.push(
3941
vscode.commands.registerCommand('vscode-github.browseProject', this.wrapCommand(this.browseProject)),
4042
vscode.commands.registerCommand('vscode-github.setGitHubToken', this.createGithubTokenCommand(context)),
43+
vscode.commands.registerCommand('vscode-github.setGitHubEnterpriseToken',
44+
this.createGithubEnterpriseTokenCommand(context)),
4145
vscode.commands.registerCommand('vscode-github.createSimplePullRequest',
4246
this.wrapCommand(this.createSimplePullRequest)),
4347
vscode.commands.registerCommand('vscode-github.createPullRequest', this.wrapCommand(this.createPullRequest)),
@@ -54,7 +58,17 @@ class Extension {
5458
);
5559
}
5660

57-
private async withinProgressUI<R>(task: (progress: vscode.Progress<{message?: string}>) => Promise<R>): Promise<R> {
61+
private migrateToken(context: vscode.ExtensionContext): void {
62+
const token = context.globalState.get<string | undefined>('token');
63+
if (token) {
64+
const tokens = context.globalState.get<Tokens>('tokens', {});
65+
tokens['github.com'] = token;
66+
context.globalState.update('tokens', tokens);
67+
context.globalState.update(token, undefined);
68+
}
69+
}
70+
71+
private async withinProgressUI<R>(task: (progress: vscode.Progress<{ message?: string }>) => Promise<R>): Promise<R> {
5872
const options: vscode.ProgressOptions = {
5973
location: vscode.ProgressLocation.SourceControl,
6074
title: 'GitHub'
@@ -69,11 +83,11 @@ class Extension {
6983
return vscode.workspace.rootPath;
7084
}
7185

72-
private async checkVersionAndToken(context: vscode.ExtensionContext, token: string|undefined): Promise<void> {
86+
private async checkVersionAndToken(context: vscode.ExtensionContext, tokens: Tokens | undefined): Promise<void> {
7387
const content = await sander.readFile(join(context.extensionPath, 'package.json'));
7488
const version = JSON.parse(content.toString()).version as string;
75-
const storedVersion = context.globalState.get<string|undefined>('version-test');
76-
if (version !== storedVersion && !Boolean(token)) {
89+
const storedVersion = context.globalState.get<string | undefined>('version-test');
90+
if (version !== storedVersion && (!tokens || Object.keys(tokens).length === 0)) {
7791
context.globalState.update('version-test', version);
7892
vscode.window.showInformationMessage(
7993
'To enable the Visual Studio Code GitHub Support, please set a Personal Access Token');
@@ -97,14 +111,14 @@ class Extension {
97111
}
98112

99113
private logAndShowError(e: Error): void {
100-
this.channel.appendLine(e.message);
101-
if (e instanceof GitHubError) {
102-
console.error(e.response);
103-
vscode.window.showErrorMessage('GitHub error: ' + e.message);
104-
} else {
105-
console.error(e);
106-
vscode.window.showErrorMessage('Error: ' + e.message);
107-
}
114+
this.channel.appendLine(e.message);
115+
if (e instanceof GitHubError) {
116+
console.error(e.response);
117+
vscode.window.showErrorMessage('GitHub error: ' + e.message);
118+
} else {
119+
console.error(e);
120+
vscode.window.showErrorMessage('Error: ' + e.message);
121+
}
108122
}
109123

110124
private createGithubTokenCommand(context: vscode.ExtensionContext): () => void {
@@ -116,8 +130,32 @@ class Extension {
116130
};
117131
const input = await vscode.window.showInputBox(options);
118132
if (input) {
119-
context.globalState.update('token', input);
120-
await this.githubManager.connect(input);
133+
const tokens = context.globalState.get<Tokens>('tokens', {});
134+
tokens['github.com'] = input;
135+
context.globalState.update('tokens', tokens);
136+
await this.githubManager.connect(tokens);
137+
}
138+
};
139+
}
140+
141+
private createGithubEnterpriseTokenCommand(context: vscode.ExtensionContext): () => void {
142+
return async() => {
143+
const hostInput = await vscode.window.showInputBox({
144+
ignoreFocusOut: true,
145+
placeHolder: 'GitHub Enterprise Hostname'
146+
});
147+
if (hostInput) {
148+
const tokenInput = await vscode.window.showInputBox({
149+
ignoreFocusOut: true,
150+
password: true,
151+
placeHolder: 'GitHub Enterprise Token'
152+
});
153+
if (tokenInput) {
154+
const tokens = context.globalState.get<Tokens>('tokens', {});
155+
tokens[hostInput] = tokenInput;
156+
context.globalState.update('tokens', tokens);
157+
this.githubManager.connect(tokens);
158+
}
121159
}
122160
};
123161
}
@@ -163,7 +201,7 @@ class Extension {
163201
progress.report(`Gather data`);
164202
let [owner, repo] = await git.getGitHubOwnerAndRepository(this.cwd);
165203
const repository = await this.githubManager.getRepository();
166-
let pullRequest: PullRequest|undefined;
204+
let pullRequest: PullRequest | undefined;
167205
if (repository.parent) {
168206
let branch: string;
169207
const items = [{
@@ -176,7 +214,7 @@ class Extension {
176214
branch: repository.parent.default_branch
177215
}];
178216
const selectedRepository = await vscode.window.showQuickPick(items,
179-
{placeHolder: 'Select a repository to create the pull request in'});
217+
{ placeHolder: 'Select a repository to create the pull request in' });
180218
if (!selectedRepository) {
181219
return;
182220
}
@@ -250,7 +288,7 @@ class Extension {
250288
});
251289
}
252290

253-
private async getMergeMethdod(): Promise<MergeMethod|undefined> {
291+
private async getMergeMethdod(): Promise<MergeMethod | undefined> {
254292
const preferedMethod = vscode.workspace.getConfiguration('github').get<MergeMethod>('preferedMergeMethod');
255293
if (preferedMethod) {
256294
return preferedMethod;
@@ -366,7 +404,7 @@ class Extension {
366404
});
367405
}
368406

369-
private async getUser(): Promise<string|undefined> {
407+
private async getUser(): Promise<string | undefined> {
370408
return await vscode.window.showInputBox({
371409
ignoreFocusOut: true,
372410
placeHolder: 'username, email or fullname'

src/github-manager.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as git from './git';
44
import {getClient, GitHub, GitHubError, PullRequest, ListPullRequestsParameters, CreatePullRequestBody,
55
PullRequestStatus, Merge, MergeMethod, Repository} from './github';
66

7+
export interface Tokens {
8+
[host: string]: string;
9+
}
10+
711
export class GitHubManager {
812

913
private cwd: string;
@@ -26,8 +30,13 @@ export class GitHubManager {
2630
return Boolean(this.github);
2731
}
2832

29-
public async connect(token: string): Promise<void> {
30-
this.github = getClient(await this.getApiEndpoint(), token);
33+
public async getGitHubHostname(): Promise<string> {
34+
return git.getGitHubHostname(this.cwd);
35+
}
36+
37+
public async connect(tokens: Tokens): Promise<void> {
38+
const hostname = await git.getGitHubHostname(this.cwd);
39+
this.github = getClient(await this.getApiEndpoint(), tokens[hostname]);
3140
}
3241

3342
private async getApiEndpoint(): Promise<string> {

0 commit comments

Comments
 (0)