use derive_more::From;
use serde::{
de::{self, Deserializer},
Deserialize,
};
use std::fmt;
use std::str::FromStr;
use crate::{
AppEvent, Comment, DateTime, Installation, Issue, Label, Oid, PullRequest,
Repository, Review, ShortRepo, User,
};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum EventType {
Wildcard,
Ping,
CheckRun,
CheckSuite,
CommitComment,
ContentReference,
Create,
Delete,
Deployment,
DeploymentStatus,
Fork,
GitHubAppAuthorization,
Gollum,
Installation,
IntegrationInstallation,
InstallationRepositories,
IntegrationInstallationRepositories,
IssueComment,
Issues,
Label,
MarketplacePurchase,
Member,
Membership,
Milestone,
Organization,
OrgBlock,
PageBuild,
ProjectCard,
ProjectColumn,
Project,
Public,
PullRequest,
PullRequestReviewComment,
PullRequestReview,
Push,
Release,
Repository,
RepositoryImport,
RepositoryVulnerabilityAlert,
SecurityAdvisory,
Status,
Team,
TeamAdd,
Watch,
}
impl EventType {
pub fn name(self) -> &'static str {
match self {
EventType::Wildcard => "*",
EventType::Ping => "ping",
EventType::CheckRun => "check_run",
EventType::CheckSuite => "check_suite",
EventType::CommitComment => "commit_comment",
EventType::ContentReference => "content_reference",
EventType::Create => "create",
EventType::Delete => "delete",
EventType::Deployment => "deployment",
EventType::DeploymentStatus => "deployment_status",
EventType::Fork => "fork",
EventType::GitHubAppAuthorization => "github_app_authorization",
EventType::Gollum => "gollum",
EventType::Installation => "installation",
EventType::IntegrationInstallation => "integration_installation",
EventType::InstallationRepositories => "installation_repositories",
EventType::IntegrationInstallationRepositories => {
"integration_installation_repositories"
}
EventType::IssueComment => "issue_comment",
EventType::Issues => "issues",
EventType::Label => "label",
EventType::MarketplacePurchase => "marketplace_purchase",
EventType::Member => "member",
EventType::Membership => "membership",
EventType::Milestone => "milestone",
EventType::Organization => "organization",
EventType::OrgBlock => "org_block",
EventType::PageBuild => "page_build",
EventType::ProjectCard => "project_card",
EventType::ProjectColumn => "project_column",
EventType::Project => "project",
EventType::Public => "public",
EventType::PullRequest => "pull_request",
EventType::PullRequestReview => "pull_request_review",
EventType::PullRequestReviewComment => {
"pull_request_review_comment"
}
EventType::Push => "push",
EventType::Release => "release",
EventType::Repository => "repository",
EventType::RepositoryImport => "repository_import",
EventType::RepositoryVulnerabilityAlert => {
"repository_vulnerability_alert"
}
EventType::SecurityAdvisory => "security_advisory",
EventType::Status => "status",
EventType::Team => "team",
EventType::TeamAdd => "team_add",
EventType::Watch => "watch",
}
}
}
impl FromStr for EventType {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"*" => Ok(EventType::Wildcard),
"ping" => Ok(EventType::Ping),
"check_run" => Ok(EventType::CheckRun),
"check_suite" => Ok(EventType::CheckSuite),
"commit_comment" => Ok(EventType::CommitComment),
"content_reference" => Ok(EventType::ContentReference),
"create" => Ok(EventType::Create),
"delete" => Ok(EventType::Delete),
"deployment" => Ok(EventType::Deployment),
"deployment_status" => Ok(EventType::DeploymentStatus),
"fork" => Ok(EventType::Fork),
"github_app_authorization" => Ok(EventType::GitHubAppAuthorization),
"gollum" => Ok(EventType::Gollum),
"installation" => Ok(EventType::Installation),
"integration_installation" => {
Ok(EventType::IntegrationInstallation)
}
"installation_repositories" => {
Ok(EventType::InstallationRepositories)
}
"integration_installation_repositories" => {
Ok(EventType::IntegrationInstallationRepositories)
}
"issue_comment" => Ok(EventType::IssueComment),
"issues" => Ok(EventType::Issues),
"label" => Ok(EventType::Label),
"marketplace_purchase" => Ok(EventType::MarketplacePurchase),
"member" => Ok(EventType::Member),
"membership" => Ok(EventType::Membership),
"milestone" => Ok(EventType::Milestone),
"organization" => Ok(EventType::Organization),
"org_block" => Ok(EventType::OrgBlock),
"page_build" => Ok(EventType::PageBuild),
"project_card" => Ok(EventType::ProjectCard),
"project_column" => Ok(EventType::ProjectColumn),
"project" => Ok(EventType::Project),
"public" => Ok(EventType::Public),
"pull_request" => Ok(EventType::PullRequest),
"pull_request_review_comment" => {
Ok(EventType::PullRequestReviewComment)
}
"pull_request_review" => Ok(EventType::PullRequestReview),
"push" => Ok(EventType::Push),
"release" => Ok(EventType::Release),
"repository" => Ok(EventType::Repository),
"repository_import" => Ok(EventType::RepositoryImport),
"repository_vulnerability_alert" => {
Ok(EventType::RepositoryVulnerabilityAlert)
}
"security_advisory" => Ok(EventType::SecurityAdvisory),
"status" => Ok(EventType::Status),
"team" => Ok(EventType::Team),
"team_add" => Ok(EventType::TeamAdd),
"watch" => Ok(EventType::Watch),
_ => Err("invalid GitHub event"),
}
}
}
impl<'de> Deserialize<'de> for EventType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(de::Error::custom)
}
}
impl fmt::Display for EventType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(
Deserialize, From, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[allow(clippy::large_enum_variant)]
pub enum Event {
Ping(PingEvent),
CommitComment(CommitCommentEvent),
Create(CreateEvent),
Delete(DeleteEvent),
GitHubAppAuthorization(GitHubAppAuthorizationEvent),
Gollum(GollumEvent),
Installation(InstallationEvent),
InstallationRepositories(InstallationRepositoriesEvent),
IntegrationInstallation(IntegrationInstallationEvent),
IntegrationInstallationRepositories(
IntegrationInstallationRepositoriesEvent,
),
IssueComment(IssueCommentEvent),
Issues(IssuesEvent),
Label(LabelEvent),
PullRequest(PullRequestEvent),
PullRequestReview(PullRequestReviewEvent),
PullRequestReviewComment(PullRequestReviewCommentEvent),
Push(PushEvent),
Repository(RepositoryEvent),
Watch(WatchEvent),
}
impl AppEvent for Event {
fn installation(&self) -> Option<u64> {
match self {
Event::Ping(e) => e.installation(),
Event::CommitComment(e) => e.installation(),
Event::Create(e) => e.installation(),
Event::Delete(e) => e.installation(),
Event::GitHubAppAuthorization(e) => e.installation(),
Event::Gollum(e) => e.installation(),
Event::Installation(e) => e.installation(),
Event::InstallationRepositories(e) => e.installation(),
Event::IntegrationInstallation(e) => e.installation(),
Event::IntegrationInstallationRepositories(e) => e.installation(),
Event::IssueComment(e) => e.installation(),
Event::Issues(e) => e.installation(),
Event::Label(e) => e.installation(),
Event::PullRequest(e) => e.installation(),
Event::PullRequestReview(e) => e.installation(),
Event::PullRequestReviewComment(e) => e.installation(),
Event::Push(e) => e.installation(),
Event::Repository(e) => e.installation(),
Event::Watch(e) => e.installation(),
}
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
pub struct InstallationId {
pub id: u64,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[serde(tag = "type")]
pub enum Hook {
Repository(RepoHook),
App(AppHook),
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct RepoHook {
pub id: u64,
pub name: String,
pub active: bool,
pub events: Vec<EventType>,
pub config: HookConfig,
pub updated_at: DateTime,
pub created_at: DateTime,
pub url: String,
pub test_url: String,
pub ping_url: String,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct HookConfig {
pub content_type: String,
pub insecure_ssl: String,
pub secret: String,
pub url: String,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct AppHook {
pub id: u64,
pub name: String,
pub active: bool,
pub events: Vec<Event>,
pub config: HookConfig,
pub updated_at: DateTime,
pub created_at: DateTime,
pub integration_id: u64,
pub app_id: u64,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PingEvent {
pub zen: String,
pub hook_id: u64,
pub hook: Hook,
pub repository: Option<Repository>,
pub sender: Option<User>,
}
impl AppEvent for PingEvent {}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum CommitCommentAction {
Created,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CommitCommentEvent {
pub action: CommitCommentAction,
pub comment: Comment,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for CommitCommentEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum CreateRefType {
Repository,
Branch,
Tag,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CreateEvent {
pub ref_type: CreateRefType,
#[serde(rename = "ref")]
pub git_ref: Option<String>,
pub master_branch: String,
pub description: Option<String>,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for CreateEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum DeleteRefType {
Branch,
Tag,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct DeleteEvent {
pub ref_type: DeleteRefType,
#[serde(rename = "ref")]
pub git_ref: String,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for DeleteEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum GitHubAppAuthorizationAction {
Revoked,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct GitHubAppAuthorizationEvent {
pub action: GitHubAppAuthorizationAction,
pub sender: User,
pub installation: InstallationId,
}
impl AppEvent for GitHubAppAuthorizationEvent {
fn installation(&self) -> Option<u64> {
Some(self.installation.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum PageAction {
Created,
Edited,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PageEvent {
pub page_name: String,
pub title: String,
pub summary: Option<String>,
pub action: PageAction,
pub sha: Oid,
pub html_url: String,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct GollumEvent {
pub pages: Vec<PageEvent>,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for GollumEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum InstallationAction {
Created,
Deleted,
NewPermissionsAccepted,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InstallationEvent {
pub action: InstallationAction,
pub installation: Installation,
pub sender: User,
}
impl AppEvent for InstallationEvent {
fn installation(&self) -> Option<u64> {
Some(self.installation.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum InstallationRepositoriesAction {
Added,
Removed,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InstallationRepositoriesEvent {
pub action: InstallationRepositoriesAction,
pub installation: Installation,
pub repository_selection: String,
pub repositories_added: Vec<ShortRepo>,
pub repositories_removed: Vec<ShortRepo>,
pub sender: User,
}
impl AppEvent for InstallationRepositoriesEvent {
fn installation(&self) -> Option<u64> {
Some(self.installation.id)
}
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct IntegrationInstallationEvent;
impl AppEvent for IntegrationInstallationEvent {
fn installation(&self) -> Option<u64> {
None
}
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct IntegrationInstallationRepositoriesEvent;
impl AppEvent for IntegrationInstallationRepositoriesEvent {
fn installation(&self) -> Option<u64> {
None
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum IssueCommentAction {
Created,
Edited,
Deleted,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct IssueCommentEvent {
pub action: IssueCommentAction,
pub issue: Issue,
pub comment: Comment,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for IssueCommentEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum IssueAction {
Opened,
Edited,
Deleted,
Transferred,
Pinned,
Unpinned,
Closed,
Reopened,
Assigned,
Unassigned,
Labeled,
Unlabeled,
Milestoned,
Demilestoned,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ChangeFrom {
pub from: String,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct IssueChanges {
pub body: Option<ChangeFrom>,
pub title: Option<ChangeFrom>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct IssuesEvent {
pub action: IssueAction,
pub issue: Issue,
pub changes: Option<IssueChanges>,
pub label: Option<Label>,
pub assignee: Option<User>,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for IssuesEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum LabelAction {
Created,
Edited,
Deleted,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct LabelChanges {
pub color: Option<ChangeFrom>,
pub name: Option<ChangeFrom>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct LabelEvent {
pub action: LabelAction,
pub label: Label,
pub changes: Option<LabelChanges>,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for LabelEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum PullRequestAction {
Assigned,
Unassigned,
ReviewRequested,
ReviewRequestRemoved,
Labeled,
Unlabeled,
Opened,
Edited,
Closed,
ReadyForReview,
Locked,
Unlocked,
Reopened,
Synchronize,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PullRequestEvent {
pub action: PullRequestAction,
pub number: u64,
pub pull_request: PullRequest,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for PullRequestEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum PullRequestReviewAction {
Submitted,
Edited,
Dismissed,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PullRequestReviewChanges {
pub body: Option<ChangeFrom>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PullRequestReviewEvent {
pub action: PullRequestReviewAction,
pub review: Review,
pub changes: Option<PullRequestReviewChanges>,
pub pull_request: PullRequest,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for PullRequestReviewEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum PullRequestReviewCommentAction {
Created,
Edited,
Deleted,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PullRequestReviewCommentChanges {
pub body: Option<ChangeFrom>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PullRequestReviewCommentEvent {
pub action: PullRequestReviewCommentAction,
pub changes: Option<PullRequestReviewCommentChanges>,
pub pull_request: PullRequest,
pub repository: Repository,
pub comment: Comment,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for PullRequestReviewCommentEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Pusher {
pub name: String,
pub email: Option<String>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PushAuthor {
pub name: String,
pub email: Option<String>,
pub username: Option<String>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PushCommit {
pub id: Oid,
pub tree_id: Oid,
pub distinct: bool,
pub message: String,
pub timestamp: DateTime,
pub url: String,
pub author: PushAuthor,
pub committer: PushAuthor,
pub added: Vec<String>,
pub removed: Vec<String>,
pub modified: Vec<String>,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PushEvent {
#[serde(rename = "ref")]
pub git_ref: String,
pub before: Oid,
pub after: Oid,
pub created: bool,
pub deleted: bool,
pub forced: bool,
pub base_ref: Option<String>,
pub compare: String,
pub commits: Vec<PushCommit>,
pub head_commit: Option<PushCommit>,
pub repository: Repository,
pub pusher: Pusher,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for PushEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum RepositoryAction {
Created,
Deleted,
Archived,
Unarchived,
Publicized,
Privatized,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct RepositoryEvent {
pub action: RepositoryAction,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for RepositoryEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}
#[derive(
Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash,
)]
#[serde(rename_all = "snake_case")]
pub enum WatchAction {
Started,
}
#[derive(Deserialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct WatchEvent {
pub action: WatchAction,
pub repository: Repository,
pub sender: User,
pub installation: Option<InstallationId>,
}
impl AppEvent for WatchEvent {
fn installation(&self) -> Option<u64> {
self.installation.map(|i| i.id)
}
}