2024-05-25 11:25:13 +01:00
|
|
|
//
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2024-05-31 07:37:40 +01:00
|
|
|
mod commit;
|
2024-05-25 11:25:13 +01:00
|
|
|
mod webhook;
|
|
|
|
|
|
|
|
use crate as github;
|
|
|
|
use git::forge::commit::Status;
|
2024-07-25 09:02:43 +01:00
|
|
|
use git_next_core::{
|
|
|
|
self as core, server, ForgeNotification, RegisteredWebhook, WebhookAuth, WebhookId,
|
|
|
|
};
|
2024-05-25 11:25:13 +01:00
|
|
|
use git_next_git as git;
|
|
|
|
|
|
|
|
use derive_more::Constructor;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Constructor)]
|
|
|
|
pub struct Github {
|
|
|
|
repo_details: git::RepoDetails,
|
2024-05-31 07:37:40 +01:00
|
|
|
net: kxio::network::Network,
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl git::ForgeLike for Github {
|
2024-06-19 07:03:08 +01:00
|
|
|
fn duplicate(&self) -> Box<dyn git::ForgeLike> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
2024-05-25 11:25:13 +01:00
|
|
|
fn name(&self) -> String {
|
|
|
|
"github".to_string()
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
fn is_message_authorised(&self, msg: &ForgeNotification, webhook_auth: &WebhookAuth) -> bool {
|
2024-05-31 07:37:40 +01:00
|
|
|
github::webhook::is_authorised(msg, webhook_auth)
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
fn should_ignore_message(&self, message: &ForgeNotification) -> bool {
|
2024-06-30 15:20:00 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
fn parse_webhook_body(
|
|
|
|
&self,
|
2024-07-25 09:02:43 +01:00
|
|
|
body: &core::webhook::forge_notification::Body,
|
|
|
|
) -> git::forge::webhook::Result<core::webhook::push::Push> {
|
2024-05-31 07:37:40 +01:00
|
|
|
github::webhook::parse_body(body)
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn commit_status(&self, commit: &git::Commit) -> Status {
|
2024-05-31 07:37:40 +01:00
|
|
|
github::commit::status(self, commit).await
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn list_webhooks(
|
|
|
|
&self,
|
2024-07-25 09:02:43 +01:00
|
|
|
webhook_url: &server::WebhookUrl,
|
|
|
|
) -> git::forge::webhook::Result<Vec<WebhookId>> {
|
2024-06-04 18:30:11 +01:00
|
|
|
github::webhook::list(self, webhook_url).await
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
async fn unregister_webhook(&self, webhook_id: &WebhookId) -> git::forge::webhook::Result<()> {
|
2024-05-31 07:37:40 +01:00
|
|
|
github::webhook::unregister(self, webhook_id).await
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.github.com/en/rest/repos/webhooks?apiVersion=2022-11-28#create-a-repository-webhook
|
|
|
|
async fn register_webhook(
|
|
|
|
&self,
|
2024-07-25 09:02:43 +01:00
|
|
|
webhook_url: &server::WebhookUrl,
|
|
|
|
) -> git::forge::webhook::Result<RegisteredWebhook> {
|
2024-05-31 07:37:40 +01:00
|
|
|
github::webhook::register(self, webhook_url).await
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 18:30:11 +01:00
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
struct GithubStatus {
|
2024-05-25 11:25:13 +01:00
|
|
|
pub state: GithubState,
|
|
|
|
// other fields that we ignore
|
|
|
|
}
|
2024-06-04 18:30:11 +01:00
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
2024-05-25 11:25:13 +01:00
|
|
|
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 {
|
2024-06-04 18:30:11 +01:00
|
|
|
id: u64,
|
|
|
|
config: Config,
|
|
|
|
}
|
|
|
|
impl GithubHook {
|
2024-07-25 09:02:43 +01:00
|
|
|
pub fn id(&self) -> WebhookId {
|
|
|
|
WebhookId::new(format!("{}", self.id))
|
2024-06-04 18:30:11 +01:00
|
|
|
}
|
2024-07-25 09:02:43 +01:00
|
|
|
pub fn url(&self) -> server::WebhookUrl {
|
|
|
|
server::WebhookUrl::new(self.config.url.clone())
|
2024-06-04 18:30:11 +01:00
|
|
|
}
|
2024-05-31 07:37:40 +01:00
|
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct Config {
|
|
|
|
pub url: String,
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|