use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Actor {
pub login: String,
pub display_name: Option<String>,
pub avatar_url: Option<String>,
}
impl Actor {
pub fn new(login: impl Into<String>) -> Self {
Self {
login: login.into(),
display_name: None,
avatar_url: None,
}
}
pub fn label(&self) -> &str {
self.display_name.as_deref().unwrap_or(&self.login)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Repo {
pub owner: String,
pub name: String,
pub url: Option<String>,
}
impl Repo {
pub fn new(owner: impl Into<String>, name: impl Into<String>) -> Self {
Self {
owner: owner.into(),
name: name.into(),
url: None,
}
}
pub fn full_name(&self) -> String {
format!("{}/{}", self.owner, self.name)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PullRequest {
pub repo: Repo,
pub number: u64,
pub title: String,
pub url: String,
pub author: Actor,
pub draft: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewState {
Approved,
ChangesRequested,
Commented,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum EventKind {
ReviewRequested,
ReReviewRequested,
ReviewSubmitted { state: ReviewState },
ReviewDismissed,
CommentReply {
on_your_comment: bool,
},
Mentioned,
Merged,
Closed,
ReadyForReview,
}
impl EventKind {
pub fn tag(&self) -> &'static str {
match self {
EventKind::ReviewRequested => "review_requested",
EventKind::ReReviewRequested => "re_review_requested",
EventKind::ReviewSubmitted { .. } => "review_submitted",
EventKind::ReviewDismissed => "review_dismissed",
EventKind::CommentReply { .. } => "comment_reply",
EventKind::Mentioned => "mentioned",
EventKind::Merged => "merged",
EventKind::Closed => "closed",
EventKind::ReadyForReview => "ready_for_review",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewerRelationship {
pub is_author: bool,
pub is_reviewer: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event {
pub source_id: String,
pub kind: EventKind,
pub pull_request: PullRequest,
pub viewer: ViewerRelationship,
pub actor: Actor,
#[serde(with = "time::serde::rfc3339")]
pub occurred_at: OffsetDateTime,
pub target_url: Option<String>,
pub excerpt: Option<String>,
pub dedup_key: String,
}
impl Event {
pub fn make_dedup_key(
source_id: &str,
repo: &Repo,
pr_number: u64,
discriminator: &str,
) -> String {
format!(
"{}:{}#{}:{}",
source_id,
repo.full_name(),
pr_number,
discriminator
)
}
}