@@ -71,13 +71,58 @@ export class GitHubServer {
7171 Logger . info ( 'Logging in...' ) ;
7272 const state = uuid ( ) ;
7373 const callbackUri = await vscode . env . asExternalUri ( vscode . Uri . parse ( `${ vscode . env . uriScheme } ://vscode.github-authentication/did-authenticate` ) ) ;
74- const clientDetails = ClientRegistrar . getClientDetails ( callbackUri ) ;
74+ const clientDetails = scopes === 'vso' ? ClientRegistrar . getGitHubAppDetails ( ) : ClientRegistrar . getClientDetails ( callbackUri ) ;
7575 const uri = vscode . Uri . parse ( `https://github.com/login/oauth/authorize?redirect_uri=${ encodeURIComponent ( callbackUri . toString ( ) ) } &scope=${ scopes } &state=${ state } &client_id=${ clientDetails . id } ` ) ;
7676
7777 vscode . env . openExternal ( uri ) ;
7878 return promiseFromEvent ( uriHandler . event , exchangeCodeForToken ( state , clientDetails ) ) ;
7979 }
8080
81+ public async hasUserInstallation ( token : string ) : Promise < boolean > {
82+ return new Promise ( ( resolve , reject ) => {
83+ Logger . info ( 'Getting user installations...' ) ;
84+ const post = https . request ( {
85+ host : 'api.github.com' ,
86+ path : `/user/installations` ,
87+ method : 'GET' ,
88+ headers : {
89+ Accept : 'application/vnd.github.machine-man-preview+json' ,
90+ Authorization : `token ${ token } ` ,
91+ 'User-Agent' : 'Visual-Studio-Code'
92+ }
93+ } , result => {
94+ const buffer : Buffer [ ] = [ ] ;
95+ result . on ( 'data' , ( chunk : Buffer ) => {
96+ buffer . push ( chunk ) ;
97+ } ) ;
98+ result . on ( 'end' , ( ) => {
99+ if ( result . statusCode === 200 ) {
100+ const json = JSON . parse ( Buffer . concat ( buffer ) . toString ( ) ) ;
101+ Logger . info ( 'Got installation info!' ) ;
102+ const hasInstallation = json . installations . some ( ( installation : { app_slug : string } ) => installation . app_slug === 'microsoft-visual-studio-code' ) ;
103+ resolve ( hasInstallation ) ;
104+ } else {
105+ reject ( new Error ( result . statusMessage ) ) ;
106+ }
107+ } ) ;
108+ } ) ;
109+
110+ post . end ( ) ;
111+ post . on ( 'error' , err => {
112+ reject ( err ) ;
113+ } ) ;
114+ } ) ;
115+ }
116+
117+ public async installApp ( ) : Promise < string > {
118+ const clientDetails = ClientRegistrar . getGitHubAppDetails ( ) ;
119+ const state = uuid ( ) ;
120+ const uri = vscode . Uri . parse ( `https://github.com/apps/microsoft-visual-studio-code/installations/new?state=${ state } ` ) ;
121+
122+ vscode . env . openExternal ( uri ) ;
123+ return promiseFromEvent ( uriHandler . event , exchangeCodeForToken ( state , clientDetails ) ) ;
124+ }
125+
81126 public async getUserInfo ( token : string ) : Promise < { id : string , accountName : string } > {
82127 return new Promise ( ( resolve , reject ) => {
83128 Logger . info ( 'Getting account info...' ) ;
0 commit comments