1
- import { join } from 'path' ;
1
+ import { join } from 'path' ;
2
2
import * as sander from 'sander' ;
3
3
import * as vscode from 'vscode' ;
4
4
5
5
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' ;
9
9
10
10
export function activate ( context : vscode . ExtensionContext ) : void {
11
11
context . subscriptions . push ( new Extension ( context ) ) ;
@@ -22,22 +22,26 @@ class Extension {
22
22
private statusBarManager : StatusBarManager ;
23
23
24
24
constructor ( context : vscode . ExtensionContext ) {
25
+ this . migrateToken ( context ) ;
26
+
25
27
this . channel = vscode . window . createOutputChannel ( 'github' ) ;
26
28
context . subscriptions . push ( this . channel ) ;
27
29
this . channel . appendLine ( 'Visual Studio Code GitHub Extension' ) ;
28
30
29
31
this . githubManager = new GitHubManager ( this . cwd , this . channel ) ;
30
32
this . statusBarManager = new StatusBarManager ( context , this . cwd , this . githubManager , this . channel ) ;
31
33
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 ) ;
35
37
}
36
- this . checkVersionAndToken ( context , token ) ;
38
+ this . checkVersionAndToken ( context , tokens ) ;
37
39
38
40
context . subscriptions . push (
39
41
vscode . commands . registerCommand ( 'vscode-github.browseProject' , this . wrapCommand ( this . browseProject ) ) ,
40
42
vscode . commands . registerCommand ( 'vscode-github.setGitHubToken' , this . createGithubTokenCommand ( context ) ) ,
43
+ vscode . commands . registerCommand ( 'vscode-github.setGitHubEnterpriseToken' ,
44
+ this . createGithubEnterpriseTokenCommand ( context ) ) ,
41
45
vscode . commands . registerCommand ( 'vscode-github.createSimplePullRequest' ,
42
46
this . wrapCommand ( this . createSimplePullRequest ) ) ,
43
47
vscode . commands . registerCommand ( 'vscode-github.createPullRequest' , this . wrapCommand ( this . createPullRequest ) ) ,
@@ -54,7 +58,17 @@ class Extension {
54
58
) ;
55
59
}
56
60
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 > {
58
72
const options : vscode . ProgressOptions = {
59
73
location : vscode . ProgressLocation . SourceControl ,
60
74
title : 'GitHub'
@@ -69,11 +83,11 @@ class Extension {
69
83
return vscode . workspace . rootPath ;
70
84
}
71
85
72
- private async checkVersionAndToken ( context : vscode . ExtensionContext , token : string | undefined ) : Promise < void > {
86
+ private async checkVersionAndToken ( context : vscode . ExtensionContext , tokens : Tokens | undefined ) : Promise < void > {
73
87
const content = await sander . readFile ( join ( context . extensionPath , 'package.json' ) ) ;
74
88
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 ) ) {
77
91
context . globalState . update ( 'version-test' , version ) ;
78
92
vscode . window . showInformationMessage (
79
93
'To enable the Visual Studio Code GitHub Support, please set a Personal Access Token' ) ;
@@ -97,14 +111,14 @@ class Extension {
97
111
}
98
112
99
113
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
+ }
108
122
}
109
123
110
124
private createGithubTokenCommand ( context : vscode . ExtensionContext ) : ( ) => void {
@@ -116,8 +130,32 @@ class Extension {
116
130
} ;
117
131
const input = await vscode . window . showInputBox ( options ) ;
118
132
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
+ }
121
159
}
122
160
} ;
123
161
}
@@ -163,7 +201,7 @@ class Extension {
163
201
progress . report ( `Gather data` ) ;
164
202
let [ owner , repo ] = await git . getGitHubOwnerAndRepository ( this . cwd ) ;
165
203
const repository = await this . githubManager . getRepository ( ) ;
166
- let pullRequest : PullRequest | undefined ;
204
+ let pullRequest : PullRequest | undefined ;
167
205
if ( repository . parent ) {
168
206
let branch : string ;
169
207
const items = [ {
@@ -176,7 +214,7 @@ class Extension {
176
214
branch : repository . parent . default_branch
177
215
} ] ;
178
216
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' } ) ;
180
218
if ( ! selectedRepository ) {
181
219
return ;
182
220
}
@@ -250,7 +288,7 @@ class Extension {
250
288
} ) ;
251
289
}
252
290
253
- private async getMergeMethdod ( ) : Promise < MergeMethod | undefined > {
291
+ private async getMergeMethdod ( ) : Promise < MergeMethod | undefined > {
254
292
const preferedMethod = vscode . workspace . getConfiguration ( 'github' ) . get < MergeMethod > ( 'preferedMergeMethod' ) ;
255
293
if ( preferedMethod ) {
256
294
return preferedMethod ;
@@ -366,7 +404,7 @@ class Extension {
366
404
} ) ;
367
405
}
368
406
369
- private async getUser ( ) : Promise < string | undefined > {
407
+ private async getUser ( ) : Promise < string | undefined > {
370
408
return await vscode . window . showInputBox ( {
371
409
ignoreFocusOut : true ,
372
410
placeHolder : 'username, email or fullname'
0 commit comments