add gitea sig header check, add Json errors, begin AppState
This commit is contained in:
+14
-4
@@ -1,17 +1,27 @@
|
||||
use anyhow::anyhow;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::errors::AppError;
|
||||
|
||||
pub enum WebhookType {
|
||||
Review(u64, String)
|
||||
Review(u64, String),
|
||||
}
|
||||
|
||||
impl TryFrom<Value> for WebhookType {
|
||||
type Error = anyhow::Error;
|
||||
type Error = AppError;
|
||||
|
||||
fn try_from(json: Value) -> Result<Self, Self::Error> {
|
||||
let pull_request = json.get("pull_request");
|
||||
let comment = json.get("comment");
|
||||
let action = json
|
||||
.get("action")
|
||||
.ok_or(anyhow!("action not found"))?
|
||||
.as_str()
|
||||
.ok_or(anyhow!("error while action"))?;
|
||||
|
||||
if action != "created" {
|
||||
return Err(AppError::BadJsonStructErr);
|
||||
}
|
||||
|
||||
if let (Some(pull_request), Some(comment)) = (pull_request, comment) {
|
||||
let comment_body = comment
|
||||
@@ -30,6 +40,6 @@ impl TryFrom<Value> for WebhookType {
|
||||
return Ok(WebhookType::Review(pr_id, comment_body));
|
||||
}
|
||||
|
||||
anyhow::bail!("unknow webhook type")
|
||||
Err(AppError::BadJsonStructErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user