automatons_github/event/
mod.rs

1//! Events on GitHub
2//!
3//! The GitHub integration enables the [automatons] framework to interact with webhook sevents on
4//! GitHub. These events are modelled as strongly-typed data types in this module, and each model
5//! maps to a [webhook event](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads)
6//! with its payload.
7//!
8//! [automatons]: https://github.com/devxbots/automatons
9
10use std::fmt::{Display, Formatter};
11
12use serde::{Deserialize, Serialize};
13
14pub use self::check_run::{CheckRunAction, CheckRunEvent};
15
16mod check_run;
17
18/// Event on GitHub
19///
20/// Webhooks allow you to build or set up integrations, such as GitHub Apps or OAuth Apps, which
21/// subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a
22/// HTTP POST payload to the webhook's configured URL. Webhooks can be used to update an external
23/// issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production
24/// server. You're only limited by your imagination.
25///
26/// Read more: https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks
27///
28/// The webhook payloads are inside a [`Box`], since their sizes vary greatly.
29#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
30#[serde(untagged)]
31pub enum GitHubEvent {
32    /// Check run event
33    CheckRun(Box<CheckRunEvent>),
34
35    /// Unsupported event
36    Unsupported,
37}
38
39impl Display for GitHubEvent {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        let string_representation = match self {
42            GitHubEvent::CheckRun(event) => format!("check run {}", event.action()),
43            GitHubEvent::Unsupported => "unsupported".into(),
44        };
45
46        write!(f, "{}", string_representation)
47    }
48}
49
50impl Default for GitHubEvent {
51    fn default() -> Self {
52        GitHubEvent::Unsupported
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::GitHubEvent;
59
60    #[test]
61    fn trait_deserialize_check_run() {
62        let github_event: GitHubEvent = serde_json::from_str(include_str!(
63            "../../tests/fixtures/event/check_run.completed.json"
64        ))
65        .unwrap();
66
67        if let GitHubEvent::CheckRun(check_run_event) = github_event {
68            assert_eq!("Run tests", check_run_event.check_run().name().get());
69        } else {
70            panic!("expected a check run event");
71        }
72    }
73
74    #[test]
75    fn trait_send() {
76        fn assert_send<T: Send>() {}
77        assert_send::<GitHubEvent>();
78    }
79
80    #[test]
81    fn trait_sync() {
82        fn assert_sync<T: Sync>() {}
83        assert_sync::<GitHubEvent>();
84    }
85}