|
1 | 1 | use crate::changelogs::ChangelogFormat;
|
2 | 2 | use crate::github::{GithubClient, Repository};
|
| 3 | +use octocrab::Octocrab; |
3 | 4 | use std::collections::{HashMap, HashSet};
|
4 | 5 | use std::fmt;
|
5 | 6 | use std::sync::{Arc, LazyLock, RwLock};
|
6 | 7 | use std::time::{Duration, Instant};
|
7 | 8 | use tracing as log;
|
8 | 9 |
|
9 | 10 | pub(crate) static CONFIG_FILE_NAME: &str = "triagebot.toml";
|
10 |
| -const REFRESH_EVERY: Duration = Duration::from_secs(2 * 60); // Every two minutes |
| 11 | +const REFRESH_EVERY_SECS: Duration = Duration::from_secs(2 * 60); // Every two minutes |
11 | 12 |
|
12 | 13 | static CONFIG_CACHE: LazyLock<
|
13 | 14 | RwLock<HashMap<String, (Result<Arc<Config>, ConfigurationError>, Instant)>>,
|
@@ -432,15 +433,15 @@ pub(crate) struct ReviewRequestedConfig {
|
432 | 433 | }
|
433 | 434 |
|
434 | 435 | pub(crate) async fn get(
|
435 |
| - gh: &GithubClient, |
| 436 | + octo_ctx: &Octocrab, |
436 | 437 | repo: &Repository,
|
437 | 438 | ) -> Result<Arc<Config>, ConfigurationError> {
|
438 | 439 | if let Some(config) = get_cached_config(&repo.full_name) {
|
439 | 440 | log::trace!("returning config for {} from cache", repo.full_name);
|
440 | 441 | config
|
441 | 442 | } else {
|
442 | 443 | log::trace!("fetching fresh config for {}", repo.full_name);
|
443 |
| - let res = get_fresh_config(gh, repo).await; |
| 444 | + let res = get_fresh_config2(&octo_ctx, repo).await; |
444 | 445 | CONFIG_CACHE
|
445 | 446 | .write()
|
446 | 447 | .unwrap()
|
@@ -525,14 +526,40 @@ fn default_true() -> bool {
|
525 | 526 | fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
|
526 | 527 | let cache = CONFIG_CACHE.read().unwrap();
|
527 | 528 | cache.get(repo).and_then(|(config, fetch_time)| {
|
528 |
| - if fetch_time.elapsed() < REFRESH_EVERY { |
| 529 | + if fetch_time.elapsed() < REFRESH_EVERY_SECS { |
529 | 530 | Some(config.clone())
|
530 | 531 | } else {
|
531 | 532 | None
|
532 | 533 | }
|
533 | 534 | })
|
534 | 535 | }
|
535 | 536 |
|
| 537 | +async fn get_fresh_config2( |
| 538 | + octo_ctx: &Octocrab, |
| 539 | + repo: &Repository, |
| 540 | +) -> Result<Arc<Config>, ConfigurationError> { |
| 541 | + let mut content_items = octo_ctx |
| 542 | + .repos(repo.owner(), repo.name()) |
| 543 | + .get_content() |
| 544 | + .path(CONFIG_FILE_NAME) |
| 545 | + .r#ref(&repo.default_branch) |
| 546 | + .send() |
| 547 | + .await |
| 548 | + .map_err(|e| ConfigurationError::Http(Arc::new(e.into())))?; |
| 549 | + |
| 550 | + let contents = content_items.take_items(); |
| 551 | + let c = &contents[0]; |
| 552 | + |
| 553 | + let contents = c |
| 554 | + .decoded_content() |
| 555 | + .ok_or(ConfigurationError::Missing) |
| 556 | + .map_err(|e| e)?; |
| 557 | + |
| 558 | + let config = Arc::new(toml::from_str::<Config>(&contents).map_err(ConfigurationError::Toml)?); |
| 559 | + log::debug!("fresh configuration for {}: {:?}", repo.full_name, config); |
| 560 | + Ok(config) |
| 561 | +} |
| 562 | + |
536 | 563 | async fn get_fresh_config(
|
537 | 564 | gh: &GithubClient,
|
538 | 565 | repo: &Repository,
|
|
0 commit comments