parse gitea pr webhook

This commit is contained in:
2026-05-28 21:35:41 +00:00
parent e33187dc80
commit c119bed142
7 changed files with 321 additions and 8 deletions
+44 -7
View File
@@ -1,10 +1,15 @@
use axum::Router;
use axum::routing::get;
use axum::response::{Response, IntoResponse};
use axum::{Json, Router};
use axum::extract::FromRequest;
use axum::routing::{post, get};
use reqwest::StatusCode;
use serde_json::Value;
use crate::env;
use crate::env::{EnvConfig};
use crate::gitea::WebhookType;
pub async fn start_api(config: env::EnvConfig) -> anyhow::Result<()> {
let app = Router::new().route("/webhook", get(webhook));
pub async fn start_api(config: EnvConfig) -> anyhow::Result<()> {
let app = Router::new().route("/", get(root)).route("/webhook", post(webhook));
let listerner = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.http_port)).await?;
axum::serve(listerner, app)
@@ -12,6 +17,38 @@ pub async fn start_api(config: env::EnvConfig) -> anyhow::Result<()> {
.map_err(|e| anyhow::anyhow!(e))
}
async fn webhook() -> &'static str {
"Hello, World!"
async fn root() -> &'static str {
"Hi, i'm Herald :)"
}
async fn webhook(WebhookExtract(wb): WebhookExtract) -> Response {
match wb {
WebhookType::Review(id, _) => format!("Received {} pr id", id),
_ => String::from("Nothing to see :/")
}.into_response()
}
pub struct WebhookExtract(pub WebhookType);
impl<S> FromRequest<S> for WebhookExtract
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request(
req: axum::extract::Request,
state: &S,
) -> Result<Self, Self::Rejection> {
let Json(value) = Json::<Value>::from_request(req, state)
.await
.map_err(|e| e.into_response())?;
let webhook = WebhookType::try_from(value)
.map_err(|e| {
(StatusCode::UNPROCESSABLE_ENTITY, e.to_string()).into_response()
})?;
Ok(WebhookExtract(webhook))
}
}
+5
View File
@@ -0,0 +1,5 @@
use crate::gitea::WebhookType;
pub async fn exec(webhook: WebhookType) {
}
+35
View File
@@ -0,0 +1,35 @@
use anyhow::anyhow;
use serde_json::Value;
pub enum WebhookType {
Review(u64, String)
}
impl TryFrom<Value> for WebhookType {
type Error = anyhow::Error;
fn try_from(json: Value) -> Result<Self, Self::Error> {
let pull_request = json.get("pull_request");
let comment = json.get("comment");
if let (Some(pull_request), Some(comment)) = (pull_request, comment) {
let comment_body = comment
.get("body")
.ok_or(anyhow!("comment body not found"))?
.as_str()
.ok_or(anyhow!("error while get pr comment"))?
.to_string();
let pr_id = pull_request
.get("id")
.ok_or(anyhow!("pr id not found"))?
.as_u64()
.ok_or(anyhow!("error while get pr id"))?;
return Ok(WebhookType::Review(pr_id, comment_body));
}
anyhow::bail!("unknow webhook type")
}
}
+2
View File
@@ -1,5 +1,7 @@
mod api;
mod env;
mod gitea;
mod bot;
#[tokio::main]
async fn main() -> anyhow::Result<()> {