Filtering to 'opened' pull request events.

This commit is contained in:
Tom Alexander 2022-05-12 08:53:20 -04:00
parent be9dcee422
commit f46a5f8a9c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 21 additions and 6 deletions

View File

@ -1,12 +1,26 @@
use crate::json_util::get_json_object;
use crate::json_util::get_json_string;
pub struct PullRequestEvent<'a> {
original_event: &'a serde_json::Value,
pub action: &'a String,
}
impl<'a> PullRequestEvent<'a> {
pub fn new(original_event: &'a serde_json::Value) -> Result<Self, Box<dyn std::error::Error>> {
Ok(PullRequestEvent { original_event })
let payload = original_event
.get("payload")
.map(get_json_object)
.expect("Ran into a PullRequestEvent without a payload.");
let action = payload
.get("action")
.map(get_json_string)
.expect("Ran into a PullRequestEvent without a payload.action.");
Ok(PullRequestEvent {
original_event,
action,
})
}
pub fn is_a(event: &'a serde_json::Value) -> Result<bool, Box<dyn std::error::Error>> {

View File

@ -25,10 +25,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
fn handle_event(event: serde_json::Value) -> Result<(), Box<dyn std::error::Error>> {
let event = if PullRequestEvent::is_a(&event)? {
Some(PullRequestEvent::new(&event)?)
} else {
None
};
if PullRequestEvent::is_a(&event)? {
let event = PullRequestEvent::new(&event)?;
if event.action == "opened" {
println!("PullRequestEvent action {}", event.action);
}
}
Ok(())
}