a06673b
use db::conn::sea_orm::*;
use db::git::git_branches as branch_entity;
use db::git::git_tags as tag_entity;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::error::{RepoError, RepoResult};
use crate::features::RepoService;
use super::types::{SyncResult, SyncStatus};
fn require_authenticated(actor: Uuid) -> RepoResult<()> {
if actor.is_nil() {
return Err(RepoError::Unauthorized("authentication required".into()));
}
Ok(())
}
pub async fn get_sync_status(
svc: &RepoService,
actor: Uuid,
repo_id: Uuid,
) -> RepoResult<SyncStatus> {
let _ = actor;
let db = svc.db.reader();
let branches_total = branch_entity::Entity::find()
.filter(branch_entity::Column::RepoId.eq(repo_id))
.filter(branch_entity::Column::IsDeleted.eq(false))
.count(&db)
.await?;
let tags_total = tag_entity::Entity::find()
.filter(tag_entity::Column::RepoId.eq(repo_id))
.count(&db)
.await?;
let repo = db::repo::repos::Entity::find_by_id(repo_id)
.one(&db)
.await?
.ok_or_else(|| RepoError::NotFound(format!("repo {repo_id}")))?;
Ok(SyncStatus {
repo_id,
branches_total: branches_total as i64,
tags_total: tags_total as i64,
commits_total: repo.nums_commits as i64,
primary_language: repo.primary_language,
size: repo.size,
last_pushed_at: repo.last_pushed_at.map(|t| {
t.format(&time::format_description::well_known::Rfc3339)
.unwrap_or_default()
}),
synced_at: OffsetDateTime::now_utc()
.format(&time::format_description::well_known::Rfc3339)
.unwrap_or_default(),
})
}
pub async fn sync_repo_metadata(
svc: &RepoService,
actor: Uuid,
repo_id: Uuid,
) -> RepoResult<SyncResult> {
require_authenticated(actor)?;
let db = svc.db.reader();
let branches_added = branch_entity::Entity::find()
.filter(branch_entity::Column::RepoId.eq(repo_id))
.filter(branch_entity::Column::IsDeleted.eq(false))
.count(&db)
.await?;
let tags_added = tag_entity::Entity::find()
.filter(tag_entity::Column::RepoId.eq(repo_id))
.count(&db)
.await?;
let _ = db::repo::repos::Entity::find_by_id(repo_id)
.one(&db)
.await?;
Ok(SyncResult {
repo_id,
branches_added: branches_added as i64,
tags_added: tags_added as i64,
commits_added: 0,
size_bytes: 0,
})
}