1
1
'use strict' ;
2
2
import * as vscode from 'vscode' ;
3
- import * as execa from 'execa' ;
3
+
4
+ import * as git from './git' ;
4
5
5
6
import { getClient , GitHub , GitHubError , ListPullRequestsParameters , CreatePullRequestBody } from './github' ;
6
7
7
- let github : GitHub ;
8
8
let cwd : string ;
9
+ let token : string ;
10
+ let github : GitHub ;
9
11
10
12
export function activate ( context : vscode . ExtensionContext ) : void {
11
13
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 ;
16
16
} ) ;
17
17
18
18
context . subscriptions . push (
19
19
vscode . commands . registerCommand ( 'extension.setGitHubToken' ,
20
20
createGithubTokenCommand ( context ) ) ) ;
21
21
context . subscriptions . push (
22
22
vscode . commands . registerCommand ( 'extension.createPullRequest' ,
23
- createPullRequest ) ) ;
23
+ wrapCommand ( createPullRequest ) ) ) ;
24
24
context . subscriptions . push (
25
25
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 ;
27
39
}
28
40
29
41
function 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 ) ;
33
48
}
34
- return createGithubTokenCommand ( context ) ( ) . then ( ( ) => {
35
- return context . globalState . get < string > ( 'token' ) ;
36
- } ) ;
49
+ return github ;
37
50
}
38
51
39
52
function createGithubTokenCommand ( context : vscode . ExtensionContext ) : ( ) => PromiseLike < void > {
@@ -44,69 +57,36 @@ function createGithubTokenCommand(context: vscode.ExtensionContext): () => Promi
44
57
placeHolder : 'GitHub Personal Access Token'
45
58
} ;
46
59
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 ;
51
63
} ) ;
52
64
} ;
53
65
}
54
66
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 ) ;
90
70
const parameters : ListPullRequestsParameters = {
91
71
state : 'open' ,
92
72
head : `${ owner } :${ branch } `
93
73
} ;
94
- const response = await github . listPullRequests ( owner , repository , parameters ) ;
74
+ const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
95
75
return response . body . length > 0 ;
96
76
}
97
77
98
78
async function createPullRequest ( ) : Promise < void > {
99
79
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 ) ;
103
83
console . log ( 'orb' , owner , repository , branch ) ;
104
84
const body : CreatePullRequestBody = {
105
- title : await getCommitMessage ( ) ,
85
+ title : await git . getCommitMessage ( cwd ) ,
106
86
head : `${ owner } :${ branch } ` ,
107
87
base : `master`
108
88
} ;
109
- const result = await github . createPullRequest ( owner , repository , body ) ;
89
+ const result = await getGitHubClient ( ) . createPullRequest ( owner , repository , body ) ;
110
90
console . log ( 'result' , result ) ;
111
91
}
112
92
} catch ( e ) {
@@ -120,18 +100,18 @@ async function createPullRequest(): Promise<void> {
120
100
121
101
async function checkoutPullRequests ( ) : Promise < void > {
122
102
try {
123
- const [ owner , repository ] = await getGitHubOwnerAndRepository ( ) ;
103
+ const [ owner , repository ] = await git . getGitHubOwnerAndRepository ( cwd ) ;
124
104
const parameters : ListPullRequestsParameters = {
125
105
state : 'open'
126
106
} ;
127
- const response = await github . listPullRequests ( owner , repository , parameters ) ;
107
+ const response = await getGitHubClient ( ) . listPullRequests ( owner , repository , parameters ) ;
128
108
vscode . window . showQuickPick ( response . body . map ( pullRequest => ( {
129
109
label : pullRequest . title ,
130
110
description : `#${ pullRequest . number } ` ,
131
111
pullRequest
132
112
} ) ) ) . then ( selected => {
133
113
if ( selected ) {
134
- checkout ( selected . pullRequest . head . ref ) ;
114
+ git . checkout ( cwd , selected . pullRequest . head . ref ) ;
135
115
}
136
116
} ) ;
137
117
} catch ( e ) {
0 commit comments