diff --git a/src/config.rs b/src/config.rs index fca6b8cf..c056c5c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use crate::changelogs::ChangelogFormat; use crate::github::{GithubClient, Repository}; +use octocrab::Octocrab; use std::collections::{HashMap, HashSet}; use std::fmt; use std::sync::{Arc, LazyLock, RwLock}; @@ -7,7 +8,7 @@ use std::time::{Duration, Instant}; use tracing as log; pub(crate) static CONFIG_FILE_NAME: &str = "triagebot.toml"; -const REFRESH_EVERY: Duration = Duration::from_secs(2 * 60); // Every two minutes +const REFRESH_EVERY_SECS: Duration = Duration::from_secs(2 * 60); // Every two minutes static CONFIG_CACHE: LazyLock< RwLock<HashMap<String, (Result<Arc<Config>, ConfigurationError>, Instant)>>, @@ -432,7 +433,7 @@ pub(crate) struct ReviewRequestedConfig { } pub(crate) async fn get( - gh: &GithubClient, + octo_ctx: &Octocrab, repo: &Repository, ) -> Result<Arc<Config>, ConfigurationError> { if let Some(config) = get_cached_config(&repo.full_name) { @@ -440,7 +441,7 @@ pub(crate) async fn get( config } else { log::trace!("fetching fresh config for {}", repo.full_name); - let res = get_fresh_config(gh, repo).await; + let res = get_fresh_config2(&octo_ctx, repo).await; CONFIG_CACHE .write() .unwrap() @@ -525,7 +526,7 @@ fn default_true() -> bool { fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> { let cache = CONFIG_CACHE.read().unwrap(); cache.get(repo).and_then(|(config, fetch_time)| { - if fetch_time.elapsed() < REFRESH_EVERY { + if fetch_time.elapsed() < REFRESH_EVERY_SECS { Some(config.clone()) } else { None @@ -533,6 +534,32 @@ fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationErro }) } +async fn get_fresh_config2( + octo_ctx: &Octocrab, + repo: &Repository, +) -> Result<Arc<Config>, ConfigurationError> { + let mut content_items = octo_ctx + .repos(repo.owner(), repo.name()) + .get_content() + .path(CONFIG_FILE_NAME) + .r#ref(&repo.default_branch) + .send() + .await + .map_err(|e| ConfigurationError::Http(Arc::new(e.into())))?; + + let contents = content_items.take_items(); + let c = &contents[0]; + + let contents = c + .decoded_content() + .ok_or(ConfigurationError::Missing) + .map_err(|e| e)?; + + let config = Arc::new(toml::from_str::<Config>(&contents).map_err(ConfigurationError::Toml)?); + log::debug!("fresh configuration for {}: {:?}", repo.full_name, config); + Ok(config) +} + async fn get_fresh_config( gh: &GithubClient, repo: &Repository, diff --git a/src/handlers.rs b/src/handlers.rs index 7b14a5bc..22cc1198 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -59,7 +59,7 @@ mod transfer; pub mod types_planning_updates; pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> { - let config = config::get(&ctx.github, event.repo()).await; + let config = config::get(&ctx.octocrab, event.repo()).await; if let Err(e) = &config { log::warn!("configuration error {}: {e}", event.repo().full_name); } diff --git a/src/handlers/major_change.rs b/src/handlers/major_change.rs index 76993a88..27d13a6c 100644 --- a/src/handlers/major_change.rs +++ b/src/handlers/major_change.rs @@ -554,7 +554,7 @@ async fn process_seconded( .await .context("failed retrieving the repository informations")?; - let config = crate::config::get(&ctx.github, &repo) + let config = crate::config::get(&ctx.octocrab, &repo) .await .context("failed to get triagebot configuration")?;