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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::model::{
    commits::{comments::CommitComment, Commit, Commits},
    reactions::{CommitCommentReactionCreated, Reaction},
};

use super::prelude::*;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct GetCommitBody {
    /// Page number of the results to fetch.
    page: usize,
    /// Results per page (maximum of 100)
    per_page: u8,
}

pub async fn get_commit<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    commit_id: impl Into<String>,
    options: Option<&GetCommitBody>,
) -> Result<Commit, GithubRestError>
where
    T: Requester,
{
    client
        .req::<GetCommitBody, String, Commit>(
            EndPoints::GetReposownerrepoCommitsref(owner.into(), repo.into(), commit_id.into()),
            options,
            None,
        )
        .await
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct GetCommitsBody {
    ///SHA or branch to start listing commits from. Default: the repository’s
    /// default branch (usually master).
    sha: Option<String>,
    ///Only commits containing this file path will be returned.
    path: Option<String>,
    ///GitHub login or email address by which to filter by commit author.
    author: Option<String>,
    ///Only show notifications updated after the given time. This is a
    /// timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
    since: Option<String>,
    ///Only commits before this date will be returned. This is a timestamp in
    /// ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
    until: Option<String>,
    ///Results per page (max 100)
    ///Default: 30
    per_page: Option<String>,
    ///Page number of the results to fetch.
    ///Default: 1
    page: Option<String>,
}

/// * tags repos
/// * get `/repos/{owner}/{repo}/commits`
/// * docs <https://docs.github.com/rest/reference/repos#list-commits>
///
/// List commits
/// **Signature verification object**
///
/// The response will include a `verification` object that describes the result
/// of verifying the commit's signature. The following fields are included in
/// the `verification` object:
///
/// | Name | Type | Description |
/// | ---- | ---- | ----------- |
/// | `verified` | `boolean` | Indicates whether GitHub considers the signature
/// in this commit to be verified. | | `reason` | `string` | The reason for
/// verified value. Possible values and their meanings are enumerated in table
/// below. | | `signature` | `string` | The signature that was extracted from
/// the commit. | | `payload` | `string` | The value that was signed. |
///
/// These are the possible values for `reason` in the `verification` object:
///
/// | Value | Description |
/// | ----- | ----------- |
/// | `expired_key` | The key that made the signature is expired. |
/// | `not_signing_key` | The "signing" flag is not among the usage flags in the
/// GPG key that made the signature. | | `gpgverify_error` | There was an error
/// communicating with the signature verification service. |
/// | `gpgverify_unavailable` | The signature verification service is currently
/// unavailable. | | `unsigned` | The object does not include a signature. |
/// | `unknown_signature_type` | A non-PGP signature was found in the commit. |
/// | `no_user` | No user was associated with the `committer` email address in
/// the commit. | | `unverified_email` | The `committer` email address in the
/// commit was associated with a user, but the email address is not verified on
/// her/his account. | | `bad_email` | The `committer` email address in the
/// commit is not included in the identities of the PGP key that made the
/// signature. | | `unknown_key` | The key that made the signature has not been
/// registered with any user's account. | | `malformed_signature` | There was an
/// error parsing the signature. | | `invalid` | The signature could not be
/// cryptographically verified using the key whose key-id was found in the
/// signature. | | `valid` | None of the above errors applied, so the signature
/// is considered to be verified. |
pub async fn get_commits<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    options: Option<&GetCommitsBody>,
) -> Result<Commits, GithubRestError>
where
    T: Requester,
{
    client
        .req::<GetCommitsBody, String, Commits>(
            EndPoints::GetReposownerrepoCommits(owner.into(), repo.into()),
            options,
            None,
        )
        .await
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct CommitCommentBody {
    /// **Required**. The contents of the comment.
    pub body: String,
    /// Relative path of the file to comment on.
    pub path: Option<String>,
    /// Line index in the diff to comment on.
    pub position: Option<String>,
    /// **Deprecated**. Use position parameter instead. Line number in the file
    /// to comment on.
    pub line: Option<String>,
}

/// * tags repos
/// * post `/repos/{owner}/{repo}/commits/{commit_sha}/comments`
/// * docs <https://docs.github.com/rest/reference/repos#create-a-commit-comment>
///
/// Create a commit comment
/// Create a comment for a commit using its `:commit_sha`.
///
/// This endpoint triggers [notifications](https://docs.github.com/en/github/managing-subscriptions-and-notifications-on-github/about-notifications). Creating content too quickly using this endpoint may result in secondary rate limiting. See "[Secondary rate limits](https://docs.github.com/rest/overview/resources-in-the-rest-api#secondary-rate-limits)" and "[Dealing with secondary rate limits](https://docs.github.com/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits)" for details.
pub async fn comment_on_commit<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    sha: impl Into<String>,
    options: &CommitCommentBody,
) -> Result<CommitComment, GithubRestError>
where
    T: Requester,
{
    client
        .req::<String, String, CommitComment>(
            EndPoints::PostReposownerrepoCommitscommitShaComments(owner.into(), repo.into(), sha.into()),
            None,
            Some(serde_json::to_string(options)?),
        )
        .await
}

/// * tags reactions
/// * post `/repos/{owner}/{repo}/comments/{comment_id}/reactions`
/// * docs <https://docs.github.com/rest/reference/reactions#create-reaction-for-a-commit-comment>
///
/// Create reaction for a commit comment
/// Create a reaction to a [commit comment](https://docs.github.com/rest/reference/repos#comments). A response with an HTTP `200` status means that you already added the reaction type to this commit comment.
pub async fn react_to_commit_comment<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    comment_id: i64,
    reaction: Reaction,
) -> Result<CommitCommentReactionCreated, GithubRestError>
where
    T: Requester,
{
    // dbg!(serde_json::to_string(&reaction).unwrap());
    let reaction = "{\"content\":stuff}"
        .to_owned()
        .replace("stuff", serde_json::to_string(&reaction).unwrap().as_str());

    client
        .req::<String, String, CommitCommentReactionCreated>(
            EndPoints::PostReposownerrepoCommentscommentIdReactions(owner.into(), repo.into(), comment_id.to_string()),
            None,
            Some(reaction),
        )
        .await
}

#[cfg(feature = "client")]
#[cfg(test)]
mod tests {
    use crate::client::DefaultRequester;

    use super::*;

    #[tokio::test]
    async fn test_get_commit() {
        let requester = DefaultRequester::new_none();

        let res = get_commit(
            &requester,
            "microsoft",
            "vscode",
            "80d30e41d790c774230b00dd480895d55451d967",
            None,
        )
        .await
        .unwrap();
        println!("{res:#?}")
    }

    #[tokio::test]
    async fn test_get_commits() {
        let requester = DefaultRequester::new_none();

        let res = get_commits(&requester, "microsoft", "vscode", None).await.unwrap();
        println!("{res:#?}")
    }
}