11'use strict' ;
22import * as vscode from 'vscode' ;
3- import * as execa from 'execa' ;
3+
4+ import * as git from './git' ;
45
56import { getClient , GitHub , GitHubError , ListPullRequestsParameters , CreatePullRequestBody } from './github' ;
67
7- let github : GitHub ;
88let cwd : string ;
9+ let token : string ;
10+ let github : GitHub ;
911
1012export function activate ( context : vscode . ExtensionContext ) : void {
1113 cwd = vscode . workspace . rootPath ;
12- getToken ( context ) . then ( token => {
13- if ( ! github ) {
14- github = getClient ( token ) ;
15- }
14+ getToken ( context ) . then ( _token => {
15+ token = _token ;
1616 } ) ;
1717
1818 context . subscriptions . push (
1919 vscode . commands . registerCommand ( 'extension.setGitHubToken' ,
2020 createGithubTokenCommand ( context ) ) ) ;
2121 context . subscriptions . push (
2222 vscode . commands . registerCommand ( 'extension.createPullRequest' ,
23- createPullRequest ) ) ;
23+ wrapCommand ( createPullRequest ) ) ) ;
2424 context . subscriptions . push (
2525 vscode . commands . registerCommand ( 'extension.checkoutPullRequests' ,
26- checkoutPullRequests ) ) ;
26+ wrapCommand ( checkoutPullRequests ) ) ) ;
27+ }
28+
29+ function wrapCommand < T > ( command : T ) : T {
30+ const wrap : any = ( ...args : any [ ] ) => {
31+ if ( Boolean ( token ) && Boolean ( cwd ) ) {
32+ return ( command as any ) . apply ( null , args ) ;
33+ } else {
34+ vscode . window . showWarningMessage ( 'Please setup your Github Personal Access Token '
35+ + 'and open a GitHub project in your workspace' ) ;
36+ }
37+ } ;
38+ return wrap ;
2739}
2840
2941function getToken ( context : vscode . ExtensionContext ) : PromiseLike < string > {
30- let token = context . globalState . get < string > ( 'token' ) ;
31- if ( token ) {
32- return Promise . resolve ( token ) ;
42+ return Promise . resolve ( context . globalState . get < string > ( 'token' ) ) ;
43+ }
44+
45+ function getGitHubClient ( ) : GitHub {
46+ if ( ! github ) {
47+ github = getClient ( token ) ;
3348 }
34- return createGithubTokenCommand ( context ) ( ) . then ( ( ) => {
35- return context . globalState . get < string > ( 'token' ) ;
36- } ) ;
49+ return github ;
3750}
3851
3952function createGithubTokenCommand ( context : vscode . ExtensionContext ) : ( ) => PromiseLike < void > {
@@ -44,69 +57,36 @@ function createGithubTokenCommand(context: vscode.ExtensionContext): () => Promi
4457 placeHolder : 'GitHub Personal Access Token'
4558 } ;
4659 return vscode . window . showInputBox ( options )
47- . then ( input => context . globalState . update ( 'token' , input ) )
48- . then ( ( ) => getToken ( context ) )
49- . then ( token => {
50- github = getClient ( token ) ;
60+ . then ( input => {
61+ context . globalState . update ( 'token' , input ) ;
62+ token = input ;
5163 } ) ;
5264 } ;
5365}
5466
55- async function getGitHubOwnerAndRepository ( ) : Promise < string [ ] > {
56- return execa ( 'git' , [ 'config' , '--get-regexp' , 'remote\\.origin\\.url' ] , { cwd} )
57- . then ( result => {
58- const match = result . stdout . match ( / ^ r e m o t e .o r i g i n .u r l g i t @ g i t h u b .c o m : ( .* ?) \/ ( .* ?) (?: .g i t ) ? $ / ) ;
59- if ( ! match ) {
60- throw new Error ( 'Not a github project?' ) ;
61- }
62- return [ match [ 1 ] , match [ 2 ] ] ;
63- } ) ;
64- }
65-
66- async function getCurrentBranch ( ) : Promise < string | undefined > {
67- return execa ( 'git' , [ 'status' , '--porcelain' , '--branch' ] , { cwd} )
68- . then ( result => {
69- const match = result . stdout . match ( / ^ # # ( .+ ?) (?: \. \. \. .* ) ? / ) ;
70- return match ? match [ 1 ] : undefined ;
71- } ) ;
72- }
73-
74- async function getCommitMessage ( ) : Promise < string > {
75- return execa ( 'git' , [ 'log' , '--oneline' , '-1' ] , { cwd} )
76- . then ( result => {
77- const match = result . stdout . match ( / ^ (?: .+ ?) ( .* ) / ) ;
78- return match ? match [ 1 ] : result . stdout ;
79- } ) ;
80- }
81-
82- async function checkout ( branch : string ) : Promise < void > {
83- return execa ( 'git' , [ 'checkout' , branch ] , { cwd} )
84- . then ( ( ) => undefined ) ;
85- }
86-
87- async function hasPullRequests ( ) : Promise < boolean > {
88- const [ owner , repository ] = await getGitHubOwnerAndRepository ( ) ;
89- const branch = await getCurrentBranch ( ) ;
67+ async function hasPullRequestForCurrentBranch ( ) : Promise < boolean > {
68+ const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
69+ const branch = await git . getCurrentBranch ( cwd ) ;
9070 const parameters : ListPullRequestsParameters = {
9171 state : 'open' ,
9272 head : `${ owner } :${ branch } `
9373 } ;
94- const response = await github . listPullRequests ( owner , repository , parameters ) ;
74+ const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
9575 return response . body . length > 0 ;
9676}
9777
9878async function createPullRequest ( ) : Promise < void > {
9979 try {
100- if ( ! await hasPullRequests ( ) ) {
101- const [ owner , repository ] = await getGitHubOwnerAndRepository ( ) ;
102- const branch = await getCurrentBranch ( ) ;
80+ if ( ! await hasPullRequestForCurrentBranch ( ) ) {
81+ const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
82+ const branch = await git . getCurrentBranch ( cwd ) ;
10383 console . log ( 'orb' , owner , repository , branch ) ;
10484 const body : CreatePullRequestBody = {
105- title : await getCommitMessage ( ) ,
85+ title : await git . getCommitMessage ( cwd ) ,
10686 head : `${ owner } :${ branch } ` ,
10787 base : `master`
10888 } ;
109- const result = await github . createPullRequest ( owner , repository , body ) ;
89+ const result = await getGitHubClient ( ) . createPullRequest ( owner , repository , body ) ;
11090 console . log ( 'result' , result ) ;
11191 }
11292 } catch ( e ) {
@@ -120,18 +100,18 @@ async function createPullRequest(): Promise<void> {
120100
121101async function checkoutPullRequests ( ) : Promise < void > {
122102 try {
123- const [ owner , repository ] = await getGitHubOwnerAndRepository ( ) ;
103+ const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
124104 const parameters : ListPullRequestsParameters = {
125105 state : 'open'
126106 } ;
127- const response = await github . listPullRequests ( owner , repository , parameters ) ;
107+ const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
128108 vscode . window . showQuickPick ( response . body . map ( pullRequest => ( {
129109 label : pullRequest . title ,
130110 description : `#${ pullRequest . number } ` ,
131111 pullRequest
132112 } ) ) ) . then ( selected => {
133113 if ( selected ) {
134- checkout ( selected . pullRequest . head . ref ) ;
114+ git . checkout ( cwd , selected . pullRequest . head . ref ) ;
135115 }
136116 } ) ;
137117 } catch ( e ) {
0 commit comments