Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/github/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
anyhow.workspace = true
urlencoding.workspace = true

106 changes: 105 additions & 1 deletion crates/github/src/commits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
use reqwest::Client;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum CommitSyncError {
#[error("Client build error: {0}")]
ClientBuild(reqwest::Error),
#[error("HTTP request error: {0}")]
RequestFailed(reqwest::Error),
#[error("API error {0}: {1}")]
ApiError(u16, String),
#[error("JSON decode error: {0}")]
JsonDecode(reqwest::Error),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommitStats {
Expand All @@ -9,6 +23,96 @@ pub struct CommitStats {
pub deletions: i32,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GitHubCommitDetail {
pub author: Option<GitHubCommitAuthor>,
pub message: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GitHubCommitAuthor {
pub name: String,
pub email: String,
pub date: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GitHubCommit {
pub sha: String,
pub commit: GitHubCommitDetail,
pub html_url: String,
}

pub fn calculate_commit_impact(additions: i32, deletions: i32) -> f64 {
(additions + deletions) as f64 * 0.1
let total = (additions + deletions) as f64;
(total * 0.1).min(100.0)
}

pub struct GithubCommitClient {
client: Client,
base_url: String,
}

impl GithubCommitClient {
pub fn new() -> Result<Self, CommitSyncError> {
Self::with_base_url("https://api.github.com".to_string())
}

pub fn with_base_url(base_url: String) -> Result<Self, CommitSyncError> {
let client = Client::builder()
.user_agent("DevResume-AI")
.build()
.map_err(CommitSyncError::ClientBuild)?;

Ok(Self { client, base_url })
}

pub async fn fetch_commits(
&self,
token: &str,
owner: &str,
repo: &str,
since: Option<&str>,
) -> Result<Vec<GitHubCommit>, CommitSyncError> {
let mut url = format!(
"{}/repos/{}/{}/commits?per_page=100",
self.base_url, owner, repo
);
if let Some(since_ts) = since {
url.push_str(&format!("&since={}", urlencoding::encode(since_ts)));
}

let response = self
.client
.get(&url)
.bearer_auth(token)
.send()
.await
.map_err(CommitSyncError::RequestFailed)?;

if !response.status().is_success() {
let status = response.status().as_u16();
let text = response.text().await.unwrap_or_default();
return Err(CommitSyncError::ApiError(status, text));
Comment on lines +94 to +96
}

response
.json::<Vec<GitHubCommit>>()
.await
.map_err(CommitSyncError::JsonDecode)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_calculate_commit_impact() {
let impact = calculate_commit_impact(50, 10);
assert_eq!(impact, 6.0);

let capped_impact = calculate_commit_impact(2000, 1000);
assert_eq!(capped_impact, 100.0);
}
}
9 changes: 7 additions & 2 deletions crates/github/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ pub mod issues;
pub mod oauth;
pub mod pull_requests;
pub mod repositories;
pub mod sync;
pub mod webhooks;

pub use actions::detect_github_actions_workflows;
pub use commits::{calculate_commit_impact, CommitStats};
pub use commits::{
calculate_commit_impact, CommitStats, GitHubCommit, GitHubCommitAuthor, GitHubCommitDetail,
GithubCommitClient,
};
pub use oauth::build_github_oauth_url;
pub use pull_requests::analyze_pr_impact;
pub use repositories::{GithubRepo, GithubRepoClient};
pub use repositories::{GithubRepo, GithubRepoClient, SyncOptions};
pub use sync::{RepositorySyncEngine, RepositorySyncReport, SyncError, SyncType};
pub use webhooks::verify_webhook_signature;
128 changes: 116 additions & 12 deletions crates/github/src/repositories.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use reqwest::Client;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Error)]
pub enum GitHubRepoError {
#[error("Client creation failed: {0}")]
ClientBuild(reqwest::Error),
#[error("HTTP request failed: {0}")]
RequestFailed(reqwest::Error),
#[error("API returned error status {0}: {1}")]
ApiError(u16, String),
#[error("Deserialization failed: {0}")]
JsonParse(reqwest::Error),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct GithubRepo {
pub id: i64,
pub name: String,
Expand All @@ -14,30 +27,121 @@ pub struct GithubRepo {
pub stargazers_count: i32,
pub forks_count: i32,
pub open_issues_count: i32,
pub updated_at: Option<String>,
}

#[derive(Debug, Clone)]
pub struct SyncOptions {
pub page: u32,
pub per_page: u32,
pub visibility: String, // "all", "public", "private"
pub sort: String, // "created", "updated", "pushed", "full_name"
}

impl Default for SyncOptions {
fn default() -> Self {
Self {
page: 1,
per_page: 30,
visibility: "all".to_string(),
sort: "updated".to_string(),
}
}
}

pub struct GithubRepoClient {
client: Client,
base_url: String,
}

impl GithubRepoClient {
pub fn new() -> Self {
Self {
client: Client::builder()
.user_agent("DevResume-AI")
.build()
.unwrap(),
pub fn new() -> Result<Self, GitHubRepoError> {
Self::with_base_url("https://api.github.com".to_string())
}

pub fn with_base_url(base_url: String) -> Result<Self, GitHubRepoError> {
let client = Client::builder()
.user_agent("DevResume-AI")
.build()
.map_err(GitHubRepoError::ClientBuild)?;

Ok(Self { client, base_url })
}

pub async fn fetch_repositories(
&self,
token: &str,
opts: &SyncOptions,
) -> Result<Vec<GithubRepo>, GitHubRepoError> {
let url = format!(
"{}/user/repos?page={}&per_page={}&visibility={}&sort={}",
self.base_url, opts.page, opts.per_page, opts.visibility, opts.sort
);

let response = self
.client
.get(&url)
.bearer_auth(token)
.send()
.await
.map_err(GitHubRepoError::RequestFailed)?;

if !response.status().is_success() {
let status = response.status().as_u16();
let text = response.text().await.unwrap_or_default();
return Err(GitHubRepoError::ApiError(status, text));
Comment on lines +90 to +92
}

response
.json::<Vec<GithubRepo>>()
.await
.map_err(GitHubRepoError::JsonParse)
}

pub async fn fetch_repositories(&self, token: &str) -> anyhow::Result<Vec<GithubRepo>> {
pub async fn fetch_single_repository(
&self,
token: &str,
owner: &str,
repo: &str,
) -> Result<GithubRepo, GitHubRepoError> {
let url = format!("{}/repos/{}/{}", self.base_url, owner, repo);

let response = self
.client
.get("https://api.github.com/user/repos")
.get(&url)
.bearer_auth(token)
.send()
.await?;
let repos = response.json::<Vec<GithubRepo>>().await?;
Ok(repos)
.await
.map_err(GitHubRepoError::RequestFailed)?;

if !response.status().is_success() {
let status = response.status().as_u16();
let text = response.text().await.unwrap_or_default();
return Err(GitHubRepoError::ApiError(status, text));
}

response
.json::<GithubRepo>()
.await
.map_err(GitHubRepoError::JsonParse)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_sync_options_default() {
let opts = SyncOptions::default();
assert_eq!(opts.page, 1);
assert_eq!(opts.per_page, 30);
assert_eq!(opts.visibility, "all");
}

#[test]
fn test_client_instantiation() {
let client = GithubRepoClient::new();
assert!(client.is_ok());
}
}
Loading