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;
|
|
|
|
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,
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_message_authorised(
|
|
|
|
&self,
|
2024-06-19 07:03:08 +01:00
|
|
|
msg: &config::ForgeNotification,
|
2024-05-25 11:25:13 +01:00
|
|
|
webhook_auth: &config::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
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_webhook_body(
|
|
|
|
&self,
|
2024-06-19 07:03:08 +01:00
|
|
|
body: &config::webhook::forge_notification::Body,
|
2024-05-25 11:25:13 +01:00
|
|
|
) -> git::forge::webhook::Result<config::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-06-04 18:30:11 +01:00
|
|
|
webhook_url: &config::server::WebhookUrl,
|
2024-05-25 11:25:13 +01:00
|
|
|
) -> git::forge::webhook::Result<Vec<config::WebhookId>> {
|
2024-06-04 18:30:11 +01:00
|
|
|
github::webhook::list(self, webhook_url).await
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn unregister_webhook(
|
|
|
|
&self,
|
|
|
|
webhook_id: &config::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,
|
|
|
|
webhook_url: &config::server::WebhookUrl,
|
|
|
|
) -> git::forge::webhook::Result<config::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 {
|
|
|
|
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())
|
|
|
|
}
|
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
|
|
|
}
|