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
use crate::{
    methods::{get_commit, GetCommitBody},
    model::{
        commits::{comments::CommitComment, Commit},
        event_types::{macros::repo_origin, RepoEventInfo},
        prelude::*,
    },
    GithubRestError, Requester,
};

/// <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#commit_comment>
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommitCommentEvent {
    pub action: CommitCommentAction,
    pub comment: CommitComment,
    #[serde(flatten)]
    pub event_info: RepoEventInfo,
}

as_ref_and_deref!(CommitCommentEvent, RepoEventInfo, event_info);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumString, EnumVariantNames)]
#[serde(rename_all = "snake_case")]
pub enum CommitCommentAction {
    Created,
}

impl CommitCommentEvent {
    /// Get the commit that the current comment refers to.
    ///
    /// See also: <https://docs.github.com/en/rest/reference/commits#get-a-commit>
    pub async fn get_commit<T>(&self, client: &T, options: Option<&GetCommitBody>) -> Result<Commit, GithubRestError>
    where
        T: Requester,
    {
        get_commit(
            client,
            self.event_info.repository.owner.login.clone(),
            self.event_info.repository.name.clone(),
            self.comment.commit_id.clone(),
            options,
        )
        .await
    }
}

/// <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#status>
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StatusEvent {
    pub id: usize,
    pub sha: String,
    pub description: Option<String>,
    pub target_url: Option<String>,
    pub commit: Commit,
    pub state: StatusState,
    pub branches: Vec<nested::Branch>,
    pub created_at: Value,
    pub updated_at: Value,
    #[serde(flatten)]
    pub event_info: RepoEventInfo,
}

as_ref_and_deref!(StatusEvent, RepoEventInfo, event_info);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumString, EnumVariantNames)]
#[serde(rename_all = "snake_case")]
pub enum StatusState {
    Pending,
    Success,
    Failure,
    Error,
}

pub mod nested {
    use crate::model::prelude::*;

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct Branch {
        pub name: String,
        pub commit: NestedCommit,
        pub protected: bool,
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct NestedCommit {
        pub sha: String,
        pub url: String,
    }
}

impl Default for CommitCommentAction {
    fn default() -> Self {
        Self::Created
    }
}

repo_origin!(CommitCommentEvent);
repo_origin!(StatusEvent);