Skip to main content

gitea_sdk_rs/options/
hook.rs

1// Copyright 2026 infinitete. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//! Request option types for webhook API endpoints.
6
7use crate::pagination::{ListOptions, QueryEncode};
8use crate::{Deserialize, Serialize};
9
10use crate::types::enums::HookType;
11
12#[derive(Debug, Clone, Default)]
13/// Options for List Hooks Option.
14pub struct ListHooksOptions {
15    pub list_options: ListOptions,
16}
17
18impl QueryEncode for ListHooksOptions {
19    fn query_encode(&self) -> String {
20        self.list_options.query_encode()
21    }
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25/// Options for Create Hook Option.
26pub struct CreateHookOption {
27    #[serde(rename = "type")]
28    pub hook_type: HookType,
29    #[serde(default)]
30    pub config: std::collections::HashMap<String, String>,
31    #[serde(default, skip_serializing_if = "Vec::is_empty")]
32    pub events: Vec<String>,
33    #[serde(rename = "branch_filter", skip_serializing_if = "Option::is_none")]
34    pub branch_filter: Option<String>,
35    #[serde(default, skip_serializing_if = "is_false")]
36    pub active: bool,
37    #[serde(
38        rename = "authorization_header",
39        skip_serializing_if = "Option::is_none"
40    )]
41    pub authorization_header: Option<String>,
42}
43
44fn is_false(b: &bool) -> bool {
45    !b
46}
47
48impl CreateHookOption {
49    /// Validate this `CreateHookOption` payload.
50    pub fn validate(&self) -> crate::Result<()> {
51        if std::mem::discriminant(&self.hook_type) == std::mem::discriminant(&HookType::Unknown) {
52            return Err(crate::Error::Validation("hook type needed".to_string()));
53        }
54        Ok(())
55    }
56}
57
58#[derive(Debug, Clone, Default, Serialize, Deserialize)]
59/// Options for Edit Hook Option.
60pub struct EditHookOption {
61    #[serde(default, skip_serializing_if = "Option::is_none")]
62    pub config: Option<std::collections::HashMap<String, String>>,
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub events: Option<Vec<String>>,
65    #[serde(rename = "branch_filter", skip_serializing_if = "Option::is_none")]
66    pub branch_filter: Option<String>,
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub active: Option<bool>,
69    #[serde(
70        rename = "authorization_header",
71        skip_serializing_if = "Option::is_none"
72    )]
73    pub authorization_header: Option<String>,
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_create_hook_option_validate_success() {
82        let opt = CreateHookOption {
83            hook_type: HookType::Gitea,
84            config: std::collections::HashMap::new(),
85            events: Vec::new(),
86            branch_filter: None,
87            active: false,
88            authorization_header: None,
89        };
90        assert!(opt.validate().is_ok());
91    }
92
93    #[test]
94    fn test_create_hook_option_validate_unknown_type() {
95        let opt = CreateHookOption {
96            hook_type: HookType::Unknown,
97            config: std::collections::HashMap::new(),
98            events: Vec::new(),
99            branch_filter: None,
100            active: false,
101            authorization_header: None,
102        };
103        assert!(opt.validate().is_err());
104    }
105}