Skip to content

Commit 5a032ca

Browse files
Implement profile sync in background jobs
- Add full profile sync support in sync job processor - Fetch and update avatar, name, bio, location, company, followers/following - Support GitHub, Gitea, and GitLab platforms - profile_only=true parameter now works correctly
1 parent e439edd commit 5a032ca

1 file changed

Lines changed: 106 additions & 5 deletions

File tree

backend/src/services/sync_job_processor.rs

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,119 @@ impl SyncJobProcessor {
522522
async fn sync_profile(
523523
&self,
524524
account: &git_platform_account::Model,
525-
_access_token: &str,
525+
access_token: &str,
526526
) -> Result<(), anyhow::Error> {
527527
log::info!(
528528
"👤 [SyncJob] Syncing profile data for {}",
529529
account.platform_username
530530
);
531531

532-
// Profile sync would be platform-specific
533-
// For now, we'll skip it in background jobs as it's less critical
534-
// The profile can still be synced via the direct endpoint
532+
let platform_config = match account.platform_type {
533+
git_platform_account::GitPlatform::GitHub => PlatformConfig::github(),
534+
git_platform_account::GitPlatform::Gitea => {
535+
let url = account
536+
.platform_url
537+
.as_ref()
538+
.ok_or_else(|| anyhow::anyhow!("Gitea URL not found"))?;
539+
PlatformConfig::gitea_custom(url)
540+
}
541+
git_platform_account::GitPlatform::GitLab => {
542+
let url = account
543+
.platform_url
544+
.as_ref()
545+
.ok_or_else(|| anyhow::anyhow!("GitLab URL not found"))?;
546+
PlatformConfig::gitlab_custom(url)
547+
}
548+
};
549+
550+
// Fetch profile data based on platform type
551+
let profile_data = match account.platform_type {
552+
git_platform_account::GitPlatform::GitHub => {
553+
let client = GitHubClient::new();
554+
client
555+
.fetch_user_profile(&platform_config, &account.platform_username, access_token)
556+
.await?
557+
}
558+
git_platform_account::GitPlatform::Gitea => {
559+
let client = GiteaClient::new();
560+
client
561+
.fetch_user_profile(&platform_config, access_token)
562+
.await?
563+
}
564+
git_platform_account::GitPlatform::GitLab => {
565+
let client = GitLabClient::new();
566+
client
567+
.fetch_user_profile(&platform_config, access_token)
568+
.await?
569+
}
570+
};
571+
572+
// Update account with new profile data
573+
let mut active_account: git_platform_account::ActiveModel = account.clone().into();
574+
575+
if let Some(avatar) = profile_data.get("avatar_url").and_then(|v| v.as_str()) {
576+
active_account.avatar_url = Set(Some(avatar.to_string()));
577+
}
578+
579+
if let Some(name) = profile_data.get("name").and_then(|v| v.as_str()) {
580+
active_account.display_name = Set(Some(name.to_string()));
581+
}
582+
583+
if let Some(bio) = profile_data.get("bio").and_then(|v| v.as_str()) {
584+
active_account.bio = Set(Some(bio.to_string()));
585+
}
586+
587+
if let Some(location) = profile_data.get("location").and_then(|v| v.as_str()) {
588+
active_account.location = Set(Some(location.to_string()));
589+
}
535590

536-
log::warn!("👤 [SyncJob] Profile sync not yet implemented in background jobs");
591+
if let Some(company) = profile_data.get("company").and_then(|v| v.as_str()) {
592+
active_account.company = Set(Some(company.to_string()));
593+
}
594+
595+
// Platform-specific fields
596+
match account.platform_type {
597+
git_platform_account::GitPlatform::GitHub => {
598+
if let Some(url) = profile_data.get("html_url").and_then(|v| v.as_str()) {
599+
active_account.profile_url = Set(Some(url.to_string()));
600+
}
601+
if let Some(followers) = profile_data.get("followers").and_then(|v| v.as_i64()) {
602+
active_account.followers_count = Set(Some(followers as i32));
603+
}
604+
if let Some(following) = profile_data.get("following").and_then(|v| v.as_i64()) {
605+
active_account.following_count = Set(Some(following as i32));
606+
}
607+
}
608+
git_platform_account::GitPlatform::Gitea => {
609+
if let Some(url) = profile_data.get("html_url").and_then(|v| v.as_str()) {
610+
active_account.profile_url = Set(Some(url.to_string()));
611+
}
612+
if let Some(followers) =
613+
profile_data.get("followers_count").and_then(|v| v.as_i64())
614+
{
615+
active_account.followers_count = Set(Some(followers as i32));
616+
}
617+
if let Some(following) =
618+
profile_data.get("following_count").and_then(|v| v.as_i64())
619+
{
620+
active_account.following_count = Set(Some(following as i32));
621+
}
622+
}
623+
git_platform_account::GitPlatform::GitLab => {
624+
if let Some(url) = profile_data.get("web_url").and_then(|v| v.as_str()) {
625+
active_account.profile_url = Set(Some(url.to_string()));
626+
}
627+
// GitLab doesn't provide followers/following in user API
628+
}
629+
}
630+
631+
active_account.updated_at = Set(Utc::now());
632+
active_account.update(&self.db).await?;
633+
634+
log::info!(
635+
"👤 [SyncJob] Profile synced successfully for {}",
636+
account.platform_username
637+
);
537638

538639
Ok(())
539640
}

0 commit comments

Comments
 (0)