Skip to main content

pr_bro/github/
client.rs

1use super::cache::{get_cache_path, CacheConfig, DiskCache};
2use anyhow::{Context, Result};
3use octocrab::Octocrab;
4use std::sync::Arc;
5
6/// Create an authenticated GitHub client using a personal access token.
7/// Returns both the Octocrab client and an optional DiskCache handle for manual cache control.
8pub fn create_client(
9    token: &str,
10    cache_config: &CacheConfig,
11) -> Result<(Octocrab, Option<Arc<DiskCache>>)> {
12    let mut builder = Octocrab::builder().personal_token(token.to_string());
13
14    let cache_handle = if cache_config.enabled {
15        let cache_path = get_cache_path();
16        let disk_cache = DiskCache::new(cache_path);
17        let cache_handle = Arc::new(disk_cache.clone());
18        builder = builder.cache(disk_cache);
19        Some(cache_handle)
20    } else {
21        None
22    };
23
24    let client = builder.build().context("Failed to create GitHub client")?;
25    Ok((client, cache_handle))
26}