1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
use crate::{
    git, server::RepoListenUrl, webhook, ForgeNotification, RegisteredWebhook, WebhookAuth,
    WebhookId,
};

#[mockall::automock]
#[async_trait::async_trait]
pub trait ForgeLike: std::fmt::Debug + Send + Sync {
    fn duplicate(&self) -> Box<dyn ForgeLike>;
    fn name(&self) -> String;

    /// Checks that the message has a valid authorisation.
    fn is_message_authorised(&self, message: &ForgeNotification, expected: &WebhookAuth) -> bool;

    /// Checks if the message should be ignored.
    ///
    /// Default implementation says that no messages should be ignored.
    #[allow(unused_variables)]
    fn should_ignore_message(&self, message: &ForgeNotification) -> bool {
        false
    }

    /// Parses the webhook body into Some(Push) struct if appropriate, or None if not.
    ///
    /// # Errors
    ///
    /// Will return an `Err` if the body is not a message in the expected format.
    fn parse_webhook_body(
        &self,
        body: &webhook::forge_notification::Body,
    ) -> git::forge::webhook::Result<webhook::push::Push>;

    /// Checks the results of any (e.g. CI) status checks for the commit.
    async fn commit_status(&self, commit: &git::Commit) -> git::forge::commit::Status;

    // Lists all the webhooks
    async fn list_webhooks(
        &self,
        repo_listen_url: &RepoListenUrl,
    ) -> git::forge::webhook::Result<Vec<WebhookId>>;

    // Unregisters a webhook
    async fn unregister_webhook(&self, webhook: &WebhookId) -> git::forge::webhook::Result<()>;

    // Registers a webhook
    async fn register_webhook(
        &self,
        repo_listen_url: &RepoListenUrl,
    ) -> git::forge::webhook::Result<RegisteredWebhook>;
}