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
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! API entities
//!
//! These entities are exposed by Gitlab via its API.
//!
//! There are some places where Gitlab does not completely specify its types. This causes
//! problems when the types and names change inside of those. If found, issues should be filed
//! upstream.

use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use chrono::{DateTime, NaiveDate, Utc};
use serde::de::{DeserializeOwned, Error};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{self, Value};

// This is only used in internal API calls.
//#[derive(Serialize, Deserialize, Debug, Clone)]
//pub struct UserSafe {
//    pub username: String,
//    pub name: String,
//}

impl_id!(UserId, "Type-safe user ID.");

/// The states a user account can be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum UserState {
    /// The user is active and may perform actions.
    #[serde(rename = "active")]
    Active,
    /// Blocked from logging in.
    #[serde(rename = "blocked")]
    Blocked,
    /// Blocked from logging in via LDAP.
    #[serde(rename = "ldap_blocked")]
    LdapBlocked,
}

/// Basic user information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserBasic {
    /// The username.
    pub username: String,
    /// The display name.
    pub name: String,
    /// The user's ID.
    pub id: UserId,
    /// The state of the user account.
    pub state: UserState,
    /// The URL of the user's avatar.
    pub avatar_url: Option<String>,
    /// The URL of the user's profile page.
    pub web_url: String,
}

/// A unifying trait for all user types.
///
/// This is used to allow (direct) user queries to return the right information because
/// administrator users receive additional information for all user queries versus
/// non-administrator users.
pub trait UserResult: DeserializeOwned {}
impl<T: DeserializeOwned + Into<UserBasic>> UserResult for T {}

/// More detailed information only accessible to administrators.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
    /// The username.
    pub username: String,
    /// The display name.
    pub name: String,
    /// The user's ID.
    pub id: UserId,
    /// The state of the user account.
    pub state: UserState,
    /// The URL of the user's avatar.
    pub avatar_url: Option<String>,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the account was created.
    pub created_at: Option<DateTime<Utc>>,
    /// Whether the user is an administrator or not.
    ///
    /// Only available when talking to GitLab as an admin.
    pub is_admin: Option<bool>,
    /// The highest access level available to the user.
    ///
    /// Only available when talking to GitLab as an admin.
    pub highest_role: Option<AccessLevel>,
    /// Self-described biography of the user.
    pub bio: Option<String>,
    /// Whether the account has a private profile.
    pub private_profile: Option<bool>,
    /// Geographic location of the user.
    pub location: Option<String>,
    /// User public email address, if any.
    pub public_email: Option<String>,

    /// Skype contact information.
    pub skype: String,
    /// LinkedIn contact information.
    pub linkedin: String,
    /// Twitter contact information.
    pub twitter: String,
    /// Custom URL for the user's website.
    pub website_url: String,
    /// Organization the user belongs to.
    pub organization: Option<String>,
}

impl From<User> for UserBasic {
    fn from(user: User) -> Self {
        UserBasic {
            username: user.username,
            name: user.name,
            id: user.id,
            state: user.state,
            avatar_url: user.avatar_url,
            web_url: user.web_url,
        }
    }
}

/// External authentication tokens.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Identity {
    /// The provider of the token.
    pub provider: String,
    /// The UID for the provider.
    pub extern_uid: String,
}

impl_id!(ThemeId, "Type-safe theme ID.");

impl_id!(ColorSchemeId, "Type-safe color scheme ID.");

/// Full user structure information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserPublic {
    /// The username.
    pub username: String,
    /// The display name.
    pub name: String,
    /// The user's ID.
    pub id: UserId,
    /// The state of the user account.
    pub state: UserState,
    /// The URL of the user's avatar.
    pub avatar_url: Option<String>,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the account was created.
    pub created_at: Option<DateTime<Utc>>,
    /// Whether the user is an administrator or not.
    ///
    /// Only available when talking to GitLab as an admin.
    pub is_admin: Option<bool>,
    /// The highest access level available to the user.
    ///
    /// Only available when talking to GitLab as an admin.
    pub highest_role: Option<AccessLevel>,
    /// Self-described biography of the user.
    pub bio: Option<String>,
    /// Whether the account has a private profile.
    pub private_profile: Option<bool>,
    /// Geographic location of the user.
    pub location: Option<String>,
    /// User public email address, if any.
    pub public_email: Option<String>,

    /// Skype contact information.
    pub skype: String,
    /// LinkedIn contact information.
    pub linkedin: String,
    /// Twitter contact information.
    pub twitter: String,
    /// Custom URL for the user's website.
    pub website_url: String,
    /// Organization the user belongs to.
    pub organization: Option<String>,

    /// When the user last logged in.
    pub last_sign_in_at: Option<DateTime<Utc>>,
    /// When the user last made an action.
    pub last_activity_on: Option<NaiveDate>,
    /// When the user's account was confirmed.
    pub confirmed_at: Option<DateTime<Utc>>,
    /// The primary email address for the user.
    pub email: String,

    /// The theme used by the user, if configured.
    pub theme_id: Option<ThemeId>,
    /// The color scheme used by the user.
    pub color_scheme_id: ColorSchemeId,
    /// The number of projects the user may create.
    pub projects_limit: u64,
    /// When the user's current session started.
    pub current_sign_in_at: Option<DateTime<Utc>>,

    /// List of identities associated with the user.
    pub identities: Vec<Identity>,

    /// Whether the user can create groups.
    pub can_create_group: bool,
    /// Whether the user can create a new project.
    pub can_create_project: bool,
    /// Whether the user has two-factor authentication enabled.
    pub two_factor_enabled: bool,
    /// Whether the account is externally controlled.
    pub external: bool,
}

impl From<UserPublic> for UserBasic {
    fn from(user: UserPublic) -> Self {
        UserBasic {
            username: user.username,
            name: user.name,
            id: user.id,
            state: user.state,
            avatar_url: user.avatar_url,
            web_url: user.web_url,
        }
    }
}

impl From<UserPublic> for User {
    fn from(user: UserPublic) -> Self {
        User {
            username: user.username,
            name: user.name,
            id: user.id,
            state: user.state,
            avatar_url: user.avatar_url,
            web_url: user.web_url,
            created_at: user.created_at,
            is_admin: user.is_admin,
            highest_role: user.highest_role,
            bio: user.bio,
            private_profile: user.private_profile,
            location: user.location,
            public_email: user.public_email,
            skype: user.skype,
            linkedin: user.linkedin,
            twitter: user.twitter,
            website_url: user.website_url,
            organization: user.organization,
        }
    }
}

impl_id!(EmailId, "Type-safe email ID.");

/// Email address.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Email {
    /// ID of the email.
    pub id: EmailId,
    /// The email address.
    pub email: String,
}

impl_id!(HookId, "Type-safe hook ID.");

/// A web hook to notify of events.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Hook {
    /// The ID of the hook.
    pub id: HookId,
    /// The URL to contact.
    pub url: String,
    /// When the hook was created.
    pub created_at: DateTime<Utc>,
    /// Whether the hook is contacted for push events.
    pub push_events: bool,
    /// Whether the hook is contacted for tag push events.
    pub tag_push_events: bool,
    /// Whether the communication with the hook is verified using TLS certificates.
    pub enable_ssl_verification: bool,
}

/// A web hook to notify of project events.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectHook {
    /// The ID of the hook.
    pub id: HookId,
    /// The URL to contact.
    pub url: String,
    /// When the hook was created.
    pub created_at: DateTime<Utc>,
    /// The project associated with the hook.
    pub project_id: ProjectId,
    /// Whether the hook is contacted for push events.
    pub push_events: bool,
    /// Filter branches for which the hook is contacted for push events.
    pub push_events_branch_filter: Option<String>,
    /// Whether the hook is contacted for tag push events.
    pub tag_push_events: bool,
    /// Whether the hook is contacted for issue events.
    pub issues_events: bool,
    /// Whether the hook is contacted for confidential issue events.
    pub confidential_issues_events: Option<bool>,
    /// Whether the hook is contacted for merge request events.
    pub merge_requests_events: bool,
    /// Whether the hook is contacted for note events.
    pub note_events: bool,
    /// Whether the hook is contacted for confidential note events.
    pub confidential_note_events: Option<bool>,
    /// Whether the hook is contacted for repository update events.
    pub repository_update_events: bool,
    /// Whether the communication with the hook is verified using TLS certificates.
    pub enable_ssl_verification: bool,
    /// Whether the hook is contacted for job events.
    pub job_events: bool,
    /// Whether the hook is contacted for pipeline events.
    pub pipeline_events: bool,
    /// Whether the hook is contacted for wiki page events.
    pub wiki_page_events: bool,
    /// Secret token to validate received payloads
    pub token: Option<String>,
}

impl From<ProjectHook> for Hook {
    fn from(hook: ProjectHook) -> Self {
        Hook {
            id: hook.id,
            url: hook.url,
            created_at: hook.created_at,
            push_events: hook.push_events,
            tag_push_events: hook.tag_push_events,
            enable_ssl_verification: hook.enable_ssl_verification,
        }
    }
}

/// Reponse of a project variable
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectVariable {
    /// The key
    pub key: String,
    // The variable type
    pub variable_type: String,
    /// The value
    pub value: String,
    /// Flag if its protected
    pub protected: bool,
    /// Flag if its masked
    pub masked: bool,
    /// Environment scope
    pub environment_scope: String,
}

/// Reponse of a project variable
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProtectedTagAccessLevel {
    /// The access level id
    pub access_level: u64,
    // The access level
    pub access_level_description: String,
}

/// Reponse of a project variable
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProtectedTag {
    /// The name or wildcard
    pub name: String,
    // The access level
    pub create_access_levels: Vec<ProtectedTagAccessLevel>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ReleaseTag {
    // The release tag name
    pub tag_name: String,
    // The release tag description
    pub description: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Tag {
    // Commit message
    pub commit: RepoCommit,
    // Release tag
    pub release: Option<ReleaseTag>,
    // Tag name
    pub name: String,
    // Target sha
    pub target: String,
    pub message: Option<String>,
    // Is tag protected
    pub protected: bool,
}

impl_id!(ProjectId, "Type-safe project ID.");

/// Basic project information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BasicProjectDetails {
    /// The ID of the project.
    pub id: ProjectId,
    /// The display name of the project.
    pub name: String,
    /// The display name of the project with the namespace.
    pub name_with_namespace: String,
    /// The path to the project's repository.
    pub path: String,
    /// The path to the project's repository with its namespace.
    pub path_with_namespace: String,
    /// The URL to the main page of the repository.
    pub http_url_to_repo: String,
    /// The URL to the main page of the repository.
    pub web_url: String,
}

/// Visibility levels of projects.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum VisibilityLevel {
    /// The project is visible to anonymous users.
    #[serde(rename = "public")]
    Public,
    /// The project is visible to logged in users.
    #[serde(rename = "internal")]
    Internal,
    /// The project is visible only to users with explicit access.
    #[serde(rename = "private")]
    Private,
}

/// Visibility levels for project features.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FeatureVisibilityLevel {
    /// Feature is disabled.
    #[serde(rename = "disabled")]
    Disabled,
    /// Feature is enabled and accessible privately.
    #[serde(rename = "private")]
    Private,
    /// Feature is enabled and accessible with project-wide visibility level.
    #[serde(rename = "enabled")]
    Enabled,
    /// Feature is enabled and accessible publicly.
    #[serde(rename = "public")]
    Public,
}

// TODO: enum for NotificationLevel

/// Structure for a group a project has been shared with.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SharedGroup {
    /// The ID of the group.
    pub group_id: GroupId,
    /// The name of the group.
    pub group_name: String,
    /// The access level of the group.
    pub group_access_level: u64,
}

/// Access information to a project.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
// Called `MemberAccess` in entities.rb, but it is just a base class for `ProjectAccess` and
// `GroupAccess`. Combine them here.
pub struct MemberAccess {
    /// The access level of the membership (see `VisibilityLevel`).
    pub access_level: u64,
    /// The notification level of the current user.
    pub notification_level: Option<u64>,
}

/// Permissions granted to the current user to a project.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Permissions {
    /// The access granted by the project to the current user.
    pub project_access: Option<MemberAccess>,
    /// The access granted by the group to the current user.
    pub group_access: Option<MemberAccess>,
}

/// The avatar of a project's namespace.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectNamespaceAvatar {
    /// The URL of the namespace avatar.
    pub url: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ProjectLinks {
    #[serde(rename = "self")]
    /// API URL of project itself.
    self_: String,
    /// API URL of project issues, if enabled.
    issues: Option<String>,
    /// API URL of project merge requests, if enabled.
    merge_requests: Option<String>,
    /// API URL of project repository branches.
    repo_branches: String,
    /// API URL of project labels.
    labels: String,
    /// API URL of project events.
    events: String,
    /// API URL of project members.
    members: String,
}

/// Project information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Project {
    /// The ID of the project.
    pub id: ProjectId,
    /// The description of the project.
    pub description: Option<String>,
    /// The default branch for the project.
    pub default_branch: Option<String>,
    /// A list of tags for the project.
    pub tag_list: Vec<String>,
    /// Whether the project is archived or not.
    pub archived: bool,
    /// Whether the project has an empty repository or not.
    pub empty_repo: bool,
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
    /// The URL to clone the repository over SSH.
    pub ssh_url_to_repo: String,
    /// The URL to clone the repository over HTTPS.
    pub http_url_to_repo: String,
    /// The URL for the project's homepage.
    pub web_url: String,
    /// The URL for the project's readme.
    pub readme_url: Option<String>,
    /// The owner of the project (`None` for a group-owned project).
    pub owner: Option<UserBasic>,
    /// The display name of the project.
    pub name: String,
    /// The display name of the project with the namespace.
    pub name_with_namespace: String,
    /// The path to the project's repository.
    pub path: String,
    /// The path to the project's repository with its namespace.
    pub path_with_namespace: String,
    /// Whether the continuous integration container registry is enabled.
    ///
    /// This is supposed to be just `bool`, but projects created before the registry was
    /// supported appear to return `null`.
    pub container_registry_enabled: Option<bool>,
    /// When the repository was created.
    pub created_at: DateTime<Utc>,
    /// When the last activity on the project occurred.
    pub last_activity_at: DateTime<Utc>,
    /// Whether continuous integration shared runners are enabled.
    pub shared_runners_enabled: bool,
    /// Whether LFS object storage is enabled.
    pub lfs_enabled: bool,
    /// The user who created the repository.
    pub creator_id: UserId,
    /// The namespace the project lives in.
    pub namespace: Namespace,
    /// If the project is a fork, details about it.
    pub forked_from_project: Option<BasicProjectDetails>,
    /// The URL to the project avatar.
    pub avatar_url: Option<String>,
    /// The path to CI config file.
    pub ci_config_path: Option<String>,
    /// Description of error if project failed to import.
    pub import_error: Option<String>,
    /// The number of stars for the project.
    pub star_count: u64,
    /// The number of forks.
    pub forks_count: u64,
    /// The number of open issues (if issues are enabled).
    pub open_issues_count: Option<u64>,
    /// The continuous integration runner token (if enabled).
    pub runners_token: Option<String>,
    /// Whether jobs are publicly visible.
    pub public_jobs: bool,
    /// Groups the project is shared with.
    pub shared_with_groups: Vec<SharedGroup>,
    /// Whether the project only enables the merge button if all pipelines are passing.
    pub only_allow_merge_if_pipeline_succeeds: Option<bool>,
    /// Whether the project only enables the merge button if all discussions are resolved.
    pub only_allow_merge_if_all_discussions_are_resolved: Option<bool>,
    /// Whether enable 'Delete source branch' option by default for all new merge requests.
    pub remove_source_branch_after_merge: Option<bool>,
    /// Whether to show the link to create/view merge request when pusing from command line.
    pub printing_merge_request_link_enabled: Option<bool>,
    /// Whether access to the project may be requested.
    pub request_access_enabled: bool,
    /// Whether to automatically resolve merge request diff discussions when they become outdated,
    /// if configured.
    pub resolve_outdated_diff_discussions: Option<bool>,

    /// Whether jobs are enabled or not.
    pub jobs_enabled: bool,
    /// Whether issues are enabled or not.
    pub issues_enabled: bool,
    /// Whether merge requests are enabled or not.
    pub merge_requests_enabled: bool,
    /// Whether snippets are enabled or not.
    pub snippets_enabled: bool,
    /// Whether the project wiki is enabled or not.
    pub wiki_enabled: bool,

    /// Visibility of builds.
    pub builds_access_level: FeatureVisibilityLevel,
    /// Visibility of issues.
    pub issues_access_level: FeatureVisibilityLevel,
    /// Visibility of merge requests.
    pub merge_requests_access_level: FeatureVisibilityLevel,
    /// Visibility of repository.
    pub repository_access_level: FeatureVisibilityLevel,
    /// Visibility of snippets.
    pub snippets_access_level: FeatureVisibilityLevel,
    /// Visibility of wiki.
    pub wiki_access_level: FeatureVisibilityLevel,

    /// The merge method used when merging merge request.
    pub merge_method: Option<String>,
    /// Statistics about the project.
    pub statistics: Option<ProjectStatistics>,

    /// If this is present, it is `ProjectWithAccess`, but since it is so similar, just have it be
    /// optional here.
    pub permissions: Option<Permissions>,

    /// Links to related API URLs provided by GitLab in response to
    /// direct project lookup.  We do not expose this because our
    /// clients do not need them.
    _links: Option<ProjectLinks>,
}

#[cfg(test)]
impl Project {
    pub fn has_links(&self) -> bool {
        self._links.is_some()
    }
}

/// Statistics about a project.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct ProjectStatistics {
    /// The number of commits in the repository.
    pub commit_count: u64,
    /// The size, in bytes, of the total storage required for the project.
    pub storage_size: u64,
    /// The size, in bytes, of the repository.
    pub repository_size: u64,
    /// The size, in bytes, of uploaded LFS files.
    pub lfs_objects_size: u64,
    /// The size, in bytes, of uploaded job artifacts.
    pub job_artifacts_size: u64,
}

/// Access levels for groups and projects.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AccessLevel {
    /// Anonymous access.
    Anonymous,
    /// Guest access (can see the project).
    Guest,
    /// Reporter access (can open issues).
    Reporter,
    /// Developer access (can push branches, handle issues and merge requests).
    Developer,
    /// Maintainer access (can push to protected branches).
    Maintainer,
    /// Owner access (full rights).
    Owner,
    /// Admin access (full rights).
    Admin,
}

impl From<AccessLevel> for u64 {
    fn from(access: AccessLevel) -> Self {
        match access {
            AccessLevel::Anonymous => 0,
            AccessLevel::Guest => 10,
            AccessLevel::Reporter => 20,
            AccessLevel::Developer => 30,
            AccessLevel::Maintainer => 40,
            AccessLevel::Owner => 50,
            AccessLevel::Admin => 60,
        }
    }
}

impl From<u64> for AccessLevel {
    fn from(access: u64) -> Self {
        if access >= 60 {
            AccessLevel::Admin
        } else if access >= 50 {
            AccessLevel::Owner
        } else if access >= 40 {
            AccessLevel::Maintainer
        } else if access >= 30 {
            AccessLevel::Developer
        } else if access >= 20 {
            AccessLevel::Reporter
        } else if access >= 10 {
            AccessLevel::Guest
        } else {
            AccessLevel::Anonymous
        }
    }
}

impl AccessLevel {
    pub fn as_str(&self) -> &str {
        match self {
            AccessLevel::Admin => "admin",
            AccessLevel::Owner => "owner",
            AccessLevel::Developer => "developer",
            AccessLevel::Anonymous => "anonymous",
            AccessLevel::Guest => "guest",
            AccessLevel::Maintainer => "maintainer",
            AccessLevel::Reporter => "reporter",
        }
    }
}

impl Display for AccessLevel {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", Into::<u64>::into(*self))
    }
}

impl Serialize for AccessLevel {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        u64::from(*self).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for AccessLevel {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(<u64 as Deserialize>::deserialize(deserializer)?.into())
    }
}

/// A member with extra permissions on a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Member {
    /// The username.
    pub username: String,
    /// The display name.
    pub name: String,
    /// The user's ID.
    pub id: UserId,
    /// The state of the user account.
    pub state: UserState,
    /// The URL of the user's avatar.
    pub avatar_url: Option<String>,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// The access level of the user.
    pub access_level: u64,
    /// When the membership expires.
    pub expires_at: Option<NaiveDate>,
}

impl From<Member> for UserBasic {
    fn from(member: Member) -> Self {
        UserBasic {
            username: member.username,
            name: member.name,
            id: member.id,
            state: member.state,
            avatar_url: member.avatar_url,
            web_url: member.web_url,
        }
    }
}

/// A member with extra permissions on a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AccessRequester {
    /// The username.
    pub username: String,
    /// The display name.
    pub name: String,
    /// The user's ID.
    pub id: UserId,
    /// The state of the user account.
    pub state: UserState,
    /// The URL of the user's avatar.
    pub avatar_url: Option<String>,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the membership request was created.
    pub requested_at: DateTime<Utc>,
}

impl From<AccessRequester> for UserBasic {
    fn from(member: AccessRequester) -> Self {
        UserBasic {
            username: member.username,
            name: member.name,
            id: member.id,
            state: member.state,
            avatar_url: member.avatar_url,
            web_url: member.web_url,
        }
    }
}

impl_id!(GroupId, "Type-safe group ID.");

/// Group information.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Group {
    /// The ID of the group.
    pub id: GroupId,
    /// The name of the group.
    pub name: String,
    /// The path to the group.
    pub path: String,
    /// The description of the group.
    pub description: Option<String>,
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
    /// Whether LFS is enabled for the group.
    pub lfs_enabled: bool,
    /// The URL to the group avatar.
    pub avatar_url: Option<String>,
    /// The URL to the group's profile page.
    pub web_url: String,
    /// Whether membership requests are allowed for the group.
    pub request_access_enabled: bool,
    pub full_name: String,
    pub full_path: String,
    pub parent_id: Option<GroupId>,
    /// Statistics about the group.
    pub statistics: Option<GroupStatistics>,
}

/// Statistics about a group.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct GroupStatistics {
    /// The size, in bytes, of the total storage required for the group.
    pub storage_size: u64,
    /// The size, in bytes, of all repositories in the group.
    pub repository_size: u64,
    /// The size, in bytes, of uploaded LFS files in the group.
    pub lfs_objects_size: u64,
    /// The size, in bytes, of uploaded job artifacts in the group.
    pub job_artifacts_size: u64,
}

/// Group information with a project listing.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GroupDetail {
    /// The ID of the group.
    pub id: GroupId,
    /// The name of the group.
    pub name: String,
    /// The path to the group.
    pub path: String,
    /// The description of the group.
    pub description: Option<String>,
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
    /// Whether LFS is enabled for the group.
    pub lfs_enabled: bool,
    /// The URL to the group avatar.
    pub avatar_url: Option<String>,
    /// The URL to the group's profile page.
    pub web_url: String,
    /// The projects in a group.
    pub projects: Vec<Project>,
    /// Projects the group shares with other groups or users.
    pub shared_projects: Vec<Project>,
    /// Whether membership requests are allowed for the group.
    pub request_access_enabled: bool,
    pub full_name: String,
    pub full_path: String,
    pub parent_id: Option<GroupId>,
    /// Statistics about the group.
    pub statistics: Option<GroupStatistics>,
}

impl From<GroupDetail> for Group {
    fn from(detail: GroupDetail) -> Self {
        Group {
            id: detail.id,
            name: detail.name,
            path: detail.path,
            description: detail.description,
            visibility: detail.visibility,
            lfs_enabled: detail.lfs_enabled,
            avatar_url: detail.avatar_url,
            web_url: detail.web_url,
            request_access_enabled: detail.request_access_enabled,
            full_name: detail.full_name,
            full_path: detail.full_path,
            parent_id: detail.parent_id,
            statistics: detail.statistics,
        }
    }
}

/// A branch on a repository.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoBranch {
    /// The name of the branch.
    pub name: String,
    /// The commit of the branch.
    pub commit: Option<RepoCommit>,
    /// Whether the branch is merged into the main branch or not.
    pub merged: Option<bool>,
    /// Whether the branch is protected or not.
    pub protected: Option<bool>,
    /// Whether the developers can push directly to the branch or not.
    pub developers_can_push: Option<bool>,
    /// Whether the developers can merge into the branch or not.
    pub developers_can_merge: Option<bool>,
    /// Whether the current user can push to the branch.
    pub can_push: Option<bool>,
    /// Whether the branch is the repository default branch.
    pub default: Option<bool>,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Deserialize, Debug, Clone)]
pub struct PRBAccessLevel {
    pub access_level: u64,
    pub access_level_description: String,
}

/// A protected branch on a repository
#[derive(Deserialize, Debug, Clone)]
pub struct ProtectedRepoBranch {
    pub name: String,
    pub push_access_levels: Vec<PRBAccessLevel>,
    pub merge_access_levels: Vec<PRBAccessLevel>,
    pub code_owner_approval_required: Option<bool>,
}

/// The ID of a git object.
#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
pub struct ObjectId(String);

impl ObjectId {
    /// Construct a new `ObjectId`
    pub fn new<O: ToString>(oid: O) -> Self {
        ObjectId(oid.to_string())
    }

    /// The value of the id.
    pub fn value(&self) -> &String {
        &self.0
    }
}

/// The kinds of objects Gitlab can return.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObjectType {
    /// A `tree` object.
    #[serde(rename = "tree")]
    Tree,
    /// A `blob` object.
    #[serde(rename = "blob")]
    Blob,
}

/// An object inside of a repository.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoTreeObject {
    /// The ID of the object.
    pub id: ObjectId,
    /// The name of the object.
    pub name: String,
    #[serde(rename = "type")]
    /// The type of the object.
    pub type_: ObjectType,
    /// The path to the object inside of the repository.
    pub path: String,
    /// The mode of the object.
    pub mode: String,
}

/// A commit in a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoCommit {
    /// The ID of the commit.
    pub id: ObjectId,
    /// The short ID of the commit.
    pub short_id: ObjectId,
    /// The summary of the commit.
    pub title: String,
    /// The commit ID of the parents of the commit.
    /// Can be null on projects/:id/repository/branches call
    pub parent_ids: Option<Vec<ObjectId>>,
    /// The commit author's name.
    pub author_name: String,
    /// The commit author's email address.
    pub author_email: String,
    /// The commit's authorship date.
    pub authored_date: DateTime<Utc>,
    /// The committer's name.
    pub committer_name: String,
    /// The committer's email address.
    pub committer_email: String,
    /// The commit's commit date.
    pub committed_date: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    /// The full commit message.
    pub message: String,
}

/// A commit in a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeRequestCommit {
    /// The ID of the commit.
    pub id: ObjectId,
    /// The short ID of the commit.
    pub short_id: ObjectId,
    /// The summary of the commit.
    pub title: String,
    /// The commit author's name.
    pub author_name: String,
    /// The commit author's email address.
    pub author_email: String,
    pub created_at: DateTime<Utc>,
    /// The full commit message.
    pub message: String,
}

/// Stats about a commit.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct RepoCommitStats {
    /// The number of lines added by the commit.
    pub additions: u64,
    /// The number of lines deleted by the commit.
    pub deletions: u64,
    /// The number of lines changed by the commit.
    pub total: u64,
}

/// A commit in a project with statistics.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoCommitDetail {
    /// The ID of the commit.
    pub id: ObjectId,
    /// The short ID of the commit.
    pub short_id: ObjectId,
    /// The summary of the commit.
    pub title: String,
    /// The commit ID of the parents of the commit.
    pub parent_ids: Vec<ObjectId>,
    /// The commit author's name.
    pub author_name: String,
    /// The commit author's email address.
    pub author_email: String,
    /// The commit's authorship date.
    pub authored_date: DateTime<Utc>,
    /// The committer's name.
    pub committer_name: String,
    /// The committer's email address.
    pub committer_email: String,
    /// The commit's commit date.
    pub committed_date: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    /// The full commit message.
    pub message: String,
    /// Statistics about the commit.
    pub stats: Option<RepoCommitStats>,
    /// The last pipeline for this commit, if any.
    pub last_pipeline: Option<PipelineBasic>,
    /// The project associated with the commit.
    pub project_id: ProjectId,
    // XXX: Investigate what this is.
    /// This looks to be CI related; ignoring without better docs.
    status: Value,
}

impl_id!(SnippetId, "Type-safe snippet ID.");

/// A project-specific snippet.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectSnippet {
    /// The ID of the snippet.
    pub id: SnippetId,
    /// The title of the snippet.
    pub title: String,
    /// The name of the snippet.
    pub file_name: String,
    /// The author of the snippet.
    pub author: UserBasic,
    /// When the snippet was last updated.
    pub updated_at: DateTime<Utc>,
    /// When the snippet was created.
    pub created_at: DateTime<Utc>,
    /// When the snippet was created.
    pub expires_at: Option<DateTime<Utc>>,
    /// The URL of the snippet.
    pub web_url: String,
}

// This is just used as a common "base class" in Ruby.
//#[derive(Serialize, Deserialize, Debug, Clone)]
//pub struct ProjectEntity {
//    pub id: ProjectEntityId,
//    pub iid: ProjectEntityInternalId,
//    pub project_id: ProjectId,
//    pub title: String,
//    pub description: String,
//    pub state: ProjectEntityState,
//    pub created_at: DateTime<Utc>,
//    pub updated_at: DateTime<Utc>,
//}

/// A diff within a repository.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoDiff {
    /// The path on the old side of the diff.
    pub old_path: String,
    /// The path on the new side of the diff.
    pub new_path: String,
    /// The mode on the old side of the diff.
    pub a_mode: String,
    /// The mode on the new side of the diff.
    pub b_mode: String,
    pub diff: String,
    /// Whether the diff indicates the addition of a file.
    pub new_file: bool,
    /// Whether the diff indicates the rename of a file.
    pub renamed_file: bool,
    /// Whether the diff indicates the deletion of a file.
    pub deleted_file: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DiffRefs {
    /// SHA referencing base commit in the source branch
    pub base_sha: Option<ObjectId>,
    /// SHA referencing head commit in the source branch
    pub head_sha: Option<ObjectId>,
    /// SHA referencing commit in target branch
    pub start_sha: Option<ObjectId>,
}

impl_id!(MilestoneId, "Type-safe milestone ID.");

impl_id!(
    MilestoneInternalId,
    "Type-safe milestone internal ID (internal to a project).",
);

/// The states a milestone may be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MilestoneState {
    /// The milestone is active.
    #[serde(rename = "active")]
    Active,
    /// The milestone has been closed.
    #[serde(rename = "closed")]
    Closed,
}

/// A milestone in a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Milestone {
    /// The ID of the milestone.
    pub id: MilestoneId,
    /// The user-visible ID of the milestone.
    pub iid: MilestoneInternalId,
    /// The ID of the project if this is a project milestone.
    pub project_id: Option<ProjectId>,
    /// The ID of the group if this is a group milestone.
    pub group_id: Option<GroupId>,
    /// The title of the milestone.
    pub title: String,
    /// The description of the milestone.
    pub description: Option<String>,
    /// The state of the milestone.
    pub state: MilestoneState,
    /// When the milestone was created.
    pub created_at: DateTime<Utc>,
    /// When the milestone was last updated.
    pub updated_at: DateTime<Utc>,
    /// When the milestone is due.
    pub due_date: Option<NaiveDate>,
    /// When the milestone was started.
    pub start_date: Option<NaiveDate>,
}

impl Milestone {
    /// Create a new blank milestone: it needs at least the ProjectId and title
    /// ProjectId and title are mandatory for new milestone API of Gitlab
    pub fn new_for_project(project_id: ProjectId, title: String) -> Milestone {
        Milestone {
            id: MilestoneId::new(0),
            iid: MilestoneInternalId::new(0),
            project_id: Some(project_id),
            group_id: None,
            title,
            description: None,
            state: MilestoneState::Active,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            due_date: None,
            start_date: None,
        }
    }
    /// Create a new blank group milestone: it needs at least the GroupId and title
    /// GroupId and title are mandatory for new milestone API of Gitlab
    pub fn new_for_group(group_id: GroupId, title: String) -> Milestone {
        Milestone {
            id: MilestoneId::new(0),
            iid: MilestoneInternalId::new(0),
            project_id: None,
            group_id: Some(group_id),
            title,
            description: None,
            state: MilestoneState::Active,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            due_date: None,
            start_date: None,
        }
    }
    /// Complements the milestone with optional paramater: description
    pub fn with_description(mut self, description: String) -> Milestone {
        self.description = Some(description);
        self
    }
    /// Complements the milestone with optional parameter: due_date
    pub fn with_due_date(mut self, due_date: NaiveDate) -> Milestone {
        self.due_date = Some(due_date);
        self
    }
    /// Complements the milestone with optional parameter: start_date
    pub fn with_start_date(mut self, start_date: NaiveDate) -> Milestone {
        self.start_date = Some(start_date);
        self
    }
}

impl_id!(LabelId, "Type-safe label ID.");

/// Type-safe label color.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct LabelColor(String);

impl LabelColor {
    /// Creates a LabelColor from RGB values
    pub fn from_rgb(r: u8, g: u8, b: u8) -> LabelColor {
        LabelColor(format!("#{:02X}{:02X}{:02X}", r, g, b))
    }

    /// Get the value from a LabelColor
    pub fn value(self) -> String {
        self.0
    }
}

impl FromStr for LabelColor {
    type Err = ();

    /// Creates a LabelColor from standard HTML values
    fn from_str(stdcolor: &str) -> Result<Self, Self::Err> {
        let hex = match stdcolor {
            "white" => "FFFFFF",
            "silver" => "C0C0C0",
            "gray" => "808080",
            "black" => "000000",
            "red" => "FF0000",
            "maroon" => "800000",
            "yellow" => "FFFF00",
            "olive" => "808000",
            "lime" => "00FF00",
            "green" => "008000",
            "aqua" => "00FFFF",
            "teal" => "008080",
            "blue" => "0000FF",
            "navy" => "000080",
            "fuchsia" => "FF00FF",
            "purple" => "800080",
            _ => "808080",
        };

        Ok(LabelColor(format!("#{}", hex)))
    }
}

/// An label on a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Label {
    /// The Id of the label.
    pub id: LabelId,
    /// The name of the label.
    pub name: String,
    /// The color of the label.
    pub color: LabelColor,
    /// The description of the label.
    pub description: Option<String>,
    /// The number of opened issues associated with the label.
    pub open_issues_count: Option<u64>,
    /// the number of closed issues associated with the label.
    pub closed_issues_count: Option<u64>,
    /// The number of open merge request associated with the label.
    pub open_merge_requests_count: Option<u64>,
    /// Whether or not the account connecting has subscribed to the label.
    pub subscribed: bool,
    /// The priority of the label.
    pub priority: Option<u64>,
}

impl Label {
    /// Create a new Label: it needs at least a name and a color.
    /// ProjectId is mandatory for Gitlab API
    pub fn new(name: String, color: LabelColor) -> Label {
        Label {
            id: LabelId::new(0),
            name,
            color,
            description: None,
            open_issues_count: None,
            closed_issues_count: None,
            open_merge_requests_count: None,
            subscribed: false,
            priority: None,
        }
    }
    /// Complements the label with optional parameter: description
    pub fn with_description(mut self, description: String) -> Label {
        self.description = Some(description);
        self
    }

    /// Complements the label with optional parameter: priority
    pub fn with_priority(mut self, priority: u64) -> Label {
        self.priority = Some(priority);
        self
    }
}

impl_id!(IssueId, "Type-safe issue ID.");

impl_id!(
    IssueInternalId,
    "Type-safe issue internal ID (internal to a project).",
);

/// The states an issue may be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueState {
    /// The issue is open.
    #[serde(rename = "opened")]
    Opened,
    /// The issue has been closed.
    #[serde(rename = "closed")]
    Closed,
    /// The issue has been opened after being closed.
    #[serde(rename = "reopened")]
    Reopened,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct IssueLinks {
    #[serde(rename = "self")]
    /// API URL of issue itself.
    self_: String,
    /// API URL of issue notes.
    notes: String,
    /// API URL of issue award emoji.
    award_emoji: String,
    /// API URL of issue project.
    project: String,
}

/// An issue on a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Issue {
    /// The ID of the issue.
    pub id: IssueId,
    /// The user-visible ID of the issue.
    pub iid: IssueInternalId,
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the issue.
    pub title: String,
    /// The description of the issue.
    pub description: Option<String>,
    /// The state of the issue.
    pub state: IssueState,
    /// When the issue was created.
    pub created_at: DateTime<Utc>,
    /// When the issue was last updated.
    pub updated_at: DateTime<Utc>,
    /// When the issue was closed, if closed.
    pub closed_at: Option<DateTime<Utc>>,
    /// The user that closed the issue.
    pub closed_by: Option<UserBasic>,
    /// The labels attached to the issue.
    pub labels: Vec<String>,
    /// The milestone of the issue.
    pub milestone: Option<Milestone>,
    /// The author of the issue.
    pub author: UserBasic,
    /// The assignee of the issue.
    pub assignee: Option<UserBasic>,
    /// The assignees of the issue.
    pub assignees: Option<Vec<UserBasic>>,
    /// Whether the current user is subscribed or not.
    /// GitLab does not include this in responses with lists of issues but
    /// does on an individual issue.
    pub subscribed: Option<bool>,
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
    /// The number of comments on the issue.
    pub user_notes_count: u64,
    /// The number of merge requests referencing the issue.
    pub merge_requests_count: u64,
    /// The number of upvotes for the issue.
    pub upvotes: u64,
    /// The number of downvotes against the issue.
    pub downvotes: u64,
    /// When the issue is due.
    pub due_date: Option<NaiveDate>,
    /// Whether the issue is has a non-empty task list.
    /// GitLab does not include this in issue references.
    pub has_tasks: Option<bool>,
    /// Whether the issue is confidential or not.
    pub confidential: bool,
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
    /// The URL of the issue.
    pub web_url: String,

    /// Links to related API URLs provided by GitLab in response to
    /// direct issue lookup.  We do not expose this because our
    /// clients do not need them.
    _links: Option<IssueLinks>,
}

impl Issue {
    /// Creates a new blank issue: it needs at least the ProjectId, title and author
    /// ProjectId and author are mandatory in the Issue struct itself
    /// title is mandatory for the new issue API of Gitlab
    pub fn new(project_id: ProjectId, title: String, author: UserBasic) -> Issue {
        // initialize with default parameters
        Issue {
            id: IssueId::new(0),
            iid: IssueInternalId::new(0),
            project_id,
            title,
            description: None,
            state: IssueState::Opened,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
            closed_by: None,
            labels: Vec::new(),
            milestone: None,
            author,
            assignee: None,
            assignees: None,
            subscribed: None,
            time_stats: IssuableTimeStats {
                time_estimate: 0,
                total_time_spent: 0,
                human_time_estimate: None,
                human_total_time_spent: None,
            },
            user_notes_count: 0,
            merge_requests_count: 0,
            upvotes: 0,
            downvotes: 0,
            due_date: None,
            has_tasks: None,
            confidential: false,
            discussion_locked: None,
            web_url: "".into(),
            _links: None,
        }
    }
    /// Complements the issue with optional parameter: iid
    pub fn with_iid(mut self, iid: IssueInternalId) -> Issue {
        self.iid = iid;
        self
    }
    /// Complements the issue with optional parameter: description
    pub fn with_description(mut self, description: String) -> Issue {
        self.description = Some(description);
        self
    }
    /// Complements the issue with optional parameter: confidential
    pub fn with_confidential(mut self, confidential: bool) -> Issue {
        self.confidential = confidential;
        self
    }
    /// Complements the issue with optional parameter: assignees
    pub fn with_assignees(mut self, assignees: Vec<UserBasic>) -> Issue {
        self.assignees = Some(assignees);
        self
    }
    /// Complements the issue with optional parameter: milestone
    pub fn with_milestone(mut self, milestone: Milestone) -> Issue {
        self.milestone = Some(milestone);
        self
    }
    /// Complements the issue with optional parameter: labels
    pub fn with_labels(mut self, labels: Vec<String>) -> Issue {
        self.labels = labels;
        self
    }
    /// Complements the issue with optional parameter: created_at
    pub fn with_created_at(mut self, created_at: DateTime<Utc>) -> Issue {
        self.created_at = created_at;
        self
    }
    /// Complements the issue with optional parameter: due_date
    pub fn with_due_date(mut self, due_date: NaiveDate) -> Issue {
        self.due_date = Some(due_date);
        self
    }
    pub fn has_links(&self) -> bool {
        self._links.is_some()
    }
}

/// A time estimate on an issue or merge request.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IssuableTimeStats {
    /// The time estimate, in seconds.
    pub time_estimate: u64,
    /// The total time spent, in seconds.
    pub total_time_spent: u64,
    /// The time estimate, as a human-readable string.
    pub human_time_estimate: Option<String>,
    /// The total time spent, as a human-readable string.
    pub human_total_time_spent: Option<String>,
}

/// Type-safe external issue ID.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExternalIssueId(u64);

/// An external issue reference.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExternalIssue {
    /// The ID of the issue.
    pub id: ExternalIssueId,
    /// The title of the issue.
    pub title: String,
}

/// A reference to an issue.
#[derive(Debug, Clone)]
pub enum IssueReference {
    /// A reference to an issue on the same Gitlab host.
    Internal(Box<Issue>),
    /// An external issue reference.
    External(ExternalIssue),
}

impl Serialize for IssueReference {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match *self {
            IssueReference::Internal(ref issue) => issue.serialize(serializer),
            IssueReference::External(ref issue) => issue.serialize(serializer),
        }
    }
}

impl<'de> Deserialize<'de> for IssueReference {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let val = <Value as Deserialize>::deserialize(deserializer)?;

        serde_json::from_value::<Issue>(val.clone())
            .map(|issue| IssueReference::Internal(Box::new(issue)))
            .or_else(|_| serde_json::from_value::<ExternalIssue>(val).map(IssueReference::External))
            .map_err(|err| D::Error::custom(format!("invalid issue reference: {:?}", err)))
    }
}

impl_id!(MergeRequestId, "Type-safe merge request ID.");

impl_id!(
    MergeRequestInternalId,
    "Type-safe merge request internal ID (internal to a project).",
);

/// The status of the possible merge for a merge request.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeStatus {
    /// The merge request has just been created.
    #[serde(rename = "preparing")]
    Preparing,
    /// The merge request has not been checked yet.
    #[serde(rename = "unchecked")]
    Unchecked,
    /// The merge request is currently being checked.
    #[serde(rename = "checking")]
    Checking,
    /// The merge request may be merged.
    #[serde(rename = "can_be_merged")]
    CanBeMerged,
    /// The merge request may not be merged yet.
    #[serde(rename = "cannot_be_merged")]
    CannotBeMerged,
    /// The merge request has not been checked but previously could not be merged.
    #[serde(rename = "cannot_be_merged_recheck")]
    CannotBeMergedRecheck,
    /// The merge request could not be merged previously, but is being rechecked.
    #[serde(rename = "cannot_be_merged_rechecking")]
    CannotBeMergedRechecking,
}

/// The states a merge request may be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeRequestState {
    /// The merge request is open.
    #[serde(rename = "opened")]
    Opened,
    /// The merge request has been closed before merging.
    #[serde(rename = "closed")]
    Closed,
    /// The merge request has been opened after closing.
    #[serde(rename = "reopened")]
    Reopened,
    /// The merge request has been merged.
    #[serde(rename = "merged")]
    Merged,
    /// The merge request is locked from further discussion or updates.
    #[serde(rename = "locked")]
    Locked,
}

/// Information about current user's access to the merge request.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeRequestUser {
    /// Whether the current user can merge the MR.
    pub can_merge: bool,
}

/// A merge request.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeRequestBasic {
    /// The ID of the merge request.
    pub id: MergeRequestId,
    /// The user-visible ID of the merge request.
    pub iid: MergeRequestInternalId,
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the merge request.
    pub title: String,
    /// The description of the merge request.
    pub description: Option<String>,
    /// The state of the merge request.
    pub state: MergeRequestState,
    /// When the merge request was created.
    pub created_at: DateTime<Utc>,
    /// When the merge request was last updated.
    pub updated_at: DateTime<Utc>,
    /// The URL of the merge request.
    pub web_url: String,
}

/// A merge request.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeRequest {
    /// The ID of the merge request.
    pub id: MergeRequestId,
    /// The user-visible ID of the merge request.
    pub iid: MergeRequestInternalId,
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the merge request.
    pub title: String,
    /// The description of the merge request.
    pub description: Option<String>,
    /// The state of the merge request.
    pub state: MergeRequestState,
    /// When the merge request was created.
    pub created_at: DateTime<Utc>,
    /// When the merge request was last updated.
    pub updated_at: DateTime<Utc>,
    /// When the merge request was merged.
    pub merged_at: Option<DateTime<Utc>>,
    /// When the merge request was closed.
    pub closed_at: Option<DateTime<Utc>>,
    /// The user that merged the merge request.
    pub merged_by: Option<UserBasic>,
    /// The user that closed the merge request.
    pub closed_by: Option<UserBasic>,
    /// The target branch of the merge request.
    pub target_branch: String,
    /// The source branch of the merge request.
    pub source_branch: String,
    /// The number of upvotes for the merge request.
    pub upvotes: u64,
    /// The number of downvotes against the merge request.
    pub downvotes: u64,
    /// The author of the merge request.
    pub author: UserBasic,
    /// The assignee of the merge request.
    pub assignee: Option<UserBasic>,
    /// The assignees of the merge request.
    pub assignees: Option<Vec<UserBasic>>,
    /// The reviewers of the merge request.
    pub reviewers: Option<Vec<UserBasic>>,
    /// The ID of the project hosting the source branch.
    pub source_project_id: Option<ProjectId>,
    /// The ID of the project hosting the target branch.
    pub target_project_id: ProjectId,
    /// The labels attached to the merge request.
    pub labels: Vec<String>,
    /// Whether the merge request is a work-in-progress or not.
    pub work_in_progress: bool,
    /// Whether the merge request allows a maintainer to collaborate.
    pub allow_collaboration: Option<bool>,
    /// Whether the merge request allows a maintainer to push (deprecated).
    pub allow_maintainer_to_push: Option<bool>,
    /// The milestone of the merge request.
    pub milestone: Option<Milestone>,
    /// Whether to squash commits on merge.
    pub squash: bool,
    /// Whether the merge request will be merged once all pipelines succeed or not.
    pub merge_when_pipeline_succeeds: bool,
    /// The status of the merge request.
    pub merge_status: MergeStatus,
    /// The object ID of the head of the source branch.
    ///
    /// This is `None` if the source branch has been deleted.
    pub sha: Option<ObjectId>,
    /// The commits used to construct the merge request diffs.
    pub diff_refs: Option<DiffRefs>,
    /// Description of error if MR failed to merge.
    pub merge_error: Option<String>,
    /// Whether a rebase is in progress.
    pub rebase_in_progress: Option<bool>,
    /// The object ID of the commit which merged the merge request.
    pub merge_commit_sha: Option<ObjectId>,
    /// The object ID of the merge request squash commit.
    pub squash_commit_sha: Option<ObjectId>,
    /// Whether the current user is subscribed or not.
    /// GitLab does not include this in responses with lists of merge requests but
    /// does on an individual merge request.
    pub subscribed: Option<bool>,
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
    /// Whether or not all blocking discussions are resolved.
    pub blocking_discussions_resolved: bool,
    /// The number of paths changed by the merge request.
    ///
    /// This is an integer suffixed by `+` if there are more files changed than some threshold
    /// (probably determined by a timeout).
    pub changes_count: Option<String>,
    /// The number of comments on the merge request.
    pub user_notes_count: u64,
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
    /// Whether the merge request should be deleted or not (set by the merger).
    pub should_remove_source_branch: Option<bool>,
    /// Whether the merge request should be deleted or not (set by the author).
    pub force_remove_source_branch: Option<bool>,
    /// Whether the merge request currently has conflicts with the target branch.
    pub has_conflicts: bool,
    /// Information about current user's access to the merge request.
    pub user: Option<MergeRequestUser>,
    /// The URL of the merge request.
    pub web_url: String,
    /// Basic pipeline information for the MR.
    pub pipeline: Option<PipelineBasic>,
    /// Whether the user doesn't have any commits in the repo, denoting the first contribution.
    pub first_contribution: Option<bool>,
}

/// A merge request with changes.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeRequestChanges {
    /// The ID of the merge request.
    pub id: MergeRequestId,
    /// The user-visible ID of the merge request.
    pub iid: MergeRequestInternalId,
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the merge request.
    pub title: String,
    /// The description of the merge request.
    pub description: Option<String>,
    /// The state of the merge request.
    pub state: MergeRequestState,
    /// When the merge request was created.
    pub created_at: DateTime<Utc>,
    /// When the merge request was last updated.
    pub updated_at: DateTime<Utc>,
    /// When the merge request was merged.
    pub merged_at: Option<DateTime<Utc>>,
    /// When the merge request was closed.
    pub closed_at: Option<DateTime<Utc>>,
    /// The user that merged the merge request.
    pub merged_by: Option<UserBasic>,
    /// The user that closed the merge request.
    pub closed_by: Option<UserBasic>,
    /// The target branch of the merge request.
    pub target_branch: String,
    /// The source branch of the merge request.
    pub source_branch: String,
    /// The number of upvotes for the merge request.
    pub upvotes: u64,
    /// The number of downvotes against the merge request.
    pub downvotes: u64,
    /// The author of the merge request.
    pub author: UserBasic,
    /// The assignee of the merge request.
    pub assignee: Option<UserBasic>,
    /// The assignees of the merge request.
    pub assignees: Option<Vec<UserBasic>>,
    /// The reviewers of the merge request.
    pub reviewers: Option<Vec<UserBasic>>,
    /// The ID of the project hosting the source branch.
    pub source_project_id: Option<ProjectId>,
    /// The ID of the project hosting the target branch.
    pub target_project_id: ProjectId,
    /// The labels attached to the merge request.
    pub labels: Vec<String>,
    /// Whether the merge request is a work-in-progress or not.
    pub work_in_progress: bool,
    /// Whether the merge request allows a maintainer to collaborate.
    pub allow_collaboration: Option<bool>,
    /// Whether the merge request allows a maintainer to push (deprecated).
    pub allow_maintainer_to_push: Option<bool>,
    /// The milestone of the merge request.
    pub milestone: Option<Milestone>,
    /// Whether to squash commits on merge.
    pub squash: bool,
    /// Whether the merge request will be merged once all jobs succeed or not.
    pub merge_when_pipeline_succeeds: bool,
    /// The status of the merge request.
    pub merge_status: MergeStatus,
    /// The object ID of the head of the source branch.
    ///
    /// This is `None` if the source branch has been deleted.
    pub sha: Option<ObjectId>,
    /// The commits used to construct the merge request diffs.
    pub diff_refs: Option<DiffRefs>,
    /// Description of error if MR failed to merge.
    pub merge_error: Option<String>,
    /// Whether a rebase is in progress.
    pub rebase_in_progress: Option<bool>,
    /// The object ID of the commit which merged the merge request.
    pub merge_commit_sha: Option<ObjectId>,
    /// The object ID of the merge request squash commit.
    pub squash_commit_sha: Option<ObjectId>,
    /// GitLab does not include this in responses with lists of merge requests but
    /// does on an individual merge request.
    pub subscribed: Option<bool>,
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
    /// Whether or not all blocking discussions are resolved.
    pub blocking_discussions_resolved: bool,
    /// The number of paths changed by the merge request.
    pub changes_count: Option<String>,
    /// The number of comments on the merge request.
    pub user_notes_count: u64,
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
    /// Whether the merge request should be deleted or not (set by the merger).
    pub should_remove_source_branch: Option<bool>,
    /// Whether the merge request should be deleted or not (set by the author).
    pub force_remove_source_branch: Option<bool>,
    /// Whether the merge request currently has conflicts with the target branch.
    pub has_conflicts: bool,
    /// Information about current user's access to the merge request.
    pub user: MergeRequestUser,
    /// The URL of the merge request.
    pub web_url: String,
    /// Basic pipeline information for the MR.
    pub pipeline: Option<PipelineBasic>,
    pub changes: Vec<RepoDiff>,
}

impl From<MergeRequestChanges> for MergeRequest {
    fn from(mr: MergeRequestChanges) -> Self {
        MergeRequest {
            id: mr.id,
            iid: mr.iid,
            project_id: mr.project_id,
            title: mr.title,
            description: mr.description,
            state: mr.state,
            created_at: mr.created_at,
            updated_at: mr.updated_at,
            merged_at: mr.merged_at,
            closed_at: mr.closed_at,
            merged_by: mr.merged_by,
            closed_by: mr.closed_by,
            target_branch: mr.target_branch,
            source_branch: mr.source_branch,
            upvotes: mr.upvotes,
            downvotes: mr.downvotes,
            author: mr.author,
            assignee: mr.assignee,
            assignees: mr.assignees,
            reviewers: mr.reviewers,
            source_project_id: mr.source_project_id,
            target_project_id: mr.target_project_id,
            labels: mr.labels,
            work_in_progress: mr.work_in_progress,
            allow_collaboration: mr.allow_collaboration,
            allow_maintainer_to_push: mr.allow_maintainer_to_push,
            milestone: mr.milestone,
            squash: mr.squash,
            merge_when_pipeline_succeeds: mr.merge_when_pipeline_succeeds,
            merge_status: mr.merge_status,
            sha: mr.sha,
            diff_refs: mr.diff_refs,
            merge_error: mr.merge_error,
            rebase_in_progress: mr.rebase_in_progress,
            merge_commit_sha: mr.merge_commit_sha,
            squash_commit_sha: mr.squash_commit_sha,
            subscribed: mr.subscribed,
            time_stats: mr.time_stats,
            blocking_discussions_resolved: mr.blocking_discussions_resolved,
            changes_count: mr.changes_count,
            user_notes_count: mr.user_notes_count,
            discussion_locked: mr.discussion_locked,
            should_remove_source_branch: mr.should_remove_source_branch,
            force_remove_source_branch: mr.force_remove_source_branch,
            has_conflicts: mr.has_conflicts,
            user: Some(mr.user),
            web_url: mr.web_url,
            pipeline: mr.pipeline,
            first_contribution: None,
        }
    }
}

impl_id!(MergeTrainId, "Type-safe merge train ID.");

/// The states a merge train may be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeTrainState {
    /// TODO: Figure out when this is used.
    #[serde(rename = "idle")]
    Idle,
    /// This merge train has been marked as stale.
    /// E.g. The MR is dropped from the merge train.
    #[serde(rename = "stale")]
    Stale,
    /// The merge train has just started.
    #[serde(rename = "fresh")]
    Fresh,
    /// The merge train finished and is in the process of merging.
    #[serde(rename = "merging")]
    Merging,
    /// The merge train succeded.
    #[serde(rename = "merged")]
    Merged,
}

/// A single MergeTrain entry
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MergeTrain {
    /// The ID of this merge train.
    pub id: MergeTrainId,
    /// The associated merge request.
    pub merge_request: MergeRequestBasic,
    /// The Pipeline for this merge.
    pub pipeline: PipelineBasic,
    /// The user that triggered this merge.
    pub user: UserBasic,
    /// When this entry has been added to the train.
    pub created_at: DateTime<Utc>,
    /// When this entry has been last updated.
    pub updated_at: DateTime<Utc>,
    /// The name of the target branch for this merge request.
    pub target_branch: String,
    /// The current status of this merge train.
    pub status: MergeTrainState,
    /// When this train has been merged.
    pub merged_at: Option<DateTime<Utc>>,
    pub duration: Option<u64>,
}

impl_id!(SshKeyId, "Type-safe SSH key ID.");

/// An uploaded SSH key.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SshKey {
    /// The ID of the SSH key.
    pub id: SshKeyId,
    /// The title of the key.
    pub title: String,
    /// The public half of the SSH key.
    pub key: String,
    /// When the key was created.
    pub created_at: DateTime<Utc>,
    /// Whether the key may push to repositories or not.
    pub can_push: bool,
}

/// An uploaded SSH key with its owner.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SshKeyWithUser {
    /// The ID of the SSH key.
    pub id: SshKeyId,
    /// The title of the key.
    pub title: String,
    /// The public half of the SSH key.
    pub key: String,
    /// When the key was created.
    pub created_at: DateTime<Utc>,
    /// The user associated with the SSH key.
    pub user: UserPublic,
}

/// The entities a note may be added to.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoteType {
    /// A note on a commit.
    Commit,
    /// A note on an issue.
    Issue,
    /// A note on a merge request.
    MergeRequest,
    /// A note on a snippet.
    Snippet,
}

/// The various types a note can have
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiscussionNoteType {
    /// A note in a discussion
    Note,
    /// A note in a standard discussion
    DiscussionNote,
    /// A note attached to a diff
    DiffNote,
}

/// The ID of an entity a note is attached to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NoteableId {
    /// The ID of the commit for a commit note.
    Commit(ObjectId),
    /// The ID of the issue for an issue note.
    Issue(IssueId),
    /// The ID of the merge request for a merge request note.
    MergeRequest(MergeRequestId),
    /// The ID of the snippet for a snippet note.
    Snippet(SnippetId),
}

/// The internal ID of an entity a note is attached to (internal to a project).
/// GitLab only has this for notes attached to issues and merge requests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NoteableInternalId {
    /// The internal ID of the issue for an issue note.
    Issue(IssueInternalId),
    /// The internal ID of the merge request for a merge request note.
    MergeRequest(MergeRequestInternalId),
}

impl_id!(NoteId, "Type-safe note (comment) ID.");

/// A note can be attached to text or an image
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum NotePositionType {
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "image")]
    Image,
}

/// When a note is against a diff, the position of the note
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NotePosition {
    /// Base commit in the source branch
    pub base_sha: ObjectId,
    /// SHA referencing the commit in the target branch
    pub start_sha: ObjectId,
    /// The HEAD of the merge request
    pub head_sha: ObjectId,
    /// Whether this note is against text or image
    /// FIXME: image not supported yet.
    pub position_type: NotePositionType,
    /// File path before change
    pub old_path: String,
    /// File path after change
    pub new_path: String,
    /// Line number before the change
    pub old_line: Option<u64>,
    /// Line number after the change
    pub new_line: Option<u64>,
}

/// A comment on an entity.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Note {
    /// The ID of the note.
    pub id: NoteId,
    /// The type of the note.
    #[serde(rename = "type")]
    pub note_type: Option<DiscussionNoteType>,
    /// The content of the note.
    pub body: String,
    /// The URL of an attachment to the note.
    pub attachment: Option<String>,
    /// The author of the note.
    pub author: UserBasic,
    /// When the note was created.
    pub created_at: DateTime<Utc>,
    /// When the note was last updated.
    pub updated_at: DateTime<Utc>,
    /// Whether the note can be resolved.
    pub resolvable: bool,
    /// Whether the note has been resolved.
    pub resolved: Option<bool>,
    /// The user that resolved the note.
    pub resolved_by: Option<UserBasic>,
    /// Whether the note was created by a user or in response to an external action.
    ///
    /// System notes include indications that the commit, issue, etc. was referenced elsewhere, a
    /// milestone, assignee, or label change, status chages, and so on.
    pub system: bool,
    // Keep as JSON because its type depends on what `noteable_type` is.
    noteable_id: Value,
    // Keep as JSON because its type depends on what `noteable_type` is.
    noteable_iid: Option<Value>,
    /// The type of entity the note is attached to.
    pub noteable_type: NoteType,
    /// If applicable, the diff data to which the note is attached
    pub position: Option<NotePosition>,
}

impl Note {
    /// The ID of the entity the note is attached to.
    pub fn noteable_id(&self) -> Option<NoteableId> {
        match self.noteable_type {
            NoteType::Commit => {
                self.noteable_id
                    .as_str()
                    .map(|id| NoteableId::Commit(ObjectId::new(id)))
            },
            NoteType::Issue => {
                self.noteable_id
                    .as_u64()
                    .map(|id| NoteableId::Issue(IssueId::new(id)))
            },
            NoteType::MergeRequest => {
                self.noteable_id
                    .as_u64()
                    .map(|id| NoteableId::MergeRequest(MergeRequestId::new(id)))
            },
            NoteType::Snippet => {
                self.noteable_id
                    .as_u64()
                    .map(|id| NoteableId::Snippet(SnippetId::new(id)))
            },
        }
    }

    /// The internal ID of the entity the note is attached to (internal to a project).
    /// This is available only for notes attached to issues and merge requests.
    pub fn noteable_iid(&self) -> Option<NoteableInternalId> {
        match self.noteable_type {
            NoteType::Commit => None,
            NoteType::Issue => {
                self.noteable_iid
                    .as_ref()
                    .and_then(|value| value.as_u64())
                    .map(|id| NoteableInternalId::Issue(IssueInternalId::new(id)))
            },
            NoteType::MergeRequest => {
                self.noteable_iid
                    .as_ref()
                    .and_then(|value| value.as_u64())
                    .map(|id| NoteableInternalId::MergeRequest(MergeRequestInternalId::new(id)))
            },
            NoteType::Snippet => None,
        }
    }
}

/// A threaded discussion
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Discussion {
    /// The discussion ID, a SHA hash
    pub id: ObjectId,
    /// True if the discussion only holds one note.
    pub individual_note: bool,
    /// The discussion notes
    pub notes: Vec<Note>,
}

impl_id!(AwardId, "Type-safe award ID.");

/// An ID of an entity which may receive an award.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AwardableId {
    /// The ID of an awarded issue.
    Issue(IssueId),
    /// The ID of an awarded merge request.
    MergeRequest(MergeRequestId),
    /// The ID of an awarded snippet.
    Snippet(SnippetId),
    /// The ID of an awarded note.
    Note(NoteId),
}

/// The entities which may be awarded.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum AwardableType {
    /// An award on an issue.
    Issue,
    /// An award on a merge request.
    MergeRequest,
    /// An award on a snippet.
    Snippet,
    /// An award on a note.
    Note,
}

/// An awarded emoji on an entity.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AwardEmoji {
    /// The ID of the award.
    pub id: AwardId,
    /// The name of the awarded emoji.
    pub name: String,
    /// The user which created the award.
    pub user: UserBasic,
    /// When the award was created.
    pub created_at: DateTime<Utc>,
    /// When the award was last updated.
    pub updated_at: DateTime<Utc>,
    awardable_id: u64,
    /// The type of entity that is awarded.
    pub awardable_type: AwardableType,
}

impl AwardEmoji {
    /// The ID of the entity the award is attached to.
    pub fn awardable_id(&self) -> AwardableId {
        match self.awardable_type {
            AwardableType::Issue => AwardableId::Issue(IssueId::new(self.awardable_id)),
            AwardableType::MergeRequest => {
                AwardableId::MergeRequest(MergeRequestId::new(self.awardable_id))
            },
            AwardableType::Snippet => AwardableId::Snippet(SnippetId::new(self.awardable_id)),
            AwardableType::Note => AwardableId::Note(NoteId::new(self.awardable_id)),
        }
    }
}

/// The type of line commented on.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineType {
    /// An added line was commented on.
    #[serde(rename = "new")]
    New,
    /// An deleted line was commented on.
    #[serde(rename = "old")]
    Old,
}

/// A note on a commit diff.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommitNote {
    /// The content of the note.
    pub note: String,
    /// The path of the file commented on.
    pub path: Option<String>,
    /// The line of the file commented on.
    pub line: Option<u64>,
    /// The type of the line commented on.
    pub line_type: Option<LineType>,
    /// The author of the note.
    pub author: UserBasic,
    /// When the note was created.
    pub created_at: DateTime<Utc>,
}

impl_id!(CommitStatusId, "Type-safe commit status ID.");

/// States for commit statuses.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusState {
    /// The check was created.
    #[serde(rename = "created")]
    Created,
    /// The check is waiting for some other resource.
    #[serde(rename = "waiting_for_resource")]
    WaitingForResource,
    /// The check is currently being prepared.
    #[serde(rename = "preparing")]
    Preparing,
    /// The check is queued.
    #[serde(rename = "pending")]
    Pending,
    /// The check is currently running.
    #[serde(rename = "running")]
    Running,
    /// The check succeeded.
    #[serde(rename = "success")]
    Success,
    /// The check failed.
    #[serde(rename = "failed")]
    Failed,
    /// The check was canceled.
    #[serde(rename = "canceled")]
    Canceled,
    /// The check was skipped.
    #[serde(rename = "skipped")]
    Skipped,
    /// The check is waiting for manual action.
    #[serde(rename = "manual")]
    Manual,
    /// The check is scheduled to run at some point in time.
    #[serde(rename = "scheduled")]
    Scheduled,
}

/// A status of a commit.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommitStatus {
    /// The ID of the commit status.
    pub id: CommitStatusId,
    /// The object ID of the commit this status is for.
    pub sha: ObjectId,
    #[serde(rename = "ref")]
    /// The name of the reference the status was created for.
    pub ref_: Option<String>,
    /// The state of the commit status.
    pub status: StatusState,
    /// The name of the commit status.
    pub name: String,
    /// The URL associated with the commit status.
    pub target_url: Option<String>,
    /// The description of the commit status.
    pub description: Option<String>,
    /// When the commit status was created.
    pub created_at: DateTime<Utc>,
    /// When the commit status started.
    pub started_at: Option<DateTime<Utc>>,
    /// When the commit status completed.
    pub finished_at: Option<DateTime<Utc>>,
    /// Whether the commit status is allowed to fail.
    pub allow_failure: bool,
    pub coverage: Option<f64>,
    /// The author of the commit status.
    pub author: UserBasic,
}

impl_id!(EnvironmentId, "Type-safe environment ID.");

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Environment {
    pub id: EnvironmentId,
    pub name: String,
    pub slug: String,
    pub external_url: Option<String>,
    pub state: Option<String>,
    pub last_deployment: Option<Deployment>,
}

impl_id!(DeploymentId, "Type-safe deployment ID.");

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Deployment {
    pub id: DeploymentId,
    pub iid: u64,
    pub r#ref: String,
    pub sha: String,
    pub created_at: String,
    pub status: Option<String>,
    pub user: UserBasic,
    pub deployable: Deployable,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Deployable {
    pub commit: Commit,
    pub status: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Commit {
    pub id: Option<String>,
    pub short_id: Option<String>,
    pub created_at: Option<String>,
    pub title: Option<String>,
}

/// The target of an event.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventTargetType {
    /// An event targeted a commit.
    #[serde(rename = "commit")]
    Commit,
    /// An event targeted an issue.
    #[serde(rename = "issue")]
    Issue,
    /// An event targeted a merge request.
    #[serde(rename = "merge_request")]
    MergeRequest,
    /// An event targeted a snippet.
    #[serde(rename = "snippet")]
    Snippet,
    /// An event targeted a project snippet.
    #[serde(rename = "project_snippet")]
    ProjectSnippet,
}

/// The ID of an event target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventTargetId {
    /// The object ID of a commit event target.
    Commit(ObjectId),
    /// The ID of an issue event target.
    Issue(IssueId),
    /// The ID of a merge request event target.
    MergeRequest(MergeRequestId),
    /// The ID of a snippet event target.
    Snippet(SnippetId),
}

/// An event on a project.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Event {
    /// The title of the event.
    pub title: Option<String>,
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The action which triggered the event.
    // FIXME: This should be an enumeration.
    pub action_name: String,
    target_id: Value,
    /// The type of the event target.
    pub target_type: EventTargetType,
    /// The ID of the author of the event.
    pub author_id: UserId,
    pub data: Option<Value>,
    /// The title of the target.
    pub target_title: String,
    /// When the event was created.
    pub created_at: DateTime<Utc>,
    pub note: Option<Note>,
    /// The author of the event.
    pub author: Option<UserBasic>,
    /// The handle of the author.
    pub author_username: Option<String>,
}

impl Event {
    /// The ID of an event's target.
    pub fn target_id(&self) -> Option<EventTargetId> {
        match self.target_type {
            EventTargetType::Commit => {
                self.target_id
                    .as_str()
                    .map(|id| EventTargetId::Commit(ObjectId(id.into())))
            },
            EventTargetType::Issue => {
                self.target_id
                    .as_u64()
                    .map(|id| EventTargetId::Issue(IssueId(id)))
            },
            EventTargetType::MergeRequest => {
                self.target_id
                    .as_u64()
                    .map(|id| EventTargetId::MergeRequest(MergeRequestId(id)))
            },
            EventTargetType::Snippet => {
                self.target_id
                    .as_u64()
                    .map(|id| EventTargetId::Snippet(SnippetId(id)))
            },
            EventTargetType::ProjectSnippet => {
                self.target_id
                    .as_u64()
                    .map(|id| EventTargetId::Snippet(SnippetId(id)))
            },
        }
    }
}

/// The kinds of namespaces supported by Gitlab.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum NamespaceKind {
    /// A user namespace.
    #[serde(rename = "user")]
    User,
    /// A group namespace.
    #[serde(rename = "group")]
    Group,
}

/// The ID of a namespace.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NamespaceId {
    /// A user namespace ID.
    User(UserId),
    /// A group namespace ID.
    Group(GroupId),
}

/// An entity which can own projects.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Namespace {
    id: u64,
    /// The URL of the namespace.
    pub path: String,
    /// The name of the namespace.
    pub name: String,
    /// The kind of the namespace.
    pub kind: NamespaceKind,
    pub full_path: String,
    /// Number of members in the namespace and its descendants.
    ///
    /// Only available when talking to GitLab as a user that can admin the namespace.
    pub members_count_with_descendants: Option<u64>,
    /// The URL of the user's avatar if namespace is a user.
    pub avatar_url: Option<String>,
    /// The URL to the namespace page (user or group).
    pub web_url: String,
}

impl Namespace {
    /// The ID of the namespace.
    pub fn id(&self) -> NamespaceId {
        match self.kind {
            NamespaceKind::User => NamespaceId::User(UserId(self.id)),
            NamespaceKind::Group => NamespaceId::Group(GroupId(self.id)),
        }
    }
}

impl_id!(RunnerId, "Type-safe runner ID.");

/// A Gitlab CI runner.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Runner {
    /// The ID of the runner.
    pub id: RunnerId,
    /// The description of the runner.
    pub description: Option<String>,
    /// Whether the runner is active or not.
    pub active: bool,
    /// Whether the runner is shared or not.
    pub is_shared: bool,
    /// The name of the runner.
    pub name: Option<String>,
}

/// An uploaded artifact from a job.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JobArtifactFile {
    /// The name of the artifact.
    pub filename: String,
    /// The size (in bytes) of the artifact.
    pub size: u64,
}

/// An uploaded artifact from a job.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JobArtifact {
    pub file_type: String,
    pub file_format: Option<String>,
    /// The name of the artifact.
    pub filename: String,
    /// The size (in bytes) of the artifact.
    pub size: u64,
}

impl_id!(JobId, "Type-safe job ID.");

/// Information about a job in Gitlab CI.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Job {
    /// The ID of the job.
    pub id: JobId,
    /// The status of the job.
    pub status: StatusState,
    pub stage: String,
    /// The name of the job.
    pub name: String,
    #[serde(rename = "ref")]
    /// The name of the reference that was tested.
    pub ref_: Option<String>,
    pub tag: bool,
    pub coverage: Option<f64>,
    /// When the job was created or marked as pending.
    pub created_at: DateTime<Utc>,
    /// When the job was started.
    pub started_at: Option<DateTime<Utc>>,
    /// When the job completed.
    pub finished_at: Option<DateTime<Utc>>,
    /// The user which ran the job.
    pub user: Option<User>,
    /// The artifact file uploaded from the job.
    pub artifacts_file: Option<JobArtifactFile>,
    /// The commit the job tested.
    pub commit: RepoCommit,
    /// The runner which ran the job.
    pub runner: Option<Runner>,
    /// The pipeline the job belongs to.
    pub pipeline: PipelineBasic,
    pub allow_failure: bool,
    pub duration: Option<f64>,
    pub artifacts: Vec<JobArtifact>,
    pub artifacts_expire_at: Option<DateTime<Utc>>,
    pub web_url: String,
}

impl_id!(PipelineId, "Type-safe pipeline ID.");

/// Information about a pipeline in Gitlab CI.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PipelineBasic {
    /// The ID of the pipeline.
    pub id: PipelineId,
    /// The ID of the project holding the pipeline.
    pub project_id: ProjectId,
    #[serde(rename = "ref")]
    /// The name of the reference that was tested.
    pub ref_: Option<String>,
    /// The object ID that was tested.
    pub sha: ObjectId,
    /// The status of the pipeline.
    pub status: StatusState,
    /// When the pipeline was created.
    pub created_at: Option<DateTime<Utc>>,
    /// When the pipeline was last updated.
    pub updated_at: Option<DateTime<Utc>>,
    /// The URL to the pipeline page.
    pub web_url: String,
}

/// More information about a pipeline in Gitlab CI.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Pipeline {
    /// The ID of the pipeline.
    pub id: PipelineId,
    /// The ID of the project holding the pipeline.
    pub project_id: ProjectId,
    /// The object ID that was tested.
    pub sha: ObjectId,
    #[serde(rename = "ref")]
    /// The name of the reference that was tested.
    pub ref_: Option<String>,
    /// The status of the pipeline.
    pub status: StatusState,
    /// The URL to the pipeline page.
    pub web_url: String,
    /// FIXME What are the semantics of this field?
    pub before_sha: Option<ObjectId>,
    /// Was this pipeline triggered by a tag.
    pub tag: bool,
    /// Error returned by the parser of `gitlab-ci.yml`, if any.
    pub yaml_errors: Option<String>,
    /// When the pipeline was created.
    pub created_at: Option<DateTime<Utc>>,
    /// When the pipeline was last updated.
    pub updated_at: Option<DateTime<Utc>>,
    /// When the pipeline began running.
    pub started_at: Option<DateTime<Utc>>,
    /// When the pipeline completed.
    pub finished_at: Option<DateTime<Utc>>,
    /// FIXME What are the semantics of this field?
    pub committed_at: Option<DateTime<Utc>>,
    /// Duration of pipeline in seconds.
    pub duration: Option<u64>,
    /// FIXME What are the semantics of this field?
    pub coverage: Option<String>,
    /// The user who triggered this pipeline.
    pub user: UserBasic,
    /// FIXME: What are the semantics of this field?
    /// See <https://gitlab.com/gitlab-org/gitlab-foss/blob/master/app/serializers/detailed_status_entity.rb>.
    pub detailed_status: Value,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PipelineVariableType {
    #[serde(rename = "env_var")]
    EnvVar,
    #[serde(rename = "file")]
    File,
}

impl Default for PipelineVariableType {
    fn default() -> Self {
        PipelineVariableType::EnvVar
    }
}

/// A pipeline variable.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PipelineVariable {
    /// Name of the variable.
    pub key: String,
    /// Value of the variable.
    pub value: String,

    /// Type of the variable (eg. `env_var`).
    #[serde(default)]
    pub variable_type: PipelineVariableType,
}

impl_id!(LabelEventId, "Type-safe label event ID.");

/// A resource label event
///
/// Note that resource events were added in Gitlab 11.2.  Any labels added or
/// removed before then will not be returned by the API.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ResourceLabelEvent {
    /// The ID for the label event
    pub id: LabelEventId,
    pub user: UserBasic,
    pub created_at: DateTime<Utc>,
    /// The merge request id, or issue id (depending on the value of resource_type)
    resource_id: u64,
    /// Either "MergeRequest" or "Issue"
    resource_type: String,
    /// The label may be None if the label has been deleted.
    pub label: Option<EventLabel>,
    pub action: String,
}

impl ResourceLabelEvent {
    /// Returns the id of the merge request or issue that this event is from
    pub fn event_target(&self) -> Option<ResourceLabelEventTarget> {
        match self.resource_type.as_ref() {
            "MergeRequest" => {
                Some(ResourceLabelEventTarget::MergeRequest(MergeRequestId::new(
                    self.resource_id,
                )))
            },
            "Issue" => {
                Some(ResourceLabelEventTarget::Issue(IssueId::new(
                    self.resource_id,
                )))
            },
            _ => None,
        }
    }
}

/// The type of object that on which the resource label event was created
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResourceLabelEventTarget {
    /// The ID of an issue event target.
    Issue(IssueId),
    /// The ID of a merge request event target.
    MergeRequest(MergeRequestId),
}

/// An label on a project.
///
/// This is like [Label], except that it doesn't have all the same fields
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EventLabel {
    /// The Id of the label.
    pub id: LabelId,
    /// The name of the label.
    pub name: String,
    /// The color of the label.
    pub color: LabelColor,
    /// The description of the label.
    pub description: Option<String>,
}