2024-05-25 11:25:13 +01:00
|
|
|
//
|
2024-07-26 06:49:09 +01:00
|
|
|
use git_next_core::{git, webhook, BranchName, WebhookId};
|
2024-07-25 09:02:43 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
mod list;
|
|
|
|
mod parse;
|
|
|
|
mod register;
|
|
|
|
mod unregister;
|
|
|
|
|
|
|
|
pub use list::list;
|
|
|
|
pub use parse::parse_body;
|
|
|
|
pub use register::register;
|
|
|
|
pub use unregister::unregister;
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct Hook {
|
|
|
|
id: i64,
|
|
|
|
config: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
impl Hook {
|
2024-07-25 09:02:43 +01:00
|
|
|
fn id(&self) -> WebhookId {
|
|
|
|
WebhookId::new(format!("{}", self.id))
|
2024-05-25 11:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
pub struct Push {
|
|
|
|
#[serde(rename = "ref")]
|
|
|
|
reference: String,
|
|
|
|
after: String,
|
|
|
|
head_commit: HeadCommit,
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
impl TryFrom<Push> for webhook::Push {
|
2024-05-25 11:25:13 +01:00
|
|
|
type Error = git::forge::webhook::Error;
|
|
|
|
fn try_from(push: Push) -> Result<Self, Self::Error> {
|
|
|
|
let branch = push
|
|
|
|
.reference
|
|
|
|
.splitn(3, '/') // should be of the form 'refs/heads/branchname'
|
|
|
|
.nth(2)
|
2024-07-25 09:02:43 +01:00
|
|
|
.map(BranchName::new)
|
2024-05-25 11:25:13 +01:00
|
|
|
.ok_or(git::forge::webhook::Error::UnknownBranch(push.reference))?;
|
|
|
|
Ok(Self::new(branch, push.after, push.head_commit.message))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct HeadCommit {
|
|
|
|
message: String,
|
|
|
|
}
|