yandex-tracker-cli 1.0.1

Token-efficient Yandex Tracker CLI for humans and AI agents
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
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
//! HTTP layer against the Tracker REST API.
//!
//! We talk to the API directly instead of using the official Python-era client:
//! see `docs/adr/0004-own-http-client.md`.

pub mod duration;
pub mod error;
pub mod models;
pub mod parse;
pub mod query;

use std::time::Duration;

use backon::{ExponentialBuilder, Retryable};
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderName, HeaderValue, USER_AGENT};

use serde_json::Value;

use crate::api::error::ApiError;
use crate::api::models::{
    Attachment, Change, ChecklistItem, Comment, DictEntry, Entity, Issue, Link, Page, Person,
    RemoteLink, User, Worklog,
};
use crate::config::OrgKind;

/// Default API root. Overridable so tests can point at a `wiremock` server.
pub const DEFAULT_BASE_URL: &str = "https://api.tracker.yandex.net";

/// Entity fields we ask for. Requesting an explicit set keeps the response small
/// and its shape predictable; the endpoints return only identity otherwise.
/// `entityType` is deliberately absent: it is an attribute of the entity, not
/// one of its fields, and asking for it makes Tracker refuse the whole request
/// with `поля [entityType] не существуют`. It comes back regardless.
const ENTITY_FIELDS: &str = "summary,description,entityStatus,start,end,lead,author,parentEntity";

/// The host part of a URL, for comparing two of them.
fn host_of(url: &str) -> Option<String> {
    let without_scheme = url.split_once("://")?.1;
    let authority = without_scheme
        .split(['/', '?', '#'])
        .next()
        .unwrap_or(without_scheme);
    Some(authority.to_ascii_lowercase())
}

/// Everything the client needs to address one organisation as one account.
#[derive(Debug, Clone)]
pub struct ClientConfig {
    pub base_url: String,
    pub token: String,
    pub org_id: String,
    pub org_kind: OrgKind,
    pub timeout: Duration,
    /// Retry attempts for transport errors, 429 and 5xx. Client errors are never retried.
    pub retries: usize,
}

impl ClientConfig {
    #[must_use]
    pub fn new(token: String, org_id: String, org_kind: OrgKind) -> Self {
        Self {
            base_url: DEFAULT_BASE_URL.to_owned(),
            token,
            org_id,
            org_kind,
            timeout: Duration::from_secs(30),
            retries: 3,
        }
    }
}

/// A configured Tracker client.
#[derive(Debug, Clone)]
pub struct Client {
    http: reqwest::Client,
    base_url: String,
    retries: usize,
    /// Which organisation this client talks to.
    ///
    /// The headers carry it already, but nothing can read them back, and one
    /// command can hold two clients: keys resolve per profile, and two profiles
    /// can be two organisations. A bulk change is one request to one of them.
    org: String,
}

impl Client {
    pub fn new(config: &ClientConfig) -> Result<Self, ApiError> {
        let mut headers = HeaderMap::new();
        headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
        headers.insert(
            USER_AGENT,
            HeaderValue::from_static(concat!("ytcli/", env!("CARGO_PKG_VERSION"))),
        );

        // A malformed token or org id must fail here, not as a confusing 401 later.
        let mut auth = HeaderValue::try_from(format!("OAuth {}", config.token))
            .map_err(|_| ApiError::Unauthorized)?;
        auth.set_sensitive(true);
        headers.insert(AUTHORIZATION, auth);

        let org_header = HeaderName::from_static(config.org_kind.header_name());
        let org_value =
            HeaderValue::try_from(config.org_id.clone()).map_err(|_| ApiError::Forbidden)?;
        headers.insert(org_header, org_value);

        let http = reqwest::Client::builder()
            .timeout(config.timeout)
            .default_headers(headers)
            .build()?;

        Ok(Self {
            http,
            base_url: config.base_url.trim_end_matches('/').to_owned(),
            retries: config.retries,
            org: config.org_id.clone(),
        })
    }

    /// The organisation id this client was built for.
    #[must_use]
    pub fn org(&self) -> &str {
        &self.org
    }

    /// `GET /v3/myself` — the cheapest call that proves the whole chain works:
    /// token, organisation header, and network.
    pub async fn myself(&self) -> Result<User, ApiError> {
        let value = self.get_value("/v3/myself", "current user").await?;
        Ok(User {
            id: value
                .get("uid")
                .map_or_else(String::new, ToString::to_string),
            login: value
                .get("login")
                .and_then(serde_json::Value::as_str)
                .map(ToOwned::to_owned),
            display: value
                .get("display")
                .and_then(serde_json::Value::as_str)
                .map(ToOwned::to_owned),
        })
    }

    /// One issue, both normalised and raw.
    ///
    /// The raw payload travels alongside so that `--json-raw` does not cost a
    /// second request, and so that a field we do not model is still reachable.
    pub async fn issue(&self, key: &str) -> Result<(Issue, Value), ApiError> {
        let raw = self
            .get_value(&format!("/v3/issues/{key}"), &format!("issue {key}"))
            .await?;
        let issue = parse::issue(&raw).ok_or_else(|| ApiError::NotFound(format!("issue {key}")))?;
        Ok((issue, raw))
    }

    /// The links of an issue, with their direction resolved.
    ///
    /// Tracker keeps links on their own endpoint, so the compact issue view
    /// costs two requests. Showing links is worth that: "what blocks this" is
    /// the question that follows "what is this", and making the caller ask twice
    /// costs more than one round trip (ADR 3).
    pub async fn issue_links(&self, key: &str) -> Result<Vec<Link>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/links"),
                &format!("issue {key} links"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::link).collect())
            .unwrap_or_default())
    }

    /// `GET /v3/issues/{key}/remotelinks` — the links that leave Tracker.
    ///
    /// Its own request rather than a second section of [`Self::issue_links`]:
    /// most issues have none, and making every `issue links` pay for a request
    /// that usually answers `[]` is the wrong trade.
    pub async fn issue_remote_links(&self, key: &str) -> Result<Vec<RemoteLink>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/remotelinks"),
                &format!("remote links of {key}"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::remote_link).collect())
            .unwrap_or_default())
    }

    /// One page of search results.
    ///
    /// Tracker reports the total in `X-Total-Count`. When it does not, the page
    /// still has to be honest about whether more exists, which is why
    /// [`Page::has_more`] falls back to "a full page probably is not the last".
    pub async fn search(
        &self,
        query: &str,
        page: u32,
        per_page: u32,
    ) -> Result<Page<Issue>, ApiError> {
        let path = format!("/v3/issues/_search?page={page}&perPage={per_page}");
        let body = serde_json::json!({ "query": query });
        let (value, headers) = self.post_value(&path, &body, "issues").await?;

        let items = value
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::issue).collect())
            .unwrap_or_default();

        Ok(Page {
            items,
            page,
            per_page,
            total: headers
                .get("x-total-count")
                .and_then(|count| count.to_str().ok())
                .and_then(|count| count.parse().ok()),
        })
    }

    /// How many issues match, without fetching any of them.
    pub async fn count(&self, query: &str) -> Result<u64, ApiError> {
        let body = serde_json::json!({ "query": query });
        let (value, _) = self
            .post_value("/v3/issues/_count", &body, "issues")
            .await?;

        value
            .as_u64()
            .ok_or_else(|| ApiError::NotFound("issue count".to_owned()))
    }

    /// `POST /v3/issues/` — create an issue, returning it normalised.
    pub async fn create_issue(&self, body: &Value) -> Result<Issue, ApiError> {
        let (value, _) = self.post_value("/v3/issues/", body, "issue").await?;
        parse::issue(&value).ok_or_else(|| ApiError::NotFound("created issue".to_owned()))
    }

    /// `PATCH /v3/issues/{key}` — change fields.
    pub async fn update_issue(&self, key: &str, body: &Value) -> Result<Issue, ApiError> {
        let value = self
            .send_value(
                reqwest::Method::PATCH,
                &format!("/v3/issues/{key}"),
                Some(body),
                &format!("issue {key}"),
            )
            .await?
            .0;
        parse::issue(&value).ok_or_else(|| ApiError::NotFound(format!("issue {key}")))
    }

    /// `POST /v3/issues/{key}/comments` — add a comment.
    pub async fn add_comment(&self, key: &str, text: &str) -> Result<Comment, ApiError> {
        let body = serde_json::json!({ "text": text });
        let (value, _) = self
            .post_value(
                &format!("/v3/issues/{key}/comments"),
                &body,
                &format!("issue {key}"),
            )
            .await?;
        parse::comment(&value).ok_or_else(|| ApiError::NotFound("created comment".to_owned()))
    }

    /// Rewrite a comment that is already there.
    ///
    /// Tracker keeps no history of the previous text and shows the comment as
    /// edited, so this replaces rather than appends: the old wording is gone.
    pub async fn update_comment(
        &self,
        key: &str,
        id: &str,
        text: &str,
    ) -> Result<Comment, ApiError> {
        let body = serde_json::json!({ "text": text });
        let (value, _) = self
            .send_value(
                reqwest::Method::PATCH,
                &format!("/v3/issues/{key}/comments/{id}"),
                Some(&body),
                &format!("comment {id} of issue {key}"),
            )
            .await?;
        parse::comment(&value).ok_or_else(|| ApiError::NotFound(format!("comment {id}")))
    }

    /// Remove a comment.
    pub async fn delete_comment(&self, key: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/issues/{key}/comments/{id}"),
            None,
            &format!("comment {id} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// Correct a worklog entry that is already recorded.
    pub async fn update_worklog(
        &self,
        key: &str,
        id: &str,
        body: &Value,
    ) -> Result<Worklog, ApiError> {
        let (value, _) = self
            .send_value(
                reqwest::Method::PATCH,
                &format!("/v3/issues/{key}/worklog/{id}"),
                Some(body),
                &format!("worklog {id} of issue {key}"),
            )
            .await?;
        parse::worklog(&value).ok_or_else(|| ApiError::NotFound(format!("worklog {id}")))
    }

    /// `GET /v3/issues/{key}/worklog` — every entry, oldest first.
    pub async fn worklogs(&self, key: &str) -> Result<Vec<Worklog>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/worklog"),
                &format!("issue {key} worklog"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::worklog).collect())
            .unwrap_or_default())
    }

    /// `POST /v3/issues/{key}/worklog` — record time spent.
    pub async fn add_worklog(&self, key: &str, body: &Value) -> Result<Worklog, ApiError> {
        let (value, _) = self
            .post_value(
                &format!("/v3/issues/{key}/worklog"),
                body,
                &format!("issue {key} worklog"),
            )
            .await?;
        parse::worklog(&value).ok_or_else(|| ApiError::NotFound("created worklog".to_owned()))
    }

    /// `DELETE /v3/issues/{key}/worklog/{id}`.
    pub async fn delete_worklog(&self, key: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/issues/{key}/worklog/{id}"),
            None,
            &format!("worklog {id} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// `GET /v3/issues/{key}/checklistItems`.
    pub async fn checklist(&self, key: &str) -> Result<Vec<ChecklistItem>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/checklistItems"),
                &format!("issue {key} checklist"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::checklist_item).collect())
            .unwrap_or_default())
    }

    /// `POST /v3/issues/{key}/checklistItems` — add a line.
    ///
    /// Tracker answers with the whole issue rather than the item, so the list
    /// comes back out of the issue's own `checklistItems`.
    pub async fn add_checklist_item(
        &self,
        key: &str,
        body: &Value,
    ) -> Result<Vec<ChecklistItem>, ApiError> {
        let (value, _) = self
            .post_value(
                &format!("/v3/issues/{key}/checklistItems"),
                body,
                &format!("issue {key} checklist"),
            )
            .await?;
        Ok(checklist_of(&value))
    }

    /// `PATCH /v3/issues/{key}/checklistItems/{id}` — tick, untick or reword.
    pub async fn update_checklist_item(
        &self,
        key: &str,
        id: &str,
        body: &Value,
    ) -> Result<Vec<ChecklistItem>, ApiError> {
        let (value, _) = self
            .send_value(
                reqwest::Method::PATCH,
                &format!("/v3/issues/{key}/checklistItems/{id}"),
                Some(body),
                &format!("checklist item {id} of issue {key}"),
            )
            .await?;
        Ok(checklist_of(&value))
    }

    /// `DELETE /v3/issues/{key}/checklistItems/{id}`.
    pub async fn delete_checklist_item(&self, key: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/issues/{key}/checklistItems/{id}"),
            None,
            &format!("checklist item {id} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// `POST /v3/issues/{key}/links` — link two issues.
    pub async fn add_link(
        &self,
        key: &str,
        relationship: &str,
        other: &str,
    ) -> Result<(), ApiError> {
        let body = serde_json::json!({ "relationship": relationship, "issue": other });
        self.post_value(
            &format!("/v3/issues/{key}/links"),
            &body,
            &format!("issue {key} links"),
        )
        .await?;
        Ok(())
    }

    /// `DELETE /v3/issues/{key}/links/{id}`.
    pub async fn delete_link(&self, key: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/issues/{key}/links/{id}"),
            None,
            &format!("link {id} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// `DELETE /v3/issues/{key}/attachments/{id}` — remove an attachment.
    ///
    /// Tracker keeps no copy: the file is gone, and the comment or description
    /// that pointed at it is left pointing at nothing.
    pub async fn delete_attachment(&self, key: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/issues/{key}/attachments/{id}"),
            None,
            &format!("attachment {id} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// Transitions available from the issue's current status.
    pub async fn transitions(&self, key: &str) -> Result<Vec<Transition>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/transitions"),
                &format!("issue {key} transitions"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Transition::parse).collect())
            .unwrap_or_default())
    }

    /// Perform a transition.
    pub async fn execute_transition(
        &self,
        key: &str,
        transition: &str,
        body: &Value,
    ) -> Result<(), ApiError> {
        self.post_value(
            &format!("/v3/issues/{key}/transitions/{transition}/_execute"),
            body,
            &format!("transition {transition} of issue {key}"),
        )
        .await?;
        Ok(())
    }

    /// Search projects, portfolios or goals.
    ///
    /// The entity endpoints answer with their own envelope (`values`, `hits`,
    /// `pages`) rather than the header-based totals the issue endpoints use, so
    /// the page is assembled from the body here.
    pub async fn entities(
        &self,
        kind: &str,
        input: Option<&str>,
        page: u32,
        per_page: u32,
    ) -> Result<Page<Entity>, ApiError> {
        let path = format!(
            "/v3/entities/{kind}/_search?page={page}&perPage={per_page}&fields={ENTITY_FIELDS}"
        );
        let mut body = serde_json::Map::new();
        if let Some(input) = input {
            body.insert("input".to_owned(), Value::String(input.to_owned()));
        }

        let (value, _) = self
            .post_value(&path, &Value::Object(body), &format!("{kind}s"))
            .await?;

        let items = value
            .get("values")
            .and_then(Value::as_array)
            .map(|entries| entries.iter().filter_map(parse::entity).collect())
            .unwrap_or_default();

        Ok(Page {
            items,
            page,
            per_page,
            total: value.get("hits").and_then(Value::as_u64),
        })
    }

    /// What a portfolio contains: the portfolios and projects under it.
    ///
    /// Two requests, because the entity endpoints are typed and containment is
    /// not: a portfolio holds both. The tally sums the two totals, so `shown N
    /// of M` is the real count even though a page is a page of each.
    pub async fn entities_in(
        &self,
        parent: &str,
        page: u32,
        per_page: u32,
    ) -> Result<Page<Entity>, ApiError> {
        let mut items = Vec::new();
        let mut total = 0;

        for kind in ["portfolio", "project"] {
            let path = format!(
                "/v3/entities/{kind}/_search?page={page}&perPage={per_page}&fields={ENTITY_FIELDS}"
            );
            let body = serde_json::json!({ "filter": { "parentEntity": parent } });
            let (value, _) = self
                .post_value(&path, &body, &format!("{kind}s in {parent}"))
                .await?;

            if let Some(entries) = value.get("values").and_then(Value::as_array) {
                items.extend(entries.iter().filter_map(parse::entity));
            }
            total += value.get("hits").and_then(Value::as_u64).unwrap_or(0);
        }

        Ok(Page {
            items,
            page,
            per_page,
            total: Some(total),
        })
    }

    /// One project, portfolio or goal, by the id the entity endpoints use.
    pub async fn entity(&self, kind: &str, id: &str) -> Result<Entity, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/entities/{kind}/{id}?fields={ENTITY_FIELDS}"),
                &format!("{kind} {id}"),
            )
            .await?;

        parse::entity(&raw).ok_or_else(|| ApiError::NotFound(format!("{kind} {id}")))
    }

    /// The attachments of an issue.
    pub async fn attachments(&self, key: &str) -> Result<Vec<Attachment>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/attachments"),
                &format!("issue {key} attachments"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::attachment).collect())
            .unwrap_or_default())
    }

    /// Download an attachment's bytes.
    ///
    /// The download URL comes out of the payload, which means it is supplied by
    /// the server rather than chosen by us. It is checked against the configured
    /// API host before being followed: a crafted `content` URL must not be able
    /// to send this client, carrying its OAuth header, to somewhere else.
    pub async fn download(&self, url: &str) -> Result<Vec<u8>, ApiError> {
        let expected = host_of(&self.base_url);
        if host_of(url) != expected {
            return Err(ApiError::Rejected {
                status: reqwest::StatusCode::BAD_REQUEST,
                message: format!(
                    "attachment points at `{}`, which is not the configured Tracker host `{}`",
                    host_of(url).unwrap_or_default(),
                    expected.unwrap_or_default(),
                ),
            });
        }

        let response = self.http.get(url).send().await?;
        let status = response.status();
        if !status.is_success() {
            return Err(match status.as_u16() {
                401 => ApiError::Unauthorized,
                403 => ApiError::Forbidden,
                404 => ApiError::NotFound("attachment".to_owned()),
                _ => ApiError::Rejected {
                    status,
                    message: String::new(),
                },
            });
        }

        Ok(response.bytes().await?.to_vec())
    }

    /// Upload a file to an issue.
    pub async fn upload(
        &self,
        key: &str,
        filename: &str,
        bytes: Vec<u8>,
    ) -> Result<Attachment, ApiError> {
        let part = reqwest::multipart::Part::bytes(bytes).file_name(filename.to_owned());
        let form = reqwest::multipart::Form::new().part("file", part);

        let url = format!("{}/v3/issues/{key}/attachments/", self.base_url);
        let response = self.http.post(&url).multipart(form).send().await?;
        let text = classify(response, &format!("issue {key}")).await?;

        let value: Value = serde_json::from_str(&text).map_err(ApiError::Decode)?;
        parse::attachment(&value)
            .ok_or_else(|| ApiError::NotFound("uploaded attachment".to_owned()))
    }

    /// Queues visible to the active profile.
    ///
    /// Tracker paginates this endpoint; the ceiling is deliberately generous
    /// because "how many queues can I see" is a question with a small answer,
    /// and a second page here would be surprising.
    pub async fn queues(&self) -> Result<Vec<Queue>, ApiError> {
        let raw = self.get_value("/v3/queues?perPage=1000", "queues").await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Queue::parse).collect())
            .unwrap_or_default())
    }

    /// Worklog entries across the whole organisation.
    ///
    /// `createdBy` takes a login or a uid and **not** `me`: Tracker reads it as
    /// a login and answers 422 saying no such user exists. Resolving `me` is
    /// the caller's job, with one extra request to `myself`.
    pub async fn worklog_search(
        &self,
        who: Option<&str>,
        since: Option<&str>,
        until: Option<&str>,
        per_page: u32,
    ) -> Result<Vec<Worklog>, ApiError> {
        use std::fmt::Write as _;

        let mut query = format!("perPage={per_page}");
        if let Some(who) = who {
            let _ = write!(query, "&createdBy={who}");
        }
        // One parameter carries both ends of the range, and Tracker accepts
        // either half on its own.
        match (since, until) {
            (Some(since), Some(until)) => {
                let _ = write!(query, "&createdAt=from:{since},to:{until}");
            }
            (Some(since), None) => {
                let _ = write!(query, "&createdAt=from:{since}");
            }
            (None, Some(until)) => {
                let _ = write!(query, "&createdAt=to:{until}");
            }
            (None, None) => {}
        }

        let raw = self
            .get_value(&format!("/v3/worklog?{query}"), "worklog")
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::worklog).collect())
            .unwrap_or_default())
    }

    /// Move an issue to another queue.
    ///
    /// The issue keeps its identity and loses its name: `PROJ-42` becomes
    /// `OTHER-17`, and there is no request that undoes it. Tracker drops fields
    /// the target queue does not define unless `moveAllFields` says otherwise,
    /// so that choice is the caller's rather than a default we picked for them.
    pub async fn move_issue(
        &self,
        key: &str,
        queue: &str,
        keep_fields: bool,
        initial_status: bool,
    ) -> Result<Issue, ApiError> {
        let path = format!(
            "/v3/issues/{key}/_move?queue={queue}&moveAllFields={keep_fields}&initialStatus={initial_status}"
        );
        let (raw, _) = self
            .send_value(
                reqwest::Method::POST,
                &path,
                Some(&serde_json::json!({})),
                &format!("move {key} to {queue}"),
            )
            .await?;

        parse::issue(&raw).ok_or_else(|| ApiError::NotFound(format!("issue {key} after the move")))
    }

    /// What changed on an issue, newest last.
    ///
    /// Tracker pages this with an opaque cursor rather than page numbers, and
    /// the cursor is only worth spending when somebody asks for more than the
    /// first page — which nobody has yet. So this asks for one page, and the
    /// caller says how big.
    pub async fn changelog(&self, key: &str, per_page: u32) -> Result<Vec<Change>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/changelog?perPage={per_page}"),
                &format!("changelog of {key}"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::change).collect())
            .unwrap_or_default())
    }

    /// The versions a queue defines.
    ///
    /// This is what an issue's `fixVersions` refers to; without it that field
    /// is an id with no meaning.
    pub async fn queue_versions(&self, key: &str) -> Result<Vec<Version>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/queues/{key}/versions"),
                &format!("versions of queue {key}"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Version::parse).collect())
            .unwrap_or_default())
    }

    /// The tags in use in a queue.
    pub async fn queue_tags(&self, key: &str) -> Result<Vec<String>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/queues/{key}/tags?perPage=1000"),
                &format!("tags of queue {key}"),
            )
            .await?;

        // Both shapes are accepted because the organisation this was written
        // against has no tags to answer with, and a listing that silently drops
        // every row is worse than one that reads a member it did not need.
        Ok(raw
            .as_array()
            .map(|entries| {
                entries
                    .iter()
                    .filter_map(|entry| match entry {
                        Value::String(name) => Some(name.clone()),
                        other => other
                            .get("name")
                            .and_then(Value::as_str)
                            .map(ToOwned::to_owned),
                    })
                    .collect()
            })
            .unwrap_or_default())
    }

    /// Everything that changes issues in a queue on its own.
    ///
    /// Three requests, and a refusal of one of them is an answer rather than a
    /// failure: triggers need queue-owner rights, so a member of the queue gets
    /// two sections and Tracker's own words about the third. All three failing
    /// is a different thing — a queue that is not there, or a token that is not
    /// allowed — and is reported as the error it is.
    pub async fn queue_automation(&self, key: &str) -> Result<Automation, ApiError> {
        let mut unreadable = Vec::new();
        let mut refused = None;

        let mut section = |name: &'static str, result: Result<Value, ApiError>| match result {
            Ok(value) => value.as_array().cloned().unwrap_or_default(),
            Err(error) => {
                unreadable.push(Unreadable {
                    // Tracker answers a 403 here with the queue owner's record
                    // and no message at all, so there are no words of its own
                    // to pass through. Saying which right is missing is the
                    // useful sentence, and our generic 403 — which also blames
                    // the organisation header — is not it.
                    section: name,
                    reason: match error {
                        ApiError::Forbidden => {
                            format!("{name} are readable by the queue owner only (403)")
                        }
                        ref other => other.to_string(),
                    },
                });
                refused.get_or_insert(error);
                Vec::new()
            }
        };

        let macros = section(
            "macros",
            self.get_value(
                &format!("/v3/queues/{key}/macros"),
                &format!("macros of queue {key}"),
            )
            .await,
        );
        let autoactions = section(
            "autoactions",
            self.get_value(
                &format!("/v3/queues/{key}/autoactions"),
                &format!("autoactions of queue {key}"),
            )
            .await,
        );
        let triggers = section(
            "triggers",
            self.get_value(
                &format!("/v3/queues/{key}/triggers"),
                &format!("triggers of queue {key}"),
            )
            .await,
        );

        if unreadable.len() == 3 {
            return Err(refused.unwrap_or(ApiError::NotFound(format!("queue {key}"))));
        }

        Ok(Automation {
            macros: macros.iter().filter_map(Macro::parse).collect(),
            autoactions: autoactions.iter().filter_map(AutoAction::parse).collect(),
            triggers: triggers.iter().filter_map(Trigger::parse).collect(),
            unreadable,
        })
    }

    /// The components of one queue, or of the whole organisation.
    ///
    /// Tracker filters by queue itself, so `--queue` is a different path rather
    /// than a listing narrowed here: asking for every component in order to
    /// throw most of them away is the kind of cost this tool exists to avoid.
    pub async fn components(&self, queue: Option<&str>) -> Result<Vec<Component>, ApiError> {
        let (path, what) = match queue {
            Some(queue) => (
                format!("/v3/queues/{queue}/components"),
                format!("components of queue {queue}"),
            ),
            None => ("/v3/components".to_owned(), "components".to_owned()),
        };
        let raw = self.get_value(&path, &what).await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Component::parse).collect())
            .unwrap_or_default())
    }

    /// Every kind of link two issues can have.
    ///
    /// Small, fixed and organisation-wide — six entries in the organisation
    /// this was checked against, `cloners` among them, which no write in this
    /// tool can produce.
    pub async fn link_types(&self) -> Result<Vec<LinkType>, ApiError> {
        let raw = self.get_value("/v3/linktypes", "link types").await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(LinkType::parse).collect())
            .unwrap_or_default())
    }

    /// Who may do what in a queue.
    ///
    /// Two endpoints saying two different things. `permissions` is the rule as
    /// somebody configured it — named people, groups and *roles*; `access` is
    /// the list of people it comes out as. A role like "assignee" resolves per
    /// issue, so only the second answers "am I one of them" on its own.
    ///
    /// Both are refused together in the organisation this was checked against —
    /// one right governs the pair — but they are separate endpoints, and a
    /// section refused is still an answer while the other one stands.
    pub async fn queue_access(&self, key: &str) -> Result<QueueAccess, ApiError> {
        let mut unreadable = Vec::new();
        let mut refused = None;

        let mut section = |name: &'static str, result: Result<Value, ApiError>| match result {
            Ok(value) => Permission::parse_all(&value),
            Err(error) => {
                unreadable.push(Unreadable {
                    section: name,
                    // Tracker does say why here — "you have no right to view the
                    // queue's access rights" — but our 403 flattens that into a
                    // sentence that also blames the organisation header, which
                    // is the wrong suspect for this endpoint.
                    reason: match error {
                        ApiError::Forbidden => {
                            format!(
                                "{name} are readable only by those who may see queue rights (403)"
                            )
                        }
                        ref other => other.to_string(),
                    },
                });
                refused.get_or_insert(error);
                Vec::new()
            }
        };

        let permissions = section(
            "permissions",
            self.get_value(
                &format!("/v3/queues/{key}/permissions"),
                &format!("permissions of queue {key}"),
            )
            .await,
        );
        let access = section(
            "access",
            self.get_value(
                &format!("/v3/queues/{key}/access"),
                &format!("access of queue {key}"),
            )
            .await,
        );

        if unreadable.len() == 2 {
            return Err(match refused {
                // Both sections missing means the queue is, and saying so about
                // the queue reads better than about the first endpoint tried.
                Some(ApiError::NotFound(_)) | None => ApiError::NotFound(format!("queue {key}")),
                Some(other) => other,
            });
        }

        // Whose rights these are compared against. A failure here loses the
        // `you` column and nothing else, so it is not worth failing the command
        // that did answer.
        let you = match self.myself().await {
            Ok(user) => Some(user.id),
            Err(_) => None,
        };

        Ok(QueueAccess {
            permissions,
            access,
            you,
            unreadable,
        })
    }

    /// `POST /v3/bulkchange/_update` — change many issues in one request.
    ///
    /// Tracker requires the keys: a query is refused with
    /// `issues: Требуется параметр`, so what this touches is exactly what the
    /// caller named and the confirmation that names them is the whole story.
    /// Unknown keys are refused before anything is written, naming them — which
    /// is better than the issue-at-a-time path, where the first few have already
    /// been changed by the time a later one turns out not to exist.
    ///
    /// The answer is an operation to poll, not a result: see [`Self::bulk_change`].
    pub async fn bulk_update(
        &self,
        keys: &[String],
        values: &Value,
    ) -> Result<BulkChange, ApiError> {
        let body = serde_json::json!({ "issues": keys, "values": values });
        let (value, _) = self
            .post_value("/v3/bulkchange/_update", &body, "bulk change")
            .await?;
        BulkChange::parse(&value).ok_or_else(|| ApiError::NotFound("bulk change".to_owned()))
    }

    /// `POST /v3/bulkchange/_transition` — one workflow step, many issues.
    ///
    /// `values` carries what the transition demands — a resolution, usually —
    /// exactly as the single-issue path sends it, and is omitted when empty
    /// rather than sent as `{}`.
    pub async fn bulk_transition(
        &self,
        keys: &[String],
        transition: &str,
        values: &Value,
    ) -> Result<BulkChange, ApiError> {
        let mut body = serde_json::json!({ "issues": keys, "transition": transition });
        if !values.as_object().is_some_and(serde_json::Map::is_empty)
            && let Some(object) = body.as_object_mut()
        {
            object.insert("values".to_owned(), values.clone());
        }
        let (value, _) = self
            .post_value("/v3/bulkchange/_transition", &body, "bulk change")
            .await?;
        BulkChange::parse(&value).ok_or_else(|| ApiError::NotFound("bulk change".to_owned()))
    }

    /// `POST /v3/bulkchange/_move` — many issues into one queue.
    ///
    /// Every key in the list changes, and nothing undoes that; the gate this
    /// goes through asks for `--yes` even for a single issue for that reason.
    pub async fn bulk_move(
        &self,
        keys: &[String],
        queue: &str,
        keep_fields: bool,
        initial_status: bool,
    ) -> Result<BulkChange, ApiError> {
        let body = serde_json::json!({
            "issues": keys,
            "queue": queue,
            "moveAllFields": keep_fields,
            "initialStatus": initial_status,
        });
        let (value, _) = self
            .post_value("/v3/bulkchange/_move", &body, "bulk change")
            .await?;
        BulkChange::parse(&value).ok_or_else(|| ApiError::NotFound("bulk change".to_owned()))
    }

    /// `GET /v3/bulkchange/{id}` — how far a bulk change got.
    pub async fn bulk_change(&self, id: &str) -> Result<BulkChange, ApiError> {
        let value = self
            .get_value(
                &format!("/v3/bulkchange/{id}"),
                &format!("bulk change {id}"),
            )
            .await?;
        BulkChange::parse(&value).ok_or_else(|| ApiError::NotFound(format!("bulk change {id}")))
    }

    /// `GET /v3/bulkchange/{id}/issues` — what happened to each issue.
    ///
    /// Only worth a request when the counts do not already say everything: the
    /// point of a bulk change is one request instead of fifty, and printing a
    /// line per issue that succeeded would spend the saving on the output.
    pub async fn bulk_change_issues(&self, id: &str) -> Result<Vec<BulkOutcome>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/bulkchange/{id}/issues"),
                &format!("bulk change {id}"),
            )
            .await?;
        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(BulkOutcome::parse).collect())
            .unwrap_or_default())
    }

    /// One of the four organisation-wide dictionaries.
    ///
    /// Small and unpaged — the largest of the four is statuses, in the dozens —
    /// so this asks for the whole thing and says nothing about pages.
    pub async fn dictionary(&self, kind: Dictionary) -> Result<Vec<DictEntry>, ApiError> {
        let raw = self
            .get_value(&format!("/v3/{}", kind.path()), kind.path())
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::dict_entry).collect())
            .unwrap_or_default())
    }

    /// One page of the organisation's directory.
    ///
    /// Paged, unlike the dictionaries: an organisation has as many people in it
    /// as it has people, and the one this was written against already answers
    /// with a three-figure total.
    pub async fn users(&self, page: u32, per_page: u32) -> Result<Page<Person>, ApiError> {
        let path = format!("/v3/users?page={page}&perPage={per_page}");
        let (value, headers) = self
            .send_value(reqwest::Method::GET, &path, None, "users")
            .await?;

        let items = value
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::person).collect())
            .unwrap_or_default();

        Ok(Page {
            items,
            page,
            per_page,
            total: headers
                .get("x-total-count")
                .and_then(|count| count.to_str().ok())
                .and_then(|count| count.parse().ok()),
        })
    }

    /// One person, by login or by uid.
    ///
    /// There is no `users/me`: Tracker answers 404 for it, and `myself` is the
    /// endpoint that question belongs to.
    pub async fn user(&self, who: &str) -> Result<Person, ApiError> {
        let raw = self
            .get_value(&format!("/v3/users/{who}"), &format!("user {who}"))
            .await?;

        parse::person(&raw).ok_or_else(|| ApiError::NotFound(format!("user {who}")))
    }

    /// Boards visible to the active profile.
    ///
    /// Not paginated by the endpoint, and not by us: an organisation has boards
    /// in the dozens, not the thousands.
    pub async fn boards(&self) -> Result<Vec<Board>, ApiError> {
        let raw = self.get_value("/v3/boards", "boards").await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Board::parse).collect())
            .unwrap_or_default())
    }

    /// One board.
    pub async fn board(&self, id: &str) -> Result<Board, ApiError> {
        let raw = self
            .get_value(&format!("/v3/boards/{id}"), &format!("board {id}"))
            .await?;

        Board::parse(&raw).ok_or_else(|| ApiError::NotFound(format!("board {id}")))
    }

    /// The sprints of a board.
    ///
    /// A board that cannot have sprints answers with a refusal rather than an
    /// empty list, and that refusal is passed through as Tracker worded it: a
    /// kanban board having no sprints is Tracker's answer to the question, not
    /// a failure of the command, and inventing an empty list here would hide
    /// which of the two happened.
    pub async fn sprints(&self, board: &str) -> Result<Vec<Sprint>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/boards/{board}/sprints"),
                &format!("board {board} sprints"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Sprint::parse).collect())
            .unwrap_or_default())
    }

    /// `GET /v3/sprints/{id}` — one sprint.
    ///
    /// How far through it is takes two counts on top of this, which is why it
    /// is a command of its own rather than a column in the listing: in a
    /// listing it would be two requests per row.
    pub async fn sprint(&self, id: &str) -> Result<Sprint, ApiError> {
        let raw = self
            .get_value(&format!("/v3/sprints/{id}"), &format!("sprint {id}"))
            .await?;
        Sprint::parse(&raw).ok_or_else(|| ApiError::NotFound(format!("sprint {id}")))
    }

    /// Every sprint in the organisation.
    ///
    /// `board sprints ID` needs the board first, and a sprint name is a thing
    /// people say without knowing which board it belongs to. This is the same
    /// records with the board named on each.
    pub async fn all_sprints(&self) -> Result<Vec<Sprint>, ApiError> {
        let raw = self.get_value("/v3/sprints", "sprints").await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Sprint::parse).collect())
            .unwrap_or_default())
    }

    /// The fields a queue defines itself.
    ///
    /// Not a subset of [`Self::queue_fields`], which lists everything the queue
    /// can use: a local field belongs to the queue, is invisible to the
    /// organisation-wide listing, and cannot be fetched through `/v3/fields` at
    /// all. So these carry their full definition — what they accept included —
    /// because there is no second command that could answer that for them.
    pub async fn queue_local_fields(&self, key: &str) -> Result<Vec<FieldSpec>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/queues/{key}/localFields"),
                &format!("local fields of queue {key}"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(FieldSpec::parse).collect())
            .unwrap_or_default())
    }

    /// Create a project, portfolio or goal with nothing but a name.
    ///
    /// Everything else about an entity is optional, and a command line is not
    /// where a portfolio's description gets written.
    pub async fn create_entity(&self, kind: &str, fields: &Value) -> Result<Entity, ApiError> {
        let body = serde_json::json!({ "fields": fields });
        let (value, _) = self
            .post_value(
                &format!("/v3/entities/{kind}?fields={ENTITY_FIELDS}"),
                &body,
                kind,
            )
            .await?;

        parse::entity(&value).ok_or_else(|| ApiError::NotFound(kind.to_owned()))
    }

    /// Delete a project, portfolio or goal.
    ///
    /// Entities can be deleted; issues cannot. That asymmetry is why the live
    /// suite may write entities and may not write issues without being told a
    /// queue to sacrifice.
    pub async fn delete_entity(&self, kind: &str, id: &str) -> Result<(), ApiError> {
        self.send_value(
            reqwest::Method::DELETE,
            &format!("/v3/entities/{kind}/{id}"),
            None,
            &format!("{kind} {id}"),
        )
        .await?;
        Ok(())
    }

    /// Change the fields of a project, portfolio or goal.
    ///
    /// Quotes the version for the same reason [`Self::place_entity`] does: a
    /// write without one lands on top of whatever happened in between.
    pub async fn update_entity(
        &self,
        kind: &str,
        id: &str,
        fields: &Value,
        version: Option<u64>,
    ) -> Result<Entity, ApiError> {
        let path = match version {
            Some(version) => {
                format!("/v3/entities/{kind}/{id}?version={version}&fields={ENTITY_FIELDS}")
            }
            None => format!("/v3/entities/{kind}/{id}?fields={ENTITY_FIELDS}"),
        };
        let body = serde_json::json!({ "fields": fields });

        let (value, _) = self
            .send_value(
                reqwest::Method::PATCH,
                &path,
                Some(&body),
                &format!("{kind} {id}"),
            )
            .await?;

        parse::entity(&value).ok_or_else(|| ApiError::NotFound(format!("{kind} {id}")))
    }

    /// Put an entity inside a portfolio, or take it out of one.
    ///
    /// `version` is Tracker's optimistic-concurrency counter and is quoted on
    /// purpose: without it the write lands whatever happened in between, and
    /// with it a portfolio that moved under us answers 412 instead of being
    /// silently overwritten.
    pub async fn place_entity(
        &self,
        kind: &str,
        id: &str,
        parent: Option<&str>,
        version: Option<u64>,
    ) -> Result<Entity, ApiError> {
        // The response is the entity as it now stands, but only of the fields
        // asked for — without this it comes back with an empty `fields` and the
        // command prints a blank summary after a write that worked.
        let path = match version {
            Some(version) => {
                format!("/v3/entities/{kind}/{id}?version={version}&fields={ENTITY_FIELDS}")
            }
            None => format!("/v3/entities/{kind}/{id}?fields={ENTITY_FIELDS}"),
        };
        let body = serde_json::json!({
            "fields": { "parentEntity": place_body(parent) }
        });

        let (value, _) = self
            .send_value(
                reqwest::Method::PATCH,
                &path,
                Some(&body),
                &format!("{kind} {id}"),
            )
            .await?;

        parse::entity(&value).ok_or_else(|| ApiError::NotFound(format!("{kind} {id}")))
    }

    /// One queue and its settings.
    pub async fn queue(&self, key: &str) -> Result<QueueSettings, ApiError> {
        let raw = self
            .get_value(&format!("/v3/queues/{key}"), &format!("queue {key}"))
            .await?;

        QueueSettings::parse(&raw).ok_or_else(|| ApiError::NotFound(format!("queue {key}")))
    }

    /// The parts of a queue that another queue can be built from.
    ///
    /// `issueTypesConfig` pairs each issue type with a workflow and a set of
    /// resolutions, and workflow ids are organisation-specific strings nobody
    /// has memorised. Copying them from a queue that already works is the only
    /// way to create one from a command line without asking for internals.
    pub async fn queue_blueprint(&self, key: &str) -> Result<Blueprint, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/queues/{key}?expand=all"),
                &format!("queue {key}"),
            )
            .await?;

        let named = |name: &str| {
            raw.get(name)
                .and_then(|field| field.get("key"))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned)
        };

        let types = raw
            .get("issueTypesConfig")
            .and_then(Value::as_array)
            .map(|entries| {
                entries
                    .iter()
                    .filter_map(|entry| {
                        Some(serde_json::json!({
                            "issueType": entry.get("issueType")?.get("key")?.as_str()?,
                            "workflow": entry.get("workflow")?.get("id")?.as_str()?,
                            "resolutions": entry
                                .get("resolutions")
                                .and_then(Value::as_array)
                                .map(|resolutions| {
                                    resolutions
                                        .iter()
                                        .filter_map(|resolution| {
                                            resolution.get("key").and_then(Value::as_str)
                                        })
                                        .collect::<Vec<_>>()
                                })
                                .unwrap_or_default(),
                        }))
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        if types.is_empty() {
            return Err(ApiError::NotFound(format!("issue types of queue {key}")));
        }

        Ok(Blueprint {
            default_type: named("defaultType"),
            default_priority: named("defaultPriority"),
            issue_types: types,
        })
    }

    /// Create a queue.
    pub async fn create_queue(&self, body: &Value) -> Result<QueueSettings, ApiError> {
        let (value, _) = self.post_value("/v3/queues", body, "queue").await?;

        QueueSettings::parse(&value)
            .ok_or_else(|| ApiError::NotFound("the created queue".to_owned()))
    }

    /// Every field defined in the organisation, not just one queue's.
    ///
    /// `queue fields` answers "what can I set on an issue here"; this answers
    /// "what exists at all", which is the question behind a field that a queue
    /// does not show.
    pub async fn fields(&self) -> Result<Vec<QueueField>, ApiError> {
        let raw = self.get_value("/v3/fields", "fields").await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(QueueField::parse).collect())
            .unwrap_or_default())
    }

    /// One field's definition, by the key `queue fields` prints.
    ///
    /// A local field defined inside one queue is not reachable here — it lives
    /// under the queue — and Tracker answers 404 for it, which is the honest
    /// answer rather than one worth papering over.
    pub async fn field(&self, key: &str) -> Result<FieldSpec, ApiError> {
        let raw = self
            .get_value(&format!("/v3/fields/{key}"), &format!("field {key}"))
            .await?;

        FieldSpec::parse(&raw).ok_or_else(|| ApiError::NotFound(format!("field {key}")))
    }

    /// Issue or comment templates.
    ///
    /// The path is `issueTemplates` and `commentTemplates`; there is no
    /// `_templates` collection, which is worth writing down because every
    /// plausible guess at one answers 400 or 404.
    pub async fn templates(&self, kind: TemplateKind) -> Result<Vec<Template>, ApiError> {
        let raw = self
            .get_value(&format!("/v3/{}", kind.path()), kind.path())
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(Template::parse).collect())
            .unwrap_or_default())
    }

    /// The comments of an issue.
    ///
    /// Fetched in one generous page: an issue with more than a hundred comments
    /// is rare enough that paginating here would cost more in complexity than it
    /// saves anyone.
    pub async fn issue_comments(&self, key: &str) -> Result<Vec<Comment>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/issues/{key}/comments?perPage=100"),
                &format!("issue {key} comments"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(parse::comment).collect())
            .unwrap_or_default())
    }

    /// The fields of a queue, including custom ones, as `(key, name, type)`.
    pub async fn queue_fields(&self, key: &str) -> Result<Vec<QueueField>, ApiError> {
        let raw = self
            .get_value(
                &format!("/v3/queues/{key}/fields"),
                &format!("queue {key} fields"),
            )
            .await?;

        Ok(raw
            .as_array()
            .map(|entries| entries.iter().filter_map(QueueField::parse).collect())
            .unwrap_or_default())
    }

    /// A POST that also hands back the response headers, which is where Tracker
    /// puts the pagination totals.
    async fn post_value(
        &self,
        path: &str,
        body: &Value,
        what: &str,
    ) -> Result<(Value, reqwest::header::HeaderMap), ApiError> {
        self.send_value(reqwest::Method::POST, path, Some(body), what)
            .await
    }

    async fn send_value(
        &self,
        method: reqwest::Method,
        path: &str,
        body: Option<&Value>,
        what: &str,
    ) -> Result<(Value, reqwest::header::HeaderMap), ApiError> {
        let url = format!("{}{path}", self.base_url);

        let send = || async {
            let mut request = self.http.request(method.clone(), &url);
            if let Some(body) = body {
                request = request.json(body);
            }
            let response = request.send().await?;
            let headers = response.headers().clone();
            let text = classify(response, what).await?;
            Ok((text, headers))
        };

        // Only idempotent work is retried. Re-sending a create after a timeout
        // would risk a duplicate issue, which is worse than a clear failure.
        let (text, headers) = if method == reqwest::Method::GET {
            send.retry(
                ExponentialBuilder::default()
                    .with_max_times(self.retries)
                    .with_jitter(),
            )
            .when(is_retryable)
            .await?
        } else {
            send().await?
        };

        // A successful write may answer with an empty body.
        let value = if text.trim().is_empty() {
            Value::Null
        } else {
            serde_json::from_str(&text).map_err(ApiError::Decode)?
        };
        Ok((value, headers))
    }

    async fn get_value(&self, path: &str, what: &str) -> Result<Value, ApiError> {
        Ok(self
            .send_value(reqwest::Method::GET, path, None, what)
            .await?
            .0)
    }
}

/// Which organisation-wide dictionary to read.
///
/// The four endpoints answer with the same shape but are not spelled the way
/// the values are: the endpoint is `issuetypes`, the field on an issue is
/// `type`, and the flag people reach for is `--type`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dictionary {
    Types,
    Priorities,
    Statuses,
    Resolutions,
}

impl Dictionary {
    /// Every dictionary, in the order a listing shows them: what an issue *is*,
    /// then how urgent, then where it stands, then how it ended.
    pub const ALL: [Self; 4] = [
        Self::Types,
        Self::Priorities,
        Self::Statuses,
        Self::Resolutions,
    ];

    #[must_use]
    pub fn path(self) -> &'static str {
        match self {
            Self::Types => "issuetypes",
            Self::Priorities => "priorities",
            Self::Statuses => "statuses",
            Self::Resolutions => "resolutions",
        }
    }

    /// What to call it in output, singular-free: these are always lists.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Types => "types",
            Self::Priorities => "priorities",
            Self::Statuses => "statuses",
            Self::Resolutions => "resolutions",
        }
    }
}

/// A workflow transition available from the current status.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Transition {
    pub id: String,
    pub name: String,
    /// The status the issue lands in.
    pub to: Option<String>,
    /// That status's key, which unlike `to` is the same in every organisation.
    ///
    /// Transition ids are per workflow — `close`, `closed` and `close_issue`
    /// are all real — so this is what lets a caller ask for a *status* and have
    /// the id found for them.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_key: Option<String>,
}

impl Transition {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: value.get("id").and_then(Value::as_str)?.to_owned(),
            name: value
                .get("display")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            to: value
                .get("to")
                .and_then(|to| to.get("display").or_else(|| to.get("key")))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            to_key: value
                .get("to")
                .and_then(|to| to.get("key"))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// A queue, reduced to what a listing shows.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Queue {
    pub key: String,
    pub name: String,
    pub lead: Option<String>,
}

impl Queue {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            key: value.get("key").and_then(Value::as_str)?.to_owned(),
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            lead: value
                .get("lead")
                .and_then(|lead| {
                    lead.get("login")
                        .or_else(|| lead.get("display"))
                        .or_else(|| lead.get("id"))
                })
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// A release a queue tracks work against.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Version {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    /// `released`, `archived`, or `open` when it is neither.
    pub state: &'static str,
    pub due: Option<String>,
}

impl Version {
    fn parse(value: &Value) -> Option<Self> {
        let flag = |member: &str| value.get(member).and_then(Value::as_bool).unwrap_or(false);

        Some(Self {
            id: match value.get("id")? {
                Value::String(id) => id.clone(),
                other => other.to_string(),
            },
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            description: value
                .get("description")
                .and_then(Value::as_str)
                .filter(|text| !text.is_empty())
                .map(ToOwned::to_owned),
            // Archived wins over released: an archived version is out of use
            // whether or not it ever shipped.
            state: if flag("archived") {
                "archived"
            } else if flag("released") {
                "released"
            } else {
                "open"
            },
            due: value
                .get("dueDate")
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// A board, reduced to what a listing shows.
///
/// Columns are the reason to look at a board from a command line: they are the
/// statuses the board arranges work by, in the order it arranges them.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Board {
    pub id: String,
    pub name: String,
    pub columns: Vec<String>,
    /// The field the board estimates by, when it estimates.
    pub estimate_by: Option<String>,
    pub owner: Option<String>,
}

impl Board {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: match value.get("id")? {
                Value::String(id) => id.clone(),
                other => other.to_string(),
            },
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            columns: value
                .get("columns")
                .and_then(Value::as_array)
                .map(|columns| {
                    columns
                        .iter()
                        .filter_map(|column| {
                            column
                                .get("display")
                                .or_else(|| column.get("id"))
                                .and_then(Value::as_str)
                                .map(ToOwned::to_owned)
                        })
                        .collect()
                })
                .unwrap_or_default(),
            estimate_by: value
                .get("estimateBy")
                .and_then(|field| field.get("id").or_else(|| field.get("display")))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            // Boards carry `createdBy`, not a lead, and a real organisation
            // showed that user has a display name and no login.
            owner: value
                .get("createdBy")
                .and_then(|user| {
                    user.get("login")
                        .or_else(|| user.get("display"))
                        .or_else(|| user.get("id"))
                })
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// One sprint of a board.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Sprint {
    pub id: String,
    pub name: String,
    pub status: Option<String>,
    pub start: Option<String>,
    pub end: Option<String>,
    /// Which board it belongs to. Absent when the sprint was read through that
    /// board, which already named it, and present when it was listed across the
    /// organisation, where it is what makes two sprints called "Sprint 1"
    /// tellable apart.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub board: Option<String>,
}

impl Sprint {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: match value.get("id")? {
                Value::String(id) => id.clone(),
                other => other.to_string(),
            },
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            status: value
                .get("status")
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            start: value
                .get("startDate")
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            end: value
                .get("endDate")
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            board: value
                .get("board")
                .and_then(|board| board.get("display").or_else(|| board.get("id")))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// The parts of an existing queue a new one can be built from.
#[derive(Debug, Clone)]
pub struct Blueprint {
    pub default_type: Option<String>,
    pub default_priority: Option<String>,
    /// `issueTypesConfig` as the create endpoint takes it: keys and ids, not
    /// the expanded objects the read answers with.
    pub issue_types: Vec<Value>,
}

/// What `parentEntity` is set to: a portfolio, or nothing.
///
/// Removing is `null`, not an empty object — an empty object is a change
/// Tracker accepts and ignores, which reads as success and is not.
fn place_body(parent: Option<&str>) -> Value {
    match parent {
        Some(parent) => serde_json::json!({ "primary": parent }),
        None => Value::Null,
    }
}

/// A queue with the settings that decide what an issue in it starts as.
///
/// The defaults are the point: `issue create -q PROJ` without a type or a
/// priority gets these, and nothing else says what they are.
#[derive(Debug, Clone, serde::Serialize)]
pub struct QueueSettings {
    pub key: String,
    pub name: String,
    pub lead: Option<String>,
    pub default_type: Option<String>,
    pub default_priority: Option<String>,
    pub version: Option<u64>,
}

impl QueueSettings {
    fn parse(value: &Value) -> Option<Self> {
        let named = |name: &str| {
            value
                .get(name)
                .and_then(|field| field.get("key").or_else(|| field.get("display")))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned)
        };

        Some(Self {
            key: value.get("key").and_then(Value::as_str)?.to_owned(),
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            lead: value
                .get("lead")
                .and_then(|lead| {
                    lead.get("login")
                        .or_else(|| lead.get("display"))
                        .or_else(|| lead.get("id"))
                })
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            default_type: named("defaultType"),
            default_priority: named("defaultPriority"),
            version: value.get("version").and_then(Value::as_u64),
        })
    }
}

/// Which templates are being asked for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateKind {
    Issue,
    Comment,
}

impl TemplateKind {
    #[must_use]
    pub const fn path(self) -> &'static str {
        match self {
            Self::Issue => "issueTemplates",
            Self::Comment => "commentTemplates",
        }
    }
}

/// One template, reduced to what a listing shows.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Template {
    pub id: String,
    pub name: String,
    /// The queue a template belongs to, when it belongs to one.
    pub queue: Option<String>,
    pub author: Option<String>,
}

impl Template {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: match value.get("id")? {
                Value::String(id) => id.clone(),
                other => other.to_string(),
            },
            name: value
                .get("name")
                .or_else(|| value.get("summary"))
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            queue: value
                .get("queue")
                .and_then(|queue| queue.get("key").or_else(|| queue.get("id")).or(Some(queue)))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            author: value
                .get("createdBy")
                .or_else(|| value.get("author"))
                .and_then(|user| {
                    user.get("login")
                        .or_else(|| user.get("display"))
                        .or_else(|| user.get("id"))
                })
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
        })
    }
}

/// One field of a queue. `queue fields` is how a caller learns the keys that
/// `--fields` and `--set` accept, so the key matters more than the name here.
#[derive(Debug, Clone, serde::Serialize)]
pub struct QueueField {
    pub key: String,
    pub name: String,
    pub field_type: String,
    /// A field Tracker ships with, as opposed to one this queue defines.
    pub system: bool,
}

impl QueueField {
    fn parse(value: &Value) -> Option<Self> {
        let id = value.get("id").and_then(Value::as_str)?;
        Some(Self {
            // Custom fields are addressed by the trailing segment of a
            // dotted id (`60...--storyPoints`), which is what the API accepts
            // back and what a caller can reasonably type.
            key: id.rsplit("--").next().unwrap_or(id).to_owned(),
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or(id)
                .to_owned(),
            field_type: value
                .get("schema")
                .and_then(|schema| schema.get("type"))
                .and_then(Value::as_str)
                .unwrap_or("unknown")
                .to_owned(),
            system: !id.contains("--"),
        })
    }
}

/// One kind of relationship two issues can have.
///
/// Deliberately not folded into [`Dictionary`]: the four dictionaries are values
/// a *field* takes and share one shape, and this has neither a key nor a name —
/// it has an id and two labels, one per direction. It is also not the vocabulary
/// a write takes, which is the whole reason it is worth printing.
#[derive(Debug, Clone, serde::Serialize)]
pub struct LinkType {
    pub id: String,
    /// Tracker's wording for the end the link points away from.
    pub outward: Option<String>,
    /// Tracker's wording for the end it points at.
    pub inward: Option<String>,
}

impl BulkChange {
    /// Whether Tracker is done with it, one way or the other.
    #[must_use]
    pub fn finished(&self) -> bool {
        matches!(self.status.as_str(), "COMPLETE" | "FAILED")
    }

    /// Whether every issue it was given actually changed.
    ///
    /// `COMPLETE` alone does not say this: a change can finish having changed
    /// nothing, and that must not exit zero.
    #[must_use]
    pub fn succeeded(&self) -> bool {
        self.status == "COMPLETE" && self.done.is_some() && self.done == self.total
    }

    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: value.get("id").and_then(Value::as_str)?.to_owned(),
            status: value
                .get("status")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            status_text: value
                .get("statusText")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            total: value.get("totalIssues").and_then(Value::as_u64),
            done: value.get("totalCompletedIssues").and_then(Value::as_u64),
        })
    }
}

impl BulkOutcome {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            key: value
                .get("issue")
                .and_then(|issue| issue.get("key"))
                .and_then(Value::as_str)?
                .to_owned(),
            status: value
                .get("status")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            error: value.get("error").and_then(field_errors),
        })
    }
}

/// Tracker's per-field complaints, joined into one sentence.
///
/// The shape is the same envelope every rejection uses — `errors` keyed by
/// field, `errorMessages` for the rest — and both halves are passed through as
/// written. A message about somebody's field is theirs, not ours to reword.
fn field_errors(error: &Value) -> Option<String> {
    let mut parts: Vec<String> = error
        .get("errors")
        .and_then(Value::as_object)
        .map(|fields| {
            fields
                .iter()
                .filter_map(|(field, message)| {
                    message
                        .as_str()
                        .map(|message| format!("{field}: {message}"))
                })
                .collect()
        })
        .unwrap_or_default();
    parts.extend(
        error
            .get("errorMessages")
            .and_then(Value::as_array)
            .map(|messages| {
                messages
                    .iter()
                    .filter_map(Value::as_str)
                    .map(ToOwned::to_owned)
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default(),
    );

    if parts.is_empty() {
        None
    } else {
        Some(parts.join("; "))
    }
}

impl Permission {
    /// Every operation in the answer, in a fixed order.
    ///
    /// Tracker's own order is whatever the JSON object happened to have, and
    /// the order of these columns is a contract. `create` before `read` before
    /// the two kinds of `write` before `grant` runs from the least to the most
    /// a right lets somebody do; anything Tracker adds later lands after them
    /// rather than silently between them.
    fn parse_all(value: &Value) -> Vec<Self> {
        const ORDER: [&str; 5] = ["create", "read", "write", "writeNoAssign", "grant"];

        let Some(object) = value.as_object() else {
            return Vec::new();
        };

        let known = ORDER
            .iter()
            .filter_map(|name| object.get(*name).map(|entry| Self::parse(name, entry)));
        let rest = object
            .iter()
            .filter(|(name, entry)| !ORDER.contains(&name.as_str()) && entry.is_object())
            // `self` and `version` are the answer's own metadata, not
            // operations, and they are objects nowhere — but the filter above
            // is about names, so they are named here too.
            .filter(|(name, _)| !matches!(name.as_str(), "self" | "version"))
            .map(|(name, entry)| Self::parse(name, entry));

        known.chain(rest).collect()
    }

    fn parse(operation: &str, value: &Value) -> Self {
        let holders = |member: &str| {
            value
                .get(member)
                .and_then(Value::as_array)
                .map(|entries| entries.iter().filter_map(Holder::parse).collect())
                .unwrap_or_default()
        };
        Self {
            operation: operation.to_owned(),
            users: holders("users"),
            groups: holders("groups"),
            roles: holders("roles"),
        }
    }
}

impl Holder {
    fn parse(value: &Value) -> Option<Self> {
        let id = id_of(value)?;
        Some(Self {
            display: value
                .get("display")
                .and_then(Value::as_str)
                // A holder with no display is still a holder; the id is a worse
                // name than the display and a better one than nothing.
                .map_or_else(|| id.clone(), ToOwned::to_owned),
            id,
        })
    }
}

impl LinkType {
    fn parse(value: &Value) -> Option<Self> {
        let text = |member: &str| {
            value
                .get(member)
                .and_then(Value::as_str)
                .map(str::to_lowercase)
        };
        Some(Self {
            id: value.get("id").and_then(Value::as_str)?.to_owned(),
            outward: text("outward"),
            inward: text("inward"),
        })
    }
}

/// A part of the product a queue splits its work by.
///
/// `components` is a field on every issue, and until it could be listed a write
/// to it was a guess. Half of them have no lead, so that column is genuinely
/// optional rather than defensively so.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Component {
    pub id: String,
    pub name: String,
    /// The queue it belongs to. A component belongs to exactly one.
    pub queue: Option<String>,
    pub lead: Option<String>,
    /// Whether adding this component assigns the issue to its lead. It changes
    /// what a write does, which is why it is a column and not a detail.
    pub assign_auto: bool,
    pub description: Option<String>,
}

impl Component {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: id_of(value)?,
            name: named(value),
            queue: value
                .get("queue")
                .and_then(|queue| queue.get("key").or_else(|| queue.get("display")))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            lead: value
                .get("lead")
                .and_then(|lead| {
                    lead.get("login")
                        .or_else(|| lead.get("display"))
                        .or_else(|| lead.get("id"))
                })
                .and_then(Value::as_str)
                .map(ToOwned::to_owned),
            assign_auto: value
                .get("assignAuto")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            description: value
                .get("description")
                .and_then(Value::as_str)
                .filter(|text| !text.is_empty())
                .map(ToOwned::to_owned),
        })
    }
}

/// What changes issues in a queue without anybody touching them.
///
/// One answer assembled from three endpoints, because they are three halves of
/// one question: an issue whose changelog says it was updated by the Tracker
/// robot was changed by one of these.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Automation {
    pub macros: Vec<Macro>,
    pub autoactions: Vec<AutoAction>,
    pub triggers: Vec<Trigger>,
    /// The parts Tracker would not show, in its own words.
    ///
    /// Triggers need queue-owner rights and answer 403 to everybody else. Two
    /// sections out of three is a useful answer, and failing the whole command
    /// because of the third would throw them away.
    pub unreadable: Vec<Unreadable>,
}

/// A change to many issues at once, which Tracker performs in the background.
#[derive(Debug, Clone, serde::Serialize)]
pub struct BulkChange {
    pub id: String,
    /// `CREATED`, `COMPLETE`, `FAILED` are the ones this has seen. Anything else
    /// is treated as still running rather than as an outcome, because guessing
    /// which way an unknown status went is the one thing worth not doing here.
    pub status: String,
    /// Tracker's own sentence, in the organisation's language.
    pub status_text: String,
    /// How many issues the change is about, once Tracker has counted them.
    pub total: Option<u64>,
    /// How many of them it finished. The tally a bulk change ends with.
    pub done: Option<u64>,
}

/// What happened to one issue in a bulk change.
#[derive(Debug, Clone, serde::Serialize)]
pub struct BulkOutcome {
    pub key: String,
    pub status: String,
    /// Tracker's own words about why this one did not change.
    pub error: Option<String>,
}

/// Who may do what in a queue: the rules, and the people they come out as.
#[derive(Debug, Clone, serde::Serialize)]
pub struct QueueAccess {
    /// The rule per operation: named holders and roles.
    pub permissions: Vec<Permission>,
    /// The people per operation, with the roles already resolved.
    pub access: Vec<Permission>,
    /// The id of the user the token belongs to, when it could be read. What
    /// makes "who is allowed" into "am I allowed".
    pub you: Option<String>,
    pub unreadable: Vec<Unreadable>,
}

/// One operation, and everybody who holds it.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Permission {
    /// `create`, `read`, `write`, `writeNoAssign`, `grant`.
    pub operation: String,
    pub users: Vec<Holder>,
    /// Documented, and absent from every queue this was checked against — so
    /// parsed, printed when present, and claimed about no further than that.
    pub groups: Vec<Holder>,
    /// `queue-lead`, `assignee`, `author`, `follower`, `access`. A role is not
    /// a set of people: which issue is being touched decides who is in it.
    pub roles: Vec<Holder>,
}

/// Somebody or something that holds a right.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Holder {
    pub id: String,
    /// Tracker's own wording, in the organisation's language.
    pub display: String,
}

/// One section that could not be read, and why.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Unreadable {
    pub section: &'static str,
    pub reason: String,
}

/// A canned change somebody applies by hand from the issue page.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Macro {
    pub id: String,
    pub name: String,
    /// The comment it posts, when it posts one.
    pub body: Option<String>,
    /// Which fields it writes. The keys, not the localised names, because the
    /// keys are what every other command here takes.
    pub updates: Vec<String>,
}

/// A change Tracker applies on a schedule to whatever matches a filter.
#[derive(Debug, Clone, serde::Serialize)]
pub struct AutoAction {
    pub id: String,
    pub name: String,
    pub active: bool,
    /// The kinds of action it performs — `Transition`, `Update`, and the rest.
    pub actions: Vec<String>,
    /// How often it runs, in seconds.
    pub interval: Option<u64>,
}

/// A change Tracker applies the moment something happens to an issue.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Trigger {
    pub id: String,
    pub name: String,
    pub active: bool,
    pub actions: Vec<String>,
    /// How many conditions have to hold. The conditions themselves are a tree
    /// of Tracker's own classes, and printing it would be longer than it is
    /// useful.
    pub conditions: usize,
}

/// The `id` of anything under a queue, whether Tracker sent it as a number or a
/// string.
fn id_of(value: &Value) -> Option<String> {
    Some(match value.get("id")? {
        Value::String(id) => id.clone(),
        other => other.to_string(),
    })
}

/// The `type` of each entry of an array, which is how Tracker names an action.
fn types_in(value: Option<&Value>) -> Vec<String> {
    value
        .and_then(Value::as_array)
        .map(|entries| {
            entries
                .iter()
                .filter_map(|entry| entry.get("type").and_then(Value::as_str))
                .map(ToOwned::to_owned)
                .collect()
        })
        .unwrap_or_default()
}

fn named(value: &Value) -> String {
    value
        .get("name")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_owned()
}

impl Macro {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: id_of(value)?,
            name: named(value),
            body: value
                .get("body")
                .and_then(Value::as_str)
                .filter(|text| !text.is_empty())
                .map(ToOwned::to_owned),
            updates: value
                .get("issueUpdate")
                .and_then(Value::as_array)
                .map(|updates| {
                    updates
                        .iter()
                        .filter_map(|update| {
                            update
                                .get("field")
                                .and_then(|field| field.get("id"))
                                .and_then(Value::as_str)
                        })
                        .map(|id| id.rsplit("--").next().unwrap_or(id).to_owned())
                        .collect()
                })
                .unwrap_or_default(),
        })
    }
}

impl AutoAction {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: id_of(value)?,
            name: named(value),
            active: value
                .get("active")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            actions: types_in(value.get("actions")),
            // Milliseconds on the wire; seconds is what a person says out loud.
            interval: value
                .get("intervalMillis")
                .and_then(Value::as_u64)
                .map(|millis| millis / 1000),
        })
    }
}

impl Trigger {
    fn parse(value: &Value) -> Option<Self> {
        Some(Self {
            id: id_of(value)?,
            name: named(value),
            active: value
                .get("active")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            actions: types_in(value.get("actions")),
            conditions: value
                .get("conditions")
                .and_then(Value::as_array)
                .map_or(0, Vec::len),
        })
    }
}

/// One field's definition: what it holds, whether it can be written, and what
/// values it accepts.
///
/// `queue fields` lists the keys; this answers the question that follows, which
/// is the one `--set` is otherwise guessing at.
#[derive(Debug, Clone, serde::Serialize)]
pub struct FieldSpec {
    pub key: String,
    pub name: String,
    /// `string`, `float`, `user`, `datetime` — Tracker's own vocabulary, which
    /// is what its error messages quote back.
    pub field_type: String,
    /// What one element is, when the field holds several of them. `None` means
    /// the field takes a single value.
    pub items: Option<String>,
    pub required: bool,
    pub readonly: bool,
    /// Where Tracker files the field: `Системные`, `Agile`, and whatever the
    /// organisation added. In the organisation's own language.
    pub category: Option<String>,
    /// How the accepted values are decided, when they are decided at all.
    pub options: Option<FieldOptions>,
}

/// What a constrained field will accept.
///
/// Two cases, and telling them apart is the point: a fixed list carries its
/// values here, and everything else names a provider that answers from
/// somewhere else in the organisation — the directory, the queue, the board.
#[derive(Debug, Clone, serde::Serialize)]
pub struct FieldOptions {
    /// Tracker's class name for the provider, passed through unchanged: an
    /// unrecognised one still says something, and inventing a friendlier word
    /// for it would only be a word we would have to keep in step.
    pub provider: String,
    pub values: Vec<String>,
}

impl FieldSpec {
    fn parse(value: &Value) -> Option<Self> {
        let id = value.get("id").and_then(Value::as_str)?;
        let schema = value.get("schema");
        let string_at = |parent: Option<&Value>, member: &str| {
            parent
                .and_then(|parent| parent.get(member))
                .and_then(Value::as_str)
                .map(ToOwned::to_owned)
        };

        let options = value.get("optionsProvider").map(|provider| FieldOptions {
            provider: provider
                .get("type")
                .and_then(Value::as_str)
                .unwrap_or("unknown")
                .to_owned(),
            // Values arrive as whatever the field holds — the numbers 0 and 1
            // for a flag, strings for a list — and a caller has to type them
            // back either way.
            values: provider
                .get("values")
                .and_then(Value::as_array)
                .map(|values| {
                    values
                        .iter()
                        .map(|value| match value {
                            Value::String(text) => text.clone(),
                            other => other.to_string(),
                        })
                        .collect()
                })
                .unwrap_or_default(),
        });

        Some(Self {
            key: id.rsplit("--").next().unwrap_or(id).to_owned(),
            name: value
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or(id)
                .to_owned(),
            field_type: string_at(schema, "type").unwrap_or_else(|| "unknown".to_owned()),
            items: string_at(schema, "items"),
            required: schema
                .and_then(|schema| schema.get("required"))
                .and_then(Value::as_bool)
                .unwrap_or(false),
            readonly: value
                .get("readonly")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            category: string_at(value.get("category"), "display"),
            options,
        })
    }
}

/// The checklist out of whatever Tracker answered a checklist write with.
///
/// It replies with the issue, not the item, so the list is under
/// `checklistItems`; a bare array is accepted too, because an endpoint that
/// changes its mind about the envelope should not empty somebody's checklist.
fn checklist_of(value: &Value) -> Vec<ChecklistItem> {
    let entries = value
        .get("checklistItems")
        .and_then(Value::as_array)
        .or_else(|| value.as_array());

    entries
        .map(|entries| entries.iter().filter_map(parse::checklist_item).collect())
        .unwrap_or_default()
}

/// Turn a response into either its body or a typed error.
///
/// `what` names the thing being fetched so a 404 can say which one, rather than
/// leaving the caller to guess between the issue and one of its subresources.
async fn classify(response: reqwest::Response, what: &str) -> Result<String, ApiError> {
    let status = response.status();
    if status.is_success() {
        return Ok(response.text().await?);
    }

    let message = response.text().await.unwrap_or_default();
    Err(match status.as_u16() {
        401 => ApiError::Unauthorized,
        403 => ApiError::Forbidden,
        404 => ApiError::NotFound(what.to_owned()),
        429 => ApiError::RateLimited,
        _ => ApiError::Rejected {
            status,
            message: complaint(&message),
        },
    })
}

/// What Tracker actually said, out of the envelope it says it in.
///
/// A rejection arrives as `{"errors": …, "errorMessages": […], "statusCode": …}`,
/// and printing the whole envelope buries the one sentence a caller can act on
/// under punctuation it cannot. The body is kept verbatim when it is not that
/// shape, since an unrecognised error is exactly when guessing is worst.
fn complaint(body: &str) -> String {
    let messages = serde_json::from_str::<Value>(body)
        .ok()
        .and_then(|value| {
            let mut said: Vec<String> = value
                .get("errorMessages")
                .and_then(Value::as_array)
                .map(|entries| {
                    entries
                        .iter()
                        .filter_map(Value::as_str)
                        .map(ToOwned::to_owned)
                        .collect()
                })
                .unwrap_or_default();
            // `errors` is keyed by field, and a field-level complaint is the
            // most specific thing in the envelope when it is there.
            if let Some(errors) = value.get("errors").and_then(Value::as_object) {
                said.extend(
                    errors
                        .iter()
                        .filter_map(|(field, text)| Some(format!("{field}: {}", text.as_str()?))),
                );
            }
            (!said.is_empty()).then(|| said.join("; "))
        })
        .unwrap_or_else(|| body.to_owned());

    messages.chars().take(400).collect()
}

/// Retry transport hiccups and server-side backpressure; never retry a request
/// the server has already judged invalid.
fn is_retryable(error: &ApiError) -> bool {
    match error {
        ApiError::RateLimited => true,
        ApiError::Transport(err) => err.is_timeout() || err.is_connect(),
        ApiError::Rejected { status, .. } => status.is_server_error(),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The sentence a caller can act on, not the envelope it arrived in.
    #[test]
    fn a_rejection_reads_as_what_tracker_said() {
        assert_eq!(
            complaint(
                r#"{"errors":{},"errorMessages":["A board of this type cannot have sprints."],"statusCode":400}"#
            ),
            "A board of this type cannot have sprints."
        );
    }

    /// A field-level complaint names its field: `summary` being required is a
    /// different fix from `queue` being wrong.
    #[test]
    fn a_field_complaint_keeps_its_field() {
        assert_eq!(
            complaint(r#"{"errors":{"summary":"cannot be empty"},"errorMessages":[]}"#),
            "summary: cannot be empty"
        );
    }

    /// An unrecognised body is passed through: guessing is worst precisely when
    /// the error is one we have not seen.
    #[test]
    fn an_unfamiliar_body_survives_untouched() {
        assert_eq!(
            complaint("<html>gateway timeout</html>"),
            "<html>gateway timeout</html>"
        );
        assert_eq!(complaint("{}"), "{}");
    }

    #[test]
    fn host_comparison_ignores_scheme_path_and_case() {
        assert_eq!(
            host_of("https://API.tracker.yandex.net/v3/issues/PROJ-1"),
            host_of("https://api.tracker.yandex.net")
        );
    }

    /// The download URL is server-supplied. A different host must not match, or
    /// a crafted attachment could send this client — and its OAuth header —
    /// somewhere else entirely.
    #[test]
    fn a_different_host_does_not_match() {
        assert_ne!(
            host_of("https://evil.example.com/steal"),
            host_of("https://api.tracker.yandex.net")
        );
    }

    /// Nor a host that merely starts the same way.
    #[test]
    fn a_prefix_of_the_real_host_does_not_match() {
        assert_ne!(
            host_of("https://api.tracker.yandex.net.evil.com/steal"),
            host_of("https://api.tracker.yandex.net")
        );
    }

    #[test]
    fn a_port_is_part_of_the_host() {
        assert_ne!(
            host_of("http://127.0.0.1:9999/x"),
            host_of("http://127.0.0.1:8888")
        );
    }
}