Skip to main content

ephemeris_forge_github/reconcile/
webhooks.rs

1use ephemeris_forge::{Mode, RepoRef};
2
3use super::Client;
4use crate::api::request::{WebhookBody, WebhookConfigBody, WebhookPatch};
5use crate::api::{Api, ForgeError};
6use crate::operation::webhooks::Operation;
7use crate::{Live, Webhook, Webhooks};
8
9impl<A: Api> Client<A> {
10    pub(super) async fn read_webhooks(
11        &self,
12        repository: &RepoRef,
13        config: Option<&Webhooks>,
14        live: &mut Live,
15    ) -> Result<(), ForgeError> {
16        let Some(config) = config else {
17            return Ok(());
18        };
19        if config.mode != Mode::Owned && config.webhooks.is_empty() {
20            return Ok(());
21        }
22        live.webhooks = self.api.webhooks(repository).await?;
23        Ok(())
24    }
25
26    pub(super) async fn apply_webhooks(
27        &self,
28        repository: &RepoRef,
29        operation: &Operation,
30    ) -> Result<(), ForgeError> {
31        match operation {
32            Operation::Create { url, webhook } => {
33                let body = WebhookBody {
34                    name: "web".to_owned(),
35                    config: config_body(url, webhook),
36                    events: webhook.events.clone(),
37                    active: webhook.active,
38                };
39                self.api.create_webhook(repository, &body).await
40            }
41            Operation::Update {
42                url,
43                id,
44                webhook,
45                config_changed,
46            } => {
47                let patch = WebhookPatch {
48                    config: config_changed.then(|| config_body(url, webhook)),
49                    events: webhook.events.clone(),
50                    active: webhook.active,
51                };
52                self.api.update_webhook(repository, *id, &patch).await
53            }
54            Operation::Delete { url: _, id } => self.api.delete_webhook(repository, *id).await,
55        }
56    }
57}
58
59fn config_body(url: &str, webhook: &Webhook) -> WebhookConfigBody {
60    WebhookConfigBody {
61        url: url.to_owned(),
62        content_type: webhook.content_type.as_str().to_owned(),
63        insecure_ssl: if webhook.insecure_ssl { "1" } else { "0" }.to_owned(),
64    }
65}