@@ -2,28 +2,31 @@ import {join} from 'path';
22import * as sander from 'sander' ;
33import * as vscode from 'vscode' ;
44import * as git from './git' ;
5- import { getClient , GitHub , GitHubError , ListPullRequestsParameters , CreatePullRequestBody } from './github' ;
5+ import { GitHubError } from './github' ;
6+ import { StatusBarManager } from './status-bar-manager' ;
7+ import { GitHubManager } from './github-manager' ;
68
79let cwd : string ;
8- let storedToken : string ;
9- let github : GitHub ;
1010let channel : vscode . OutputChannel ;
11- let statusBar : vscode . StatusBarItem ;
11+ let githubManager : GitHubManager ;
12+ let statusBarManager : StatusBarManager ;
1213
1314export function activate ( context : vscode . ExtensionContext ) : void {
14- checkVersionAndToken ( context ) ;
15-
1615 cwd = vscode . workspace . rootPath ;
17- getToken ( context )
18- . then ( _token => {
19- storedToken = _token ;
20- refreshPullRequestStatus ( ) ;
21- } ) ;
2216
2317 channel = vscode . window . createOutputChannel ( 'github' ) ;
2418 context . subscriptions . push ( channel ) ;
2519 channel . appendLine ( 'Visual Studio Code GitHub Extension' ) ;
2620
21+ githubManager = new GitHubManager ( cwd , channel ) ;
22+ statusBarManager = new StatusBarManager ( context , githubManager ) ;
23+
24+ const token = getToken ( context ) ;
25+ if ( token ) {
26+ githubManager . connect ( token ) ;
27+ }
28+ checkVersionAndToken ( context , token ) ;
29+
2730 context . subscriptions . push (
2831 vscode . commands . registerCommand ( 'extension.setGitHubToken' ,
2932 createGithubTokenCommand ( context ) ) ) ;
@@ -36,23 +39,13 @@ export function activate(context: vscode.ExtensionContext): void {
3639 context . subscriptions . push (
3740 vscode . commands . registerCommand ( 'extension.browserPullRequest' ,
3841 wrapCommand ( browserPullRequest ) ) ) ;
39-
40- statusBar = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Left ) ;
41- statusBar . command = '' ;
42- statusBar . text = '$(git-pull-request)' ;
43- statusBar . color = '#888' ;
44- context . subscriptions . push ( statusBar ) ;
4542}
4643
47- function checkVersionAndToken ( context : vscode . ExtensionContext ) : void {
44+ function checkVersionAndToken ( context : vscode . ExtensionContext , token : string | undefined ) : void {
4845 sander . readFile ( join ( context . extensionPath , 'package.json' ) )
4946 . then ( content => JSON . parse ( content ) )
5047 . then ( json => json . version as string )
51- . then ( version => {
52- return getToken ( context )
53- . then ( token => ( { token, version} ) ) ;
54- } )
55- . then ( ( { version, token} ) => {
48+ . then ( ( version ) => {
5649 const storedVersion = context . globalState . get ( 'version-test' ) ;
5750 if ( version !== storedVersion && ! Boolean ( token ) ) {
5851 context . globalState . update ( 'version-test' , version ) ;
@@ -62,41 +55,9 @@ function checkVersionAndToken(context: vscode.ExtensionContext): void {
6255 } ) ;
6356}
6457
65- async function refreshPullRequestStatus ( ) : Promise < void > {
66- if ( storedToken ) {
67- await updatePullRequestStatus ( ) ;
68- }
69- setTimeout ( refreshPullRequestStatus , 5000 ) ;
70- }
71-
72- async function updatePullRequestStatus ( forceState ?: boolean ) : Promise < void > {
73- const hasPullRequest = await hasPullRequestForCurrentBranch ( ) ;
74- statusBar . show ( ) ;
75- if ( forceState || hasPullRequest ) {
76- const status = await getCombinedStatusForPullRequest ( ) ;
77- switch ( status ) {
78- case 'failure' :
79- statusBar . color = '#f00' ;
80- break ;
81- case 'success' :
82- statusBar . color = '#0f0' ;
83- break ;
84- default :
85- statusBar . color = '#fff' ;
86- break ;
87- }
88- statusBar . tooltip = '' ;
89- statusBar . command = '' ;
90- } else {
91- statusBar . color = '#888' ;
92- statusBar . tooltip = 'Create pull-request for current branch' ;
93- statusBar . command = 'extension.createPullRequest' ;
94- }
95- }
96-
9758function wrapCommand < T > ( command : T ) : T {
9859 const wrap : any = ( ...args : any [ ] ) => {
99- if ( Boolean ( storedToken ) && Boolean ( cwd ) ) {
60+ if ( githubManager . connected && cwd ) {
10061 return ( command as any ) . apply ( null , args ) ;
10162 } else {
10263 vscode . window . showWarningMessage ( 'Please setup your Github Personal Access Token '
@@ -106,15 +67,8 @@ function wrapCommand<T>(command: T): T {
10667 return wrap ;
10768}
10869
109- function getToken ( context : vscode . ExtensionContext ) : PromiseLike < string > {
110- return Promise . resolve ( context . globalState . get < string > ( 'token' ) ) ;
111- }
112-
113- function getGitHubClient ( ) : GitHub {
114- if ( ! github ) {
115- github = getClient ( storedToken ) ;
116- }
117- return github ;
70+ function getToken ( context : vscode . ExtensionContext ) : string | undefined {
71+ return context . globalState . get < string | undefined > ( 'token' ) ;
11872}
11973
12074function logAndShowError ( e : Error ) : void {
@@ -137,46 +91,16 @@ function createGithubTokenCommand(context: vscode.ExtensionContext): () => Promi
13791 return vscode . window . showInputBox ( options )
13892 . then ( input => {
13993 context . globalState . update ( 'token' , input ) ;
140- storedToken = input ;
94+ githubManager . connect ( input ) ;
14195 } ) ;
14296 } ;
14397}
14498
145- async function hasPullRequestForCurrentBranch ( ) : Promise < boolean > {
146- const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
147- const branch = await git . getCurrentBranch ( cwd ) ;
148- const parameters : ListPullRequestsParameters = {
149- state : 'open' ,
150- head : `${ owner } :${ branch } `
151- } ;
152- const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
153- return response . length > 0 ;
154- }
155-
156- async function getCombinedStatusForPullRequest ( ) : Promise < 'failure' | 'pending' | 'success' | undefined > {
157- const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
158- const branch = await git . getCurrentBranch ( cwd ) ;
159- if ( ! branch ) {
160- return undefined ;
161- }
162- const response = await getGitHubClient ( ) . getStatusForRef ( owner , repository , branch ) ;
163- return response . state ;
164- }
165-
16699async function createPullRequest ( ) : Promise < void > {
167100 try {
168- if ( ! await hasPullRequestForCurrentBranch ( ) ) {
169- const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
170- const branch = await git . getCurrentBranch ( cwd ) ;
171- const body : CreatePullRequestBody = {
172- title : await git . getCommitMessage ( cwd ) ,
173- head : `${ owner } :${ branch } ` ,
174- base : `master`
175- } ;
176- channel . appendLine ( 'Create pull request:' ) ;
177- channel . appendLine ( JSON . stringify ( body , undefined , ' ' ) ) ;
178- const pullRequest = await getGitHubClient ( ) . createPullRequest ( owner , repository , body ) ;
179- updatePullRequestStatus ( true ) ;
101+ const pullRequest = await githubManager . createPullRequest ( ) ;
102+ if ( pullRequest ) {
103+ statusBarManager . updatePullRequestStatus ( true ) ;
180104 vscode . window . showInformationMessage ( `Successfully created #${ pullRequest . number } ` ) ;
181105 }
182106 } catch ( e ) {
@@ -186,18 +110,17 @@ async function createPullRequest(): Promise<void> {
186110
187111async function checkoutPullRequests ( ) : Promise < void > {
188112 try {
189- const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
190- const parameters : ListPullRequestsParameters = {
191- state : 'open'
192- } ;
193- const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
194- vscode . window . showQuickPick ( response . map ( pullRequest => ( {
113+ const pullRequests = await githubManager . listPullRequests ( ) ;
114+ vscode . window . showQuickPick ( pullRequests . map ( pullRequest => ( {
195115 label : pullRequest . title ,
196116 description : `#${ pullRequest . number } ` ,
197117 pullRequest
198118 } ) ) ) . then ( selected => {
199119 if ( selected ) {
200- git . checkout ( cwd , selected . pullRequest . head . ref ) ;
120+ git . checkout ( cwd , selected . pullRequest . head . ref )
121+ . then ( ( ) => {
122+ statusBarManager . updatePullRequestStatus ( ) ;
123+ } ) ;
201124 }
202125 } ) ;
203126 } catch ( e ) {
@@ -207,11 +130,7 @@ async function checkoutPullRequests(): Promise<void> {
207130
208131async function browserPullRequest ( ) : Promise < void > {
209132 try {
210- const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
211- const parameters : ListPullRequestsParameters = {
212- state : 'open'
213- } ;
214- const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
133+ const response = await githubManager . listPullRequests ( ) ;
215134 vscode . window . showQuickPick ( response . map ( pullRequest => ( {
216135 label : pullRequest . title ,
217136 description : `#${ pullRequest . number } ` ,
0 commit comments