use serde::{Deserialize, Serialize};
use super::user::User;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Webhook {
pub uuid: Option<String>,
pub url: String,
pub description: Option<String>,
pub active: Option<bool>,
#[serde(default)]
pub events: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateWebhookRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub url: String,
pub active: bool,
pub events: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchRestriction {
pub id: Option<u64>,
pub kind: String,
pub pattern: Option<String>,
pub branch_match_kind: Option<String>,
pub value: Option<u64>,
pub users: Option<Vec<User>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserPermission {
pub user: Option<User>,
pub permission: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupPermission {
pub group: Option<GroupRef>,
pub permission: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroupRef {
pub name: Option<String>,
pub slug: Option<String>,
}
#[cfg(test)]
mod tests {
use super::{CreateWebhookRequest, Webhook};
#[test]
fn create_webhook_request_omits_unset_description() {
let request = CreateWebhookRequest {
description: None,
url: "https://example.com/hook".to_string(),
active: true,
events: vec!["repo:push".to_string()],
};
let json = serde_json::to_value(&request).unwrap();
assert_eq!(
json,
serde_json::json!({
"url": "https://example.com/hook",
"active": true,
"events": ["repo:push"],
})
);
}
#[test]
fn create_webhook_request_includes_description_when_set() {
let request = CreateWebhookRequest {
description: Some("CI trigger".to_string()),
url: "https://example.com/hook".to_string(),
active: false,
events: vec![],
};
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["description"], "CI trigger");
assert_eq!(json["active"], false);
}
#[test]
fn webhook_deserializes_without_events() {
let webhook: Webhook =
serde_json::from_str(r#"{ "url": "https://example.com/hook" }"#).unwrap();
assert!(webhook.events.is_empty());
assert_eq!(webhook.uuid, None);
}
}