ghrs 0.1.3

A simple client for GitHub v3 API
Documentation
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use chrono::{DateTime, Utc};
use serde::{de::Error, Deserialize, Deserializer, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PullRequest {
    pub id: u64,
    pub url: String,
    pub html_url: String,
    pub diff_url: String,
    pub patch_url: String,
    pub issue_url: String,
    pub commits_url: String,
    pub review_comments_url: String,
    pub review_comment_url: String,
    pub comments_url: String,
    pub statuses_url: String,
    pub number: u64,
    pub state: String,
    pub title: String,
    pub body: String,
    pub labels: Vec<Label>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<User>,
    pub assignees: Vec<User>,
    pub requested_reviewers: Vec<User>,
    pub requested_teams: Vec<Team>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub milestone: Option<Milestone>,
    pub locked: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_lock_reason: Option<String>,
    pub created_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub closed_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub merged_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub merge_commit_sha: Option<String>,
    pub head: Head,
    pub base: Base,
    #[serde(rename = "_links")]
    pub links: Links,
    pub author_association: String,
    pub draft: bool,
    pub user: User,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct User {
    pub login: String,
    pub id: i64,
    pub node_id: String,
    pub avatar_url: String,
    pub gravatar_id: String,
    pub url: String,
    pub html_url: String,
    pub followers_url: String,
    pub following_url: String,
    pub gists_url: String,
    pub starred_url: String,
    pub subscriptions_url: String,
    pub organizations_url: String,
    pub repos_url: String,
    pub events_url: String,
    pub received_events_url: String,
    pub r#type: String,
    pub site_admin: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Milestone {
    pub url: String,
    pub html_url: String,
    pub labels_url: String,
    pub id: i64,
    pub node_id: String,
    pub number: i64,
    pub state: String,
    pub title: String,
    pub description: String,
    pub creator: User,
    pub open_issues: i64,
    pub closed_issues: i64,
    pub created_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub closed_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub due_on: Option<DateTime<Utc>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Label {
    pub id: i64,
    pub node_id: String,
    pub url: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub color: String,
    pub default: bool,
}

#[derive(Clone, Debug, Serialize)]
pub struct Event {
    pub id: String,
    pub r#type: EventType,
    pub actor: Actor,
    pub repo: Repository,
    pub public: bool,
    pub created_at: DateTime<Utc>,
    pub payload: Option<Payload>,
    pub org: Option<Org>,
}

impl<'de> Deserialize<'de> for Event {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Tmp {
            id: String,
            r#type: EventType,
            actor: Actor,
            repo: Repository,
            public: bool,
            created_at: DateTime<Utc>,
            payload: Option<serde_json::Value>,
            org: Option<Org>,
        }
        let tmp = Tmp::deserialize(deserializer)?;
        let payload = tmp.payload.clone().map_or(Ok(None), |data| {
            deserialize_payload(&tmp.r#type, data).map_err(|e| Error::custom(e.to_string()))
        })?;
        let event = Event {
            id: tmp.id,
            r#type: tmp.r#type,
            actor: tmp.actor,
            repo: tmp.repo,
            public: tmp.public,
            created_at: tmp.created_at,
            payload,
            org: tmp.org,
        };
        Ok(event)
    }
}

fn deserialize_payload(
    event_type: &EventType,
    data: serde_json::Value,
) -> Result<Option<Payload>, serde_json::Error> {
    let payload = match event_type {
        EventType::IssuesEvent => Some(
            serde_json::from_value::<IssuesEventPayload>(data).map(Payload::IssuesEventPayload)?,
        ),
        EventType::PullRequestEvent => Some(
            serde_json::from_value::<PullRequestEventPayload>(data)
                .map(Payload::PullRequestEventPayload)?,
        ),
        EventType::PullRequestReviewCommentEvent => Some(
            serde_json::from_value::<PullRequestReviewCommentEventPayload>(data)
                .map(Payload::PullRequestReviewCommentEventPayload)?,
        ),
        EventType::IssueCommentEvent => Some(
            serde_json::from_value::<IssueCommentEventPayload>(data)
                .map(Payload::IssueCommentEventPayload)?,
        ),
        EventType::CommitCommentEvent => Some(
            serde_json::from_value::<CommitCommentEventPayload>(data)
                .map(Payload::CommitCommentEventPayload)?,
        ),
        _ => None,
    };
    Ok(payload)
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum EventType {
    IssuesEvent,
    PullRequestEvent,
    PullRequestReviewCommentEvent,
    IssueCommentEvent,
    CommitCommentEvent,
    #[serde(other)]
    UnknownEvent,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Payload {
    IssuesEventPayload(IssuesEventPayload),
    PullRequestEventPayload(PullRequestEventPayload),
    PullRequestReviewCommentEventPayload(PullRequestReviewCommentEventPayload),
    IssueCommentEventPayload(IssueCommentEventPayload),
    CommitCommentEventPayload(CommitCommentEventPayload),
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IssuesEventPayload {
    pub action: String,
    pub issue: Issue,
    // pub changes: Changes,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<User>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<Label>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PullRequestEventPayload {
    pub action: String,
    pub number: u64,
    // pub changes: Changes,
    pub pull_request: PullRequest,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PullRequestReviewCommentEventPayload {
    pub action: String,
    // pub changes: Changes,
    pub pull_request: PullRequest,
    pub comment: Comment,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IssueCommentEventPayload {
    pub action: String,
    // pub changes: Changes,
    pub issue: Issue,
    pub comment: Comment,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CommitCommentEventPayload {
    pub action: String,
    pub comment: Comment,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Repository {
    pub id: u64,
    pub name: String,
    pub url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Actor {
    pub id: u64,
    pub login: String,
    pub gravatar_id: String,
    pub avatar_url: String,
    pub url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Org {
    pub id: u64,
    pub login: String,
    pub gravatar_id: String,
    pub avatar_url: String,
    pub url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Issue {
    pub id: i64,
    pub node_id: String,
    pub url: String,
    pub repository_url: String,
    pub labels_url: String,
    pub comments_url: String,
    pub events_url: String,
    pub html_url: String,
    pub number: i64,
    pub state: String,
    pub title: String,
    pub body: String,
    pub user: User,
    pub labels: Vec<Label>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assignee: Option<User>,
    pub assignees: Vec<User>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub milestone: Option<Milestone>,
    pub locked: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_lock_reason: Option<String>,
    pub comments: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pull_request: Option<PullRequestLink>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub closed_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
    // TODO
    // pub repository: Repository,
    pub author_association: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PullRequestLink {
    pub url: String,
    pub html_url: String,
    pub diff_url: String,
    pub patch_url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Comment {
    pub id: u64,
    pub node_id: String,
    pub url: String,
    pub html_url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body_html: Option<String>,
    pub user: User,
    pub created_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Team {
    pub id: u64,
    pub node_id: String,
    pub url: String,
    pub html_url: String,
    pub name: String,
    pub slug: String,
    pub description: String,
    pub privacy: String,
    pub permission: String,
    pub members_url: String,
    pub repositories_url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Head {
    pub label: String,
    #[serde(rename = "ref")]
    pub ref_field: String,
    pub sha: String,
    pub user: User,
    // TODO
    // pub repo: Repo,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Base {
    pub label: String,
    #[serde(rename = "ref")]
    pub ref_field: String,
    pub sha: String,
    pub user: User,
    // TODO
    // pub repo: Repo,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Links {
    #[serde(rename = "self")]
    pub self_link: SelfLink,
    pub html: HtmlLink,
    pub issue: IssueLink,
    pub comments: CommentsLink,
    pub review_comments: ReviewCommentsLink,
    pub review_comment: ReviewCommentLink,
    pub commits: CommitsLink,
    pub statuses: StatusesLink,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SelfLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct HtmlLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IssueLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CommentsLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReviewCommentsLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReviewCommentLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CommitsLink {
    pub href: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct StatusesLink {
    pub href: String,
}