add "protection" against non-pr events

This commit is contained in:
b1ek 2023-08-05 14:57:55 +10:00
parent 613442ff62
commit 9afa1cde0c
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 20 additions and 3 deletions

View File

@ -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<SharedState>) -> tide::Result {
let body = req.body_string().await.unwrap();
let pr = serde_json::from_str::<gitea::PullWh>(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());
}