From f7d2a2e57d01a27a8d8674a6b50504fccccda4bf Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 8 May 2022 18:28:40 -0400 Subject: [PATCH] Filtering for pull request events. --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b68d9f..ee158a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ extern crate log; mod githubctl; -use serde_json; +use serde_json::{self, Map}; const USERNAME: &'static str = include_str!("../.username"); const TOKEN: &'static str = include_str!("../.pat"); @@ -17,6 +17,41 @@ async fn main() -> Result<(), Box> { github.watch_repo(ORG, REPO).await?; loop { let event = github.get_event().await?; - println!("{}", serde_json::to_string(&event)?); + // println!("{}", serde_json::to_string(&event)?); + handle_event(event)?; + } +} + +fn handle_event(event: serde_json::Value) -> Result<(), Box> { + let event_object = get_json_object(&event); + let event_type = event_object.get("type").map(get_json_string); + match event_type { + Some(event_type_string) if event_type_string == "PullRequestEvent" => { + println!("{}", serde_json::to_string(&event)?); + } + _ => {} + }; + Ok(()) +} + +fn get_json_object(obj: &serde_json::Value) -> &Map { + match obj { + serde_json::Value::Null => panic!("Unexpected json type"), + serde_json::Value::Bool(_) => panic!("Unexpected json type"), + serde_json::Value::Number(_) => panic!("Unexpected json type"), + serde_json::Value::String(_) => panic!("Unexpected json type"), + serde_json::Value::Array(_) => panic!("Unexpected json type"), + serde_json::Value::Object(event_object) => event_object, + } +} + +fn get_json_string(str: &serde_json::Value) -> &String { + match str { + serde_json::Value::Null => panic!("Unexpected json type"), + serde_json::Value::Bool(_) => panic!("Unexpected json type"), + serde_json::Value::Number(_) => panic!("Unexpected json type"), + serde_json::Value::String(txt) => txt, + serde_json::Value::Array(_) => panic!("Unexpected json type"), + serde_json::Value::Object(_) => panic!("Unexpected json type"), } }