Paul Campbell
538728c491
All checks were successful
Rust / build (push) Successful in 1m30s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
Groups 'http' and 'webhook' sections under 'listen'. Renames 'notification' section as 'shout'.
114 lines
3 KiB
Rust
114 lines
3 KiB
Rust
//
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod commit;
|
|
mod webhook;
|
|
|
|
use crate as github;
|
|
use git_next_core::{
|
|
self as core,
|
|
git::{self, forge::commit::Status},
|
|
server::{self, RepoListenUrl},
|
|
ForgeNotification, RegisteredWebhook, WebhookAuth, WebhookId,
|
|
};
|
|
|
|
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: &ForgeNotification, webhook_auth: &WebhookAuth) -> bool {
|
|
github::webhook::is_authorised(msg, webhook_auth)
|
|
}
|
|
|
|
fn should_ignore_message(&self, message: &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: &core::webhook::forge_notification::Body,
|
|
) -> git::forge::webhook::Result<core::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,
|
|
repo_listen_url: &RepoListenUrl,
|
|
) -> git::forge::webhook::Result<Vec<WebhookId>> {
|
|
github::webhook::list(self, repo_listen_url).await
|
|
}
|
|
|
|
async fn unregister_webhook(&self, webhook_id: &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,
|
|
repo_listen_url: &RepoListenUrl,
|
|
) -> git::forge::webhook::Result<RegisteredWebhook> {
|
|
github::webhook::register(self, repo_listen_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) -> WebhookId {
|
|
WebhookId::new(format!("{}", self.id))
|
|
}
|
|
pub fn url(&self) -> server::ListenUrl {
|
|
server::ListenUrl::new(self.config.url.clone())
|
|
}
|
|
}
|
|
#[derive(Debug, serde::Deserialize)]
|
|
struct Config {
|
|
pub url: String,
|
|
}
|