From ee1c8934b09c5d66191182a8773a82f6d3af4786 Mon Sep 17 00:00:00 2001 From: Evan Graham Date: Tue, 27 Feb 2024 06:54:11 +0000 Subject: [PATCH] Add Public and Release events (#589) --- src/models/events.rs | 18 ++++++- src/models/events/payload.rs | 8 ++++ src/models/events/payload/public.rs | 6 +++ src/models/events/payload/release.rs | 61 +++++++++++++++++++++++ tests/resources/public_event.json | 1 + tests/resources/release_event.json | 72 ++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/models/events/payload/public.rs create mode 100644 src/models/events/payload/release.rs create mode 100644 tests/resources/public_event.json create mode 100644 tests/resources/release_event.json diff --git a/src/models/events.rs b/src/models/events.rs index c052f02c..0268ab49 100644 --- a/src/models/events.rs +++ b/src/models/events.rs @@ -5,8 +5,9 @@ use crate::models::events::payload::EventInstallation; use self::payload::{ CommitCommentEventPayload, CreateEventPayload, DeleteEventPayload, EventPayload, ForkEventPayload, GollumEventPayload, IssueCommentEventPayload, IssuesEventPayload, - PullRequestEventPayload, PullRequestReviewCommentEventPayload, PullRequestReviewEventPayload, - PushEventPayload, WatchEventPayload, WorkflowRunEventPayload, WrappedEventPayload, + PublicEventPayload, PullRequestEventPayload, PullRequestReviewCommentEventPayload, + PullRequestReviewEventPayload, PushEventPayload, ReleaseEventPayload, WatchEventPayload, + WorkflowRunEventPayload, WrappedEventPayload, }; use super::{ActorId, OrgId, RepositoryId}; use chrono::{DateTime, Utc}; @@ -82,9 +83,11 @@ event_type! { (ForkEvent, ForkEventPayload), (GollumEvent, GollumEventPayload), (MemberEvent, MemberEventPayload), + (PublicEvent, PublicEventPayload), (PullRequestEvent, PullRequestEventPayload), (PullRequestReviewEvent, PullRequestReviewEventPayload), (PullRequestReviewCommentEvent, PullRequestReviewCommentEventPayload), + (ReleaseEvent, ReleaseEventPayload), (WatchEvent, WatchEventPayload), (WorkflowRunEvent, WorkflowRunEventPayload) } @@ -225,6 +228,13 @@ mod test { assert_eq!(event.r#type, EventType::PullRequestReviewCommentEvent); } + #[test] + fn should_deserialize_release_event() { + let json = include_str!("../../tests/resources/release_event.json"); + let event: Event = serde_json::from_str(json).unwrap(); + assert_eq!(event.r#type, EventType::ReleaseEvent); + } + #[test] fn should_deserialize_workflow_run_event() { let json = include_str!("../../tests/resources/workflow_run_event.json"); @@ -363,6 +373,10 @@ mod test { "PullRequestReviewCommentEvent", include_str!("../../tests/resources/pull_request_review_comment_event.json"), ), + ( + "ReleaseEvent", + include_str!("../../tests/resources/release_event.json"), + ), ( "CommitCommentEvent", include_str!("../../tests/resources/commit_comment_event.json"), diff --git a/src/models/events/payload.rs b/src/models/events/payload.rs index 8b29c6ee..a5ecbb15 100644 --- a/src/models/events/payload.rs +++ b/src/models/events/payload.rs @@ -9,10 +9,12 @@ mod installation_target; mod issue_comment; mod issues; mod member; +mod public; mod pull_request; mod pull_request_review; mod pull_request_review_comment; mod push; +mod release; mod watch; mod workflow_run; @@ -27,10 +29,12 @@ pub use installation_target::*; pub use issue_comment::*; pub use issues::*; pub use member::*; +pub use public::*; pub use pull_request::*; pub use pull_request_review::*; pub use pull_request_review_comment::*; pub use push::*; +pub use release::*; pub use watch::*; pub use workflow_run::*; @@ -88,9 +92,11 @@ pub enum EventPayload { ForkEvent(Box), GollumEvent(Box), MemberEvent(Box), + PublicEvent(Box), PullRequestEvent(Box), PullRequestReviewEvent(Box), PullRequestReviewCommentEvent(Box), + ReleaseEvent(Box), WatchEvent(Box), WorkflowRunEvent(Box), UnknownEvent(Box), @@ -169,9 +175,11 @@ mod tests { | EventPayload::ForkEvent(_) | EventPayload::GollumEvent(_) | EventPayload::MemberEvent(_) + | EventPayload::PublicEvent(_) | EventPayload::PullRequestEvent(_) | EventPayload::PullRequestReviewEvent(_) | EventPayload::PullRequestReviewCommentEvent(_) + | EventPayload::ReleaseEvent(_) | EventPayload::WorkflowRunEvent(_) | EventPayload::WatchEvent(_) | EventPayload::UnknownEvent(_) => { diff --git a/src/models/events/payload/public.rs b/src/models/events/payload/public.rs new file mode 100644 index 00000000..bb90c307 --- /dev/null +++ b/src/models/events/payload/public.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; + +/// The payload in a [`super::EventPayload::PublicEvent`] type. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] +pub struct PublicEventPayload {} diff --git a/src/models/events/payload/release.rs b/src/models/events/payload/release.rs new file mode 100644 index 00000000..8e866c5f --- /dev/null +++ b/src/models/events/payload/release.rs @@ -0,0 +1,61 @@ +use crate::models::repos::Release; +use serde::{Deserialize, Serialize}; + +/// The payload in a [`super::EventPayload::ReleaseEvent`] type. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] +pub struct ReleaseEventPayload { + /// The action this event represents. + pub action: ReleaseEventAction, + /// The release this event corresponds to. + pub release: Release, + /// The changes to body or name if this event is of type [`ReleaseEventAction::Edited`]. + pub changes: Option, +} + +/// The change which occurred in an event of type [`ReleaseEventAction::Edited`]. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] +pub enum ReleaseEventChanges { + Body(ReleaseEventChangesFrom), + Name(ReleaseEventChangesFrom), +} + +/// The previous value of the item (either the body or title) of a release which has changed. Only +/// available in an event of type [`ReleaseEventAction::Edited`]. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] +pub struct ReleaseEventChangesFrom { + pub from: String, +} + +/// The action on a release this event corresponds to. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] +pub enum ReleaseEventAction { + Published, + Edited, +} + +#[cfg(test)] +mod test { + use crate::models::events::{ + payload::{EventPayload, ReleaseEventAction}, + Event, + }; + + #[test] + fn should_deserialize_with_correct_payload() { + let json = include_str!("../../../../tests/resources/release_event.json"); + let event: Event = serde_json::from_str(json).unwrap(); + if let Some(EventPayload::ReleaseEvent(ref payload)) = + event.payload.as_ref().unwrap().specific + { + assert_eq!(payload.action, ReleaseEventAction::Published); + } else { + panic!("unexpected event payload encountered: {:#?}", event.payload); + } + } +} diff --git a/tests/resources/public_event.json b/tests/resources/public_event.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/tests/resources/public_event.json @@ -0,0 +1 @@ +{} diff --git a/tests/resources/release_event.json b/tests/resources/release_event.json new file mode 100644 index 00000000..50fba537 --- /dev/null +++ b/tests/resources/release_event.json @@ -0,0 +1,72 @@ +{ + "id": "36029458403", + "type": "ReleaseEvent", + "actor": { + "id": 41898282, + "login": "github-actions[bot]", + "display_login": "github-actions", + "gravatar_id": "", + "url": "https://api.github.com/users/github-actions[bot]", + "avatar_url": "https://avatars.githubusercontent.com/u/41898282?" + }, + "repo": { + "id": 5390641, + "name": "open-watcom/open-watcom-v2", + "url": "https://api.github.com/repos/open-watcom/open-watcom-v2" + }, + "payload": { + "action": "published", + "release": { + "url": "https://api.github.com/repos/open-watcom/open-watcom-v2/releases/143718142", + "assets_url": "https://api.github.com/repos/open-watcom/open-watcom-v2/releases/143718142/assets", + "upload_url": "https://uploads.github.com/repos/open-watcom/open-watcom-v2/releases/143718142/assets{?name,label}", + "html_url": "https://github.com/open-watcom/open-watcom-v2/releases/tag/Last-CI-build", + "id": 143718142, + "author": { + "login": "github-actions[bot]", + "id": 41898282, + "node_id": "MDM6Qm90NDE4OTgyODI=", + "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/github-actions%5Bbot%5D", + "html_url": "https://github.com/apps/github-actions", + "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers", + "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}", + "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}", + "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions", + "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs", + "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos", + "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}", + "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events", + "type": "Bot", + "site_admin": false + }, + "node_id": "RE_kwDOAFJBMc4IkPb-", + "tag_name": "Last-CI-build", + "target_commitish": "master", + "name": "Last-CI-build", + "draft": false, + "prerelease": true, + "created_at": "2024-02-26T23:17:41Z", + "published_at": "2024-02-26T23:17:44Z", + "assets": [ + + ], + "tarball_url": "https://api.github.com/repos/open-watcom/open-watcom-v2/tarball/Last-CI-build", + "zipball_url": "https://api.github.com/repos/open-watcom/open-watcom-v2/zipball/Last-CI-build", + "body": "Last updated 2024-02-26 23:17:34 UTC", + "short_description_html": "

Last updated 2024-02-26 23:17:34 UTC

", + "is_short_description_html_truncated": false + } + }, + "public": true, + "created_at": "2024-02-26T23:17:44Z", + "org": { + "id": 2045606, + "login": "open-watcom", + "gravatar_id": "", + "url": "https://api.github.com/orgs/open-watcom", + "avatar_url": "https://avatars.githubusercontent.com/u/2045606?" + } + }