Skip to main content

ephemeris_forge_forgejo/api/request/
webhooks.rs

1use std::collections::BTreeMap;
2
3use facet::Facet;
4
5use crate::config::{HookType, Webhook};
6
7/// `CreateHookOption`, with every property the spec lists but the
8/// authorization header.
9#[derive(Clone, Debug, Eq, Facet, PartialEq)]
10pub struct CreateHookOption {
11    /// The kind of hook.
12    #[facet(rename = "type")]
13    pub kind: HookType,
14    /// The delivery URL and the content type.
15    pub config: BTreeMap<String, String>,
16    /// The events that trigger a delivery.
17    pub events: Vec<String>,
18    /// Whether Forgejo delivers events.
19    pub active: bool,
20    /// The glob of branches that trigger push events.
21    pub branch_filter: String,
22}
23
24/// The managed subset of `EditHookOption`.
25///
26/// Forgejo overwrites the events, the branch filter and the authorization
27/// header on every edit, so a complete edit sends all three. It leaves the
28/// secret alone.
29#[derive(Clone, Debug, Default, Eq, Facet, PartialEq)]
30#[facet(skip_all_unless_truthy)]
31pub struct EditHookOption {
32    /// Configuration entries to change.
33    pub config: Option<BTreeMap<String, String>>,
34    /// The events that trigger a delivery.
35    pub events: Option<Vec<String>>,
36    /// Whether Forgejo delivers events.
37    pub active: Option<bool>,
38    /// The glob of branches that trigger push events.
39    pub branch_filter: Option<String>,
40    /// The authorization header, sent back as it is.
41    pub authorization_header: Option<String>,
42}
43
44impl EditHookOption {
45    /// Whether no property would be written.
46    #[must_use]
47    pub fn is_empty(&self) -> bool {
48        *self == Self::default()
49    }
50}
51
52impl CreateHookOption {
53    /// The body that creates the hook for `url` as declared.
54    #[must_use]
55    pub fn new(url: &str, hook: &Webhook) -> Self {
56        Self {
57            kind: hook.kind,
58            config: BTreeMap::from([
59                ("url".to_owned(), url.to_owned()),
60                (
61                    "content_type".to_owned(),
62                    hook.content_type.as_str().to_owned(),
63                ),
64            ]),
65            events: hook.events.clone(),
66            active: hook.active,
67            branch_filter: hook.branch_filter.clone(),
68        }
69    }
70}