Skip to content

Commit

Permalink
Add WatchEvent (#462)
Browse files Browse the repository at this point in the history
Co-authored-by: Björn Barwinski <[email protected]>
  • Loading branch information
BjoernLange and Björn Barwinski authored Oct 2, 2023
1 parent e4251f2 commit 77e633c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/models/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -85,6 +85,7 @@ event_type! {
(PullRequestEvent, PullRequestEventPayload),
(PullRequestReviewEvent, PullRequestReviewEventPayload),
(PullRequestReviewCommentEvent, PullRequestReviewCommentEventPayload),
(WatchEvent, WatchEventPayload),
(WorkflowRunEvent, WorkflowRunEventPayload)
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions src/models/events/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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};
Expand Down Expand Up @@ -89,6 +91,7 @@ pub enum EventPayload {
PullRequestEvent(Box<PullRequestEventPayload>),
PullRequestReviewEvent(Box<PullRequestReviewEventPayload>),
PullRequestReviewCommentEvent(Box<PullRequestReviewCommentEventPayload>),
WatchEvent(Box<WatchEventPayload>),
WorkflowRunEvent(Box<WorkflowRunEventPayload>),
UnknownEvent(Box<serde_json::Value>),
}
Expand Down Expand Up @@ -170,6 +173,7 @@ mod tests {
| EventPayload::PullRequestReviewEvent(_)
| EventPayload::PullRequestReviewCommentEvent(_)
| EventPayload::WorkflowRunEvent(_)
| EventPayload::WatchEvent(_)
| EventPayload::UnknownEvent(_) => {
panic!("Expected an installation event, got {:?}", specific)
}
Expand Down
38 changes: 38 additions & 0 deletions src/models/events/payload/watch.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
22 changes: 22 additions & 0 deletions tests/resources/watch_event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 77e633c

Please sign in to comment.