parse gitea pr webhook
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user