use crate::error::*;
use crate::github::client::GitHubClient;
use crate::github::graphql::{self, Repository};
use crate::github::{rest, search};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum FallbackPolicy {
Never,
#[default]
OnServerUnavailable,
}
pub struct GitHubService {
pub client: GitHubClient,
fallback_policy: FallbackPolicy,
pub(crate) fetch_options: super::FetchOptions,
}
impl GitHubService {
#[must_use = "Creating a service without using it is wasteful"]
pub fn new() -> Result<Self> {
let client = GitHubClient::new()?;
Ok(Self::with_client(client))
}
#[must_use]
pub fn with_client(client: GitHubClient) -> Self {
Self {
client,
fallback_policy: FallbackPolicy::default(),
fetch_options: super::FetchOptions::default(),
}
}
#[must_use]
pub fn with_fallback_policy(mut self, policy: FallbackPolicy) -> Self {
self.fallback_policy = policy;
self
}
pub async fn get_repository_info(&self, owner: &str, name: &str) -> Result<Repository> {
if !self.client.has_token() {
return rest::get_repository_info(&self.client, owner, name).await;
}
match graphql::get_repository_info(&self.client, owner, name).await {
Ok(repo) => Ok(repo),
Err(graphql_error)
if self.fallback_policy == FallbackPolicy::OnServerUnavailable
&& matches!(
&graphql_error,
GitHubError::ApiError {
status: 502..=504,
..
}
) =>
{
tracing::debug!("GraphQL unavailable, trying REST: {graphql_error}");
rest::get_repository_info(&self.client, owner, name)
.await
.map_err(|rest_error| GitHubError::FallbackError {
graphql: Box::new(graphql_error),
rest: Box::new(rest_error),
})
}
Err(error) => Err(error),
}
}
pub async fn search_repositories(
&self,
days_back: u32,
limit: usize,
language: Option<&str>,
min_stars: u32,
) -> Result<Vec<search::SearchRepository>> {
search::search_repositories(&self.client, days_back, limit, language, min_stars).await
}
pub async fn check_rate_limit(&self) -> Result<crate::RateLimit> {
self.client.check_rate_limit().await
}
pub async fn check_rate_limits(&self) -> Result<crate::RateLimits> {
self.client.check_rate_limits().await
}
#[must_use]
pub fn has_token(&self) -> bool {
self.client.has_token()
}
pub async fn get_user_starred_repositories(&self) -> Result<Vec<String>> {
rest::get_user_starred_repositories(&self.client).await
}
pub async fn get_user_profile(&self) -> Result<rest::UserProfile> {
rest::get_user_profile(&self.client).await
}
pub async fn get_repository_stargazers(
&self,
owner: &str,
name: &str,
per_page: Option<u32>,
page: Option<u32>,
) -> Result<Vec<crate::github::types::StargazerWithDate>> {
rest::get_repository_stargazers(&self.client, owner, name, per_page, page).await
}
}
impl GitHubService {
pub fn with_fetch_options(mut self, options: super::FetchOptions) -> Result<Self> {
self.fetch_options = options.validate()?;
Ok(self)
}
pub async fn get_viewer(&self) -> Result<super::Viewer> {
super::accounts::viewer(&self.client, self.fetch_options).await
}
pub async fn get_owned_repositories(&self, login: &str) -> Result<super::OwnedRepositories> {
super::accounts::repositories(
&self.client,
login,
self.fetch_options,
None::<super::pagination::NoProgress<_>>,
)
.await
}
pub async fn get_owned_repositories_with_progress<F>(
&self,
login: &str,
progress: F,
) -> Result<super::OwnedRepositories>
where
F: AsyncFnMut(&super::RepositoryPage) -> Result<()>,
{
super::accounts::repositories(&self.client, login, self.fetch_options, Some(progress)).await
}
}
impl GitHubService {
pub async fn get_open_issues(&self, owner: &str, name: &str) -> Result<Vec<super::Issue>> {
self.get_open_issues_for_repositories(&[super::RepositoryCoordinates::new(owner, name)?])
.await
}
pub async fn get_open_issues_for_repositories(
&self,
repositories: &[super::RepositoryCoordinates],
) -> Result<Vec<super::Issue>> {
super::work_items::collect(
&self.client,
repositories,
self.fetch_options,
None::<super::pagination::NoProgress<_>>,
)
.await
}
pub async fn get_open_issues_with_progress<F>(
&self,
repositories: &[super::RepositoryCoordinates],
progress: F,
) -> Result<Vec<super::Issue>>
where
F: AsyncFnMut(&super::WorkItemPage<super::Issue>) -> Result<()>,
{
super::work_items::collect(
&self.client,
repositories,
self.fetch_options,
Some(progress),
)
.await
}
}
impl GitHubService {
pub async fn get_open_pull_requests(
&self,
owner: &str,
name: &str,
) -> Result<Vec<super::PullRequest>> {
self.get_open_pull_requests_for_repositories(&[super::RepositoryCoordinates::new(
owner, name,
)?])
.await
}
pub async fn get_open_pull_requests_for_repositories(
&self,
repositories: &[super::RepositoryCoordinates],
) -> Result<Vec<super::PullRequest>> {
super::work_items::collect(
&self.client,
repositories,
self.fetch_options,
None::<super::pagination::NoProgress<_>>,
)
.await
}
pub async fn get_open_pull_requests_with_progress<F>(
&self,
repositories: &[super::RepositoryCoordinates],
progress: F,
) -> Result<Vec<super::PullRequest>>
where
F: AsyncFnMut(&super::WorkItemPage<super::PullRequest>) -> Result<()>,
{
super::work_items::collect(
&self.client,
repositories,
self.fetch_options,
Some(progress),
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_github_service_creation() {
let service = GitHubService::new();
assert!(service.is_ok());
}
#[test]
fn test_github_service_has_token_detection() {
let service = GitHubService::new().unwrap();
let _has_token = service.has_token();
}
}