Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support member/gollum/fork events #72

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/models/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ pub mod payload;

use self::payload::{
CommitCommentEventPayload, CreateEventPayload, DeleteEventPayload, EventPayload,
IssueCommentEventPayload, IssuesEventPayload, PullRequestEventPayload,
PullRequestReviewCommentEventPayload, PushEventPayload,
ForkEventPayload, GollumEventPayload, IssueCommentEventPayload, IssuesEventPayload,
PullRequestEventPayload, PullRequestReviewCommentEventPayload, PushEventPayload,
};
use chrono::{DateTime, Utc};
use payload::MemberEventPayload;
use reqwest::Url;
use serde::{de::Error, Deserialize, Serialize};

Expand Down Expand Up @@ -34,6 +35,9 @@ pub enum EventType {
IssuesEvent,
IssueCommentEvent,
CommitCommentEvent,
ForkEvent,
GollumEvent,
MemberEvent,
PullRequestEvent,
PullRequestReviewCommentEvent,
UnknownEvent(String),
Expand Down Expand Up @@ -115,6 +119,9 @@ fn deserialize_event_type(event_type: &str) -> EventType {
"IssuesEvent" => EventType::IssuesEvent,
"IssueCommentEvent" => EventType::IssueCommentEvent,
"CommitCommentEvent" => EventType::CommitCommentEvent,
"ForkEvent" => EventType::ForkEvent,
"GollumEvent" => EventType::GollumEvent,
"MemberEvent" => EventType::MemberEvent,
"PullRequestEvent" => EventType::PullRequestEvent,
"PullRequestReviewCommentEvent" => EventType::PullRequestReviewCommentEvent,
unknown => EventType::UnknownEvent(unknown.to_owned()),
Expand Down Expand Up @@ -142,6 +149,15 @@ fn deserialize_payload(
.map(EventPayload::IssueCommentEvent)?,
EventType::CommitCommentEvent => serde_json::from_value::<CommitCommentEventPayload>(data)
.map(EventPayload::CommitCommentEvent)?,
EventType::ForkEvent => {
serde_json::from_value::<ForkEventPayload>(data).map(EventPayload::ForkEvent)?
}
EventType::GollumEvent => {
serde_json::from_value::<GollumEventPayload>(data).map(EventPayload::GollumEvent)?
}
EventType::MemberEvent => {
serde_json::from_value::<MemberEventPayload>(data).map(EventPayload::MemberEvent)?
}
EventType::PullRequestEvent => serde_json::from_value::<PullRequestEventPayload>(data)
.map(|payload| EventPayload::PullRequestEvent(Box::new(payload)))?,
EventType::PullRequestReviewCommentEvent => {
Expand Down Expand Up @@ -214,6 +230,27 @@ mod test {
assert_eq!(event.r#type, EventType::DeleteEvent);
}

#[test]
fn should_deserialize_fork_event() {
let json = include_str!("../../tests/resources/fork_event.json");
let event: Event = serde_json::from_str(json).unwrap();
assert_eq!(event.r#type, EventType::ForkEvent);
}

#[test]
fn should_deserialize_gollum_event() {
let json = include_str!("../../tests/resources/gollum_event.json");
let event: Event = serde_json::from_str(json).unwrap();
assert_eq!(event.r#type, EventType::GollumEvent);
}

#[test]
fn should_deserialize_member_event() {
let json = include_str!("../../tests/resources/member_event.json");
let event: Event = serde_json::from_str(json).unwrap();
assert_eq!(event.r#type, EventType::MemberEvent);
}

#[test]
fn should_deserialize_with_org_when_present() {
let json = include_str!("../../tests/resources/create_event.json");
Expand Down
9 changes: 9 additions & 0 deletions src/models/events/payload.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod commit_comment;
mod create;
mod delete;
mod fork;
mod gollum;
mod issue_comment;
mod issues;
mod member;
mod pull_request;
mod pull_request_review_comment;
mod push;
Expand All @@ -11,8 +14,11 @@ use crate::models::repos::GitUser;
pub use commit_comment::*;
pub use create::*;
pub use delete::*;
pub use fork::*;
pub use gollum::*;
pub use issue_comment::*;
pub use issues::*;
pub use member::*;
pub use pull_request::*;
pub use pull_request_review_comment::*;
pub use push::*;
Expand All @@ -34,6 +40,9 @@ pub enum EventPayload {
IssuesEvent(IssuesEventPayload),
IssueCommentEvent(IssueCommentEventPayload),
CommitCommentEvent(CommitCommentEventPayload),
ForkEvent(ForkEventPayload),
GollumEvent(GollumEventPayload),
MemberEvent(MemberEventPayload),
PullRequestEvent(Box<PullRequestEventPayload>),
PullRequestReviewCommentEvent(Box<PullRequestReviewCommentEventPayload>),
UnknownEvent(serde_json::Value),
Expand Down
26 changes: 26 additions & 0 deletions src/models/events/payload/fork.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::models::Repository;
use serde::{Deserialize, Serialize};

/// The payload in a [`super::EventPayload::ForkEvent`] type.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ForkEventPayload {
/// The fork.
pub forkee: Repository,
}

#[cfg(test)]
mod test {
use crate::models::events::{payload::EventPayload, Event};

#[test]
fn should_deserialize_with_correct_payload() {
let json = include_str!("../../../../tests/resources/fork_event.json");
let event: Event = serde_json::from_str(json).unwrap();
if let Some(EventPayload::ForkEvent(payload)) = event.payload {
assert_eq!(payload.forkee.id, 334843423);
} else {
panic!("unexpected event payload encountered: {:#?}", event.payload);
}
}
}
74 changes: 74 additions & 0 deletions src/models/events/payload/gollum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use reqwest::Url;
use serde::{Deserialize, Serialize};

/// The payload in a [`super::EventPayload::GollumEvent`] type.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GollumEventPayload {
/// The pages that were updated.
pub pages: Vec<GollumEventPage>,
}

/// A page in a [`GollumEventPayload`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GollumEventPage {
/// The name of the page.
pub page_name: String,
/// The title of the page.
pub title: String,
/// The action performed on the page.
pub action: GollumEventPageAction,
/// The latest commit SHA of the page.
pub sha: String,
/// Url to the page.
pub html_url: Url,
}

/// The action performed on a given page.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum GollumEventPageAction {
Created,
Edited,
}

#[cfg(test)]
mod test {
use super::GollumEventPageAction;
use crate::models::events::{payload::EventPayload, Event};

#[test]
fn should_deserialize_action_from_lowercase() {
let actions = vec![
(r#""created""#, GollumEventPageAction::Created),
(r#""edited""#, GollumEventPageAction::Edited),
];
for (action_str, action) in actions {
let deserialized = serde_json::from_str(&action_str).unwrap();
assert_eq!(action, deserialized);
}
}

#[test]
fn should_deserialize_with_correct_payload() {
let json = include_str!("../../../../tests/resources/gollum_event.json");
let event: Event = serde_json::from_str(json).unwrap();
if let Some(EventPayload::GollumEvent(payload)) = event.payload {
assert_eq!(payload.pages[0].page_name, "Home");
assert_eq!(payload.pages[0].title, "Home");
assert_eq!(payload.pages[0].action, GollumEventPageAction::Created);
assert_eq!(
payload.pages[0].sha,
"738b45139cbf06c11f3013e4b2b1a1ad370696ca"
);
assert_eq!(
payload.pages[0].html_url.to_string(),
"https://github.com/wayofthepie/test-events/wiki/Home"
);
} else {
panic!("unexpected event payload encountered: {:#?}", event.payload);
}
}
}
93 changes: 93 additions & 0 deletions src/models/events/payload/member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::models::User;
use serde::{Deserialize, Serialize};

/// The payload in a [`super::EventPayload::MemberEvent`] type.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MemberEventPayload {
/// The action this event represents.
pub action: MemberEventAction,
/// The user this event corresponds to.
pub member: User,
/// Only available on webhooks.
///
/// Changes to the collaborator permissions.
pub changes: Option<MemberEventChanges>,
}

/// The change which occurred.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum MemberEventChanges {
Permission(MemberEventChangesTo),
}

/// The new permission given to the collaborator.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MemberEventChangesTo {
/// The optional previous permission.
pub from: Option<String>,
/// The new permission.
pub to: String,
}

/// The action this event corresponds to.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum MemberEventAction {
Added,
/// Only available on webhooks.
Edited,
}

#[cfg(test)]
mod test {
use super::{MemberEventAction, MemberEventChanges, MemberEventChangesTo};
use crate::models::events::{payload::EventPayload, Event};
use serde_json::json;

#[test]
fn should_deserialize_action_from_lowercase() {
let actions = vec![
(r#""added""#, MemberEventAction::Added),
(r#""edited""#, MemberEventAction::Edited),
];
for (action_str, action) in actions {
let deserialized = serde_json::from_str(&action_str).unwrap();
assert_eq!(action, deserialized);
}
}

#[test]
fn should_deserialize_permission_changes() {
let json = json!({
"permission": {
"from": "triage",
"to": "write"
}
});
let deserialized = serde_json::from_value::<MemberEventChanges>(json).unwrap();
assert_eq!(
deserialized,
MemberEventChanges::Permission(MemberEventChangesTo {
from: Some("triage".to_owned()),
to: "write".to_owned()
})
);
}

#[test]
fn should_deserialize_with_correct_payload() {
let json = include_str!("../../../../tests/resources/member_event.json");
let event: Event = serde_json::from_str(json).unwrap();
if let Some(EventPayload::MemberEvent(payload)) = event.payload {
assert_eq!(payload.action, MemberEventAction::Added);
assert_eq!(payload.member.id, 58522265);
} else {
panic!("unexpected event payload encountered: {:#?}", event.payload);
}
}
}
Loading