1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::model::{organizations::SimpleOrganization, prelude::*, repositories::Repository, user::SimpleUser};

/// Used to represent all possible values for the `x-github-event` header sent
/// with all webhook payloads.
///
/// See also: <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads>
#[derive(Deserialize, EnumString, EnumVariantNames, Debug)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum EventTypes {
    // GitHub Apps
    GithubAppAuthorization,
    Installation,
    InstallationRepositories,

    // Wiki page created/updated
    Gollum,

    // Repositories
    DeployKey,
    Member,
    Milestone,
    Public,
    Release,
    Repository,
    RepositoryDispatch,
    RepositoryImport,
    RepositoryVulnerabilityAlert,
    SecretScanningAlert,
    SecurityAdvisory,
    Star,
    Watch,

    // Pulls
    PullRequest,
    PullRequestReview,
    PullRequestReviewComment,

    // Commits
    CommitComment,
    Push,
    Status,

    // Issues
    IssueComment,
    Issues,
    Label,

    // Discussions
    Discussion,
    DiscussionComment,

    // Branches/Tags
    BranchProtectionRule,
    Create,
    Delete,
    Fork,

    // CI/Workflows
    CheckRun,
    CheckSuite,
    CodeScanningAlert,
    Deployment,
    DeploymentStatus,
    PageBuild,
    WorkflowDispatch,
    WorkflowJob,
    WorkflowRun,

    // Organizations
    Membership,
    OrgBlock,
    Organization,
    Team,
    TeamAdd,

    // Projects (for both organizations and repositories)
    Project,
    ProjectCard,
    ProjectColumn,

    // GitHub Marketplace
    MarketplacePurchase,

    // Misc
    Meta,
    Package,
    /* <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#package> */
    Ping,
    Sponsorship,
}

/// Used to represent the base fields provided by events originating from
/// repositories.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct RepoEventInfo {
    pub repository: Repository,
    pub organization: Option<SimpleOrganization>,
    pub installation: Option<Value>,
    pub sender: SimpleUser,
}

/// Used to represent the base fields provided by events originating from
/// organizations.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct OrgEventInfo {
    pub organization: SimpleOrganization,
    pub installation: Option<Value>,
    pub sender: SimpleUser,
}

/// The base trait used to represent different types of events. This will
/// eventually have some subtraits with convenience methods.
///
/// **All** event types implement this.
pub trait Event<'de>: Serialize + Deserialize<'de> {
    type Origin: Serialize + Deserialize<'de>;

    fn origin(&self) -> Option<Self::Origin> {
        None
    }
}

pub(crate) mod macros {
    macro_rules! repo_origin {
        ($ev:ident) => {
            impl crate::model::event_types::Event<'_> for $ev {
                type Origin = crate::model::repositories::Repository;

                fn origin(&self) -> Option<Self::Origin> {
                    Some(self.event_info.repository.clone())
                }
            }
        };
    }

    macro_rules! org_origin {
        ($ev:ident) => {
            impl crate::model::event_types::Event<'_> for $ev {
                type Origin = crate::model::organizations::Organization;
            }
        };
    }

    pub(crate) use org_origin;
    pub(crate) use repo_origin;
}