diff --git a/src/models/events.rs b/src/models/events.rs index 0b623a95..c052f02c 100644 --- a/src/models/events.rs +++ b/src/models/events.rs @@ -6,7 +6,7 @@ use self::payload::{ CommitCommentEventPayload, CreateEventPayload, DeleteEventPayload, EventPayload, ForkEventPayload, GollumEventPayload, IssueCommentEventPayload, IssuesEventPayload, PullRequestEventPayload, PullRequestReviewCommentEventPayload, PullRequestReviewEventPayload, - PushEventPayload, WorkflowRunEventPayload, WrappedEventPayload, + PushEventPayload, WatchEventPayload, WorkflowRunEventPayload, WrappedEventPayload, }; use super::{ActorId, OrgId, RepositoryId}; use chrono::{DateTime, Utc}; @@ -85,6 +85,7 @@ event_type! { (PullRequestEvent, PullRequestEventPayload), (PullRequestReviewEvent, PullRequestReviewEventPayload), (PullRequestReviewCommentEvent, PullRequestReviewCommentEventPayload), + (WatchEvent, WatchEventPayload), (WorkflowRunEvent, WorkflowRunEventPayload) } @@ -275,6 +276,13 @@ mod test { assert_eq!(event.r#type, EventType::MemberEvent); } + #[test] + fn should_deserialize_watch_event() { + let json = include_str!("../../tests/resources/watch_event.json"); + let event: Event = serde_json::from_str(json).unwrap(); + assert_eq!(event.r#type, EventType::WatchEvent); + } + #[test] fn should_deserialize_with_org_when_present() { let json = include_str!("../../tests/resources/create_event.json"); @@ -359,6 +367,10 @@ mod test { "CommitCommentEvent", include_str!("../../tests/resources/commit_comment_event.json"), ), + ( + "WatchEvent", + include_str!("../../tests/resources/watch_event.json"), + ), ( "WorkflowRunEvent", include_str!("../../tests/resources/workflow_run_event.json"), diff --git a/src/models/events/payload.rs b/src/models/events/payload.rs index 58f2d8a2..8b29c6ee 100644 --- a/src/models/events/payload.rs +++ b/src/models/events/payload.rs @@ -13,6 +13,7 @@ mod pull_request; mod pull_request_review; mod pull_request_review_comment; mod push; +mod watch; mod workflow_run; pub use commit_comment::*; @@ -30,6 +31,7 @@ pub use pull_request::*; pub use pull_request_review::*; pub use pull_request_review_comment::*; pub use push::*; +pub use watch::*; pub use workflow_run::*; use serde::{Deserialize, Serialize}; @@ -89,6 +91,7 @@ pub enum EventPayload { PullRequestEvent(Box), PullRequestReviewEvent(Box), PullRequestReviewCommentEvent(Box), + WatchEvent(Box), WorkflowRunEvent(Box), UnknownEvent(Box), } @@ -170,6 +173,7 @@ mod tests { | EventPayload::PullRequestReviewEvent(_) | EventPayload::PullRequestReviewCommentEvent(_) | EventPayload::WorkflowRunEvent(_) + | EventPayload::WatchEvent(_) | EventPayload::UnknownEvent(_) => { panic!("Expected an installation event, got {:?}", specific) } diff --git a/src/models/events/payload/watch.rs b/src/models/events/payload/watch.rs new file mode 100644 index 00000000..d5a48623 --- /dev/null +++ b/src/models/events/payload/watch.rs @@ -0,0 +1,38 @@ +use serde::{Deserialize, Serialize}; + +/// The payload in a [`super::EventPayload::WatchEvent`] type. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] +pub struct WatchEventPayload { + /// The action that was performed. + pub action: WatchEventAction, +} + +/// The action that was performed as part of the [`super::EventPayload::WatchEvent`]. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] +pub enum WatchEventAction { + Started, +} + +#[cfg(test)] +mod test { + use crate::models::events::{ + payload::{EventPayload, WatchEventAction}, + Event, + }; + + #[test] + fn should_deserialize_with_correct_payload() { + let json = include_str!("../../../../tests/resources/watch_event.json"); + let event: Event = serde_json::from_str(json).unwrap(); + if let Some(EventPayload::WatchEvent(ref payload)) = + event.payload.as_ref().unwrap().specific + { + assert_eq!(payload.action, WatchEventAction::Started); + } else { + panic!("unexpected event payload encountered: {:#?}", event.payload); + } + } +} diff --git a/tests/resources/watch_event.json b/tests/resources/watch_event.json new file mode 100644 index 00000000..9037c254 --- /dev/null +++ b/tests/resources/watch_event.json @@ -0,0 +1,22 @@ +{ + "id": "31123637655", + "type": "WatchEvent", + "actor": { + "id": 94867353, + "login": "octocat", + "display_login": "octocat", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "avatar_url": "https://avatars.githubusercontent.com/u/94867353?v=4" + }, + "repo": { + "id": 316335970, + "name": "wayofthepie/test-events", + "url": "https://api.github.com/repos/wayofthepie/test-events" + }, + "public": true, + "created_at": "2023-10-01T07:54:09Z", + "payload": { + "action": "started" + } +} \ No newline at end of file