gitpoulpi-hosting 1.1.1

Hosting and issue-tracker API clients (GitHub, GitLab, Gitea/Forgejo, Azure DevOps, Bitbucket, Gerrit, Jira, Trello, Linear, YouTrack, Redmine, local Markdown issues): project listing, clone helpers, SSH key generation and upload, remote-URL provider matching.
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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
//! The GitHub client (github.com and GitHub Enterprise Server).

use std::sync::Arc;

use crate::client::{
    encode_component, fetch_pages, fetch_pages_with, CreatedIssue, CreatedPullRequest,
    HostingClient, NewIssue, NewPullRequest, PAGE_SIZE,
};
use crate::conversation::{
    Comment, ConversationCaps, ItemKey, ItemKind, PageCursor, PostingIdentity, TimelineOpts,
    TimelinePage,
};
use crate::http::{HttpRequest, HttpResponse, HttpTransport};
use crate::integration::Integration;
use crate::metadata::{HostedLabel, HostedMilestone, IssueEdit, ItemMeta, MetaKind, PrEdit};
use crate::model::{
    best_pull_request, excess_scopes, missing_scopes, ChecksState, HostedUser, IssueFilter,
    IssueState, IssueSummary, PrFilter, PullRequestState, PullRequestSummary, StateFilter,
    TokenCheck, TokenFlavor,
};
use crate::project::{HostedProject, HostedSshKey};
use crate::{Error, Result};

mod conversation;
#[cfg(test)]
mod conversation_tests;
mod protection;

pub struct GitHubClient {
    pub(crate) integration: Integration,
    token: String,
    pub(crate) transport: std::sync::Arc<dyn HttpTransport>,
    pub(crate) scopes: crate::scope_handle::ScopeHandle,
}

impl GitHubClient {
    pub(crate) fn new(
        integration: Integration,
        token: String,
        transport: Arc<dyn HttpTransport>,
    ) -> Self {
        Self {
            integration,
            token,
            transport,
            scopes: Default::default(),
        }
    }

    /// The headers every GitHub request carries. `Bearer` is what GitHub
    /// documents for fine-grained tokens and accepts for classic ones;
    /// `token <x>` works for neither fine-grained nor GitHub App tokens.
    /// Pinning the API version stops a future default from reshaping the
    /// JSON under a released GitPoulpi.
    pub(crate) fn authed(&self, request: HttpRequest) -> HttpRequest {
        request
            .header("Authorization", format!("Bearer {}", self.token))
            .header("Accept", "application/vnd.github+json")
            .header("X-GitHub-Api-Version", "2022-11-28")
    }

    pub(crate) fn delete(&self, url: String) -> HttpRequest {
        self.authed(HttpRequest::delete(url))
    }

    pub(crate) fn get(&self, url: String) -> HttpRequest {
        self.authed(HttpRequest::get(url))
    }

    pub(crate) fn post(&self, url: String, body: &serde_json::Value) -> HttpRequest {
        self.authed(HttpRequest::post_json(url, body))
    }
}

#[derive(serde::Deserialize)]
struct GitHubRepo {
    full_name: String,
    name: String,
    description: Option<String>,
    default_branch: Option<String>,
    ssh_url: Option<String>,
    clone_url: Option<String>,
    html_url: Option<String>,
    #[serde(default)]
    private: bool,
}

#[derive(serde::Deserialize)]
struct GitHubKey {
    id: i64,
    title: String,
    key: String,
}

#[derive(serde::Deserialize)]
struct GitHubPull {
    number: u64,
    html_url: String,
}

#[async_trait::async_trait]
impl HostingClient for GitHubClient {
    fn integration(&self) -> &Integration {
        &self.integration
    }

    async fn list_projects(&self) -> Result<Vec<HostedProject>> {
        // `/user/repos` rather than `/users/<login>/repos`: the former is
        // what the *token* can see, including private repositories and
        // ones owned by organisations; the latter shows only public ones.
        let url = format!(
            "{}/user/repos?sort=pushed&affiliation=owner,collaborator,organization_member",
            self.integration.api_base()
        );
        let repos: Vec<GitHubRepo> = fetch_pages(
            &*self.transport,
            |u| self.get(u),
            &url,
            "listing GitHub repositories",
        )
        .await?;
        Ok(repos
            .into_iter()
            .map(|r| HostedProject {
                integration_id: self.integration.id.clone(),
                id: r.full_name.clone(),
                name: r.name,
                full_name: r.full_name,
                description: r.description,
                default_branch: r.default_branch,
                ssh_url: r.ssh_url,
                https_url: r.clone_url,
                web_url: r.html_url,
                private: r.private,
            })
            .collect())
    }

    async fn list_ssh_keys(&self) -> Result<Vec<HostedSshKey>> {
        let url = format!(
            "{}/user/keys?per_page={PAGE_SIZE}",
            self.integration.api_base()
        );
        let response = self.transport.send(self.get(url)).await?;
        response.error_for_status("listing GitHub SSH keys")?;
        let keys: Vec<GitHubKey> = response.json()?;
        Ok(keys
            .into_iter()
            .map(|k| HostedSshKey {
                id: k.id.to_string(),
                title: k.title,
                key: k.key,
            })
            .collect())
    }

    async fn upload_ssh_key(&self, title: &str, public_key: &str) -> Result<HostedSshKey> {
        let url = format!("{}/user/keys", self.integration.api_base());
        let body = serde_json::json!({ "title": title, "key": public_key.trim() });
        let response = self.transport.send(self.post(url, &body)).await?;
        response.error_for_status("uploading an SSH key to GitHub")?;
        let key: GitHubKey = response.json()?;
        Ok(HostedSshKey {
            id: key.id.to_string(),
            title: key.title,
            key: key.key,
        })
    }

    async fn create_pull_request(&self, req: &NewPullRequest) -> Result<CreatedPullRequest> {
        // `{p}/pulls`, not `/repos/{p}/pulls`+something else: `project`
        // is already `owner/name`, exactly what this endpoint wants.
        let url = format!(
            "{}/repos/{}/pulls",
            self.integration.api_base(),
            req.project
        );
        // A fork's branch is addressed `owner:branch`.
        let head = match req.fork_source() {
            Some(fork) => format!(
                "{}:{}",
                fork.split('/').next().unwrap_or(fork),
                req.source_branch
            ),
            None => req.source_branch.clone(),
        };
        let body = serde_json::json!({
            "title": req.title,
            "head": head,
            "base": req.target_branch,
            "body": req.body,
            "draft": req.draft,
        });
        let response = self.transport.send(self.post(url, &body)).await?;
        response.error_for_status("opening a GitHub pull request")?;
        let pr: GitHubPull = response.json()?;
        // The pulls endpoint takes neither assignees, labels, a milestone
        // nor reviewers: they follow in a second round. A failure there
        // must not hide that the request exists.
        let warning = self.apply_new_pr_meta(req, pr.number).await.err().map(|e| {
            format!("the pull request was opened, but its metadata could not be set: {e}")
        });
        Ok(CreatedPullRequest {
            number: pr.number,
            web_url: pr.html_url,
            warning,
        })
    }

    async fn create_issue_branch(
        &self,
        req: &crate::issue_flow::NewIssueBranch,
    ) -> Result<crate::issue_flow::CreatedBranch> {
        self.flow_create_branch(req).await
    }

    async fn issue_links(
        &self,
        project: &str,
        number: u64,
    ) -> Result<crate::issue_flow::IssueLinks> {
        self.flow_issue_links(project, number).await
    }

    async fn branch_names(&self, project: &str, search: &str) -> Result<Vec<String>> {
        self.flow_branch_names(project, search).await
    }

    async fn account_items(
        &self,
        query: &crate::account::AccountQuery,
    ) -> Result<crate::account::AccountItems> {
        crate::account::github::fetch(self, query).await
    }

    async fn current_user(&self) -> Result<HostedUser> {
        Ok(self.fetch_user().await?.0)
    }

    async fn protected_branches(
        &self,
        project: &str,
    ) -> Result<crate::protection::ProtectedBranches> {
        self.protection(project).await
    }

    async fn check_token(&self) -> Result<TokenCheck> {
        let (user, response) = self.fetch_user().await?;
        // Classic tokens list their scopes in `X-OAuth-Scopes`. A
        // fine-grained token sends the header empty or not at all, which
        // means *unknown* -- reporting every scope as missing would tell
        // a working token it is broken.
        let scopes = header_value(&response, "x-oauth-scopes")
            .map(|raw| {
                raw.split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect::<Vec<_>>()
            })
            .filter(|scopes| !scopes.is_empty());
        let required = self
            .integration
            .kind
            .token_help()
            .map(|h| h.scopes)
            .unwrap_or(&[]);
        let missing = scopes
            .as_deref()
            .map(|granted| missing_scopes(required, granted))
            .unwrap_or_default();
        // Optional scopes a feature can use: not an excess.
        let excess = scopes
            .as_deref()
            .map(|granted| excess_scopes(required, &["read:org", "workflow"], granted))
            .unwrap_or_default();
        // The prefix says what the header cannot: a fine-grained token has no
        // scope list to read, a `ghp_` one is classic.
        let flavor = if self.token.starts_with("github_pat_") {
            TokenFlavor::FineGrained
        } else if self.token.starts_with("ghp_") || scopes.is_some() {
            TokenFlavor::Classic
        } else {
            TokenFlavor::Unknown
        };
        Ok(TokenCheck {
            user,
            scopes,
            missing_scopes: missing,
            expires_at: header_value(&response, "github-authentication-token-expiration"),
            excess_scopes: excess,
            token_flavor: Some(flavor),
            recommend_fine_grained: self.token.starts_with("ghp_"),
        })
    }

    async fn list_pull_requests(
        &self,
        project: &str,
        filter: &PrFilter,
    ) -> Result<Vec<PullRequestSummary>> {
        let base = self.integration.api_base();
        let state = match filter.state {
            StateFilter::Open => "open",
            StateFilter::Closed => "closed",
            StateFilter::All => "all",
        };
        let url = format!("{base}/repos/{project}/pulls?state={state}&sort=updated&direction=desc");
        let pulls: Vec<GitHubPullRow> = fetch_pages(
            &*self.transport,
            |u| self.get(u),
            &url,
            "listing GitHub pull requests",
        )
        .await?;

        // Which of them await the current user's review comes from the
        // search API: the pulls listing only carries the *requested
        // reviewers*, and matching those against a login would need
        // team expansion GitHub does server-side here.
        let awaiting: std::collections::HashSet<u64> = if filter.review_requested {
            self.review_requested_numbers(project).await?
        } else {
            Default::default()
        };
        let me = if filter.mine {
            Some(self.login().await?)
        } else {
            None
        };
        let mentioning = if filter.mentioned {
            Some(self.mentioned_numbers(project, true, filter.state).await?)
        } else {
            None
        };

        let mut rows: Vec<(PullRequestSummary, String)> = pulls
            .into_iter()
            .filter(|p| !filter.review_requested || awaiting.contains(&p.number))
            .filter(|p| {
                mentioning
                    .as_ref()
                    .is_none_or(|set| set.contains(&p.number))
            })
            .filter(|p| {
                me.as_deref().is_none_or(|me| {
                    p.user
                        .as_ref()
                        .is_some_and(|u| u.login.eq_ignore_ascii_case(me))
                })
            })
            .map(|p| {
                let review_requested = awaiting.contains(&p.number);
                let sha = p.head.sha.clone();
                (p.into_summary(review_requested), sha)
            })
            .collect();
        if filter.checks {
            self.fill_status(project, &mut rows).await;
        }
        Ok(rows.into_iter().map(|(summary, _)| summary).collect())
    }

    async fn pull_request_for_branch(
        &self,
        project: &str,
        branch: &str,
    ) -> Result<Option<PullRequestSummary>> {
        let owner = project.split('/').next().unwrap_or(project);
        let url = format!(
            "{}/repos/{project}/pulls?state=all&head={}",
            self.integration.api_base(),
            encode_component(&format!("{owner}:{branch}"))
        );
        let pulls: Vec<GitHubPullRow> = fetch_pages(
            &*self.transport,
            |u| self.get(u),
            &url,
            "looking up the pull request for a branch",
        )
        .await?;
        Ok(best_pull_request(
            pulls.into_iter().map(|p| p.into_summary(false)).collect(),
        ))
    }

    async fn list_issues(&self, project: &str, filter: &IssueFilter) -> Result<Vec<IssueSummary>> {
        let state = match filter.state {
            StateFilter::Open => "open",
            StateFilter::Closed => "closed",
            StateFilter::All => "all",
        };
        let mut url = format!(
            "{}/repos/{project}/issues?state={state}&sort=updated&direction=desc",
            self.integration.api_base()
        );
        if filter.assigned_to_me {
            url.push_str(&format!(
                "&assignee={}",
                encode_component(&self.login().await?)
            ));
        }
        let rows: Vec<GitHubIssueRow> = fetch_pages(
            &*self.transport,
            |u| self.get(u),
            &url,
            "listing GitHub issues",
        )
        .await?;
        let mentioning = if filter.mentioned {
            Some(self.mentioned_numbers(project, false, filter.state).await?)
        } else {
            None
        };
        // This endpoint returns pull requests too, flagged by a
        // `pull_request` key; they belong to the PR list, not here.
        Ok(rows
            .into_iter()
            .filter(|r| r.pull_request.is_none())
            .filter(|r| {
                mentioning
                    .as_ref()
                    .is_none_or(|set| set.contains(&r.number))
            })
            .map(|r| IssueSummary {
                number: r.number,
                id: r.number.to_string(),
                reference: format!("#{}", r.number),
                iid_label: format!("#{}", r.number),
                title: r.title,
                state: if r.state == "closed" {
                    IssueState::Closed
                } else {
                    IssueState::Open
                },
                author: r.user.map(|u| u.login).unwrap_or_default(),
                assignees: r.assignees.into_iter().map(|u| u.login).collect(),
                labels: r.labels.into_iter().map(|l| l.name).collect(),
                web_url: r.html_url,
                updated_at: r.updated_at,
                milestone: r.milestone.map(|m| m.title).filter(|t| !t.is_empty()),
                ..IssueSummary::default()
            })
            .collect())
    }

    async fn create_issue(&self, req: &NewIssue) -> Result<CreatedIssue> {
        let url = format!(
            "{}/repos/{}/issues",
            self.integration.api_base(),
            req.project
        );
        let mut body = serde_json::json!({ "title": req.title, "body": req.body });
        if !req.labels.is_empty() {
            body["labels"] = serde_json::json!(req.labels);
        }
        if !req.assignees.is_empty() {
            body["assignees"] = serde_json::json!(req.assignees);
        }
        if let Some(milestone) = req.milestone {
            body["milestone"] = serde_json::json!(milestone);
        }
        let response = self.transport.send(self.post(url, &body)).await?;
        response.error_for_status("opening a GitHub issue")?;
        let issue: GitHubCreated = response.json()?;
        Ok(CreatedIssue {
            number: issue.number,
            web_url: issue.html_url,
            reference: format!("#{}", issue.number),
        })
    }

    async fn delete_ssh_key(&self, id: &str) -> Result<()> {
        let url = format!("{}/user/keys/{id}", self.integration.api_base());
        let response = self.transport.send(self.delete(url)).await?;
        response.error_for_status("deleting a GitHub SSH key")
    }

    fn review_capabilities(&self) -> crate::review::ReviewCapabilities {
        crate::review::github::CAPABILITIES
    }

    fn conversation_caps(&self, kind: ItemKind) -> ConversationCaps {
        ConversationCaps::github(kind)
    }

    async fn posting_identity(&self, project: &str) -> Result<PostingIdentity> {
        self.conv_identity(project).await
    }

    async fn item_timeline(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
        opts: &TimelineOpts,
    ) -> Result<TimelinePage> {
        self.conv_timeline(project, key, older, opts).await
    }

    async fn post_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        body: &str,
        internal: bool,
    ) -> Result<Comment> {
        self.conv_post(project, key, body, internal).await
    }

    async fn reply_in_discussion(
        &self,
        project: &str,
        key: &ItemKey,
        discussion_id: &str,
        body: &str,
    ) -> Result<Comment> {
        self.conv_reply(project, key, discussion_id, body).await
    }

    async fn edit_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
        body: &str,
    ) -> Result<Comment> {
        self.conv_edit(project, key, comment_id, body).await
    }

    async fn delete_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
    ) -> Result<()> {
        self.conv_delete(project, key, comment_id).await
    }

    async fn pull_request_detail(
        &self,
        project: &str,
        number: u64,
    ) -> Result<crate::review::PullRequestDetail> {
        self.review_detail(project, number).await
    }

    async fn pull_request_files(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<crate::review::ReviewFile>> {
        self.review_files(project, number).await
    }

    async fn pull_request_threads(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<crate::review::ReviewThread>> {
        self.review_threads(project, number).await
    }

    async fn pull_request_conversation(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<crate::review::ReviewComment>> {
        self.review_conversation(project, number).await
    }

    async fn pull_request_reviews(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<crate::review::SubmittedReview>> {
        self.review_list_reviews(project, number).await
    }

    async fn add_comment(
        &self,
        project: &str,
        number: u64,
        body: &str,
    ) -> Result<crate::review::ReviewComment> {
        let c = self
            .conv_post(project, &ItemKey::pull_request(number), body, false)
            .await?;
        Ok(crate::review::ReviewComment {
            id: c.id,
            author: c.author.login,
            body: c.body.text,
            created_at: c.created_at,
            in_reply_to: None,
            web_url: c.web_url,
        })
    }

    async fn reply_to_thread(
        &self,
        _project: &str,
        _number: u64,
        thread_id: &str,
        body: &str,
    ) -> Result<crate::review::ReviewComment> {
        self.review_reply(thread_id, body).await
    }

    async fn resolve_thread(
        &self,
        _project: &str,
        _number: u64,
        thread_id: &str,
        resolved: bool,
    ) -> Result<()> {
        self.review_resolve(thread_id, resolved).await
    }

    async fn submit_review(
        &self,
        project: &str,
        number: u64,
        review: &crate::review::ReviewSubmission,
    ) -> Result<crate::review::ReviewOutcome> {
        self.review_submit(project, number, review).await
    }

    async fn project_members(&self, project: &str, query: &str) -> Result<Vec<HostedUser>> {
        self.meta_members(project, query).await
    }

    async fn project_labels(&self, project: &str) -> Result<Vec<HostedLabel>> {
        self.meta_labels(project).await
    }

    async fn project_milestones(&self, project: &str) -> Result<Vec<HostedMilestone>> {
        self.meta_milestones(project).await
    }

    async fn item_meta(&self, project: &str, number: u64, kind: MetaKind) -> Result<ItemMeta> {
        self.meta_item(project, number, kind).await
    }

    async fn update_issue(&self, project: &str, number: u64, edit: &IssueEdit) -> Result<()> {
        self.meta_update_issue(project, number, edit).await
    }

    fn board_source(&self) -> crate::board::BoardSource {
        crate::board::BoardSource::Labels
    }

    async fn set_issue_state(&self, project: &str, number: u64, closed: bool) -> Result<()> {
        self.meta_set_issue_state(project, number, closed).await
    }

    async fn update_pull_request(&self, project: &str, number: u64, edit: &PrEdit) -> Result<()> {
        self.meta_update_pull_request(project, number, edit).await
    }

    async fn pull_request_diff(
        &self,
        project: &str,
        number: u64,
        _patch_set: Option<u32>,
        _compare: Option<u32>,
    ) -> Result<Vec<crate::review::ApiFileDiff>> {
        self.review_diff(project, number).await
    }

    async fn pull_request_commits(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<crate::review::ApiCommit>> {
        self.review_commits(project, number).await
    }

    async fn commit_diff(
        &self,
        project: &str,
        sha: &str,
    ) -> Result<Vec<crate::review::ApiFileDiff>> {
        self.review_commit_diff(project, sha).await
    }

    async fn file_content(
        &self,
        project: &str,
        path: &str,
        rev: Option<&str>,
    ) -> Result<Option<Vec<u8>>> {
        self.review_file(project, path, rev).await
    }
}

impl GitHubClient {
    /// The metadata a new pull request was asked to carry.
    async fn apply_new_pr_meta(&self, req: &NewPullRequest, number: u64) -> Result<()> {
        let assignees = (!req.assignees.is_empty()).then_some(req.assignees.as_slice());
        let labels = (!req.labels.is_empty()).then_some(req.labels.as_slice());
        self.patch_issue_fields(
            &req.project,
            number,
            assignees,
            labels,
            req.milestone.map(Some),
        )
        .await?;
        self.change_reviewers(&req.project, number, &req.reviewers, &[])
            .await
    }

    async fn fetch_user(&self) -> Result<(HostedUser, HttpResponse)> {
        // An App installation token has no user behind it: there is no
        // "me" to ask for, so do not ask.
        if crate::scope::is_app_installation_token(&self.token) {
            return Err(Error::BotToken);
        }
        let url = format!("{}/user", self.integration.api_base());
        let response = self.transport.send(self.get(url)).await?;
        // The same token seen from a host that answers instead: `/user` is
        // 403 or 404 for an installation token.
        if matches!(response.status, 403 | 404) && !crate::conditional::is_rate_limited(&response) {
            return Err(Error::BotToken);
        }
        response.error_for_status("checking the GitHub token")?;
        let row: GitHubUser = response.json()?;
        let user = HostedUser {
            login: row.login,
            name: row.name.unwrap_or_default(),
            id: row.id.to_string(),
            web_url: row.html_url,
        };
        Ok((user, response))
    }

    /// The cached login when the profile has one, else a `/user` call.
    pub(crate) async fn login(&self) -> Result<String> {
        if let Some(user) = &self.integration.user {
            if !user.login.is_empty() {
                return Ok(user.login.clone());
            }
        }
        Ok(self.fetch_user().await?.0.login)
    }

    /// The numbers of the open (per `state`) pull requests or issues of
    /// `project` that mention the current user: the search API's
    /// `mentions:` qualifier, which the list endpoints have no twin for.
    async fn mentioned_numbers(
        &self,
        project: &str,
        pull_requests: bool,
        state: StateFilter,
    ) -> Result<std::collections::HashSet<u64>> {
        #[derive(serde::Deserialize)]
        struct Search {
            #[serde(default)]
            items: Vec<Item>,
        }
        #[derive(serde::Deserialize)]
        struct Item {
            number: u64,
        }
        let kind = if pull_requests { "is:pr" } else { "is:issue" };
        let state = match state {
            StateFilter::Open => " is:open",
            StateFilter::Closed => " is:closed",
            StateFilter::All => "",
        };
        let login = self.login().await?;
        let query = encode_component(&format!("{kind}{state} mentions:{login} repo:{project}"));
        let url = format!("{}/search/issues?q={query}", self.integration.api_base());
        let items: Vec<Item> = fetch_pages_with(
            &*self.transport,
            |u| self.get(u),
            &url,
            "searching mentions on GitHub",
            |r| Ok(r.json::<Search>()?.items),
        )
        .await?;
        Ok(items.into_iter().map(|i| i.number).collect())
    }

    /// Fills `checks` and `has_conflicts` of the live requests: the list
    /// endpoint carries neither, so each costs three calls (the request for
    /// `mergeable_state`, the head's combined status, its check runs). At most
    /// [`STATUS_FANOUT`] requests are asked, the user's own first; a failing
    /// call (a rate limit, a token without the scope) ends the fan-out and
    /// leaves the rest unknown rather than failing the list.
    async fn fill_status(&self, project: &str, rows: &mut [(PullRequestSummary, String)]) {
        let me = self.login().await.unwrap_or_default();
        let mut order: Vec<usize> = (0..rows.len())
            .filter(|&i| {
                matches!(
                    rows[i].0.state,
                    PullRequestState::Open | PullRequestState::Draft
                ) && !rows[i].1.is_empty()
            })
            .collect();
        order.sort_by_key(|&i| !rows[i].0.author.eq_ignore_ascii_case(&me));
        order.truncate(STATUS_FANOUT);
        for i in order {
            let Ok((checks, conflicts)) = self
                .pull_request_status(project, rows[i].0.number, &rows[i].1)
                .await
            else {
                return;
            };
            rows[i].0.checks = checks;
            rows[i].0.has_conflicts = conflicts;
        }
    }

    async fn pull_request_status(
        &self,
        project: &str,
        number: u64,
        sha: &str,
    ) -> Result<(Option<ChecksState>, Option<bool>)> {
        #[derive(serde::Deserialize)]
        struct Detail {
            mergeable_state: Option<String>,
        }
        #[derive(serde::Deserialize)]
        struct Combined {
            #[serde(default)]
            state: String,
            #[serde(default)]
            total_count: u64,
        }
        #[derive(serde::Deserialize)]
        struct Runs {
            #[serde(default)]
            check_runs: Vec<Run>,
        }
        #[derive(serde::Deserialize)]
        struct Run {
            #[serde(default)]
            status: String,
            conclusion: Option<String>,
        }
        let base = self.integration.api_base();
        let detail: Detail = self
            .get_json(
                format!("{base}/repos/{project}/pulls/{number}"),
                "reading a GitHub pull request",
            )
            .await?;
        let combined: Combined = self
            .get_json(
                format!("{base}/repos/{project}/commits/{sha}/status"),
                "reading GitHub commit statuses",
            )
            .await?;
        let runs: Runs = self
            .get_json(
                format!("{base}/repos/{project}/commits/{sha}/check-runs?per_page=100"),
                "reading GitHub check runs",
            )
            .await?;
        let runs: Vec<(&str, Option<&str>)> = runs
            .check_runs
            .iter()
            .map(|r| (r.status.as_str(), r.conclusion.as_deref()))
            .collect();
        let conflicts = match detail.mergeable_state.as_deref() {
            None | Some("unknown") => None,
            Some(state) => Some(state == "dirty"),
        };
        Ok((
            roll_up_checks(&combined.state, combined.total_count, &runs),
            conflicts,
        ))
    }

    pub(crate) async fn get_json<T: serde::de::DeserializeOwned>(
        &self,
        url: String,
        what: &str,
    ) -> Result<T> {
        let response = self.transport.send(self.get(url)).await?;
        response.error_for_status(what)?;
        response.json()
    }

    async fn review_requested_numbers(
        &self,
        project: &str,
    ) -> Result<std::collections::HashSet<u64>> {
        #[derive(serde::Deserialize)]
        struct Search {
            #[serde(default)]
            items: Vec<Item>,
        }
        #[derive(serde::Deserialize)]
        struct Item {
            number: u64,
        }
        let query = encode_component(&format!(
            "is:pr is:open review-requested:@me repo:{project}"
        ));
        let url = format!("{}/search/issues?q={query}", self.integration.api_base());
        let items: Vec<Item> = fetch_pages_with(
            &*self.transport,
            |u| self.get(u),
            &url,
            "searching review requests on GitHub",
            |r| Ok(r.json::<Search>()?.items),
        )
        .await?;
        Ok(items.into_iter().map(|i| i.number).collect())
    }
}

/// How many requests of one list get their checks and conflict state asked.
const STATUS_FANOUT: usize = 15;

/// One state out of a commit's combined status (`state`, with how many
/// statuses it combines) and its check runs (`status`, `conclusion`): any
/// failure wins, then anything still running, else passing; no status and no
/// run means no CI, which is unknown, not green.
fn roll_up_checks(
    combined: &str,
    combined_total: u64,
    runs: &[(&str, Option<&str>)],
) -> Option<ChecksState> {
    let failed_status = matches!(combined, "failure" | "error") && combined_total > 0;
    let failed_run = runs.iter().any(|(_, conclusion)| {
        matches!(
            conclusion,
            Some("failure" | "timed_out" | "cancelled" | "action_required" | "startup_failure")
        )
    });
    if failed_status || failed_run {
        return Some(ChecksState::Failing);
    }
    let pending_status = combined == "pending" && combined_total > 0;
    let pending_run = runs.iter().any(|(status, _)| *status != "completed");
    if pending_status || pending_run {
        return Some(ChecksState::Pending);
    }
    (combined_total > 0 || !runs.is_empty()).then_some(ChecksState::Passing)
}

fn header_value(response: &HttpResponse, name: &str) -> Option<String> {
    response
        .headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case(name))
        .map(|(_, v)| v.trim().to_string())
        .filter(|v| !v.is_empty())
}

#[derive(serde::Deserialize)]
struct GitHubUser {
    login: String,
    id: i64,
    name: Option<String>,
    html_url: Option<String>,
}

#[derive(serde::Deserialize)]
struct GitHubLogin {
    login: String,
}

#[derive(serde::Deserialize)]
struct GitHubRef {
    #[serde(rename = "ref")]
    name: String,
    #[serde(default)]
    sha: String,
    repo: Option<GitHubRefRepo>,
}

#[derive(serde::Deserialize)]
struct GitHubRefRepo {
    full_name: String,
}

#[derive(serde::Deserialize)]
pub(crate) struct GitHubPullRow {
    number: u64,
    title: String,
    state: String,
    #[serde(default)]
    draft: bool,
    merged_at: Option<String>,
    html_url: String,
    #[serde(default)]
    updated_at: String,
    user: Option<GitHubLogin>,
    head: GitHubRef,
    base: GitHubRef,
    #[serde(default)]
    assignees: Vec<GitHubLogin>,
    #[serde(default)]
    requested_reviewers: Vec<GitHubLogin>,
}

impl GitHubPullRow {
    pub(crate) fn into_summary(self, review_requested: bool) -> PullRequestSummary {
        let state = if self.merged_at.is_some() {
            PullRequestState::Merged
        } else if self.state == "closed" {
            PullRequestState::Closed
        } else if self.draft {
            PullRequestState::Draft
        } else {
            PullRequestState::Open
        };
        let base_repo = self.base.repo.as_ref().map(|r| r.full_name.as_str());
        let source_project = self
            .head
            .repo
            .as_ref()
            .map(|r| r.full_name.clone())
            .filter(|head| Some(head.as_str()) != base_repo);
        PullRequestSummary {
            number: self.number,
            iid_label: format!("#{}", self.number),
            title: self.title,
            state,
            source_branch: self.head.name,
            target_branch: self.base.name,
            source_project,
            author: self.user.map(|u| u.login).unwrap_or_default(),
            web_url: self.html_url,
            updated_at: self.updated_at,
            review_requested,
            assignees: self.assignees.into_iter().map(|u| u.login).collect(),
            reviewers: self
                .requested_reviewers
                .into_iter()
                .map(|u| u.login)
                .collect(),
            checks: None,
            has_conflicts: None,
        }
    }
}

#[derive(serde::Deserialize)]
struct GitHubLabel {
    name: String,
}

#[derive(serde::Deserialize)]
struct GitHubIssueRow {
    number: u64,
    title: String,
    state: String,
    html_url: String,
    #[serde(default)]
    updated_at: String,
    user: Option<GitHubLogin>,
    #[serde(default)]
    assignees: Vec<GitHubLogin>,
    #[serde(default)]
    labels: Vec<GitHubLabel>,
    pull_request: Option<serde_json::Value>,
    milestone: Option<crate::model::MilestoneTitle>,
}

#[derive(serde::Deserialize)]
struct GitHubCreated {
    number: u64,
    html_url: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::client_for;
    use crate::http::RecordedTransport;
    use crate::Error;
    use gitpoulpi_config::IntegrationKind;

    fn block_on<F: std::future::Future>(f: F) -> F::Output {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap()
            .block_on(f)
    }

    fn client(
        kind: IntegrationKind,
        url: &str,
        transport: Arc<RecordedTransport>,
    ) -> Box<dyn HostingClient> {
        client_for(
            Integration::new("gh", kind, url),
            "tok".into(),
            transport,
            &crate::trust::TrustGate::for_tests(),
        )
        .unwrap()
    }

    const USER: &str =
        r#"{"login":"octo","id":9,"name":"Octo Cat","html_url":"https://github.com/octo"}"#;

    fn pull(n: u64, state: &str, extra: &str) -> String {
        format!(
            r#"{{"number":{n},"title":"t{n}","state":"{state}","html_url":"https://github.com/me/thing/pull/{n}",
            "updated_at":"2026-01-0{n}T00:00:00Z","user":{{"login":"octo"}},
            "head":{{"ref":"feature","repo":{{"full_name":"me/thing"}}}},
            "base":{{"ref":"main","repo":{{"full_name":"me/thing"}}}}{extra}}}"#
        )
    }

    #[test]
    fn check_token_reads_scopes_and_reports_missing_ones() {
        let transport = Arc::new(RecordedTransport::new().on_with_headers(
            "/user",
            200,
            USER,
            &[
                ("X-OAuth-Scopes", "repo, read:user"),
                (
                    "github-authentication-token-expiration",
                    "2026-12-01 10:00:00 UTC",
                ),
            ],
        ));
        let c = client(IntegrationKind::Github, "https://github.com", transport);
        let check = block_on(c.check_token()).unwrap();
        assert_eq!(check.user.login, "octo");
        assert_eq!(check.user.name, "Octo Cat");
        assert_eq!(check.user.id, "9");
        assert_eq!(check.missing_scopes, vec!["admin:public_key"]);
        assert_eq!(check.expires_at.as_deref(), Some("2026-12-01 10:00:00 UTC"));
    }

    fn client_with_token(token: &str, transport: Arc<RecordedTransport>) -> Box<dyn HostingClient> {
        client_for(
            Integration::new("gh", IntegrationKind::Github, "https://github.com"),
            token.into(),
            transport,
            &crate::trust::TrustGate::for_tests(),
        )
        .unwrap()
    }

    #[test]
    fn a_classic_token_with_extra_rights_lists_them_dangerous_first() {
        let transport = Arc::new(RecordedTransport::new().on_with_headers(
            "/user",
            200,
            USER,
            &[
                (
                    "X-OAuth-Scopes",
                    "repo, read:user, admin:public_key, gist, delete_repo, repo:status",
                ),
                (
                    "github-authentication-token-expiration",
                    "2026-12-01 10:00:00 UTC",
                ),
            ],
        ));
        let c = client_with_token("ghp_abc", transport);
        let check = block_on(c.check_token()).unwrap();
        assert!(check.missing_scopes.is_empty());
        // `repo:status` is part of `repo`; `delete_repo` leads.
        assert_eq!(check.excess_scopes, vec!["delete_repo", "gist"]);
        assert_eq!(check.token_flavor, Some(crate::TokenFlavor::Classic));
        assert!(check.recommend_fine_grained);
        assert_eq!(check.expires_at.as_deref(), Some("2026-12-01 10:00:00 UTC"));
    }

    #[test]
    fn a_token_with_exactly_the_needed_scopes_and_no_expiry_has_nothing_to_say() {
        let transport = Arc::new(RecordedTransport::new().on_with_headers(
            "/user",
            200,
            USER,
            &[("X-OAuth-Scopes", "repo, read:user, admin:public_key")],
        ));
        let c = client_with_token("0123456789abcdef", transport);
        let check = block_on(c.check_token()).unwrap();
        assert!(check.excess_scopes.is_empty());
        assert!(!check.recommend_fine_grained);
        assert_eq!(check.expires_at, None);
    }

    #[test]
    fn a_fine_grained_token_shows_only_its_expiry() {
        let transport = Arc::new(RecordedTransport::new().on_with_headers(
            "/user",
            200,
            USER,
            &[(
                "github-authentication-token-expiration",
                "2026-12-01 10:00:00 UTC",
            )],
        ));
        let c = client_with_token("github_pat_xyz", transport);
        let check = block_on(c.check_token()).unwrap();
        assert_eq!(check.scopes, None);
        assert!(check.excess_scopes.is_empty() && check.missing_scopes.is_empty());
        assert_eq!(check.token_flavor, Some(crate::TokenFlavor::FineGrained));
        assert!(!check.recommend_fine_grained);
        assert_eq!(check.expires_at.as_deref(), Some("2026-12-01 10:00:00 UTC"));
    }

    #[test]
    fn a_fine_grained_token_has_unknown_scopes_not_missing_ones() {
        let transport = Arc::new(RecordedTransport::new().on("/user", 200, USER));
        let c = client(IntegrationKind::Github, "https://github.com", transport);
        let check = block_on(c.check_token()).unwrap();
        assert_eq!(check.scopes, None);
        assert!(check.missing_scopes.is_empty());
    }

    #[test]
    fn enterprise_calls_go_to_the_api_v3_path() {
        let transport = Arc::new(RecordedTransport::new().on("/user", 200, USER));
        let c = client(
            IntegrationKind::GithubEnterprise,
            "https://ghe.example.com",
            transport.clone(),
        );
        block_on(c.current_user()).unwrap();
        assert_eq!(
            transport.requests()[0].url,
            "https://ghe.example.com/api/v3/user"
        );
    }

    #[test]
    fn a_rejected_token_and_an_underscoped_one_are_distinct_errors() {
        for (status, unauthorized) in [(401, true), (403, false)] {
            let transport =
                Arc::new(RecordedTransport::new().on("/user", status, r#"{"message":"no"}"#));
            let c = client(IntegrationKind::Github, "https://github.com", transport);
            let err = block_on(c.check_token()).unwrap_err();
            assert_eq!(matches!(err, Error::Unauthorized { .. }), unauthorized);
        }
    }

    #[test]
    fn pull_requests_normalise_state_labels_and_forks() {
        let merged = pull(3, "closed", r#","merged_at":"2026-01-03T00:00:00Z""#);
        let draft = pull(2, "open", r#","draft":true"#);
        let fork = pull(1, "open", "").replace(
            r#""head":{"ref":"feature","repo":{"full_name":"me/thing"}}"#,
            r#""head":{"ref":"feature","repo":{"full_name":"fork/thing"}}"#,
        );
        let body = format!("[{merged},{draft},{fork}]");
        let transport = Arc::new(RecordedTransport::new().on("/pulls?", 200, &body));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let prs = block_on(c.list_pull_requests("me/thing", &PrFilter::default())).unwrap();
        assert_eq!(prs[0].state, PullRequestState::Merged);
        assert_eq!(prs[0].iid_label, "#3");
        assert_eq!(prs[1].state, PullRequestState::Draft);
        assert_eq!(prs[2].source_project.as_deref(), Some("fork/thing"));
        assert_eq!(prs[0].source_project, None);
        assert_eq!(prs[0].source_branch, "feature");
        assert_eq!(prs[0].target_branch, "main");
        assert!(transport.requests()[0].url.contains("state=open"));
    }

    #[test]
    fn assignees_and_requested_reviewers_are_carried_into_the_summary() {
        let row = pull(4, "open", "").replace(
            r#""user":{"login":"octo"}"#,
            r#""user":{"login":"octo"},"assignees":[{"login":"me"}],"requested_reviewers":[{"login":"rev1"},{"login":"me"}]"#,
        );
        let transport = Arc::new(RecordedTransport::new().on("/pulls?", 200, &format!("[{row}]")));
        let c = client(IntegrationKind::Github, "https://github.com", transport);
        let prs = block_on(c.list_pull_requests("me/thing", &PrFilter::default())).unwrap();
        assert_eq!(prs[0].assignees, vec!["me"]);
        assert_eq!(prs[0].reviewers, vec!["rev1", "me"]);
    }

    #[test]
    fn pull_request_lists_follow_full_pages() {
        let full = format!(
            "[{}]",
            (0..PAGE_SIZE)
                .map(|n| pull(n as u64 + 1, "open", ""))
                .collect::<Vec<_>>()
                .join(",")
        );
        let transport = Arc::new(RecordedTransport::new().on("&page=1", 200, &full).on(
            "&page=2",
            200,
            &format!("[{}]", pull(500, "open", "")),
        ));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let prs = block_on(c.list_pull_requests("me/thing", &PrFilter::default())).unwrap();
        assert_eq!(prs.len(), PAGE_SIZE as usize + 1);
        assert_eq!(transport.requests().len(), 2);
    }

    #[test]
    fn mine_filters_by_the_users_login_and_review_requested_uses_search() {
        let mut other = pull(2, "open", "");
        other = other.replace(
            r#""user":{"login":"octo"}"#,
            r#""user":{"login":"someone"}"#,
        );
        let body = format!("[{},{other}]", pull(1, "open", ""));
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/search/issues", 200, r#"{"items":[{"number":2}]}"#)
                .on("/pulls?", 200, &body),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );

        let mine = PrFilter {
            mine: true,
            ..Default::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &mine)).unwrap();
        assert_eq!(prs.len(), 1);
        assert_eq!(prs[0].number, 1);

        let review = PrFilter {
            review_requested: true,
            ..Default::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &review)).unwrap();
        assert_eq!(prs.len(), 1);
        assert_eq!(prs[0].number, 2);
        assert!(prs[0].review_requested);
        let search = transport
            .requests()
            .into_iter()
            .find(|r| r.url.contains("/search/issues"))
            .unwrap();
        assert!(
            search.url.contains("review-requested%3A%40me"),
            "{}",
            search.url
        );
    }

    #[test]
    fn the_pull_request_for_a_branch_prefers_open_over_merged() {
        let merged = pull(5, "closed", r#","merged_at":"2026-01-05T00:00:00Z""#);
        let open = pull(2, "open", "");
        let transport =
            Arc::new(RecordedTransport::new().on("/pulls?", 200, &format!("[{merged},{open}]")));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let found = block_on(c.pull_request_for_branch("me/thing", "feature/x"))
            .unwrap()
            .unwrap();
        assert_eq!(found.number, 2);
        let url = &transport.requests()[0].url;
        assert!(url.contains("head=me%3Afeature%2Fx"), "{url}");

        let empty = Arc::new(RecordedTransport::new().on("/pulls?", 200, "[]"));
        let c = client(IntegrationKind::Github, "https://github.com", empty);
        assert!(block_on(c.pull_request_for_branch("me/thing", "b"))
            .unwrap()
            .is_none());
    }

    #[test]
    fn issues_drop_pull_request_rows_and_map_fields() {
        let body = r#"[
            {"number":1,"title":"bug","state":"open","html_url":"https://github.com/me/thing/issues/1",
             "updated_at":"2026-01-01T00:00:00Z","user":{"login":"octo"},
             "assignees":[{"login":"octo"}],"labels":[{"name":"bug"}]},
            {"number":2,"title":"a pr","state":"open","html_url":"x","pull_request":{"url":"y"}}
        ]"#;
        let transport = Arc::new(RecordedTransport::new().on("/issues?", 200, body));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let issues = block_on(c.list_issues("me/thing", &IssueFilter::default())).unwrap();
        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].iid_label, "#1");
        assert_eq!(issues[0].reference, "#1");
        assert_eq!(issues[0].id, "1");
        assert_eq!(issues[0].kind_label, None);
        assert_eq!(issues[0].labels, vec!["bug"]);
        assert_eq!(issues[0].assignees, vec!["octo"]);
        assert_eq!(issues[0].state, IssueState::Open);
    }

    #[test]
    fn assigned_to_me_passes_the_login() {
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/issues?", 200, "[]"),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let filter = IssueFilter {
            assigned_to_me: true,
            ..Default::default()
        };
        block_on(c.list_issues("me/thing", &filter)).unwrap();
        assert!(transport
            .requests()
            .iter()
            .any(|r| r.url.contains("assignee=octo")));
    }

    #[test]
    fn creating_an_issue_posts_title_body_and_labels() {
        let transport = Arc::new(RecordedTransport::new().on(
            "/repos/me/thing/issues",
            201,
            r#"{"number":12,"html_url":"https://github.com/me/thing/issues/12"}"#,
        ));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let created = block_on(c.create_issue(&NewIssue {
            project: "me/thing".into(),
            title: "It breaks".into(),
            body: "Steps".into(),
            labels: vec!["bug".into()],
            ..Default::default()
        }))
        .unwrap();
        assert_eq!(created.number, 12);
        assert_eq!(created.reference, "#12");
        assert_eq!(created.web_url, "https://github.com/me/thing/issues/12");
        let request = &transport.requests()[0];
        assert_eq!(request.method, crate::http::Method::Post);
        assert_eq!(request.url, "https://api.github.com/repos/me/thing/issues");
        let body: serde_json::Value =
            serde_json::from_str(request.body.as_deref().unwrap()).unwrap();
        assert_eq!(body["title"], "It breaks");
        assert_eq!(body["body"], "Steps");
        assert_eq!(body["labels"], serde_json::json!(["bug"]));
    }

    #[test]
    fn creating_an_issue_without_labels_omits_them() {
        let transport =
            Arc::new(RecordedTransport::new().on("/issues", 201, r#"{"number":1,"html_url":"u"}"#));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        block_on(c.create_issue(&NewIssue {
            project: "me/thing".into(),
            title: "t".into(),
            ..Default::default()
        }))
        .unwrap();
        let body: serde_json::Value =
            serde_json::from_str(transport.requests()[0].body.as_deref().unwrap()).unwrap();
        assert!(body.get("labels").is_none());
    }

    #[test]
    fn creating_an_issue_surfaces_each_failure_clearly() {
        for (status, body) in [
            (401, r#"{"message":"Bad credentials"}"#),
            (
                403,
                r#"{"message":"Resource not accessible by personal access token"}"#,
            ),
            (404, r#"{"message":"Not Found"}"#),
            (
                422,
                r#"{"message":"Validation Failed","errors":[{"resource":"Issue","field":"title","code":"missing_field"}]}"#,
            ),
        ] {
            let transport = Arc::new(RecordedTransport::new().on("/issues", status, body));
            let c = client(IntegrationKind::Github, "https://github.com", transport);
            let err = block_on(c.create_issue(&NewIssue {
                project: "me/thing".into(),
                title: "t".into(),
                ..Default::default()
            }))
            .unwrap_err();
            match status {
                401 => assert!(matches!(err, Error::Unauthorized { .. })),
                403 => assert!(matches!(err, Error::Forbidden { .. })),
                _ => assert!(matches!(err, Error::Api { status: s, .. } if s == status)),
            }
            let text = err.to_string();
            assert!(text.contains("opening a GitHub issue"), "{text}");
            if status == 422 {
                assert!(
                    text.contains("Validation Failed") && text.contains("title"),
                    "{text}"
                );
            }
        }
    }

    #[test]
    fn deleting_a_key_sends_delete_to_the_key_id() {
        let transport = Arc::new(RecordedTransport::new().on("/user/keys/7", 204, ""));
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        block_on(c.delete_ssh_key("7")).unwrap();
        let request = &transport.requests()[0];
        assert_eq!(request.method, crate::http::Method::Delete);
        assert_eq!(request.url, "https://api.github.com/user/keys/7");
    }

    fn pull_with_sha(n: u64, login: &str, sha: &str) -> String {
        pull(n, "open", "")
            .replace(
                r#""user":{"login":"octo"}"#,
                &format!(r#""user":{{"login":"{login}"}}"#),
            )
            .replace(
                r#""head":{"ref":"feature","#,
                &format!(r#""head":{{"sha":"{sha}","ref":"feature","#),
            )
    }

    #[test]
    fn checks_and_conflicts_are_asked_per_request_only_on_demand() {
        let list = format!(
            "[{},{}]",
            pull_with_sha(1, "someone", "aaa"),
            pull_with_sha(2, "octo", "bbb")
        );
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/pulls?", 200, &list)
                .on("/pulls/1", 200, r#"{"mergeable_state":"dirty"}"#)
                .on("/pulls/2", 200, r#"{"mergeable_state":"clean"}"#)
                .on(
                    "/commits/aaa/status",
                    200,
                    r#"{"state":"pending","total_count":0}"#,
                )
                .on(
                    "/commits/aaa/check-runs",
                    200,
                    r#"{"check_runs":[{"status":"completed","conclusion":"failure"}]}"#,
                )
                .on(
                    "/commits/bbb/status",
                    200,
                    r#"{"state":"success","total_count":1}"#,
                )
                .on(
                    "/commits/bbb/check-runs",
                    200,
                    r#"{"check_runs":[{"status":"completed","conclusion":"success"}]}"#,
                ),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        // Not asked: nothing beyond the list, nothing filled.
        let plain = block_on(c.list_pull_requests("me/thing", &PrFilter::default())).unwrap();
        assert_eq!(transport.requests().len(), 1);
        assert!(plain
            .iter()
            .all(|p| p.checks.is_none() && p.has_conflicts.is_none()));

        let filter = PrFilter {
            checks: true,
            ..PrFilter::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &filter)).unwrap();
        assert_eq!(prs[0].checks, Some(ChecksState::Failing));
        assert_eq!(prs[0].has_conflicts, Some(true));
        assert_eq!(prs[1].checks, Some(ChecksState::Passing));
        assert_eq!(prs[1].has_conflicts, Some(false));
        // The user's own request (#2) is asked first.
        let urls: Vec<String> = transport.requests().into_iter().map(|r| r.url).collect();
        let first_detail = urls.iter().position(|u| u.contains("/pulls/")).unwrap();
        assert!(urls[first_detail].ends_with("/pulls/2"), "{urls:?}");
    }

    #[test]
    fn the_fan_out_is_bounded_and_a_failing_call_ends_it_quietly() {
        let list = format!(
            "[{}]",
            (1..=(STATUS_FANOUT as u64 + 10))
                .map(|n| pull_with_sha(n, "octo", &format!("s{n}")))
                .collect::<Vec<_>>()
                .join(",")
        );
        // Every detail call is a rate limit: the list still succeeds.
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/pulls?", 200, &list)
                .on("/pulls/", 403, r#"{"message":"rate limit"}"#),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let filter = PrFilter {
            checks: true,
            ..PrFilter::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &filter)).unwrap();
        assert_eq!(prs.len(), STATUS_FANOUT + 10);
        assert!(prs.iter().all(|p| p.checks.is_none()));
        let details = transport
            .requests()
            .iter()
            .filter(|r| r.url.contains("/pulls/"))
            .count();
        assert_eq!(details, 1, "the first refusal ends the fan-out");
    }

    #[test]
    fn the_fan_out_asks_at_most_the_bound() {
        let list = format!(
            "[{}]",
            (1..=(STATUS_FANOUT as u64 + 10))
                .map(|n| pull_with_sha(n, "octo", &format!("s{n}")))
                .collect::<Vec<_>>()
                .join(",")
        );
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/pulls?", 200, &list)
                .on("/pulls/", 200, r#"{"mergeable_state":"clean"}"#)
                .on("/status", 200, r#"{"state":"success","total_count":1}"#)
                .on("/check-runs", 200, r#"{"check_runs":[]}"#),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let filter = PrFilter {
            checks: true,
            ..PrFilter::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &filter)).unwrap();
        assert_eq!(
            prs.iter().filter(|p| p.checks.is_some()).count(),
            STATUS_FANOUT
        );
        let details = transport
            .requests()
            .iter()
            .filter(|r| r.url.contains("/pulls/"))
            .count();
        assert_eq!(details, STATUS_FANOUT);
    }

    #[test]
    fn checks_roll_up_failure_then_pending_then_passing() {
        use ChecksState::*;
        assert_eq!(
            roll_up_checks("success", 2, &[("completed", Some("success"))]),
            Some(Passing)
        );
        assert_eq!(roll_up_checks("failure", 1, &[]), Some(Failing));
        assert_eq!(
            roll_up_checks(
                "success",
                1,
                &[("in_progress", None), ("completed", Some("failure"))]
            ),
            Some(Failing)
        );
        assert_eq!(
            roll_up_checks("success", 1, &[("queued", None)]),
            Some(Pending)
        );
        assert_eq!(roll_up_checks("pending", 2, &[]), Some(Pending));
        // GitHub says "pending" for a commit with no statuses at all: no CI.
        assert_eq!(roll_up_checks("pending", 0, &[]), None);
        assert_eq!(
            roll_up_checks("pending", 0, &[("completed", Some("skipped"))]),
            Some(Passing)
        );
    }

    #[test]
    fn mentioned_asks_the_search_api_and_keeps_only_those_requests() {
        let list = format!("[{},{}]", pull(1, "open", ""), pull(2, "open", ""));
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/pulls?", 200, &list)
                .on("/search/issues", 200, r#"{"items":[{"number":2}]}"#),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let filter = PrFilter {
            mentioned: true,
            ..PrFilter::default()
        };
        let prs = block_on(c.list_pull_requests("me/thing", &filter)).unwrap();
        assert_eq!(prs.iter().map(|p| p.number).collect::<Vec<_>>(), vec![2]);
        let search = transport
            .requests()
            .into_iter()
            .find(|r| r.url.contains("/search/issues"))
            .unwrap();
        assert!(search.url.contains("is%3Apr"), "{}", search.url);
        assert!(search.url.contains("mentions%3Aocto"), "{}", search.url);
        assert!(search.url.contains("repo%3Ame%2Fthing"), "{}", search.url);
    }

    #[test]
    fn mentioned_issues_search_issues_not_requests() {
        let issues = r#"[
            {"number":5,"title":"a","state":"open","html_url":"u5","updated_at":"x","user":{"login":"o"}},
            {"number":6,"title":"b","state":"open","html_url":"u6","updated_at":"x","user":{"login":"o"}}
        ]"#;
        let transport = Arc::new(
            RecordedTransport::new()
                .on("/user", 200, USER)
                .on("/thing/issues?", 200, issues)
                .on("/search/issues", 200, r#"{"items":[{"number":6}]}"#),
        );
        let c = client(
            IntegrationKind::Github,
            "https://github.com",
            transport.clone(),
        );
        let filter = IssueFilter {
            mentioned: true,
            ..IssueFilter::default()
        };
        let found = block_on(c.list_issues("me/thing", &filter)).unwrap();
        assert_eq!(found.iter().map(|i| i.number).collect::<Vec<_>>(), vec![6]);
        let search = transport
            .requests()
            .into_iter()
            .find(|r| r.url.contains("/search/issues"))
            .unwrap();
        assert!(search.url.contains("is%3Aissue"), "{}", search.url);
    }
}