automatons_github/event/
check_run.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5use crate::resource::{Account, CheckRun, Installation, Organization, Repository};
6
7/// Check run action
8///
9/// The type of activity that has occurred.
10#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum CheckRunAction {
13    /// A new check run was created.
14    Created,
15
16    /// The `status` of the [`CheckRun`] is `completed`.
17    Completed,
18
19    /// Someone requested to re-run your check run from the pull request UI.
20    Rerequested,
21
22    /// Someone requested an action your app provides to be taken.
23    RequestedAction,
24}
25
26/// Check run event
27///
28/// A check run event contains the action that occurred, the latest state of the check run, and the
29/// repository that the check run was created in. If the webhook was configured for an organization,
30/// or if the repository is owned by one, the organization is included in the payload. If the event
31/// is sent to a GitHub App, the payload contains the installation.
32#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
33pub struct CheckRunEvent {
34    action: CheckRunAction,
35    check_run: CheckRun,
36    repository: Repository,
37    organization: Option<Organization>,
38    installation: Option<Installation>,
39    sender: Account,
40}
41
42impl CheckRunEvent {
43    /// Returns the check run event's action.
44    #[cfg_attr(feature = "tracing", tracing::instrument)]
45    pub fn action(&self) -> CheckRunAction {
46        self.action
47    }
48
49    /// Returns the check run event's check run.
50    #[cfg_attr(feature = "tracing", tracing::instrument)]
51    pub fn check_run(&self) -> &CheckRun {
52        &self.check_run
53    }
54
55    /// Returns the check run event's repository.
56    #[cfg_attr(feature = "tracing", tracing::instrument)]
57    pub fn repository(&self) -> &Repository {
58        &self.repository
59    }
60
61    /// Returns the check run event's organization.
62    #[cfg_attr(feature = "tracing", tracing::instrument)]
63    pub fn organization(&self) -> &Option<Organization> {
64        &self.organization
65    }
66
67    /// Returns the check run event's installation.
68    #[cfg_attr(feature = "tracing", tracing::instrument)]
69    pub fn installation(&self) -> &Option<Installation> {
70        &self.installation
71    }
72
73    /// Returns the check run event's sender.
74    #[cfg_attr(feature = "tracing", tracing::instrument)]
75    pub fn sender(&self) -> &Account {
76        &self.sender
77    }
78}
79
80impl Display for CheckRunAction {
81    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
82        let string_representation = match self {
83            CheckRunAction::Created => "created",
84            CheckRunAction::Completed => "completed",
85            CheckRunAction::Rerequested => "rerequested",
86            CheckRunAction::RequestedAction => "requested action",
87        };
88
89        write!(f, "{}", string_representation)
90    }
91}
92
93impl Display for CheckRunEvent {
94    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
95        write!(f, "{} ({})", self.check_run.name(), self.action)
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::{CheckRunAction, CheckRunEvent};
102
103    #[test]
104    fn trait_deserialize() {
105        let check_run_event: CheckRunEvent = serde_json::from_str(include_str!(
106            "../../tests/fixtures/event/check_run.completed.json"
107        ))
108        .unwrap();
109
110        assert!(matches!(
111            check_run_event.action(),
112            CheckRunAction::Completed
113        ));
114    }
115
116    #[test]
117    fn trait_display() {
118        let check_run_event: CheckRunEvent = serde_json::from_str(include_str!(
119            "../../tests/fixtures/event/check_run.completed.json"
120        ))
121        .unwrap();
122
123        assert_eq!("Run tests (completed)", check_run_event.to_string());
124    }
125
126    #[test]
127    fn trait_send() {
128        fn assert_send<T: Send>() {}
129        assert_send::<CheckRunEvent>();
130    }
131
132    #[test]
133    fn trait_sync() {
134        fn assert_sync<T: Sync>() {}
135        assert_sync::<CheckRunEvent>();
136    }
137}