//! The periodic sync's diff: the organisation's repositories against the //! tenant's managed targets. A safety net for lost webhook deliveries. use mongodb::bson::oid::ObjectId; use compliance_core::models::OnboardedTarget; use super::gitea::GiteaRepo; /// What a sync does. #[derive(Debug, Default, PartialEq, Eq)] pub struct SyncPlan { /// Repositories whose target is missing, archived or out of date /// (renamed, other default branch): ensured. pub ensure: Vec, /// Targets whose repository is gone: archived. pub archive: Vec, } /// Diff `repos` (the organisation's repositories now) against `targets` (the /// tenant's managed targets of this source, archived ones included). pub fn plan_sync( repos: &[GiteaRepo], targets: &[OnboardedTarget], base_url: &str, org: &str, ) -> SyncPlan { let repos: Vec<&GiteaRepo> = repos.iter().filter(|r| r.in_org(org)).collect(); let target_of = |id: i64| { targets.iter().find(|t| { t.source_repo .as_ref() .is_some_and(|s| s.repo_id == id && s.base_url == base_url) }) }; let ensure = repos .iter() .filter(|r| match target_of(r.id) { None => true, Some(t) => super::refresh_update(t, base_url, r).is_some(), }) .map(|r| (*r).clone()) .collect(); let archive = targets .iter() .filter(|t| t.archived_at.is_none()) .filter_map(|t| { let source = t.source_repo.as_ref()?; let gone = source.base_url == base_url && !repos.iter().any(|r| r.id == source.repo_id); if gone { t.id } else { None } }) .collect(); SyncPlan { ensure, archive } } #[cfg(test)] #[allow(clippy::expect_used, clippy::unwrap_used)] mod tests { use super::*; use crate::git_source::target_for_repo; const BASE: &str = "https://kunde.code.breakpilot.com"; fn repo(id: i64, full_name: &str) -> GiteaRepo { serde_json::from_value(serde_json::json!({ "id": id, "name": full_name.split('/').nth(1).unwrap(), "full_name": full_name, "owner": { "login": full_name.split('/').next().unwrap() }, "default_branch": "main", })) .unwrap() } fn target(id: i64, full_name: &str) -> OnboardedTarget { let mut t = target_for_repo(BASE, &repo(id, full_name), "enc:v1:x".into()); t.id = Some(ObjectId::new()); t } #[test] fn creates_missing_archives_gone_and_leaves_the_rest() { let repos = [repo(1, "kunde/a"), repo(2, "kunde/b"), repo(3, "andere/c")]; let gone = target(9, "kunde/old"); let targets = vec![target(1, "kunde/a"), gone.clone()]; let plan = plan_sync(&repos, &targets, BASE, "kunde"); assert_eq!(plan.ensure, vec![repo(2, "kunde/b")]); assert_eq!(plan.archive, vec![gone.id.unwrap()]); } #[test] fn a_second_sync_of_the_same_state_does_nothing() { let repos = [repo(1, "kunde/a"), repo(2, "kunde/b")]; let targets = vec![target(1, "kunde/a"), target(2, "kunde/b")]; assert_eq!( plan_sync(&repos, &targets, BASE, "kunde"), SyncPlan::default() ); } #[test] fn renamed_and_reappeared_repos_are_ensured() { let mut archived = target(2, "kunde/b"); archived.archived_at = Some(chrono::Utc::now()); let targets = vec![target(1, "kunde/a"), archived]; let repos = [repo(1, "kunde/a-neu"), repo(2, "kunde/b")]; let plan = plan_sync(&repos, &targets, BASE, "kunde"); assert_eq!(plan.ensure, repos.to_vec()); assert!(plan.archive.is_empty()); } #[test] fn archived_targets_are_not_archived_again_and_other_instances_untouched() { let mut archived = target(5, "kunde/x"); archived.archived_at = Some(chrono::Utc::now()); let mut elsewhere = target(6, "kunde/y"); elsewhere.source_repo.as_mut().unwrap().base_url = "https://alt.code.breakpilot.com".into(); let plan = plan_sync(&[], &[archived, elsewhere], BASE, "kunde"); assert_eq!(plan, SyncPlan::default()); } }