Skip to main content

ephemeris_forge_github/config/
webhooks.rs

1use std::collections::BTreeMap;
2
3use ephemeris_forge::{Mode, WebhookContentType, non_empty_name};
4use facet::Facet;
5
6use super::ConfigError;
7
8non_empty_name! {
9    /// The delivery URL of a webhook, which is never empty.
10    pub struct WebhookUrl;
11    /// A webhook delivery URL was the empty string.
12    pub error EmptyWebhookUrl("webhook URL cannot be empty");
13}
14
15/// A repository webhook. The delivery URL is its key.
16#[derive(Clone, Debug, Eq, Facet, PartialEq)]
17#[facet(deny_unknown_fields)]
18pub struct Webhook {
19    /// The body format of a delivery.
20    pub content_type: WebhookContentType,
21    /// Whether GitHub skips TLS certificate checks on delivery.
22    pub insecure_ssl: bool,
23    /// The events that trigger a delivery.
24    pub events: Vec<String>,
25    /// Whether GitHub delivers events.
26    pub active: bool,
27}
28
29/// Desired webhooks of a repository.
30#[derive(Clone, Debug, Eq, Facet, PartialEq)]
31#[facet(deny_unknown_fields)]
32pub struct Webhooks {
33    /// Whether web hooks that are not declared are kept or deleted.
34    pub mode: Mode,
35    /// The webhooks keyed by delivery URL.
36    pub webhooks: BTreeMap<WebhookUrl, Webhook>,
37}
38
39impl Webhooks {
40    /// Check what decoding cannot.
41    ///
42    /// # Errors
43    /// Returns an error when a webhook declares no events.
44    pub fn validate(&self) -> Result<(), ConfigError> {
45        match self
46            .webhooks
47            .iter()
48            .find(|(_, webhook)| webhook.events.is_empty())
49        {
50            Some((url, _)) => Err(ConfigError::EmptyWebhookEvents { url: url.clone() }),
51            None => Ok(()),
52        }
53    }
54}