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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug)]
pub struct User {
    pub id: usize,
    pub username: String,
}

#[derive(Deserialize, Debug)]
pub struct Pipeline {
    pub id: usize,
    pub sha: String,
    #[serde(rename = "ref")]
    pub branch: String,
    pub status: String,
    pub updated_at: DateTime<Utc>,
}

#[derive(Deserialize, Debug)]
pub struct Emoji {
    pub id: usize,
    pub name: String,
    pub user: User,
    pub awardable_id: usize,
    pub awardable_type: String,
}

/// Merge request message from GitLab
///
/// See [api/merge_requests](https://docs.gitlab.com/ee/api/merge_requests.html)
#[derive(Deserialize, Debug)]
pub struct MergeRequest {
    pub title: String,
    pub description: Option<String>,
    pub project_id: usize,
    pub iid: usize, // internal id
    pub work_in_progress: bool,
    pub author: User,
    pub merge_status: String,
    pub source_branch: String,
    pub target_branch: String,
    pub sha: String,
    pub references: Reference,
    pub web_url: String,
    pub main_issue: Option<Issue>,
}

#[derive(Serialize, Debug, Default)]
pub struct CreateMergeRequest {
    #[serde(rename = "id")]
    pub project_id: String,
    pub source_branch: String,
    pub target_branch: String,
    pub title: String,
    pub milestone_id: Option<usize>,
    pub description: Option<String>,
    pub assignee_ids: Vec<usize>,
    /// Comma separated
    pub labels: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct MergeRequestDetails {
    pub title: String,
    pub project_id: usize,
    pub iid: usize, // internal id
    pub work_in_progress: bool,
    pub author: User,
    pub merge_status: String,
    pub source_branch: String,
    pub target_branch: String,
    pub sha: Option<String>,
    // should be there with `include_rebase_in_progress=true`, but sometime is missing
    pub rebase_in_progress: Option<bool>,
    pub merge_error: Option<String>,
    pub pipeline: Option<Pipeline>,
    // should be there with `include_diverged_commits_count=true`, but perhaps 0 is not shown?
    pub diverged_commits_count: Option<i32>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum MergeRequestState {
    Opened,
    Closed,
    Merged,
}

impl MergeRequestState {
    pub fn name(&self) -> &str {
        match self {
            MergeRequestState::Opened => "opened",
            MergeRequestState::Closed => "closed",
            MergeRequestState::Merged => "merged",
        }
    }
}

impl std::str::FromStr for MergeRequestState {
    type Err = String;
    fn from_str(state: &str) -> Result<Self, Self::Err> {
        match state {
            "opened" => Ok(MergeRequestState::Opened),
            "closed" => Ok(MergeRequestState::Closed),
            "merged" => Ok(MergeRequestState::Merged),
            other_state => Err(format!(
                "Invalid state, {} not in [opened, closed, merged]",
                other_state
            )
            .to_string()),
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct ProjectMilestone {
    pub title: String,
    pub id: usize,
    pub iid: usize,
    pub project_id: Option<usize>,
    pub group_id: Option<usize>,
    pub description: String,
    pub due_date: Option<String>,
    pub start_date: Option<String>,
    pub state: String,
}

#[derive(Serialize, Debug)]
pub struct CreateProjectMilestone {
    #[serde(rename = "id")]
    pub project_id: usize,
    pub title: String,
    pub description: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct Issue {
    pub title: String,
    pub iid: usize, // internal id
    pub state: IssueState,
    pub description: Option<String>,
    pub author: User,
    pub milestone: Option<ProjectMilestone>,
    pub project_id: usize,
    pub assignees: Vec<User>,
    pub web_url: String,
    pub weight: Option<usize>,
    pub labels: Vec<String>,
    pub references: Reference,
}

#[derive(Deserialize, Debug)]
pub struct Reference {
    pub short: String,
    pub relative: String,
    pub full: String,
}

#[derive(Serialize, Debug, Default)]
pub struct CreateIssue {
    #[serde(rename = "id")]
    pub project_id: String,
    pub title: String,
    pub description: Option<String>,
    pub milestone_id: Option<usize>,
    pub assignee_ids: Vec<usize>,
    pub weight: Option<usize>,
    /// Comma separated
    pub labels: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum IssueState {
    All,
    Opened,
    Closed,
}

impl IssueState {
    pub fn name(&self) -> &str {
        match self {
            IssueState::All => "all",
            IssueState::Closed => "closed",
            IssueState::Opened => "opened",
        }
    }
}

impl std::str::FromStr for IssueState {
    type Err = String;
    fn from_str(state: &str) -> Result<Self, Self::Err> {
        match state {
            "all" => Ok(IssueState::All),
            "closed" => Ok(IssueState::Closed),
            "opened" => Ok(IssueState::Opened),
            other_state => Err(format!(
                "Invalid state, {} not in [all, opened, closed]",
                other_state
            )
            .to_string()),
        }
    }
}

#[derive(Serialize, Debug)]
pub struct RebaseBranch {
    pub id: usize,
    pub merge_request_iid: usize,
}

impl<'a> From<&'a MergeRequest> for RebaseBranch {
    fn from(mr: &'a MergeRequest) -> RebaseBranch {
        RebaseBranch {
            id: mr.project_id,
            merge_request_iid: mr.iid,
        }
    }
}

#[derive(Serialize, Debug)]
pub struct RemoveSourceBranch {
    pub id: usize,
    pub merge_request_iid: usize,
    pub remove_source_branch: bool,
}

impl<'a> From<&'a MergeRequest> for RemoveSourceBranch {
    fn from(mr: &'a MergeRequest) -> RemoveSourceBranch {
        RemoveSourceBranch {
            id: mr.project_id,
            merge_request_iid: mr.iid,
            remove_source_branch: true,
        }
    }
}

#[derive(Serialize, Debug)]
pub struct ListPipelines {
    pub id: usize,
    pub merge_request_iid: usize,
}

impl<'a> From<&'a MergeRequest> for ListPipelines {
    fn from(mr: &'a MergeRequest) -> ListPipelines {
        ListPipelines {
            id: mr.project_id,
            merge_request_iid: mr.iid,
        }
    }
}

#[derive(Serialize, Debug)]
pub struct AcceptMergeRequest {
    pub id: usize,
    pub merge_request_iid: usize,
    pub merge_when_pipeline_succeeds: bool,
    pub should_remove_source_branch: bool,
    pub sha: String,
}

impl<'a> From<&'a MergeRequest> for AcceptMergeRequest {
    fn from(mr: &'a MergeRequest) -> AcceptMergeRequest {
        AcceptMergeRequest {
            id: mr.project_id,
            merge_request_iid: mr.iid,
            merge_when_pipeline_succeeds: true,
            should_remove_source_branch: true,
            sha: mr.sha.clone(),
        }
    }
}

#[derive(Serialize, Debug)]
pub struct GetMergeRequestDetails {
    pub id: usize,
    pub merge_request_iid: usize,
    pub include_diverged_commits_count: bool,
    pub include_rebase_in_progress: bool,
}

impl GetMergeRequestDetails {
    pub fn new(project_id: usize, merge_request_iid: usize) -> Self {
        GetMergeRequestDetails {
            id: project_id,
            merge_request_iid,
            include_diverged_commits_count: true,
            include_rebase_in_progress: true,
        }
    }
}

impl<'a> From<&'a MergeRequest> for GetMergeRequestDetails {
    fn from(mr: &'a MergeRequest) -> GetMergeRequestDetails {
        GetMergeRequestDetails::new(mr.project_id, mr.iid)
    }
}

#[derive(Serialize, Debug)]
pub struct AwardEmoji {
    pub id: usize,
    pub awardable_id: usize,
}

impl<'a> From<&'a MergeRequest> for AwardEmoji {
    fn from(mr: &'a MergeRequest) -> AwardEmoji {
        AwardEmoji {
            id: mr.project_id,
            awardable_id: mr.iid,
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct Project {
    pub id: usize,
    pub description: Option<String>,
    pub ssh_url_to_repo: String,
    pub http_url_to_repo: String,
    pub web_url: String,
    pub name: String,
    pub name_with_namespace: String,
    pub path: String,
    pub path_with_namespace: String,
    pub avatar_url: Option<String>,
}