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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::model::{
    issues::{Issue, Issues},
    pull_requests::{PullRequestState, Pulls},
};

use super::prelude::*;

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct CreateIssueBody {
    pub title: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub milestone: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub labels: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignees: Option<Vec<String>>,
}

/// * tags issues
/// * post `/repos/{owner}/{repo}/issues`
/// * docs <https://docs.github.com/rest/reference/issues#create-an-issue>
///
/// Create an issue
/// Any user with pull access to a repository can create an issue. If [issues are disabled in the repository](https://help.github.com/articles/disabling-issues/), the API returns a `410 Gone` status.
///
/// 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 create_issue<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    body: &CreateIssueBody,
) -> Result<Issue, GithubRestError>
where
    T: Requester,
{
    client
        .req::<String, String, Issue>(
            EndPoints::PostReposownerrepoIssues(owner.into(), repo.into()),
            None,
            Some(serde_json::to_string(body)?),
        )
        .await
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct GetIssuesBody {
    /// If an integer is passed, it should refer to a milestone by its number
    /// field. If the string * is passed, issues with any milestone are
    /// accepted. If the string none is passed, issues without milestones are
    /// returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub milestone: Option<String>,
    /// Indicates the state of the issues to return. Can be either open, closed,
    /// or all. Default: open
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<IssueState>,
    /// Can be the name of a user. Pass in none for issues with no assigned
    /// user, and * for issues assigned to any user.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<String>,
    /// The user that created the issue.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub creator: Option<String>,
    /// A user that's mentioned in the issue.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mentioned: Option<String>,
    /// A list of comma separated label names. Example: bug,ui,@high
    #[serde(skip_serializing_if = "Option::is_none")]
    pub labels: Option<String>,
    /// What to sort results by. Can be either created, updated, comments.
    /// Default: created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<String>,
    /// One of asc (ascending) or desc (descending).
    /// Default: desc
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<String>,
    /// Only show notifications updated after the given time. This is a
    /// timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub since: Option<String>,
    /// Results per page (max 100)
    /// Default: 30
    #[serde(skip_serializing_if = "Option::is_none")]
    pub per_page: Option<String>,
    /// Page number of the results to fetch.
    /// Default: 1
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<String>,
}

/// * docs <https://docs.github.com/en/rest/reference/issues#list-issues-assigned-to-the-authenticated-user--parameters>
///
/// Represents the state of an issue. Possible variants are open, closed, and
/// all.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IssueState {
    Open,
    Closed,
    All,
}

/// * tags issues
/// * get `/repos/{owner}/{repo}/issues`
/// * docs <https://docs.github.com/rest/reference/issues#list-repository-issues>
///
/// List repository issues
/// List issues in a repository.
///
/// **Note**: GitHub's REST API v3 considers every pull request an issue, but
/// not every issue is a pull request. For this reason, "Issues" endpoints may
/// return both issues and pull requests in the response. You can identify pull
/// requests by the `pull_request` key. Be aware that the `id` of a pull
/// request returned from "Issues" endpoints will be an _issue id_. To find out the pull request id, use the "[List pull requests](https://docs.github.com/rest/reference/pulls#list-pull-requests)" endpoint.
pub async fn get_issues<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    options: Option<&GetIssuesBody>,
) -> Result<Issues, GithubRestError>
where
    T: Requester,
{
    client
        .req::<GetIssuesBody, String, Issues>(
            EndPoints::GetReposownerrepoIssues(owner.into(), repo.into()),
            options,
            None,
        )
        .await
}

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct GetPullsBody {
    /// Either open, closed, or all to filter by state.
    /// Default: open
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<PullRequestState>,
    /// Filter pulls by head user or head organization and branch name in the
    /// format of user:ref-name or organization:ref-name. For example:
    /// github:new-script-format or octocat:test-branch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<String>,
    /// Filter pulls by base branch name. Example: gh-pages.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
    /// What to sort results by. Can be either created, updated, popularity
    /// (comment count) or long-running (age, filtering by pulls updated in the
    /// last month). Default: created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<String>,
    /// One of asc (ascending) or desc (descending).
    /// Default: desc
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<String>,
    /// Only show notifications updated after the given time. This is a
    /// timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub since: Option<String>,
    /// Results per page (max 100)
    /// Default: 30
    #[serde(skip_serializing_if = "Option::is_none")]
    pub per_page: Option<String>,
    /// Page number of the results to fetch.
    /// Default: 1
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<String>,
}

/// * tags pulls
/// * get `/repos/{owner}/{repo}/pulls`
/// * docs <https://docs.github.com/rest/reference/pulls#list-pull-requests>
///
/// List pull requests
/// Draft pull requests are available in public repositories with GitHub Free and GitHub Free for organizations, GitHub Pro, and legacy per-repository billing plans, and in public and private repositories with GitHub Team and GitHub Enterprise Cloud. For more information, see [GitHub's products](https://help.github.com/github/getting-started-with-github/githubs-products) in the GitHub Help documentation.
pub async fn get_pulls<T>(
    client: &T,
    owner: impl Into<String>,
    repo: impl Into<String>,
    options: Option<&GetPullsBody>,
) -> Result<Pulls, GithubRestError>
where
    T: Requester,
{
    client
        .req::<GetPullsBody, String, Pulls>(
            EndPoints::GetReposownerrepoPulls(owner.into(), repo.into()),
            options,
            None,
        )
        .await
}

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

    use super::*;

    #[tokio::test]
    async fn test_create_issue() {
        let body = CreateIssueBody {
            title: "tricked is cool".to_owned(),
            body: Some("This is very true".to_owned()),
            assignee: None,
            milestone: None,
            labels: None,
            assignees: None,
        };

        let res = create_issue(&util::github_auth(), "Tricked-dev", "octo-computing-machine", &body)
            .await
            .unwrap();

        dbg!(res);
    }

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

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

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

        let body = GetIssuesBody {
            milestone: None,
            state: None,
            assignee: None,
            creator: None,
            mentioned: None,
            labels: None,
            sort: None,
            since: None,
            direction: None,
            per_page: Some("1".to_owned()),
            page: None,
        };

        let res = get_issues(&requester, "microsoft", "vscode", Some(&body))
            .await
            .unwrap();

        dbg!(res);
    }
}