1
1
#![ allow( clippy:: doc_markdown) ]
2
2
#![ allow( clippy:: missing_errors_doc) ]
3
3
#![ allow( clippy:: single_match_else) ]
4
- use crate :: { Comment , GitHubApi , GithubError } ;
4
+ use crate :: { Comment , DEFAULT_GITHUB_API_URL , GitHubApi , GithubError } ;
5
5
use jsonwebtoken:: { Algorithm , EncodingKey , Header } ;
6
6
7
7
use log:: info;
@@ -32,10 +32,14 @@ pub struct GithubAccessToken {
32
32
pub token : String ,
33
33
}
34
34
/// https://developer.github.com/v3/apps/#create-an-installation-access-token-for-an-app
35
- fn create_access_token ( jwt : & str , install_id : i64 ) -> Result < GithubAccessToken , GithubError > {
35
+ fn create_access_token (
36
+ github_api_url : & str ,
37
+ jwt : & str ,
38
+ install_id : i64 ,
39
+ ) -> Result < GithubAccessToken , GithubError > {
36
40
Ok ( reqwest:: Client :: new ( )
37
41
. post ( & format ! (
38
- "https://api.github.com /app/installations/{install_id}/access_tokens" ,
42
+ "{github_api_url} /app/installations/{install_id}/access_tokens" ,
39
43
) )
40
44
. header ( AUTHORIZATION , format ! ( "Bearer {jwt}" ) )
41
45
. header ( ACCEPT , "application/vnd.github.machine-man-preview+json" )
@@ -45,11 +49,15 @@ fn create_access_token(jwt: &str, install_id: i64) -> Result<GithubAccessToken,
45
49
}
46
50
47
51
/// https://developer.github.com/v3/issues/comments/#create-an-issue-comment
48
- pub ( crate ) fn create_comment ( comment : CommentArgs , secret : & str ) -> Result < ( ) , GithubError > {
52
+ pub ( crate ) fn create_comment (
53
+ github_api_url : & str ,
54
+ comment : CommentArgs ,
55
+ secret : & str ,
56
+ ) -> Result < ( ) , GithubError > {
49
57
let comment_body = CommentBody { body : comment. body } ;
50
58
reqwest:: Client :: new ( )
51
59
. post ( & format ! (
52
- "https://api.github.com /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
60
+ "{github_api_url} /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
53
61
owner = comment. owner,
54
62
repo = comment. repo,
55
63
issue_number = comment. issue
@@ -68,9 +76,9 @@ pub struct GitHubAppInfo {
68
76
}
69
77
70
78
/// Get the bot name for finding existing comments on a PR
71
- pub fn get_app_info ( jwt : & str ) -> Result < GitHubAppInfo , GithubError > {
79
+ pub fn get_app_info ( github_api_url : & str , jwt : & str ) -> Result < GitHubAppInfo , GithubError > {
72
80
Ok ( reqwest:: Client :: new ( )
73
- . get ( "https://api.github.com/ app")
81
+ . get ( & format ! ( "{github_api_url}/ app") )
74
82
. header ( AUTHORIZATION , format ! ( "Bearer {jwt}" ) )
75
83
. send ( ) ?
76
84
. error_for_status ( ) ?
@@ -142,12 +150,16 @@ pub struct PullRequest {
142
150
}
143
151
144
152
/// https://developer.github.com/v3/issues/comments/#list-issue-comments
145
- pub ( crate ) fn list_comments ( pr : & PullRequest , secret : & str ) -> Result < Vec < Comment > , GithubError > {
153
+ pub ( crate ) fn list_comments (
154
+ github_api_url : & str ,
155
+ pr : & PullRequest ,
156
+ secret : & str ,
157
+ ) -> Result < Vec < Comment > , GithubError > {
146
158
// TODO(sbdchd): use the next links to get _all_ the comments
147
159
// see: https://developer.github.com/v3/guides/traversing-with-pagination/
148
160
Ok ( reqwest:: Client :: new ( )
149
161
. get ( & format ! (
150
- "https://api.github.com /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
162
+ "{github_api_url} /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
151
163
owner = pr. owner,
152
164
repo = pr. repo,
153
165
issue_number = pr. issue
@@ -161,6 +173,7 @@ pub(crate) fn list_comments(pr: &PullRequest, secret: &str) -> Result<Vec<Commen
161
173
162
174
/// https://developer.github.com/v3/issues/comments/#update-an-issue-comment
163
175
pub ( crate ) fn update_comment (
176
+ github_api_url : & str ,
164
177
owner : & str ,
165
178
repo : & str ,
166
179
comment_id : i64 ,
@@ -169,7 +182,7 @@ pub(crate) fn update_comment(
169
182
) -> Result < ( ) , GithubError > {
170
183
reqwest:: Client :: new ( )
171
184
. patch ( & format ! (
172
- "https://api.github.com /repos/{owner}/{repo}/issues/comments/{comment_id}" ,
185
+ "{github_api_url} /repos/{owner}/{repo}/issues/comments/{comment_id}" ,
173
186
) )
174
187
. header ( AUTHORIZATION , format ! ( "Bearer {secret}" ) )
175
188
. json ( & CommentBody { body } )
@@ -179,19 +192,30 @@ pub(crate) fn update_comment(
179
192
}
180
193
181
194
pub struct GitHub {
195
+ github_api_url : String ,
182
196
slug_name : String ,
183
197
installation_access_token : String ,
184
198
}
185
199
186
200
impl GitHub {
187
201
pub fn new ( private_key : & str , app_id : i64 , installation_id : i64 ) -> Result < Self , GithubError > {
202
+ Self :: new_with_url ( DEFAULT_GITHUB_API_URL , private_key, app_id, installation_id)
203
+ }
204
+
205
+ pub fn new_with_url (
206
+ github_api_url : & str ,
207
+ private_key : & str ,
208
+ app_id : i64 ,
209
+ installation_id : i64 ,
210
+ ) -> Result < Self , GithubError > {
188
211
info ! ( "generating jwt" ) ;
189
212
let jwt = generate_jwt ( private_key, app_id) ?;
190
213
info ! ( "getting app info" ) ;
191
- let app_info = get_app_info ( & jwt) ?;
192
- let access_token = create_access_token ( & jwt, installation_id) ?;
214
+ let app_info = get_app_info ( github_api_url , & jwt) ?;
215
+ let access_token = create_access_token ( github_api_url , & jwt, installation_id) ?;
193
216
194
217
Ok ( GitHub {
218
+ github_api_url : github_api_url. to_string ( ) ,
195
219
slug_name : format ! ( "{}[bot]" , app_info. slug) ,
196
220
installation_access_token : access_token. token ,
197
221
} )
@@ -210,6 +234,7 @@ impl GitHubApi for GitHub {
210
234
body : & str ,
211
235
) -> Result < ( ) , GithubError > {
212
236
create_comment (
237
+ & self . github_api_url ,
213
238
CommentArgs {
214
239
owner : owner. to_string ( ) ,
215
240
repo : repo. to_string ( ) ,
@@ -226,6 +251,7 @@ impl GitHubApi for GitHub {
226
251
issue_id : i64 ,
227
252
) -> Result < Vec < Comment > , GithubError > {
228
253
list_comments (
254
+ & self . github_api_url ,
229
255
& PullRequest {
230
256
owner : owner. to_string ( ) ,
231
257
repo : repo. to_string ( ) ,
@@ -242,6 +268,7 @@ impl GitHubApi for GitHub {
242
268
body : & str ,
243
269
) -> Result < ( ) , GithubError > {
244
270
update_comment (
271
+ & self . github_api_url ,
245
272
owner,
246
273
repo,
247
274
comment_id,
0 commit comments