use crate::models::{issues::Issue, Author, Label};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IssuesEventPayload {
pub action: IssuesEventAction,
pub issue: Issue,
pub changes: Option<IssuesEventChanges>,
pub assignee: Option<Author>,
pub label: Option<Label>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum IssuesEventChanges {
Title(IssuesEventChangesFrom),
Body(IssuesEventChangesFrom),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IssuesEventChangesFrom {
pub from: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum IssuesEventAction {
Opened,
Closed,
Reopened,
Edited,
Assigned,
Unassigned,
Labeled,
Unlabeled,
}
#[cfg(test)]
mod test {
use super::{IssuesEventAction, IssuesEventChanges, IssuesEventChangesFrom};
use crate::models::events::{payload::EventPayload, Event};
use serde_json::json;
#[test]
fn should_deserialize_action_from_lowercase() {
let actions = vec![
(r#""opened""#, IssuesEventAction::Opened),
(r#""closed""#, IssuesEventAction::Closed),
(r#""edited""#, IssuesEventAction::Edited),
(r#""reopened""#, IssuesEventAction::Reopened),
(r#""assigned""#, IssuesEventAction::Assigned),
(r#""unassigned""#, IssuesEventAction::Unassigned),
(r#""labeled""#, IssuesEventAction::Labeled),
(r#""unlabeled""#, IssuesEventAction::Unlabeled),
];
for (action_str, action) in actions {
let deserialized = serde_json::from_str(action_str).unwrap();
assert_eq!(action, deserialized);
}
}
#[test]
fn should_deserialize_title_changes() {
let json = json!({
"title": {
"from": "test"
}
});
let deserialized = serde_json::from_value::<IssuesEventChanges>(json).unwrap();
assert_eq!(
deserialized,
IssuesEventChanges::Title(IssuesEventChangesFrom {
from: "test".to_owned()
})
);
}
#[test]
fn should_deserialize_body_changes() {
let json = json!({
"body": {
"from": "test"
}
});
let deserialized = serde_json::from_value::<IssuesEventChanges>(json).unwrap();
assert_eq!(
deserialized,
IssuesEventChanges::Body(IssuesEventChangesFrom {
from: "test".to_owned()
})
);
}
#[test]
fn should_deserialize_with_correct_payload() {
let json = include_str!("../../../../tests/resources/issues_event.json");
let event: Event = serde_json::from_str(json).unwrap();
if let Some(EventPayload::IssuesEvent(ref payload)) =
event.payload.as_ref().unwrap().specific
{
assert_eq!(payload.action, IssuesEventAction::Opened);
assert_eq!(payload.issue.id.0, 786747990);
} else {
panic!("unexpected event payload encountered: {:#?}", event.payload);
}
}
}