1
1
use anyhow:: Context ;
2
2
use octocrab:: Octocrab ;
3
3
use octocrab:: models:: checks:: CheckRun ;
4
- use octocrab:: models:: { App , Repository } ;
4
+ use octocrab:: models:: { App , CheckSuiteId , Repository } ;
5
5
use octocrab:: params:: checks:: { CheckRunConclusion , CheckRunOutput , CheckRunStatus } ;
6
+ use std:: fmt:: Debug ;
6
7
use tracing:: log;
7
8
8
9
use crate :: bors:: event:: PullRequestComment ;
@@ -16,6 +17,7 @@ use crate::github::api::operations::{
16
17
use crate :: github:: { CommitSha , GithubRepoName , PullRequest , PullRequestNumber } ;
17
18
use crate :: utils:: timing:: { measure_network_request, perform_network_request_with_retry} ;
18
19
use futures:: TryStreamExt ;
20
+ use serde:: de:: DeserializeOwned ;
19
21
20
22
/// Provides access to a single app installation (repository) using the GitHub API.
21
23
pub struct GithubRepositoryClient {
@@ -96,11 +98,7 @@ impl GithubRepositoryClient {
96
98
perform_network_request_with_retry ( "get_branch_sha" , || async {
97
99
// https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#get-a-branch
98
100
let branch: octocrab:: models:: repos:: Branch = self
99
- . client
100
- . get (
101
- format ! ( "/repos/{}/branches/{name}" , self . repository( ) ) . as_str ( ) ,
102
- None :: < & ( ) > ,
103
- )
101
+ . get_request ( & format ! ( "branches/{name}" ) )
104
102
. await
105
103
. context ( "Cannot deserialize branch" ) ?;
106
104
Ok ( CommitSha ( branch. commit . sha ) )
@@ -210,6 +208,7 @@ impl GithubRepositoryClient {
210
208
) -> anyhow:: Result < Vec < CheckSuite > > {
211
209
#[ derive( serde:: Deserialize , Debug ) ]
212
210
struct CheckSuitePayload {
211
+ id : CheckSuiteId ,
213
212
conclusion : Option < String > ,
214
213
head_branch : String ,
215
214
}
@@ -221,17 +220,7 @@ impl GithubRepositoryClient {
221
220
222
221
perform_network_request_with_retry ( "get_check_suites_for_commit" , || async {
223
222
let response: CheckSuiteResponse = self
224
- . client
225
- . get (
226
- format ! (
227
- "/repos/{}/{}/commits/{}/check-suites" ,
228
- self . repo_name. owner( ) ,
229
- self . repo_name. name( ) ,
230
- sha. 0
231
- )
232
- . as_str ( ) ,
233
- None :: < & ( ) > ,
234
- )
223
+ . get_request ( & format ! ( "commits/{}/check-suites" , sha. 0 ) )
235
224
. await
236
225
. context ( "Cannot fetch CheckSuiteResponse" ) ?;
237
226
@@ -240,6 +229,7 @@ impl GithubRepositoryClient {
240
229
. into_iter ( )
241
230
. filter ( |suite| suite. head_branch == branch)
242
231
. map ( |suite| CheckSuite {
232
+ id : suite. id ,
243
233
status : match suite. conclusion {
244
234
Some ( status) => match status. as_str ( ) {
245
235
"success" => CheckSuiteStatus :: Success ,
@@ -249,14 +239,19 @@ impl GithubRepositoryClient {
249
239
}
250
240
_ => {
251
241
tracing:: warn!(
252
- "Received unknown check suite status for {}/{}: {status}" ,
253
- self . repo_name,
254
- sha
242
+ "Received unknown check suite conclusion for {}@{sha}: {status}" ,
243
+ self . repo_name
255
244
) ;
256
245
CheckSuiteStatus :: Pending
257
246
}
258
247
} ,
259
- None => CheckSuiteStatus :: Pending ,
248
+ None => {
249
+ tracing:: warn!(
250
+ "Received empty check suite conclusion for {}@{sha}" ,
251
+ self . repo_name,
252
+ ) ;
253
+ CheckSuiteStatus :: Pending
254
+ }
260
255
} ,
261
256
} )
262
257
. collect ( ) ;
@@ -384,6 +379,18 @@ impl GithubRepositoryClient {
384
379
Ok ( prs)
385
380
}
386
381
382
+ async fn get_request < T : DeserializeOwned + Debug > ( & self , path : & str ) -> anyhow:: Result < T > {
383
+ let url = format ! (
384
+ "/repos/{}/{}/{path}" ,
385
+ self . repo_name. owner( ) ,
386
+ self . repo_name. name( ) ,
387
+ ) ;
388
+ tracing:: debug!( "Sending request to {url}" ) ;
389
+ let response: T = self . client . get ( url. as_str ( ) , None :: < & ( ) > ) . await ?;
390
+ tracing:: debug!( "Received response: {response:?}" ) ;
391
+ Ok ( response)
392
+ }
393
+
387
394
fn format_pr ( & self , pr : PullRequestNumber ) -> String {
388
395
format ! ( "{}/{}" , self . repository( ) , pr)
389
396
}
0 commit comments