types.rs 67.4 KB
Newer Older
1
2
3
4
5
6
7
8
// Copyright 2016 Kitware, Inc.
//
// 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.

Ben Boeckel's avatar
Ben Boeckel committed
9
10
11
12
13
14
15
16
//! 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.

Brad King's avatar
Brad King committed
17
use crates::chrono::{DateTime, NaiveDate, Utc};
18
use crates::serde::{Deserialize, Deserializer, Serialize, Serializer};
19
use crates::serde::de::{DeserializeOwned, Error, Unexpected};
20
use crates::serde_json::{self, Value};
21
22
23
24

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

// This is only used in internal API calls.
25
//#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
26
27
28
29
30
31
//#[derive(Serialize, Deserialize, Debug, Clone)]
//pub struct UserSafe {
//    pub username: String,
//    pub name: String,
//}

32
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe user ID.
pub struct UserId(u64);
impl_id!(UserId);

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

54
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Basic user information.
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: 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.
77
78
pub trait UserResult: DeserializeOwned {}
impl<T: DeserializeOwned + Into<UserBasic>> UserResult for T {}
79

80
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[derive(Serialize, Deserialize, Debug, Clone)]
/// More detailed information only accessible to administrators.
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: String,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the account was created.
Brad King's avatar
Brad King committed
97
    pub created_at: DateTime<Utc>,
98
    /// Whether the user is an administrator or not.
Brad King's avatar
Brad King committed
99
    ///
100
101
    /// Only available when talking to GitLab as an admin.
    pub is_admin: Option<bool>,
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
    /// Self-described biography of the user.
    pub bio: Option<String>,
    /// Geographic location of the user.
    pub location: 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,
        }
    }
}

132
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
133
134
135
136
137
138
139
140
141
#[derive(Serialize, Deserialize, Debug, Clone)]
/// External authentication tokens.
pub struct Identity {
    /// The provider of the token.
    pub provider: String,
    /// The UID for the provider.
    pub extern_uid: String,
}

142
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
143
144
145
146
147
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe theme ID.
pub struct ThemeId(u64);
impl_id!(ThemeId);

148
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
149
150
151
152
153
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe color scheme ID.
pub struct ColorSchemeId(u64);
impl_id!(ColorSchemeId);

154
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Full user structure information.
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: String,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the account was created.
Brad King's avatar
Brad King committed
171
    pub created_at: DateTime<Utc>,
172
    /// Whether the user is an administrator or not.
Brad King's avatar
Brad King committed
173
    ///
174
175
    /// Only available when talking to GitLab as an admin.
    pub is_admin: Option<bool>,
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    /// Self-described biography of the user.
    pub bio: Option<String>,
    /// Geographic location of the user.
    pub location: 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.
Brad King's avatar
Brad King committed
193
    pub last_sign_in_at: Option<DateTime<Utc>>,
Ben Boeckel's avatar
Ben Boeckel committed
194
195
    /// When the user last made an action.
    pub last_activity_on: Option<NaiveDate>,
196
    /// When the user's account was confirmed.
Brad King's avatar
Brad King committed
197
    pub confirmed_at: Option<DateTime<Utc>>,
198
199
200
    /// The primary email address for the user.
    pub email: String,

Brad King's avatar
Brad King committed
201
202
    /// The theme used by the user, if configured.
    pub theme_id: Option<ThemeId>,
203
204
205
206
207
    /// 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.
Brad King's avatar
Brad King committed
208
    pub current_sign_in_at: Option<DateTime<Utc>>,
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

    /// 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,
246
            is_admin: user.is_admin,
247
248
249
250
251
252
253
254
255
256
257
            bio: user.bio,
            location: user.location,
            skype: user.skype,
            linkedin: user.linkedin,
            twitter: user.twitter,
            website_url: user.website_url,
            organization: user.organization,
        }
    }
}

258
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
259
260
261
262
263
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe email ID.
pub struct EmailId(u64);
impl_id!(EmailId);

264
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
265
266
267
268
269
270
271
272
273
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Email address.
pub struct Email {
    /// ID of the email.
    pub id: EmailId,
    /// The email address.
    pub email: String,
}

274
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
275
276
277
278
279
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe hook ID.
pub struct HookId(u64);
impl_id!(HookId);

280
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
281
282
283
284
285
286
287
288
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A web hook to notify of events.
pub struct Hook {
    /// The ID of the hook.
    pub id: HookId,
    /// The URL to contact.
    pub url: String,
    /// When the hook was created.
Brad King's avatar
Brad King committed
289
    pub created_at: DateTime<Utc>,
290
291
292
293
294
295
296
297
    /// 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,
}

298
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
299
300
301
302
303
304
305
306
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A web hook to notify of project events.
pub struct ProjectHook {
    /// The ID of the hook.
    pub id: HookId,
    /// The URL to contact.
    pub url: String,
    /// When the hook was created.
Brad King's avatar
Brad King committed
307
    pub created_at: DateTime<Utc>,
308
309
310
311
312
313
314
315
316
317
318
319
    /// The project associated with the hook.
    pub project_id: ProjectId,
    /// 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 hook is contacted for issue events.
    pub issues_events: 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,
Brad King's avatar
Brad King committed
320
321
    /// Whether the hook is contacted for repository update events.
    pub repository_update_events: bool,
322
323
    /// Whether the communication with the hook is verified using TLS certificates.
    pub enable_ssl_verification: bool,
Brad King's avatar
Brad King committed
324
325
    /// Whether the hook is contacted for job events.
    pub job_events: bool,
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    /// 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,
}

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,
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
/// The events a webhook listener may receive.
pub struct WebhookEvents {
Brad King's avatar
Brad King committed
348
349
    /// Whether to receive job events of not.
    job: bool,
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    /// Whether to receive issue events of not.
    issues: bool,
    /// Whether to receive merge request events of not.
    merge_requests: bool,
    /// Whether to receive note (comment) events of not.
    note: bool,
    /// Whether to receive pipeline events of not.
    pipeline: bool,
    /// Whether to receive push events of not.
    push: bool,
    /// Whether to receive wiki events of not.
    wiki_page: bool,
}

impl WebhookEvents {
    /// Create a new, empty webhook event set.
    pub fn new() -> Self {
        WebhookEvents {
Brad King's avatar
Brad King committed
368
            job: false,
369
370
371
372
373
374
375
376
377
            issues: false,
            merge_requests: false,
            note: false,
            pipeline: false,
            push: false,
            wiki_page: false,
        }
    }

Brad King's avatar
Brad King committed
378
    with_event!{with_job, job}
379
380
381
382
383
384
385
    with_event!{with_issues, issues}
    with_event!{with_merge_requests, merge_requests}
    with_event!{with_note, note}
    with_event!{with_pipeline, pipeline}
    with_event!{with_push, push}
    with_event!{with_wiki_page, wiki_page}

Brad King's avatar
Brad King committed
386
    get_event!{job}
387
388
389
390
391
392
393
394
    get_event!{issues}
    get_event!{merge_requests}
    get_event!{note}
    get_event!{pipeline}
    get_event!{push}
    get_event!{wiki_page}
}

395
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
396
397
398
399
400
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe project ID.
pub struct ProjectId(u64);
impl_id!(ProjectId);

401
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Basic project information.
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(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum VisibilityLevel {
    /// The project is visible to anonymous users.
Brad King's avatar
Brad King committed
425
    Public,
426
    /// The project is visible to logged in users.
Brad King's avatar
Brad King committed
427
    Internal,
428
    /// The project is visible only to users with explicit access.
Brad King's avatar
Brad King committed
429
    Private,
430
}
Brad King's avatar
Brad King committed
431
432
433
434
435
enum_serialize!(VisibilityLevel -> "visibility level",
    Public => "public",
    Internal => "internal",
    Private => "private",
);
436
437
438

// TODO: enum for NotificationLevel

439
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
440
441
442
443
444
445
446
447
448
449
450
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Structure for a group a project has been shared with.
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,
}

451
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
452
453
454
455
456
457
458
459
460
461
462
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Access information to a project.
// 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>,
}

463
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
464
465
466
467
468
469
470
471
472
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Permissions granted to the current user to a project.
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>,
}

473
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
474
475
476
477
478
479
480
#[derive(Serialize, Deserialize, Debug, Clone)]
/// The avatar of a project's namespace.
pub struct ProjectNamespaceAvatar {
    /// The URL of the namespace avatar.
    pub url: Option<String>,
}

Brad King's avatar
Brad King committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
#[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,
}

501
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
502
503
504
505
506
507
508
509
510
511
512
513
514
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Project information.
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,
Brad King's avatar
Brad King committed
515
516
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    /// 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 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.
Brad King's avatar
Brad King committed
539
    pub created_at: DateTime<Utc>,
540
    /// When the last activity on the project occurred.
Brad King's avatar
Brad King committed
541
    pub last_activity_at: DateTime<Utc>,
542
543
544
545
546
547
548
549
550
551
552
553
    /// 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>,
Brad King's avatar
Brad King committed
554
555
556
557
    /// 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>,
558
559
560
561
562
563
564
565
    /// 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>,
Brad King's avatar
Brad King committed
566
567
    /// Whether jobs are publicly visible.
    pub public_jobs: bool,
568
569
    /// Groups the project is shared with.
    pub shared_with_groups: Vec<SharedGroup>,
Brad King's avatar
Brad King committed
570
571
    /// Whether the project only enables the merge button if all pipelines are passing.
    pub only_allow_merge_if_pipeline_succeeds: Option<bool>,
572
573
    /// Whether the project only enables the merge button if all discussions are resolved.
    pub only_allow_merge_if_all_discussions_are_resolved: Option<bool>,
Brad King's avatar
Brad King committed
574
575
    /// Whether to show the link to create/view merge request when pusing from command line.
    pub printing_merge_request_link_enabled: Option<bool>,
576
577
    /// Whether access to the project may be requested.
    pub request_access_enabled: bool,
Brad King's avatar
Brad King committed
578
579
    /// Whether jobs are enabled or not.
    pub jobs_enabled: bool,
Brad King's avatar
Brad King committed
580
581
582
    /// Whether to automatically resolve merge request diff discussions when they become outdated,
    /// if configured.
    pub resolve_outdated_diff_discussions: Option<bool>,
583
584
585
586
587
588
589
590
591
592
593
594
595
596
    /// 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,
    /// 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>,
Brad King's avatar
Brad King committed
597
598
599
600
601
602
603
604
605
606
607
608

    /// 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()
    }
609
610
}

611
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
612
613
614
615
616
617
618
619
620
621
622
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Statistics about a project.
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,
Brad King's avatar
Brad King committed
623
624
    /// The size, in bytes, of uploaded job artifacts.
    pub job_artifacts_size: u64,
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
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// Access levels for groups and projects.
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,
    /// Master access (can push to protected branches).
    Master,
    /// Owner access (full rights).
    Owner,
}

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::Master => 40,
            AccessLevel::Owner => 50,
        }
    }
}

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

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

681
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A member with extra permissions on a project.
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: 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.
Brad King's avatar
Brad King committed
700
    pub expires_at: Option<NaiveDate>,
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
}

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,
        }
    }
}

716
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A member with extra permissions on a project.
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: String,
    /// The URL of the user's profile page.
    pub web_url: String,
    /// When the membership request was created.
Brad King's avatar
Brad King committed
733
    pub requested_at: DateTime<Utc>,
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}

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,
        }
    }
}

749
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
750
751
752
753
754
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe group ID.
pub struct GroupId(u64);
impl_id!(GroupId);

755
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
756
757
758
759
760
761
762
763
764
765
766
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Group information.
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>,
Brad King's avatar
Brad King committed
767
768
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
769
770
771
772
773
774
775
776
    /// Whether LFS is enabled for the group.
    pub lfs_enabled: bool,
    /// The URL to the group avatar.
    pub avatar_url: 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,
Ben Boeckel's avatar
Ben Boeckel committed
777
778
    pub full_name: String,
    pub full_path: String,
Ben Boeckel's avatar
Ben Boeckel committed
779
    pub parent_id: Option<GroupId>,
780
    /// Statistics about the group.
Brad King's avatar
Brad King committed
781
    pub statistics: Option<GroupStatistics>,
782
783
}

784
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
785
786
787
788
789
790
791
792
793
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Statistics about a group.
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,
Brad King's avatar
Brad King committed
794
795
    /// The size, in bytes, of uploaded job artifacts in the group.
    pub job_artifacts_size: u64,
796
797
}

798
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
799
800
801
802
803
804
805
806
807
808
809
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Group information with a project listing.
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>,
Brad King's avatar
Brad King committed
810
811
    /// Whether the project is public, internal, or private.
    pub visibility: VisibilityLevel,
812
813
814
815
816
817
818
819
820
821
822
823
    /// Whether LFS is enabled for the group.
    pub lfs_enabled: bool,
    /// The URL to the group avatar.
    pub avatar_url: 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,
Ben Boeckel's avatar
Ben Boeckel committed
824
825
    pub full_name: String,
    pub full_path: String,
Ben Boeckel's avatar
Ben Boeckel committed
826
    pub parent_id: Option<GroupId>,
827
    /// Statistics about the group.
Brad King's avatar
Brad King committed
828
    pub statistics: Option<GroupStatistics>,
829
830
831
832
833
834
835
836
837
}

impl From<GroupDetail> for Group {
    fn from(detail: GroupDetail) -> Self {
        Group {
            id: detail.id,
            name: detail.name,
            path: detail.path,
            description: detail.description,
Brad King's avatar
Brad King committed
838
            visibility: detail.visibility,
839
840
841
842
            lfs_enabled: detail.lfs_enabled,
            avatar_url: detail.avatar_url,
            web_url: detail.web_url,
            request_access_enabled: detail.request_access_enabled,
Ben Boeckel's avatar
Ben Boeckel committed
843
844
            full_name: detail.full_name,
            full_path: detail.full_path,
Ben Boeckel's avatar
Ben Boeckel committed
845
            parent_id: detail.parent_id,
846
847
848
849
850
            statistics: detail.statistics,
        }
    }
}

851
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
852
853
854
855
856
857
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A branch on a repository.
pub struct RepoBranch {
    /// The name of the branch.
    pub name: String,
    /// The commit of the branch.
Ben Boeckel's avatar
Ben Boeckel committed
858
    pub commit: Option<RepoCommit>,
859
860
861
862
863
864
865
866
867
868
    /// 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>,
}

869
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
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
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
/// The ID of a git object.
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
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The kinds of objects Gitlab can return.
pub enum ObjectType {
    /// A `tree` object.
    Tree,
    /// A `blob` object.
    Blob,
}
enum_serialize!(ObjectType -> "object type",
    Tree => "tree",
    Blob => "blob",
);

899
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An object inside of a repository.
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,
}

916
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
917
918
919
920
921
922
923
924
925
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A commit in a project.
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,
Ben Boeckel's avatar
Ben Boeckel committed
926
927
    /// The commit ID of the parents of the commit.
    pub parent_ids: Vec<ObjectId>,
928
929
930
931
    /// The commit author's name.
    pub author_name: String,
    /// The commit author's email address.
    pub author_email: String,
Ben Boeckel's avatar
Ben Boeckel committed
932
    /// The commit's authorship date.
Brad King's avatar
Brad King committed
933
    pub authored_date: DateTime<Utc>,
934
935
936
937
    /// The committer's name.
    pub committer_name: String,
    /// The committer's email address.
    pub committer_email: String,
Ben Boeckel's avatar
Ben Boeckel committed
938
    /// The commit's commit date.
Brad King's avatar
Brad King committed
939
940
    pub committed_date: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
941
942
943
944
    /// The full commit message.
    pub message: String,
}

945
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
946
947
948
949
950
951
952
953
954
955
956
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Stats about a commit.
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,
}

957
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
958
959
960
961
962
963
964
965
966
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A commit in a project with statistics.
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,
Ben Boeckel's avatar
Ben Boeckel committed
967
968
    /// The commit ID of the parents of the commit.
    pub parent_ids: Vec<ObjectId>,
969
970
971
972
    /// The commit author's name.
    pub author_name: String,
    /// The commit author's email address.
    pub author_email: String,
Ben Boeckel's avatar
Ben Boeckel committed
973
    /// The commit's authorship date.
Brad King's avatar
Brad King committed
974
    pub authored_date: DateTime<Utc>,
Ben Boeckel's avatar
Ben Boeckel committed
975
976
977
978
979
    /// The committer's name.
    pub committer_name: String,
    /// The committer's email address.
    pub committer_email: String,
    /// The commit's commit date.
Brad King's avatar
Brad King committed
980
981
    pub committed_date: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
982
983
984
985
    /// The full commit message.
    pub message: String,
    /// Statistics about the commit.
    pub stats: RepoCommitStats,
Brad King's avatar
Brad King committed
986
987
    /// The last pipeline for this commit, if any.
    pub last_pipeline: Option<PipelineBasic>,
988
989
990
    // XXX: Investigate what this is.
    /// This looks to be CI related; ignoring without better docs.
    status: Value,
991
992
}

993
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
994
995
996
997
998
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe snippet ID.
pub struct SnippetId(u64);
impl_id!(SnippetId);

999
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A project-specific snippet.
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.
Brad King's avatar
Brad King committed
1012
    pub updated_at: DateTime<Utc>,
1013
    /// When the snippet was created.
Brad King's avatar
Brad King committed
1014
    pub created_at: DateTime<Utc>,
Ben Boeckel's avatar
Ben Boeckel committed
1015
    /// When the snippet was created.
Brad King's avatar
Brad King committed
1016
    pub expires_at: Option<DateTime<Utc>>,
1017
1018
1019
1020
1021
    /// The URL of the snippet.
    pub web_url: String,
}

// This is just used as a common "base class" in Ruby.
1022
//#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1023
1024
1025
//#[derive(Serialize, Deserialize, Debug, Clone)]
//pub struct ProjectEntity {
//    pub id: ProjectEntityId,
Brad King's avatar
Brad King committed
1026
//    pub iid: ProjectEntityInternalId,
1027
1028
1029
1030
//    pub project_id: ProjectId,
//    pub title: String,
//    pub description: String,
//    pub state: ProjectEntityState,
Brad King's avatar
Brad King committed
1031
1032
//    pub created_at: DateTime<Utc>,
//    pub updated_at: DateTime<Utc>,
1033
1034
//}

1035
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A diff within a repository.
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,
}

1056
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1057
1058
1059
1060
1061
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe milestone ID.
pub struct MilestoneId(u64);
impl_id!(MilestoneId);

Brad King's avatar
Brad King committed
1062
1063
1064
1065
1066
1067
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe milestone internal ID (internal to a project).
pub struct MilestoneInternalId(u64);
impl_id!(MilestoneInternalId);

1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The states a milestone may be in.
pub enum MilestoneState {
    /// The milestone is active.
    Active,
    /// The milestone has been closed.
    Closed,
}
enum_serialize!(MilestoneState -> "milestone type",
    Active => "active",
    Closed => "closed",
);

1081
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1082
1083
1084
1085
1086
1087
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A milestone in a project.
pub struct Milestone {
    /// The ID of the milestone.
    pub id: MilestoneId,
    /// The user-visible ID of the milestone.
Brad King's avatar
Brad King committed
1088
    pub iid: MilestoneInternalId,
1089
1090
1091
1092
1093
1094
1095
1096
1097
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the milestone.
    pub title: String,
    /// The description of the milestone.
    pub description: String,
    /// The state of the milestone.
    pub state: MilestoneState,
    /// When the milestone was created.
Brad King's avatar
Brad King committed
1098
    pub created_at: DateTime<Utc>,
1099
    /// When the milestone was last updated.
Brad King's avatar
Brad King committed
1100
    pub updated_at: DateTime<Utc>,
1101
1102
1103
    /// When the milestone is due.
    pub due_date: Option<NaiveDate>,
    /// When the milestone was started.
1104
    pub start_date: Option<NaiveDate>,
1105
1106
}

1107
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1108
1109
1110
1111
1112
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe issue ID.
pub struct IssueId(u64);
impl_id!(IssueId);

Brad King's avatar
Brad King committed
1113
1114
1115
1116
1117
1118
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe issue internal ID (internal to a project).
pub struct IssueInternalId(u64);
impl_id!(IssueInternalId);

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The states an issue may be in.
pub enum IssueState {
    /// The issue is open.
    Opened,
    /// The issue has been closed.
    Closed,
    /// The issue has been opened after being closed.
    Reopened,
}
enum_serialize!(IssueState -> "issue type",
    Opened => "opened",
    Closed => "closed",
    Reopened => "reopened",
);

Brad King's avatar
Brad King committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
#[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,
}

1149
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1150
1151
1152
1153
1154
1155
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An issue on a project.
pub struct Issue {
    /// The ID of the issue.
    pub id: IssueId,
    /// The user-visible ID of the issue.
Brad King's avatar
Brad King committed
1156
    pub iid: IssueInternalId,
1157
1158
1159
1160
1161
    /// The ID of the project.
    pub project_id: ProjectId,
    /// The title of the issue.
    pub title: String,
    /// The description of the issue.
Brad King's avatar
Brad King committed
1162
    pub description: Option<String>,
1163
1164
1165
    /// The state of the issue.
    pub state: IssueState,
    /// When the issue was created.
Brad King's avatar
Brad King committed
1166
    pub created_at: DateTime<Utc>,
1167
    /// When the issue was last updated.
Brad King's avatar
Brad King committed
1168
    pub updated_at: DateTime<Utc>,
Brad King's avatar
Brad King committed
1169
1170
    /// When the issue was closed, if closed.
    pub closed_at: Option<DateTime<Utc>>,
1171
1172
1173
1174
1175
1176
1177
1178
    /// 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>,
Brad King's avatar
Brad King committed
1179
1180
    /// The assignees of the issue.
    pub assignees: Option<Vec<UserBasic>>,
1181
    /// Whether the current user is subscribed or not.
Brad King's avatar
Brad King committed
1182
1183
1184
    /// GitLab does not include this in responses with lists of issues but
    /// does on an individual issue.
    pub subscribed: Option<bool>,
Brad King's avatar
Brad King committed
1185
1186
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
    /// The number of comments on the issue.
    pub user_notes_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 confidential or not.
    pub confidential: bool,
Brad King's avatar
Brad King committed
1197
1198
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
1199
1200
    /// The URL of the issue.
    pub web_url: String,
Brad King's avatar
Brad King committed
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212

    /// 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>,
}

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

1215
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1216
1217
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A time estimate on an issue or merge request.
Brad King's avatar
Brad King committed
1218
pub struct IssuableTimeStats {
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
    /// 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>,
}

1229
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1230
1231
1232
1233
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe external issue ID.
pub struct ExternalIssueId(u64);

1234
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
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
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An external issue reference.
pub struct ExternalIssue {
    /// The ID of the issue.
    pub id: ExternalIssueId,
    /// The title of the issue.
    pub title: String,
}

#[derive(Debug, Clone)]
/// A reference to an issue.
pub enum IssueReference {
    /// A reference to an issue on the same Gitlab host.
    Internal(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),
        }
    }
}

1262
1263
1264
1265
impl<'de> Deserialize<'de> for IssueReference {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>,
    {
1266
1267
1268
1269
1270
1271
1272
1273
1274
        let val = <Value as Deserialize>::deserialize(deserializer)?;

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

1275
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1276
1277
1278
1279
1280
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe merge request ID.
pub struct MergeRequestId(u64);
impl_id!(MergeRequestId);

Brad King's avatar
Brad King committed
1281
1282
1283
1284
1285
1286
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe merge request internal ID (internal to a project).
pub struct MergeRequestInternalId(u64);
impl_id!(MergeRequestInternalId);

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
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The status of the possible merge for a merge request.
pub enum MergeStatus {
    /// The merge request has not been checked yet.
    Unchecked,
    /// The merge request may be merged.
    CanBeMerged,
    /// The merge request may not be merged yet.
    CannotBeMerged,
}
enum_serialize!(MergeStatus -> "merge status",
    Unchecked => "unchecked",
    CanBeMerged => "can_be_merged",
    CannotBeMerged => "cannot_be_merged",
);

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

1325
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1326
1327
1328
1329
1330
1331
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A merge request.
pub struct MergeRequest {
    /// The ID of the merge request.
    pub id: MergeRequestId,
    /// The user-visible ID of the merge request.
Brad King's avatar
Brad King committed
1332
    pub iid: MergeRequestInternalId,
1333
1334
1335
1336
1337
1338
1339
1340
1341
    /// 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.
Brad King's avatar
Brad King committed
1342
    pub created_at: DateTime<Utc>,
1343
    /// When the merge request was last updated.
Brad King's avatar
Brad King committed
1344
    pub updated_at: DateTime<Utc>,
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
    /// 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>,
Brad King's avatar
Brad King committed
1357
1358
    /// The assignees of the merge request.
    pub assignees: Option<Vec<UserBasic>>,
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
    /// The ID of the project hosting the source branch.
    pub source_project_id: 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,
    /// The milestone of the merge request.
    pub milestone: Option<Milestone>,
Brad King's avatar
Brad King committed
1369
1370
    /// Whether the merge request will be merged once all pipelines succeed or not.
    pub merge_when_pipeline_succeeds: bool,
1371
1372
1373
1374
1375
1376
1377
1378
1379
    /// 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 object ID of the commit which merged the merge request.
    pub merge_commit_sha: Option<ObjectId>,
    /// Whether the current user is subscribed or not.
Brad King's avatar
Brad King committed
1380
1381
1382
    /// GitLab does not include this in responses with lists of merge requests but
    /// does on an individual merge request.
    pub subscribed: Option<bool>,
Brad King's avatar
Brad King committed
1383
1384
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
Brad King's avatar
Brad King committed
1385
    /// The number of paths changed by the merge request.
1386
1387
1388
    ///
    /// This is an integer suffixed by `+` if there are more files changed than some threshold
    /// (probably determined by a timeout).
1389
    pub changes_count: Option<String>,
1390
1391
    /// The number of comments on the merge request.
    pub user_notes_count: u64,
Brad King's avatar
Brad King committed
1392
1393
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
1394
1395
1396
1397
1398
1399
1400
1401
    /// 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>,
    /// The URL of the merge request.
    pub web_url: String,
}

1402
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1403
1404
1405
1406
1407
1408
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A merge request with changes.
pub struct MergeRequestChanges {
    /// The ID of the merge request.
    pub id: MergeRequestId,
    /// The user-visible ID of the merge request.
Brad King's avatar
Brad King committed
1409
    pub iid: MergeRequestInternalId,
1410
1411
1412
1413
1414
1415
1416
1417
1418
    /// 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.
Brad King's avatar
Brad King committed
1419
    pub created_at: DateTime<Utc>,
1420
    /// When the merge request was last updated.
Brad King's avatar
Brad King committed
1421
    pub updated_at: DateTime<Utc>,
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
    /// 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>,
Brad King's avatar
Brad King committed
1434
1435
    /// The assignees of the merge request.
    pub assignees: Option<Vec<UserBasic>>,
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
    /// The ID of the project hosting the source branch.
    pub source_project_id: 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,
    /// The milestone of the merge request.
    pub milestone: Option<Milestone>,
Brad King's avatar
Brad King committed
1446
1447
    /// Whether the merge request will be merged once all jobs succeed or not.
    pub merge_when_pipeline_succeeds: bool,
1448
1449
1450
1451
1452
1453
1454
1455
    /// 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 object ID of the commit which merged the merge request.
    pub merge_commit_sha: Option<ObjectId>,
Brad King's avatar
Brad King committed
1456
1457
1458
    /// GitLab does not include this in responses with lists of merge requests but
    /// does on an individual merge request.
    pub subscribed: Option<bool>,
Brad King's avatar
Brad King committed
1459
1460
    /// Time estimates.
    pub time_stats: IssuableTimeStats,
Brad King's avatar
Brad King committed
1461
    /// The number of paths changed by the merge request.
1462
    pub changes_count: Option<String>,
1463
1464
    /// The number of comments on the merge request.
    pub user_notes_count: u64,
Brad King's avatar
Brad King committed
1465
1466
    /// Whether the discussion has been locked.
    pub discussion_locked: Option<bool>,
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
    /// 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>,
    /// The URL of the merge request.
    pub web_url: String,
    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,
            target_branch: mr.target_branch,
            source_branch: mr.source_branch,
            upvotes: mr.upvotes,
            downvotes: mr.downvotes,
            author: mr.author,
            assignee: mr.assignee,
Brad King's avatar
Brad King committed
1493
            assignees: mr.assignees,
1494
1495
1496
1497
1498
            source_project_id: mr.source_project_id,
            target_project_id: mr.target_project_id,
            labels: mr.labels,
            work_in_progress: mr.work_in_progress,
            milestone: mr.milestone,
Brad King's avatar
Brad King committed
1499
            merge_when_pipeline_succeeds: mr.merge_when_pipeline_succeeds,
1500
1501
1502
1503
            merge_status: mr.merge_status,
            sha: mr.sha,
            merge_commit_sha: mr.merge_commit_sha,
            subscribed: mr.subscribed,
Brad King's avatar
Brad King committed
1504
            time_stats: mr.time_stats,
Brad King's avatar
Brad King committed
1505
            changes_count: mr.changes_count,
1506
            user_notes_count: mr.user_notes_count,
Brad King's avatar
Brad King committed
1507
            discussion_locked: mr.discussion_locked,
1508
1509
1510
1511
1512
1513
1514
            should_remove_source_branch: mr.should_remove_source_branch,
            force_remove_source_branch: mr.force_remove_source_branch,
            web_url: mr.web_url,
        }
    }
}

1515
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1516
1517
1518
1519
1520
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe SSH key ID.
pub struct SshKeyId(u64);
impl_id!(SshKeyId);

1521
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An uploaded SSH key.
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.
Brad King's avatar
Brad King committed
1532
    pub created_at: DateTime<Utc>,
1533
1534
1535
1536
    /// Whether the key may push to repositories or not.
    pub can_push: bool,
}

1537
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An uploaded SSH key with its owner.
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.
Brad King's avatar
Brad King committed
1548
    pub created_at: DateTime<Utc>,
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
    /// The user associated with the SSH key.
    pub user: UserPublic,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The entities a note may be added to.
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,
}
enum_serialize!(NoteType -> "note type",
    Commit => "Commit",
    Issue => "Issue",
    MergeRequest => "MergeRequest",
    Snippet => "Snippet",
);

#[derive(Debug, Clone, PartialEq, Eq)]
/// The ID of an entity a note is attached to.
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),
}

Brad King's avatar
Brad King committed
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
#[derive(Debug, Clone, PartialEq, Eq)]
/// 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.
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),
}

1595
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1596
1597
1598
1599
1600
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe note (comment) ID.
pub struct NoteId(u64);
impl_id!(NoteId);

1601
#[cfg_attr(feature="strict", serde(deny_unknown_fields))]
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A comment on an entity.
pub struct Note {
    /// The ID of the note.
    pub id: NoteId,
    /// 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.
Brad King's avatar
Brad King committed
1614
    pub created_at: DateTime<Utc>,
1615
    /// When the note was last updated.
Brad King's avatar
Brad King committed
1616
    pub updated_at: DateTime<Utc>,
1617
1618
1619
1620
1621
1622
1623
    /// 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,
Brad King's avatar
Brad King committed