@@ -270,15 +270,26 @@ async fn fetch_all_github_repos(
270270
271271 for org in orgs_to_monitor {
272272 debug ! ( "Fetching repos for org: {}" , org) ;
273+
274+ let token_env_var = github_token_env_var_name ( org) ;
275+ let token = match std:: env:: var ( & token_env_var) {
276+ Ok ( token) => Some ( token) ,
277+ Err ( std:: env:: VarError :: NotPresent ) => None ,
278+ Err ( std:: env:: VarError :: NotUnicode ( _) ) => {
279+ bail ! ( "environment variable {token_env_var} is not valid Unicode" )
280+ }
281+ } ;
282+
273283 let mut page = 1 ;
274284
275285 loop {
276286 let url = format ! ( "orgs/{}/repos?per_page=100&page={}" , org, page) ;
277287
278- let repos: Vec < GitHubRepo > = github
279- . get ( & url)
280- . await
281- . with_context ( || format ! ( "Failed to fetch repos for org: {}" , org) ) ?;
288+ let repos: Vec < GitHubRepo > = match token. as_deref ( ) {
289+ Some ( token) => github. get_with_token ( & url, token) . await ,
290+ None => github. get ( & url) . await ,
291+ }
292+ . with_context ( || format ! ( "Failed to fetch repos for org: {}" , org) ) ?;
282293
283294 if repos. is_empty ( ) {
284295 break ;
@@ -295,6 +306,13 @@ async fn fetch_all_github_repos(
295306 Ok ( all_repos)
296307}
297308
309+ fn github_token_env_var_name ( org : & str ) -> String {
310+ format ! (
311+ "GITHUB_TOKEN_{}" ,
312+ org. to_ascii_uppercase( ) . replace( '-' , "_" )
313+ )
314+ }
315+
298316fn parse_tracked_repos ( data : & Data ) -> HashSet < ( String , String ) > {
299317 data. all_repos ( )
300318 . map ( |repo| ( repo. org . clone ( ) , repo. name . clone ( ) ) )
@@ -390,7 +408,10 @@ fn create_missing_repo_configs(data_dir: &Path, repos: &[UntrackedRepo]) -> anyh
390408
391409#[ cfg( test) ]
392410mod tests {
393- use super :: { GitHubRepo , UntrackedRepo , create_missing_repo_configs, find_untracked_repos} ;
411+ use super :: {
412+ GitHubRepo , UntrackedRepo , create_missing_repo_configs, find_untracked_repos,
413+ github_token_env_var_name,
414+ } ;
394415 use crate :: schema:: Repo ;
395416 use std:: collections:: HashSet ;
396417
@@ -440,6 +461,20 @@ mod tests {
440461 ) ;
441462 }
442463
464+ #[ test]
465+ fn creates_github_token_environment_variable_name ( ) {
466+ for ( org, env_var) in [
467+ ( "rust-lang" , "GITHUB_TOKEN_RUST_LANG" ) ,
468+ ( "rust-lang-nursery" , "GITHUB_TOKEN_RUST_LANG_NURSERY" ) ,
469+ ( "rust-lang-deprecated" , "GITHUB_TOKEN_RUST_LANG_DEPRECATED" ) ,
470+ ( "rust-dev-tools" , "GITHUB_TOKEN_RUST_DEV_TOOLS" ) ,
471+ ( "rust-embedded" , "GITHUB_TOKEN_RUST_EMBEDDED" ) ,
472+ ( "rust-analyzer" , "GITHUB_TOKEN_RUST_ANALYZER" ) ,
473+ ] {
474+ assert_eq ! ( github_token_env_var_name( org) , env_var) ;
475+ }
476+ }
477+
443478 #[ test]
444479 fn creates_parseable_repository_config ( ) {
445480 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
0 commit comments