diff --git a/src/githubctl/githubctl.rs b/src/githubctl/githubctl.rs index 8cd62c5..18e6d2d 100644 --- a/src/githubctl/githubctl.rs +++ b/src/githubctl/githubctl.rs @@ -1,3 +1,5 @@ +use crate::json_util::get_json_string; +use std::collections::HashSet; use tokio::sync::mpsc; use super::github_endpoint_watcher::GithubEndpointWatcher; @@ -75,9 +77,10 @@ impl<'a> GithubCtl<'a> { let event_stream = self.event_writer.clone(); tokio::spawn(async move { + let mut already_seen_event_ids = HashSet::new(); let mut endpoint_watcher = GithubEndpointWatcher::new(username, token, url) .expect("Failed to create endpoint watcher."); - endpoint_watcher.skip_results_before_now().await.expect("Failed to fetch initial historical results."); + let mut should_notify: bool = false; // Skip the first page of results because theses events already occurred in the past loop { let api_result = match endpoint_watcher.get_results().await { Ok(result) => result, @@ -88,9 +91,17 @@ impl<'a> GithubCtl<'a> { }; if let Some(serde_json::Value::Array(events)) = api_result { for event in events { - if let Err(_) = event_stream.send(event).await { - error!("Receiver dropped."); - return; + let event_id = event + .get("id") + .map(get_json_string) + .expect("Ran into an event without a id.") + .to_owned(); + + if should_notify && already_seen_event_ids.insert(event_id) { + if let Err(_) = event_stream.send(event).await { + error!("Receiver dropped."); + return; + } } } } else if let None = api_result { @@ -98,6 +109,7 @@ impl<'a> GithubCtl<'a> { } else { error!("Unsupported JSON type."); } + should_notify = true; } }); } diff --git a/src/main.rs b/src/main.rs index 535e7bd..e274b27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,11 +25,12 @@ async fn main() -> Result<(), Box> { } } -fn handle_event(event: serde_json::Value) -> Result<(), Box> { - if PullRequestEvent::is_a(&event)? { - let event = PullRequestEvent::new(&event)?; +fn handle_event(json_event: serde_json::Value) -> Result<(), Box> { + if PullRequestEvent::is_a(&json_event)? { + let event = PullRequestEvent::new(&json_event)?; if event.action == "opened" { - println!("PullRequestEvent action {}", event.action); + // println!("PullRequestEvent action {}", event.action); + println!("{}", serde_json::to_string(&json_event)?); let notification_body = format!("Pull Request opened: {}", event.title); Notification::new() .summary("Pull request opened.") @@ -39,7 +40,14 @@ fn handle_event(event: serde_json::Value) -> Result<(), Box info!("you clicked \"default\""), + "clicked" => info!("that was correct"), + // here "__closed" is a hard coded keyword + "__closed" => info!("the notification was closed"), + _ => info!("Unrecognized action {}", action), + }); } } Ok(())