use crate::github::client::GitHubClient;
use crate::github::types::*;
use crate::{config::*, error::*};
use serde::Deserialize;
use std::collections::HashMap;
pub use crate::github::models::Repository;
#[derive(Deserialize)]
struct GraphQLRepository {
#[serde(rename = "id")]
pub node_id: String,
#[serde(rename = "databaseId")]
pub database_id: Option<u64>,
pub name: String,
#[serde(rename = "nameWithOwner")]
pub name_with_owner: String,
pub description: Option<String>,
pub url: String,
#[serde(rename = "homepageUrl")]
pub homepage_url: Option<String>,
#[serde(rename = "createdAt")]
pub created_at: String,
#[serde(rename = "updatedAt")]
pub updated_at: String,
#[serde(rename = "pushedAt")]
pub pushed_at: Option<String>,
#[serde(rename = "isPrivate")]
pub is_private: bool,
#[serde(rename = "isFork")]
pub is_fork: bool,
#[serde(rename = "isArchived")]
pub is_archived: bool,
#[serde(rename = "stargazerCount")]
pub stargazer_count: u32,
#[serde(rename = "forkCount")]
pub fork_count: u32,
pub watchers: TotalCount,
pub issues: TotalCount,
#[serde(rename = "pullRequests")]
pub pull_requests: TotalCount,
pub releases: TotalCount,
#[serde(rename = "primaryLanguage")]
pub primary_language: Option<Language>,
pub languages: GraphQLLanguages,
#[serde(rename = "licenseInfo")]
pub license_info: Option<License>,
#[serde(rename = "defaultBranchRef")]
pub default_branch_ref: Option<Branch>,
#[serde(rename = "repositoryTopics")]
pub repository_topics: TopicConnection,
}
#[derive(Deserialize)]
struct GraphQLLanguages {
edges: Vec<LanguageEdge>,
#[serde(rename = "pageInfo")]
page_info: LanguagePageInfo,
}
#[derive(Deserialize)]
struct LanguagePageInfo {
#[serde(rename = "hasNextPage")]
has_next_page: bool,
}
impl From<GraphQLRepository> for Repository {
fn from(repo: GraphQLRepository) -> Self {
Self {
node_id: repo.node_id,
database_id: repo.database_id,
name: repo.name,
name_with_owner: repo.name_with_owner,
description: repo.description,
url: repo.url,
homepage_url: repo.homepage_url,
created_at: repo.created_at,
updated_at: repo.updated_at,
pushed_at: repo.pushed_at,
is_private: repo.is_private,
is_fork: repo.is_fork,
is_archived: repo.is_archived,
stargazer_count: repo.stargazer_count,
fork_count: repo.fork_count,
watcher_count: Some(repo.watchers.total_count),
open_issue_count: Some(repo.issues.total_count),
pull_request_count: Some(repo.pull_requests.total_count),
release_count: Some(repo.releases.total_count),
primary_language: repo.primary_language,
languages: Some(
repo.languages
.edges
.into_iter()
.map(|edge| crate::github::models::LanguageUsage {
language: edge.node,
bytes: edge.size,
})
.collect(),
),
languages_complete: !repo.languages.page_info.has_next_page,
license_info: repo.license_info,
default_branch: repo.default_branch_ref.map(|branch| branch.name),
topics: repo
.repository_topics
.edges
.into_iter()
.map(|edge| edge.node.topic.name)
.collect(),
}
}
}
#[derive(Deserialize)]
struct RepositoryResponse {
repository: Option<GraphQLRepository>,
}
pub async fn get_repository_info(
client: &GitHubClient,
owner: &str,
name: &str,
) -> Result<Repository> {
let mut variables = HashMap::new();
variables.insert("owner".to_string(), owner.to_string());
variables.insert("name".to_string(), name.to_string());
let query: GraphQLQuery<HashMap<String, String>> = GraphQLQuery {
query: GRAPHQL_REPOSITORY_QUERY.to_string(),
variables,
};
let response = client
.post(client.graphql_url())
.json(&query)
.send()
.await?;
let data: RepositoryResponse = super::response::graphql(response)
.await
.map_err(|error| error.with_repository_context(owner, name))?;
data.repository
.map(Into::into)
.ok_or_else(|| GitHubError::NotFoundError(format!("{owner}/{name}")))
}