From 9afa1cde0c6821b0a0759fb090aef1c193ab5c74 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 5 Aug 2023 14:57:55 +1000 Subject: [PATCH] add "protection" against non-pr events --- src/main.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index fcf6350..c93479c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,6 @@ #![forbid(unsafe_code)] #![feature(new_uninit)] -use std::mem::MaybeUninit; - -use gitea::PullWh; use log; use teloxide_core::{prelude::*, types::{Recipient, Chat}}; use teloxide::Bot; @@ -55,6 +52,26 @@ async fn webhook(mut req: Request) -> tide::Result { let body = req.body_string().await.unwrap(); let pr = serde_json::from_str::(body.as_str()); + let event_type = req.header("X-Gitea-Event-Type"); + if event_type.is_none() { + return Ok( + Response::builder(400) + .body("{\"error\":\"no event type (X-Gitea-Event-Type)\"}") + .content_type("application/json") + .build() + ) + } + + let event_type = event_type.unwrap().get(0).unwrap().to_string(); + if event_type != "pull_request" { + return Ok( + Response::builder(200) + .body("{\"status\":\"ignoring non-pr event\"}") + .content_type("application/json") + .build() + ) + } + if pr.is_err() { return Ok(format!("Bad serialization: {}", pr.unwrap_err().to_string()).into()); }