//! A tenant's managed git source (#439). //! //! BreakPilot runs one Gitea instance per customer. A [`GitSource`] names that //! instance and the organisation whose repositories Prüfwerk checks: every //! repository in it becomes a target automatically (by system/org webhook and //! by a periodic sync), and findings come back as issues in the repository. //! //! One source per tenant, stored in the tenant database under a fixed id. use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use super::credentials::Credentials; /// The `_id` of a tenant's (single) git source document. pub const GIT_SOURCE_ID: &str = "default"; /// The managed git instance of a tenant, as stored. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct GitSource { /// Always [`GIT_SOURCE_ID`]: one source per tenant. #[serde(rename = "_id")] pub id: String, /// The instance origin, `https://` without a path or trailing slash. pub base_url: String, /// The organisation whose repositories are checked. pub org: String, /// The bot user's access token, sealed at rest. #[serde(default, skip_serializing_if = "Option::is_none")] pub bot_token: Option, /// The HMAC secret of the system/org webhook, sealed at rest. #[serde(default, skip_serializing_if = "Option::is_none")] pub webhook_secret: Option, /// Whether webhooks and the periodic sync act on this source. #[serde(default)] pub enabled: bool, /// When the source was first configured. #[serde( default = "chrono::Utc::now", with = "super::serde_helpers::bson_datetime" )] pub created_at: DateTime, /// When the source was last changed. #[serde( default = "chrono::Utc::now", with = "super::serde_helpers::bson_datetime" )] pub updated_at: DateTime, /// When the periodic sync last listed the organisation's repositories. #[serde(default, with = "super::serde_helpers::opt_bson_datetime")] pub last_sync_at: Option>, } impl Credentials for GitSource { fn credentials_mut(&mut self) -> Vec<&mut Option> { vec![&mut self.bot_token, &mut self.webhook_secret] } } /// A git source as the API shows it: no credentials, only whether they are set. #[derive(Debug, Clone, PartialEq, Serialize)] pub struct GitSourceView { pub base_url: String, pub org: String, pub enabled: bool, pub has_bot_token: bool, pub has_webhook_secret: bool, pub created_at: DateTime, pub updated_at: DateTime, pub last_sync_at: Option>, } impl From<&GitSource> for GitSourceView { fn from(s: &GitSource) -> Self { Self { base_url: s.base_url.clone(), org: s.org.clone(), enabled: s.enabled, has_bot_token: s.bot_token.is_some(), has_webhook_secret: s.webhook_secret.is_some(), created_at: s.created_at, updated_at: s.updated_at, last_sync_at: s.last_sync_at, } } } /// The repository a target was created for by a git source. Its presence is /// what makes a target "managed": created, renamed and archived by the source, /// with CRA-labelled issues. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SourceRepo { /// The source's `base_url` when the target was created. pub base_url: String, /// Gitea's repository id: stable across renames and transfers. pub repo_id: i64, /// `owner/name` as last seen. pub full_name: String, } #[cfg(test)] #[allow(clippy::expect_used, clippy::unwrap_used)] mod tests { use super::*; fn source() -> GitSource { let now = Utc::now(); GitSource { id: GIT_SOURCE_ID.into(), base_url: "https://kunde.code.breakpilot.com".into(), org: "kunde".into(), bot_token: Some("sec-token".into()), webhook_secret: Some("sec-hook".into()), enabled: true, created_at: now, updated_at: now, last_sync_at: None, } } #[test] fn both_secrets_are_credentials() { let mut s = source(); let mut v: Vec = s .credentials_mut() .into_iter() .filter_map(|slot| slot.clone()) .collect(); v.sort(); assert_eq!(v, ["sec-hook", "sec-token"]); } #[test] fn the_view_carries_no_secret() { let view = GitSourceView::from(&source()); let json = serde_json::to_string(&view).unwrap(); assert!(!json.contains("sec-"), "{json}"); assert!(view.has_bot_token && view.has_webhook_secret); } #[test] fn bson_round_trip() { let s = source(); let b = bson::to_bson(&s).unwrap(); let back: GitSource = bson::from_bson(b).unwrap(); assert_eq!(back.base_url, s.base_url); assert_eq!(back.bot_token, s.bot_token); } }