@@ -6,8 +6,9 @@ use std::time::Duration;
66
77use anyhow:: Context ;
88use bors:: {
9- create_app, create_bors_process, create_github_client, load_repositories, BorsContext ,
10- BorsGlobalEvent , CommandParser , PgDbClient , ServerState , TeamApiClient , WebhookSecret ,
9+ create_app, create_bors_process, create_github_client, create_github_client_from_access_token,
10+ load_repositories, BorsContextBuilder , BorsGlobalEvent , CommandParser , GithubRepoName ,
11+ PgDbClient , ServerState , TeamApiClient , WebhookSecret ,
1112} ;
1213use clap:: Parser ;
1314use sqlx:: postgres:: PgConnectOptions ;
@@ -18,6 +19,8 @@ use tracing_subscriber::filter::EnvFilter;
1819/// How often should the bot check DB state, e.g. for handling timeouts.
1920const PERIODIC_REFRESH : Duration = Duration :: from_secs ( 120 ) ;
2021
22+ const GITHUB_API_URL : & str = "https://api.github.com" ;
23+
2124#[ derive( clap:: Parser ) ]
2225struct Opts {
2326 /// Github App ID.
@@ -39,6 +42,10 @@ struct Opts {
3942 /// Prefix used for bot commands in PR comments.
4043 #[ arg( long, env = "CMD_PREFIX" , default_value = "@bors" ) ]
4144 cmd_prefix : String ,
45+
46+ /// Prefix used for bot commands in PR comments.
47+ #[ arg( long, env = "CI_ACCESS_TOKEN" ) ]
48+ ci_access_token : Option < String > ,
4249}
4350
4451/// Starts a server that receives GitHub webhooks and generates events into a queue
@@ -81,18 +88,34 @@ fn try_main(opts: Opts) -> anyhow::Result<()> {
8188 let db = runtime
8289 . block_on ( initialize_db ( & opts. db ) )
8390 . context ( "Cannot initialize database" ) ?;
84- let team_api = TeamApiClient :: default ( ) ;
85- let ( client, loaded_repos ) = runtime. block_on ( async {
86- let client = create_github_client (
91+ let team_api_client = TeamApiClient :: default ( ) ;
92+ let client = runtime. block_on ( async {
93+ create_github_client (
8794 opts. app_id . into ( ) ,
88- "https://api.github.com" . to_string ( ) ,
95+ GITHUB_API_URL . to_string ( ) ,
8996 opts. private_key . into ( ) ,
90- ) ?;
91- let repos = load_repositories ( & client, & team_api) . await ?;
92- Ok :: < _ , anyhow:: Error > ( ( client, repos) )
97+ )
98+ } ) ?;
99+ let ci_client = match opts. ci_access_token {
100+ Some ( access_token) => {
101+ let client = runtime. block_on ( async {
102+ tracing:: warn!( "creating client ci" ) ;
103+ create_github_client_from_access_token (
104+ GITHUB_API_URL . to_string ( ) ,
105+ access_token. into ( ) ,
106+ )
107+ } ) ?;
108+ Some ( client)
109+ }
110+ None => None ,
111+ } ;
112+ let loaded_repos = runtime. block_on ( async {
113+ let repos = load_repositories ( & client, ci_client. clone ( ) , & team_api_client) . await ?;
114+ Ok :: < _ , anyhow:: Error > ( repos)
93115 } ) ?;
94116
95117 let mut repos = HashMap :: default ( ) ;
118+ let mut ci_repo_map: HashMap < GithubRepoName , GithubRepoName > = HashMap :: default ( ) ;
96119 for ( name, repo) in loaded_repos {
97120 let repo = match repo {
98121 Ok ( repo) => {
@@ -105,11 +128,27 @@ fn try_main(opts: Opts) -> anyhow::Result<()> {
105128 ) ) ;
106129 }
107130 } ;
131+ if repo. ci_client . repository ( ) != repo. client . repository ( ) {
132+ ci_repo_map. insert (
133+ repo. ci_client . repository ( ) . clone ( ) ,
134+ repo. client . repository ( ) . clone ( ) ,
135+ ) ;
136+ }
108137 repos. insert ( name, Arc :: new ( repo) ) ;
109138 }
110139
111- let ctx = BorsContext :: new ( CommandParser :: new ( opts. cmd_prefix ) , Arc :: new ( db) , repos) ;
112- let ( repository_tx, global_tx, bors_process) = create_bors_process ( ctx, client, team_api) ;
140+ let ctx = BorsContextBuilder :: default ( )
141+ . parser ( CommandParser :: new ( opts. cmd_prefix ) )
142+ . db ( Arc :: new ( db) )
143+ . repositories ( repos)
144+ . gh_client ( client)
145+ . ci_client ( ci_client)
146+ . ci_repo_map ( ci_repo_map)
147+ . team_api_client ( team_api_client)
148+ . build ( )
149+ . unwrap ( ) ;
150+
151+ let ( repository_tx, global_tx, bors_process) = create_bors_process ( ctx) ;
113152
114153 let refresh_tx = global_tx. clone ( ) ;
115154 let refresh_process = async move {
0 commit comments