Attempt at Forgejo Webhook support
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
2ef603151e
commit
8e94435604
7 changed files with 124 additions and 27 deletions
|
@ -8,6 +8,7 @@ license = "AGPL-3.0+"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5", features = [ "cargo" ] }
|
clap = { version = "4.5", features = [ "cargo" ] }
|
||||||
|
forgejo-hooks = "*"
|
||||||
gitlab = "0.1610"
|
gitlab = "0.1610"
|
||||||
hyper = { version = "1.4", default-features = false, features = [ "http1", "server" ] }
|
hyper = { version = "1.4", default-features = false, features = [ "http1", "server" ] }
|
||||||
hyper-util = { version = "0.1", features = [ "tokio" ] }
|
hyper-util = { version = "0.1", features = [ "tokio" ] }
|
||||||
|
@ -21,3 +22,6 @@ serde = { version = "1.0", features = [ "derive" ] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
xmpp = "0.5"
|
xmpp = "0.5"
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
forgejo-hooks = { path = "forgejo-hooks" }
|
||||||
|
|
7
forgejo-hooks/Cargo.toml
Normal file
7
forgejo-hooks/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "forgejo-hooks"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
63
forgejo-hooks/src/lib.rs
Normal file
63
forgejo-hooks/src/lib.rs
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct CommitAuthor {
|
||||||
|
pub name: String,
|
||||||
|
pub email: String,
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: u32,
|
||||||
|
pub login: String,
|
||||||
|
pub full_name: String,
|
||||||
|
pub email: String,
|
||||||
|
pub avatar_url: String,
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct Commit {
|
||||||
|
pub id: String,
|
||||||
|
pub message: String,
|
||||||
|
pub url: String,
|
||||||
|
pub author: CommitAuthor,
|
||||||
|
pub committer: CommitAuthor,
|
||||||
|
pub timestamp: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct Repository {
|
||||||
|
pub id: u32,
|
||||||
|
pub owner: User,
|
||||||
|
pub name: String,
|
||||||
|
pub full_name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub private: bool,
|
||||||
|
pub fork: bool,
|
||||||
|
pub html_url: String,
|
||||||
|
pub ssh_url: String,
|
||||||
|
pub clone_url: String,
|
||||||
|
pub website: String,
|
||||||
|
pub stars_count: u32,
|
||||||
|
pub forks_count: u32,
|
||||||
|
pub watchers_count: u32,
|
||||||
|
pub open_issues_count: u32,
|
||||||
|
pub default_branch: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct Hook {
|
||||||
|
#[serde(rename(deserialize = "ref"))]
|
||||||
|
pub ref_: String,
|
||||||
|
pub before: String,
|
||||||
|
pub compare_url: String,
|
||||||
|
pub commits: Vec<Commit>,
|
||||||
|
pub repository: Repository,
|
||||||
|
pub pusher: User,
|
||||||
|
pub sender: User,
|
||||||
|
}
|
|
@ -13,7 +13,7 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::webhook::{format_webhook, WebHook};
|
use crate::webhook::{format_webhook, GitlabHook};
|
||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use xmpp::parsers::message::MessageType;
|
use xmpp::parsers::message::MessageType;
|
||||||
|
@ -74,7 +74,7 @@ impl XmppClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn webhook(&mut self, wh: WebHook) {
|
pub async fn webhook(&mut self, wh: GitlabHook) {
|
||||||
debug!("Received Webhook");
|
debug!("Received Webhook");
|
||||||
if let Some(display) = format_webhook(&wh) {
|
if let Some(display) = format_webhook(&wh) {
|
||||||
debug!("Webhook: {}", display);
|
debug!("Webhook: {}", display);
|
||||||
|
|
|
@ -24,7 +24,7 @@ mod webhook;
|
||||||
use crate::bot::XmppClient;
|
use crate::bot::XmppClient;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::web::webhooks;
|
use crate::web::webhooks;
|
||||||
use crate::webhook::WebHook;
|
use crate::webhook::Hook;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read};
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read};
|
||||||
|
@ -130,7 +130,7 @@ async fn main() -> Result<!, Error> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
|
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<Hook>();
|
||||||
|
|
||||||
let mut client = XmppClient::new(
|
let mut client = XmppClient::new(
|
||||||
config.jid,
|
config.jid,
|
||||||
|
@ -169,8 +169,8 @@ async fn main() -> Result<!, Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wh = value_rx.recv() => {
|
wh = value_rx.recv() => {
|
||||||
if let Some(wh) = wh {
|
if let Some(Hook::Gitlab(hook)) = wh {
|
||||||
client.webhook(wh).await
|
client.webhook(hook).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
28
src/web.rs
28
src/web.rs
|
@ -14,7 +14,7 @@
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::webhook::WebHook;
|
use crate::webhook::{ForgejoHook, GitlabHook, Hook};
|
||||||
|
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
@ -36,14 +36,13 @@ fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Full<Bytes>>, Infallib
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn webhooks_inner(req: Request<Incoming>, token: &str) -> Result<WebHook, Error> {
|
async fn webhooks_inner(req: Request<Incoming>, token: &str) -> Result<Hook, Error> {
|
||||||
match req.method() {
|
match req.method() {
|
||||||
&Method::POST => (),
|
&Method::POST => (),
|
||||||
_ => return Err(Error::MethodMismatch),
|
_ => return Err(Error::MethodMismatch),
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Headers: {:?}", req.headers());
|
debug!("Headers: {:?}", req.headers());
|
||||||
|
|
||||||
let headers = req.headers();
|
let headers = req.headers();
|
||||||
if let Some(content_type) = headers.get(header::CONTENT_TYPE)
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE)
|
||||||
&& content_type != "application/json"
|
&& content_type != "application/json"
|
||||||
|
@ -51,27 +50,36 @@ async fn webhooks_inner(req: Request<Incoming>, token: &str) -> Result<WebHook,
|
||||||
return Err(Error::InvalidContentType);
|
return Err(Error::InvalidContentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(token) = token {
|
if let Some(val) = headers.get("X-Gitlab-Token")
|
||||||
match headers.get("X-Gitlab-Token") {
|
&& token != val
|
||||||
Some(val) if val == token => (),
|
{
|
||||||
_ => return Err(Error::InvalidToken),
|
return Err(Error::InvalidToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE)
|
||||||
|
&& content_type != "application/json"
|
||||||
|
{
|
||||||
|
return Err(Error::InvalidContentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
let whole_body = req.collect().await?.aggregate();
|
let whole_body = req.collect().await?.aggregate();
|
||||||
Ok(serde_json::from_reader(whole_body.reader())?)
|
let hook = serde_json::from_reader(whole_body.reader())?;
|
||||||
|
Ok(Hook::Gitlab(hook))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn webhooks(
|
pub async fn webhooks(
|
||||||
req: Request<Incoming>,
|
req: Request<Incoming>,
|
||||||
token: &str,
|
token: &str,
|
||||||
value_tx: Arc<Mutex<UnboundedSender<WebHook>>>,
|
value_tx: Arc<Mutex<UnboundedSender<Hook>>>,
|
||||||
) -> Result<Response<Full<Bytes>>, Infallible> {
|
) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
match webhooks_inner(req, token).await {
|
match webhooks_inner(req, token).await {
|
||||||
Ok(wh) => {
|
Ok(wh) => {
|
||||||
debug!("Passed: {:?}", wh);
|
debug!("Passed: {:?}", wh);
|
||||||
|
|
||||||
value_tx.lock().unwrap().send(wh).unwrap();
|
match wh {
|
||||||
|
hook @ Hook::Gitlab(_) => value_tx.lock().unwrap().send(hook).unwrap(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
|
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,27 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
pub use gitlab::webhooks::{IssueAction, MergeRequestAction, WebHook, WikiPageAction};
|
pub use forgejo_hooks::Hook as ForgejoHook;
|
||||||
|
pub use gitlab::webhooks::{
|
||||||
|
IssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction,
|
||||||
|
};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
#[derive(Debug)]
|
||||||
Some(match wh {
|
pub enum Hook {
|
||||||
WebHook::Push(push) => {
|
Forgejo(ForgejoHook),
|
||||||
|
Gitlab(GitlabHook),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<GitlabHook> for Hook {
|
||||||
|
fn from(hook: GitlabHook) -> Hook {
|
||||||
|
Hook::Gitlab(hook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn format_webhook(glh: &GitlabHook) -> Option<String> {
|
||||||
|
Some(match glh {
|
||||||
|
GitlabHook::Push(push) => {
|
||||||
if push.ref_ != "refs/heads/main" {
|
if push.ref_ != "refs/heads/main" {
|
||||||
// Ignore: Action not on 'main' branch
|
// Ignore: Action not on 'main' branch
|
||||||
return None;
|
return None;
|
||||||
|
@ -45,7 +60,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
||||||
}
|
}
|
||||||
text
|
text
|
||||||
}
|
}
|
||||||
WebHook::Issue(issue) => {
|
GitlabHook::Issue(issue) => {
|
||||||
let action = match issue.object_attributes.action {
|
let action = match issue.object_attributes.action {
|
||||||
Some(IssueAction::Update) => return None,
|
Some(IssueAction::Update) => return None,
|
||||||
Some(IssueAction::Open) => "opened",
|
Some(IssueAction::Open) => "opened",
|
||||||
|
@ -68,7 +83,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
||||||
.unwrap_or("".to_owned())
|
.unwrap_or("".to_owned())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
WebHook::MergeRequest(merge_req) => {
|
GitlabHook::MergeRequest(merge_req) => {
|
||||||
let action = match merge_req.object_attributes.action {
|
let action = match merge_req.object_attributes.action {
|
||||||
Some(MergeRequestAction::Update) => return None,
|
Some(MergeRequestAction::Update) => return None,
|
||||||
Some(MergeRequestAction::Open) => "opened",
|
Some(MergeRequestAction::Open) => "opened",
|
||||||
|
@ -93,7 +108,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
||||||
.unwrap_or("".to_owned())
|
.unwrap_or("".to_owned())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
WebHook::Note(note) => {
|
GitlabHook::Note(note) => {
|
||||||
if let Some(_) = note.snippet {
|
if let Some(_) = note.snippet {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -120,11 +135,11 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WebHook::Build(build) => {
|
GitlabHook::Build(build) => {
|
||||||
println!("Build: {:?}", build);
|
println!("Build: {:?}", build);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
WebHook::WikiPage(page) => {
|
GitlabHook::WikiPage(page) => {
|
||||||
let action = match page.object_attributes.action {
|
let action = match page.object_attributes.action {
|
||||||
WikiPageAction::Update => "updated",
|
WikiPageAction::Update => "updated",
|
||||||
WikiPageAction::Create => "created",
|
WikiPageAction::Create => "created",
|
||||||
|
@ -138,7 +153,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
|
||||||
page.object_attributes.url,
|
page.object_attributes.url,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_wh => {
|
_glh => {
|
||||||
debug!("Webhook not supported");
|
debug!("Webhook not supported");
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue