ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Typed GraphQL operations, validated against GitHub's schema at compile
//! time. These mirror the wire format; [`crate::api::model`] holds what the rest of
//! the app uses.

use crate::schema::schema;

use crate::api::model::{MergeState, Mergeable, ReviewDecision, ReviewState, Side, ViewedState};

#[derive(cynic::Scalar, Debug, Clone)]
pub struct DateTime(pub String);

#[derive(cynic::Scalar, Debug, Clone)]
#[cynic(graphql_type = "URI")]
pub struct Uri(pub String);

#[derive(cynic::Scalar, Debug, Clone)]
#[cynic(graphql_type = "GitObjectID")]
pub struct GitObjectId(pub String);

/// A number too big for GraphQL's `Int`, as GitHub writes it: a string.
#[derive(cynic::Scalar, Debug, Clone)]
pub struct BigInt(pub String);

/// The non-null nodes of a GraphQL list.
pub(crate) fn nodes<T>(list: Option<Vec<Option<T>>>) -> impl Iterator<Item = T> {
    list.into_iter().flatten().flatten()
}

/// Fragments with the same single field on several GraphQL types. Field
/// types are written out in the arms: cynic's derive fails on a type passed
/// in whole.
macro_rules! fragments {
    ($field:ident: Option<$ty:ident>; $($name:ident = $graphql:literal),* $(,)?) => {$(
        #[derive(cynic::QueryFragment, Debug)]
        #[cynic(graphql_type = $graphql)]
        pub struct $name {
            pub $field: Option<$ty>,
        }
    )*};
    (total_count: i32; $($name:ident = $graphql:literal),* $(,)?) => {$(
        #[derive(cynic::QueryFragment, Debug)]
        #[cynic(graphql_type = $graphql)]
        pub struct $name {
            pub total_count: i32,
        }
    )*};
    (id: cynic::Id; $($name:ident = $graphql:literal),* $(,)?) => {$(
        #[derive(cynic::QueryFragment, Debug)]
        #[cynic(graphql_type = $graphql)]
        pub struct $name {
            pub id: cynic::Id,
        }
    )*};
    // A page of nodes and how many there are in all.
    (counted: $($name:ident = $graphql:literal => $ty:ident),* $(,)?) => {$(
        #[derive(cynic::QueryFragment, Debug)]
        #[cynic(graphql_type = $graphql)]
        pub struct $name {
            pub total_count: i32,
            pub nodes: Option<Vec<Option<$ty>>>,
        }
    )*};
    (nodes: $($name:ident = $graphql:literal => $ty:ident),* $(,)?) => {$(
        #[derive(cynic::QueryFragment, Debug)]
        #[cynic(graphql_type = $graphql)]
        pub struct $name {
            pub nodes: Option<Vec<Option<$ty>>>,
        }
    )*};
}
pub(crate) use fragments;

// Connection sizes.
fragments! {
    total_count: i32;
    CommentCount = "IssueCommentConnection",
    UserCount = "UserConnection",
    IssueCount = "IssueConnection",
    PrCount = "PullRequestConnection",
    CommitCount = "CommitHistoryConnection",
    ReviewCommentCount = "PullRequestReviewCommentConnection",
    FollowCount = "FollowerConnection",
    FollowingCount = "FollowingConnection",
    RefCount = "RefConnection",
}

// Results of mutations that only need to succeed.
fragments! {
    client_mutation_id: Option<String>;
    MarkFileAsViewedPayload = "MarkFileAsViewedPayload",
    UnmarkFileAsViewedPayload = "UnmarkFileAsViewedPayload",
    AddCommentPayload = "AddCommentPayload",
    StarPayload = "AddStarPayload",
    UnstarPayload = "RemoveStarPayload",
    MergePayload = "MergePullRequestPayload",
    ClosePrPayload = "ClosePullRequestPayload",
    ReopenPrPayload = "ReopenPullRequestPayload",
    CloseIssuePayload = "CloseIssuePayload",
    ReopenIssuePayload = "ReopenIssuePayload",
    ReadyPayload = "MarkPullRequestReadyForReviewPayload",
    UpdateBranchPayload = "UpdatePullRequestBranchPayload",
    AutoMergePayload = "EnablePullRequestAutoMergePayload",
    NoAutoMergePayload = "DisablePullRequestAutoMergePayload",
    EnqueuePayload = "EnqueuePullRequestPayload",
    DequeuePayload = "DequeuePullRequestPayload",
}

// Node IDs, and the mutation results that carry them.
fragments! {
    id: cynic::Id;
    ReviewId = "PullRequestReview",
    ThreadId = "PullRequestReviewThread",
    CommentId = "PullRequestReviewComment",
}
fragments! {
    pull_request_review: Option<ReviewId>;
    StartReviewPayload = "AddPullRequestReviewPayload",
    SubmitReviewPayload = "SubmitPullRequestReviewPayload",
}
fragments! {
    thread: Option<ThreadId>;
    ThreadPayload = "AddPullRequestReviewThreadPayload",
    ResolvePayload = "ResolveReviewThreadPayload",
    UnresolvePayload = "UnresolveReviewThreadPayload",
}

// Lists of nodes and their totals, and their nodes' types.
fragments! {
    counted:
    LabelConnection = "LabelConnection" => Label,
    ReviewCommentConnection = "PullRequestReviewCommentConnection" => ReviewComment,
    FirstCommentConnection = "PullRequestReviewCommentConnection" => FirstComment,
}

// Lists of nodes, and their nodes' types.
fragments! {
    nodes:
    CommitRollupConnection = "PullRequestCommitConnection" => CommitRollupNode,
    PendingReviewConnection = "PullRequestReviewConnection" => PendingReview,
    ReviewCommitConnection = "PullRequestReviewConnection" => ReviewCommit,
}

/// An issue or pull request by number.
#[derive(cynic::QueryVariables, Debug)]
pub struct NumberVariables {
    pub owner: String,
    pub name: String,
    pub number: i32,
}

/// A page of a pull request's files or threads.
#[derive(cynic::QueryVariables, Debug)]
pub struct PageVariables {
    pub owner: String,
    pub name: String,
    pub number: i32,
    pub after: Option<String>,
}

// ---- checks on the pull requests a search found ------------------------------

/// Pull requests by ID, for their checks: a search finds them, and this
/// reads their checks apart, so neither request runs into GitHub's
/// 10-second limit (see `GitHub::search`).
#[derive(cynic::QueryVariables, Debug)]
pub struct NodesVariables {
    pub ids: Vec<cynic::Id>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "NodesVariables")]
pub struct RollupsQuery {
    #[arguments(ids: $ids)]
    pub nodes: Vec<Option<RollupNode>>,
}

#[derive(cynic::InlineFragments, Debug)]
#[cynic(graphql_type = "Node")]
pub enum RollupNode {
    PullRequest(PrRollup),
    #[cynic(fallback)]
    Other,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest")]
pub struct PrRollup {
    pub id: cynic::Id,
    #[arguments(last: 1)]
    pub commits: CommitRollupConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest")]
pub struct PrSummary {
    pub title: String,
    #[cynic(spread)]
    pub status: crate::api::model::PrStatus,
    pub updated_at: DateTime,
    pub author: Option<Actor>,
    pub additions: i32,
    pub deletions: i32,
    pub review_decision: Option<ReviewDecision>,
    #[arguments(last: 1)]
    pub commits: CommitRollupConnection,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct Actor {
    pub login: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository")]
pub struct RepositoryName {
    pub name_with_owner: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Milestone")]
pub struct MilestoneName {
    pub number: i32,
    pub title: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestCommit")]
pub struct CommitRollupNode {
    pub commit: CommitRollup,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Commit")]
pub struct CommitRollup {
    pub status_check_rollup: Option<StatusCheckRollup>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct StatusCheckRollup {
    pub state: StatusState,
}

#[derive(cynic::Enum, Debug, Clone, Copy)]
pub enum PullRequestState {
    Open,
    Closed,
    Merged,
}

#[derive(cynic::Enum, Debug, Clone, Copy)]
pub enum StatusState {
    Error,
    Expected,
    Failure,
    Pending,
    Success,
}

// ---- one pull request ------------------------------------------------------

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "NumberVariables")]
pub struct PullRequestQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryWithPr>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "NumberVariables")]
pub struct RepositoryWithPr {
    #[arguments(number: $number)]
    pub pull_request: Option<PrDetail>,
    pub merge_commit_allowed: bool,
    pub squash_merge_allowed: bool,
    pub rebase_merge_allowed: bool,
    pub viewer_default_merge_method: crate::api::change::MergeMethod,
    pub viewer_permission: Option<RepositoryPermission>,
    pub auto_merge_allowed: bool,
}

/// What you may do in a repository.
#[derive(cynic::Enum, Debug, Clone, Copy, PartialEq, Eq)]
pub enum RepositoryPermission {
    Admin,
    Maintain,
    Write,
    TriagePlus,
    Triage,
    Read,
}

impl RepositoryPermission {
    /// It may push, merge, and re-run or cancel workflows.
    pub fn writes(permission: Option<Self>) -> bool {
        matches!(permission, Some(Self::Admin | Self::Maintain | Self::Write))
    }
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest")]
pub struct PrDetail {
    pub id: cynic::Id,
    #[cynic(spread)]
    pub summary: PrSummary,
    pub body: String,
    pub created_at: DateTime,
    pub base_ref_name: String,
    pub head_ref_name: String,
    pub base_ref_oid: GitObjectId,
    pub head_ref_oid: GitObjectId,
    pub head_repository: Option<RepositoryName>,
    pub changed_files: i32,
    pub mergeable: Mergeable,
    pub merge_state_status: MergeState,
    pub viewer_can_close: bool,
    pub viewer_can_reopen: bool,
    pub viewer_can_update: bool,
    pub viewer_can_merge_as_admin: bool,
    pub viewer_did_author: bool,
    pub maintainer_can_modify: bool,
    pub is_cross_repository: bool,
    pub viewer_can_enable_auto_merge: bool,
    pub viewer_can_disable_auto_merge: bool,
    pub auto_merge_request: Option<AutoMergeRequest>,
    pub is_merge_queue_enabled: bool,
    pub merge_queue_entry: Option<MergeQueueEntry>,
    #[arguments(first: 20)]
    pub labels: Option<LabelConnection>,
    pub milestone: Option<MilestoneName>,
    pub viewer_can_react: bool,
    pub viewer_can_label: bool,
    pub viewer_can_assign: bool,
    pub reaction_groups: Option<Vec<crate::api::edit::WireReactionGroup>>,
    #[arguments(first: 10)]
    pub assignees: crate::api::browse::Assignees,
    pub head_ref: Option<RefId>,
    pub viewer_can_delete_head_ref: bool,
    pub locked: bool,
    pub active_lock_reason: Option<crate::api::timeline::LockReason>,
}

/// A branch or tag, by its node ID.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Ref")]
pub struct RefId {
    pub id: cynic::Id,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct AutoMergeRequest {
    pub enabled_by: Option<Actor>,
    pub merge_method: crate::api::change::MergeMethod,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct MergeQueueEntry {
    pub position: i32,
    pub state: crate::api::model::QueueState,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct Label {
    pub name: String,
    pub color: String,
}

/// How far a branch is behind another.
#[derive(cynic::QueryVariables, Debug)]
pub struct BehindVariables {
    pub owner: String,
    pub name: String,
    /// `refs/heads/main`.
    pub base: String,
    /// A commit or a branch (`owner:branch` in a fork).
    pub head: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "BehindVariables")]
pub struct BehindQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryBehind>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "BehindVariables")]
pub struct RepositoryBehind {
    #[arguments(qualifiedName: $base)]
    #[cynic(rename = "ref")]
    pub base: Option<RefCompare>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Ref", variables = "BehindVariables")]
pub struct RefCompare {
    #[arguments(headRef: $head)]
    pub compare: Option<Behind>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Comparison")]
pub struct Behind {
    pub behind_by: i32,
}

// ---- viewed files ------------------------------------------------------------

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "PageVariables")]
pub struct PrFilesQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryWithPrFiles>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "PageVariables")]
pub struct RepositoryWithPrFiles {
    #[arguments(number: $number)]
    pub pull_request: Option<PrFiles>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest", variables = "PageVariables")]
pub struct PrFiles {
    pub id: cynic::Id,
    #[arguments(first: 100, after: $after)]
    pub files: Option<PrChangedFiles>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestChangedFileConnection")]
pub struct PrChangedFiles {
    pub page_info: PageInfo,
    pub nodes: Option<Vec<Option<PrChangedFile>>>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct PageInfo {
    pub has_next_page: bool,
    pub end_cursor: Option<String>,
}

impl PageInfo {
    /// The cursor for the next page, if there is one.
    pub fn next(self) -> Option<String> {
        self.end_cursor.filter(|_| self.has_next_page)
    }
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestChangedFile")]
pub struct PrChangedFile {
    pub path: String,
    pub viewer_viewed_state: ViewedState,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ViewedVariables {
    pub pull_request_id: cynic::Id,
    pub path: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "ViewedVariables")]
pub struct MarkFileAsViewed {
    #[arguments(input: { pullRequestId: $pull_request_id, path: $path })]
    pub mark_file_as_viewed: Option<MarkFileAsViewedPayload>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "ViewedVariables")]
pub struct UnmarkFileAsViewed {
    #[arguments(input: { pullRequestId: $pull_request_id, path: $path })]
    pub unmark_file_as_viewed: Option<UnmarkFileAsViewedPayload>,
}

// ---- review threads ----------------------------------------------------------

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "PageVariables")]
pub struct ThreadsQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryWithThreads>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "PageVariables")]
pub struct RepositoryWithThreads {
    #[arguments(number: $number)]
    pub pull_request: Option<PrThreads>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest", variables = "PageVariables")]
pub struct PrThreads {
    #[arguments(first: 100, after: $after)]
    pub review_threads: ThreadConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReviewThreadConnection")]
pub struct ThreadConnection {
    pub page_info: PageInfo,
    pub nodes: Option<Vec<Option<ReviewThread>>>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReviewThread")]
pub struct ReviewThread {
    pub id: cynic::Id,
    pub path: String,
    pub diff_side: Side,
    pub start_diff_side: Option<Side>,
    pub line: Option<i32>,
    pub start_line: Option<i32>,
    pub original_line: Option<i32>,
    pub original_start_line: Option<i32>,
    pub is_outdated: bool,
    pub is_resolved: bool,
    pub subject_type: ThreadSubjectType,
    pub viewer_can_reply: bool,
    pub viewer_can_resolve: bool,
    pub viewer_can_unresolve: bool,
    /// The first comment: it's on the line the thread is about.
    #[arguments(first: 1)]
    #[cynic(rename = "comments", alias)]
    pub first_comment: FirstCommentConnection,
    /// The newest replies, oldest first; with how many there are in all.
    #[arguments(last: 99)]
    pub comments: ReviewCommentConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReviewComment")]
pub struct ReviewComment {
    pub id: cynic::Id,
    pub author: Option<Actor>,
    pub body: String,
    pub created_at: DateTime,
    pub url: Uri,
    pub original_commit: Option<CommitOid>,
    pub state: ReviewCommentState,
    pub viewer_can_update: bool,
    pub viewer_can_delete: bool,
    pub viewer_can_react: bool,
    pub reaction_groups: Option<Vec<crate::api::edit::WireReactionGroup>>,
    /// The review that made it.
    pub pull_request_review: Option<ReviewDatabaseId>,
}

/// A thread's first comment, with the diff it was made on: the lines
/// it's about come last.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReviewComment")]
pub struct FirstComment {
    #[cynic(spread)]
    pub comment: ReviewComment,
    pub diff_hunk: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReview")]
pub struct ReviewDatabaseId {
    /// `databaseId` overflows GraphQL's `Int` on newer reviews.
    pub full_database_id: Option<BigInt>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Commit")]
pub struct CommitOid {
    pub oid: GitObjectId,
}

/// Your pending review, and the commit it's on.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReview")]
pub struct PendingReview {
    pub id: cynic::Id,
    pub commit: Option<CommitOid>,
    /// Comments in it, wherever they were written.
    pub comments: ReviewCommentCount,
}

#[derive(cynic::Enum, Debug, Clone, Copy, PartialEq, Eq)]
#[cynic(graphql_type = "PullRequestReviewCommentState")]
pub enum ReviewCommentState {
    Pending,
    Submitted,
}

#[derive(cynic::Enum, Debug, Clone, Copy, PartialEq, Eq)]
#[cynic(graphql_type = "PullRequestReviewThreadSubjectType")]
pub enum ThreadSubjectType {
    File,
    Line,
}

// ---- pending review ---------------------------------------------------------

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "NumberVariables")]
pub struct PendingReviewQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryWithPendingReview>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "NumberVariables")]
pub struct RepositoryWithPendingReview {
    #[arguments(number: $number)]
    pub pull_request: Option<PrPendingReview>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest")]
pub struct PrPendingReview {
    pub id: cynic::Id,
    /// Only the viewer's own pending review is visible.
    #[arguments(states: [PENDING], first: 1)]
    pub reviews: Option<PendingReviewConnection>,
}

// ---- review mutations --------------------------------------------------------

#[derive(cynic::QueryVariables, Debug)]
pub struct StartReviewVariables {
    pub pull_request_id: cynic::Id,
    pub commit: GitObjectId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "StartReviewVariables")]
pub struct StartReview {
    #[arguments(input: { pullRequestId: $pull_request_id, commitOID: $commit })]
    pub add_pull_request_review: Option<StartReviewPayload>,
}

#[derive(cynic::InputObject, Debug)]
#[cynic(
    graphql_type = "AddPullRequestReviewThreadInput",
    rename_all = "camelCase"
)]
pub struct AddThreadInput {
    pub pull_request_review_id: Option<cynic::Id>,
    pub path: Option<String>,
    pub body: String,
    #[cynic(skip_serializing_if = "Option::is_none")]
    pub line: Option<i32>,
    #[cynic(skip_serializing_if = "Option::is_none")]
    pub side: Option<Side>,
    #[cynic(skip_serializing_if = "Option::is_none")]
    pub start_line: Option<i32>,
    #[cynic(skip_serializing_if = "Option::is_none")]
    pub start_side: Option<Side>,
    pub subject_type: Option<ThreadSubjectType>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct AddThreadVariables {
    pub input: AddThreadInput,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "AddThreadVariables")]
pub struct AddThread {
    #[arguments(input: $input)]
    pub add_pull_request_review_thread: Option<ThreadPayload>,
}

#[derive(cynic::Enum, Debug, Clone, Copy, PartialEq, Eq)]
#[cynic(graphql_type = "PullRequestReviewEvent")]
pub enum ReviewEvent {
    Approve,
    Comment,
    Dismiss,
    RequestChanges,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct SubmitReviewVariables {
    pub review_id: cynic::Id,
    pub event: ReviewEvent,
    pub body: Option<String>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "SubmitReviewVariables")]
pub struct SubmitReview {
    #[arguments(input: { pullRequestReviewId: $review_id, event: $event, body: $body })]
    pub submit_pull_request_review: Option<SubmitReviewPayload>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ReplyVariables {
    pub thread_id: cynic::Id,
    pub body: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "ReplyVariables")]
pub struct Reply {
    #[arguments(input: { pullRequestReviewThreadId: $thread_id, body: $body })]
    pub add_pull_request_review_thread_reply: Option<ReplyPayload>,
}

fragments! {
    comment: Option<CommentId>;
    ReplyPayload = "AddPullRequestReviewThreadReplyPayload",
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ThreadIdVariables {
    pub thread_id: cynic::Id,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "ThreadIdVariables")]
pub struct Resolve {
    #[arguments(input: { threadId: $thread_id })]
    pub resolve_review_thread: Option<ResolvePayload>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "ThreadIdVariables")]
pub struct Unresolve {
    #[arguments(input: { threadId: $thread_id })]
    pub unresolve_review_thread: Option<UnresolvePayload>,
}

// ---- the viewer's last review --------------------------------------------------

#[derive(cynic::QueryVariables, Debug)]
pub struct LastReviewVariables {
    pub owner: String,
    pub name: String,
    pub number: i32,
    pub login: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "LastReviewVariables")]
pub struct LastReviewQuery {
    #[arguments(owner: $owner, name: $name)]
    pub repository: Option<RepositoryWithReviews>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Repository", variables = "LastReviewVariables")]
pub struct RepositoryWithReviews {
    #[arguments(number: $number)]
    pub pull_request: Option<PrReviews>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequest", variables = "LastReviewVariables")]
pub struct PrReviews {
    #[arguments(author: $login, last: 20)]
    pub reviews: Option<ReviewCommitConnection>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "PullRequestReview")]
pub struct ReviewCommit {
    pub state: ReviewState,
    pub commit: Option<CommitOid>,
}