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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
#[cfg(test)]
mod tests;

mod commit;
mod webhook;

use crate as github;
use git::forge::commit::Status;
use git_next_config as config;
use git_next_git as git;

use derive_more::Constructor;

#[derive(Clone, Debug, Constructor)]
pub struct Github {
    repo_details: git::RepoDetails,
    net: kxio::network::Network,
}
#[async_trait::async_trait]
impl git::ForgeLike for Github {
    fn duplicate(&self) -> Box<dyn git::ForgeLike> {
        Box::new(self.clone())
    }
    fn name(&self) -> String {
        "github".to_string()
    }

    fn is_message_authorised(
        &self,
        msg: &config::ForgeNotification,
        webhook_auth: &config::WebhookAuth,
    ) -> bool {
        github::webhook::is_authorised(msg, webhook_auth)
    }

    fn should_ignore_message(&self, message: &config::ForgeNotification) -> bool {
        let Some(event) = message.header("x-github-event") else {
            return false;
        };
        if event == "ping" {
            tracing::info!("successfull ping received");
            return true;
        }
        tracing::info!(%event, "message");
        false
    }

    fn parse_webhook_body(
        &self,
        body: &config::webhook::forge_notification::Body,
    ) -> git::forge::webhook::Result<config::webhook::push::Push> {
        github::webhook::parse_body(body)
    }

    async fn commit_status(&self, commit: &git::Commit) -> Status {
        github::commit::status(self, commit).await
    }

    async fn list_webhooks(
        &self,
        webhook_url: &config::server::WebhookUrl,
    ) -> git::forge::webhook::Result<Vec<config::WebhookId>> {
        github::webhook::list(self, webhook_url).await
    }

    async fn unregister_webhook(
        &self,
        webhook_id: &config::WebhookId,
    ) -> git::forge::webhook::Result<()> {
        github::webhook::unregister(self, webhook_id).await
    }

    // https://docs.github.com/en/rest/repos/webhooks?apiVersion=2022-11-28#create-a-repository-webhook
    async fn register_webhook(
        &self,
        webhook_url: &config::server::WebhookUrl,
    ) -> git::forge::webhook::Result<config::RegisteredWebhook> {
        github::webhook::register(self, webhook_url).await
    }
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct GithubStatus {
    pub state: GithubState,
    // other fields that we ignore
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
enum GithubState {
    #[serde(rename = "success")]
    Success,
    #[serde(rename = "pending")]
    Pending,
    #[serde(rename = "failure")]
    Failure,
    #[serde(rename = "error")]
    Error,
    #[serde(rename = "")]
    Blank,
}

#[derive(Debug, serde::Deserialize)]
struct GithubHook {
    id: u64,
    config: Config,
}
impl GithubHook {
    pub fn id(&self) -> config::WebhookId {
        config::WebhookId::new(format!("{}", self.id))
    }
    pub fn url(&self) -> config::server::WebhookUrl {
        config::server::WebhookUrl::new(self.config.url.clone())
    }
}
#[derive(Debug, serde::Deserialize)]
struct Config {
    pub url: String,
}