-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add GitHub issue ingestion with admin #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tusharshah21
wants to merge
8
commits into
main
Choose a base branch
from
feature/github-issues-ingestion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6b8e772
feat: add GitHub issue ingestion with admin sync endpoint (#74)
tusharshah21 16f83ba
fix: resolve clippy warnings for CI
tusharshah21 a7064d9
chores: clippy errors
tusharshah21 6fcc877
fix: format code and resolve reqwest version conflict
tusharshah21 5411943
refactor: address PR feedback - field renames, env vars, auth, and docs
tusharshah21 7ec4e9b
chore: apply PR review fixes and add GET /github/issues
tusharshah21 61f60a7
fix: resolve clippy unnecessary_map_or in github sync tests
tusharshah21 7fb9424
fix: use Npts label format per notebook and clean integration test diff
tusharshah21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| CREATE TABLE IF NOT EXISTS github_issues ( | ||
| repo_id BIGINT NOT NULL, | ||
| github_issue_id BIGINT NOT NULL, | ||
| repo TEXT NOT NULL, | ||
tusharshah21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| issue_number INT NOT NULL, | ||
| title TEXT NOT NULL, | ||
| state TEXT NOT NULL CHECK (state IN ('open', 'closed')), | ||
| labels JSONB NOT NULL DEFAULT '[]', | ||
| points INT NOT NULL DEFAULT 0, | ||
| assignee_logins JSONB NOT NULL DEFAULT '[]', | ||
| url TEXT NOT NULL, | ||
| created_at TIMESTAMPTZ NOT NULL, | ||
| closed_at TIMESTAMPTZ, | ||
| rewarded_sepolia BOOLEAN NOT NULL DEFAULT false, | ||
| distribution_id TEXT, | ||
| updated_at TIMESTAMPTZ NOT NULL, | ||
| PRIMARY KEY (repo_id, github_issue_id) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_github_issues_repo ON github_issues(repo); | ||
| CREATE INDEX IF NOT EXISTS idx_github_issues_state ON github_issues(state); | ||
| CREATE INDEX IF NOT EXISTS idx_github_issues_rewarded_sepolia ON github_issues(rewarded_sepolia); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use regex::Regex; | ||
|
|
||
| use crate::domain::{ | ||
| entities::github_issue::GithubIssue, | ||
| repositories::github_issue_repository::GithubIssueRepository, | ||
| services::github_service::{GitHubApiIssue, GithubService}, | ||
| }; | ||
|
|
||
| /// Derive points from labels matching the pattern `Npts` (e.g. `3pts`, `10pts`). | ||
| /// Label names are normalized to lower-case. | ||
| pub fn derive_points(labels: &[crate::domain::services::github_service::GitHubApiLabel]) -> i32 { | ||
| let re = Regex::new(r"^(\d+)pts$").expect("Invalid regex"); | ||
| for label in labels { | ||
| let name = label.name.to_lowercase(); | ||
| if let Some(caps) = re.captures(&name) { | ||
| if let Ok(pts) = caps[1].parse::<i32>() { | ||
| return pts; | ||
| } | ||
| } | ||
| } | ||
| 0 | ||
| } | ||
|
|
||
| /// Transform a GitHub API issue into a domain GithubIssue entity. | ||
| pub fn transform_issue( | ||
| repo: &str, | ||
| repo_id: i64, | ||
| api_issue: &GitHubApiIssue, | ||
| ) -> Result<GithubIssue, String> { | ||
| let labels_normalized: Vec<serde_json::Value> = api_issue | ||
| .labels | ||
| .iter() | ||
| .map(|l| serde_json::Value::String(l.name.to_lowercase())) | ||
| .collect(); | ||
|
|
||
| let assignee_logins: Vec<serde_json::Value> = api_issue | ||
| .assignees | ||
| .iter() | ||
| .map(|a| serde_json::Value::String(a.login.clone())) | ||
| .collect(); | ||
|
|
||
| let points = derive_points(&api_issue.labels); | ||
|
|
||
| let created_at = chrono::DateTime::parse_from_rfc3339(&api_issue.created_at) | ||
| .map_err(|e| format!("Invalid created_at: {e}"))? | ||
| .with_timezone(&chrono::Utc); | ||
|
|
||
| let closed_at = api_issue | ||
| .closed_at | ||
| .as_ref() | ||
| .map(|s| chrono::DateTime::parse_from_rfc3339(s).map(|dt| dt.with_timezone(&chrono::Utc))) | ||
| .transpose() | ||
| .map_err(|e| format!("Invalid closed_at: {e}"))?; | ||
|
|
||
| let updated_at = chrono::DateTime::parse_from_rfc3339(&api_issue.updated_at) | ||
| .map_err(|e| format!("Invalid updated_at: {e}"))? | ||
| .with_timezone(&chrono::Utc); | ||
|
|
||
| Ok(GithubIssue { | ||
| repo_id, | ||
| github_issue_id: api_issue.id, | ||
| repo: repo.to_string(), | ||
| issue_number: api_issue.number, | ||
| title: api_issue.title.clone(), | ||
| state: api_issue.state.clone(), | ||
| labels: serde_json::Value::Array(labels_normalized), | ||
| points, | ||
| assignee_logins: serde_json::Value::Array(assignee_logins), | ||
| url: api_issue.html_url.clone(), | ||
| created_at, | ||
| closed_at, | ||
| rewarded_sepolia: false, | ||
| distribution_id: None, | ||
| updated_at, | ||
| }) | ||
| } | ||
|
|
||
| /// Sync GitHub issues for the given repos. | ||
| pub async fn sync_github_issues( | ||
| github_service: Arc<dyn GithubService>, | ||
| issue_repository: Arc<dyn GithubIssueRepository>, | ||
| repos: Vec<String>, | ||
| since: Option<String>, | ||
| ) -> Result<usize, String> { | ||
| let mut total_synced: usize = 0; | ||
|
|
||
| for repo in &repos { | ||
| let (repo_id, api_issues) = github_service | ||
| .fetch_issues(repo, since.as_deref()) | ||
| .await | ||
| .map_err(|e| format!("Failed to fetch issues for {repo}: {e}"))?; | ||
|
|
||
| for api_issue in &api_issues { | ||
| // Ignore PRs | ||
| if api_issue.pull_request.is_some() { | ||
| continue; | ||
| } | ||
|
|
||
| let issue = transform_issue(repo, repo_id, api_issue)?; | ||
|
|
||
| issue_repository | ||
| .upsert(&issue) | ||
| .await | ||
| .map_err(|e| format!("Failed to upsert issue {}: {e}", api_issue.id))?; | ||
|
|
||
| total_synced += 1; | ||
| } | ||
| } | ||
|
|
||
| Ok(total_synced) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| /// Request DTO for POST /admin/github/sync | ||
| #[derive(Debug, Deserialize)] | ||
| pub struct GithubSyncRequest { | ||
| pub repos: Vec<String>, | ||
| pub since: Option<String>, | ||
| } | ||
|
|
||
| /// Response DTO for POST /admin/github/sync | ||
| #[derive(Debug, Serialize)] | ||
| pub struct GithubSyncResponse { | ||
| pub synced: usize, | ||
| pub repos: Vec<String>, | ||
| } | ||
|
|
||
| /// Query parameters for GET /github/issues | ||
| #[derive(Debug, Deserialize)] | ||
| pub struct GithubIssuesQuery { | ||
| pub repo: String, | ||
| pub state: Option<String>, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod auth_dtos; | ||
| pub mod github_dtos; | ||
| pub mod profile_dtos; | ||
| pub mod project_dtos; | ||
| pub use auth_dtos::*; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use chrono::{DateTime, Utc}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct GithubIssue { | ||
tusharshah21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub repo_id: i64, | ||
| pub github_issue_id: i64, | ||
| pub repo: String, | ||
| pub issue_number: i32, | ||
| pub title: String, | ||
| pub state: String, | ||
| pub labels: serde_json::Value, | ||
| pub points: i32, | ||
| pub assignee_logins: serde_json::Value, | ||
| pub url: String, | ||
| pub created_at: DateTime<Utc>, | ||
| pub closed_at: Option<DateTime<Utc>>, | ||
| pub rewarded_sepolia: bool, | ||
| pub distribution_id: Option<String>, | ||
| pub updated_at: DateTime<Utc>, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod github_issue; | ||
| pub mod profile; | ||
| pub mod projects; | ||
|
|
||
|
|
||
23 changes: 23 additions & 0 deletions
23
backend/src/domain/repositories/github_issue_repository.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use async_trait::async_trait; | ||
|
|
||
| use crate::domain::entities::github_issue::GithubIssue; | ||
|
|
||
| #[async_trait] | ||
| pub trait GithubIssueRepository: Send + Sync { | ||
| /// Upsert a GitHub issue (insert or update based on composite key repo_id + github_issue_id) | ||
| async fn upsert(&self, issue: &GithubIssue) -> Result<(), Box<dyn std::error::Error>>; | ||
|
|
||
| /// Find an issue by its composite key | ||
| async fn find_by_key( | ||
| &self, | ||
| repo_id: i64, | ||
| github_issue_id: i64, | ||
| ) -> Result<Option<GithubIssue>, Box<dyn std::error::Error>>; | ||
|
|
||
| /// List issues filtered by repo name and optional state | ||
| async fn list_by_repo( | ||
| &self, | ||
| repo: &str, | ||
| state: Option<&str>, | ||
| ) -> Result<Vec<GithubIssue>, Box<dyn std::error::Error>>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| pub mod github_issue_repository; | ||
| pub mod profile_repository; | ||
| pub mod project_repository; | ||
|
|
||
| pub use github_issue_repository::GithubIssueRepository; | ||
| pub use profile_repository::ProfileRepository; | ||
| pub use project_repository::ProjectRepository; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| use async_trait::async_trait; | ||
| use serde::Deserialize; | ||
|
|
||
| /// Raw issue data returned from the GitHub API | ||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct GitHubApiIssue { | ||
| pub id: i64, | ||
| pub number: i32, | ||
| pub title: String, | ||
| pub state: String, | ||
| pub html_url: String, | ||
| pub labels: Vec<GitHubApiLabel>, | ||
| pub assignees: Vec<GitHubApiUser>, | ||
| pub created_at: String, | ||
| pub closed_at: Option<String>, | ||
| pub updated_at: String, | ||
| pub pull_request: Option<serde_json::Value>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct GitHubApiLabel { | ||
| pub name: String, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct GitHubApiUser { | ||
| pub login: String, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct GitHubApiRepo { | ||
| pub id: i64, | ||
| } | ||
|
|
||
| #[async_trait] | ||
| pub trait GithubService: Send + Sync { | ||
| /// Fetch issues from a GitHub repository via REST API. | ||
| /// `repo` is the repository name (e.g. "TheGuildGenesis"); owner comes from GITHUB_OWNER env var. | ||
| /// `since` is an optional ISO 8601 timestamp to filter issues updated since that time. | ||
| async fn fetch_issues( | ||
| &self, | ||
| repo: &str, | ||
| since: Option<&str>, | ||
| ) -> Result<(i64, Vec<GitHubApiIssue>), Box<dyn std::error::Error>>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod auth_service; | ||
| pub mod github_service; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| pub mod postgres_github_issue_repository; | ||
| pub mod postgres_profile_repository; | ||
| pub mod postgres_project_repository; | ||
|
|
||
| pub use postgres_github_issue_repository::PostgresGithubIssueRepository; | ||
| pub use postgres_profile_repository::PostgresProfileRepository; | ||
| pub use postgres_project_repository::PostgresProjectRepository; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.