Skip to main content

bitbucket_cli/models/
webhook.rs

1use serde::{Deserialize, Serialize};
2
3use super::user::User;
4
5/// A repository webhook subscription.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Webhook {
8    /// Server-assigned identifier, a UUID wrapped in braces (e.g. `{1a2b-...}`).
9    pub uuid: Option<String>,
10    /// Destination URL the events are delivered to.
11    pub url: String,
12    pub description: Option<String>,
13    pub active: Option<bool>,
14    /// Event keys this hook subscribes to (e.g. `repo:push`).
15    #[serde(default)]
16    pub events: Vec<String>,
17}
18
19/// Body for creating (POST) or replacing (PUT) a webhook subscription.
20///
21/// Bitbucket's PUT replaces the whole subscription, so updates send a full
22/// body built by merging the current hook with the changed fields.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[non_exhaustive]
25pub struct CreateWebhookRequest {
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub description: Option<String>,
28    pub url: String,
29    pub active: bool,
30    pub events: Vec<String>,
31}
32
33/// A branch restriction rule on a repository.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct BranchRestriction {
36    pub id: Option<u64>,
37    /// Rule kind (e.g. `push`, `force`, `delete`, `require_approvals_to_merge`).
38    pub kind: String,
39    /// Branch pattern the rule applies to (glob, e.g. `main` or `release/*`).
40    pub pattern: Option<String>,
41    /// How `pattern` is interpreted (`glob` or `branching_model`).
42    pub branch_match_kind: Option<String>,
43    /// Numeric parameter for kinds that need one (e.g. required approval count).
44    pub value: Option<u64>,
45    /// Users exempt from the rule (for kinds like `push`).
46    pub users: Option<Vec<User>>,
47}
48
49/// A user's explicit permission on a repository.
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct UserPermission {
52    pub user: Option<User>,
53    /// Permission level: `read`, `write`, or `admin`.
54    pub permission: String,
55}
56
57/// A group's explicit permission on a repository.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct GroupPermission {
60    pub group: Option<GroupRef>,
61    /// Permission level: `read`, `write`, or `admin`.
62    pub permission: String,
63}
64
65/// Minimal reference to a workspace group as embedded in permission entries.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct GroupRef {
68    pub name: Option<String>,
69    pub slug: Option<String>,
70}
71
72#[cfg(test)]
73mod tests {
74    use super::{CreateWebhookRequest, Webhook};
75
76    #[test]
77    fn create_webhook_request_omits_unset_description() {
78        let request = CreateWebhookRequest {
79            description: None,
80            url: "https://example.com/hook".to_string(),
81            active: true,
82            events: vec!["repo:push".to_string()],
83        };
84
85        let json = serde_json::to_value(&request).unwrap();
86        assert_eq!(
87            json,
88            serde_json::json!({
89                "url": "https://example.com/hook",
90                "active": true,
91                "events": ["repo:push"],
92            })
93        );
94    }
95
96    #[test]
97    fn create_webhook_request_includes_description_when_set() {
98        let request = CreateWebhookRequest {
99            description: Some("CI trigger".to_string()),
100            url: "https://example.com/hook".to_string(),
101            active: false,
102            events: vec![],
103        };
104
105        let json = serde_json::to_value(&request).unwrap();
106        assert_eq!(json["description"], "CI trigger");
107        assert_eq!(json["active"], false);
108    }
109
110    #[test]
111    fn webhook_deserializes_without_events() {
112        let webhook: Webhook =
113            serde_json::from_str(r#"{ "url": "https://example.com/hook" }"#).unwrap();
114        assert!(webhook.events.is_empty());
115        assert_eq!(webhook.uuid, None);
116    }
117}