1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use super::repo::Repository;
5use super::user::{Link, User};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct PullRequest {
9 pub id: u64,
10 pub title: String,
11 pub description: Option<String>,
12 pub state: PullRequestState,
13 pub author: User,
14 pub source: PullRequestEndpoint,
15 pub destination: PullRequestEndpoint,
16 pub merge_commit: Option<Commit>,
17 pub close_source_branch: Option<bool>,
18 pub closed_by: Option<User>,
19 pub reason: Option<String>,
20 pub created_on: DateTime<Utc>,
21 pub updated_on: DateTime<Utc>,
22 pub reviewers: Option<Vec<User>>,
23 pub participants: Option<Vec<Participant>>,
24 pub links: Option<PullRequestLinks>,
25 pub comment_count: Option<u32>,
26 pub task_count: Option<u32>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
30#[serde(rename_all = "UPPERCASE")]
31pub enum PullRequestState {
32 Open,
33 Merged,
34 Declined,
35 Superseded,
36}
37
38impl std::fmt::Display for PullRequestState {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 match self {
41 PullRequestState::Open => write!(f, "OPEN"),
42 PullRequestState::Merged => write!(f, "MERGED"),
43 PullRequestState::Declined => write!(f, "DECLINED"),
44 PullRequestState::Superseded => write!(f, "SUPERSEDED"),
45 }
46 }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct PullRequestEndpoint {
51 pub branch: BranchInfo,
52 pub commit: Option<CommitInfo>,
53 pub repository: Option<Repository>,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct BranchInfo {
58 pub name: String,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct CommitInfo {
63 pub hash: String,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct Commit {
68 pub hash: String,
69 pub message: Option<String>,
70 pub author: Option<CommitAuthor>,
71 pub date: Option<DateTime<Utc>>,
72 pub links: Option<CommitLinks>,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct CommitAuthor {
77 pub raw: Option<String>,
78 pub user: Option<User>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct CommitLinks {
83 pub html: Option<Link>,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Participant {
88 pub user: User,
89 pub role: ParticipantRole,
90 pub approved: bool,
91 pub state: Option<ParticipantState>,
92 pub participated_on: Option<DateTime<Utc>>,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
96#[serde(rename_all = "UPPERCASE")]
97pub enum ParticipantRole {
98 Participant,
99 Reviewer,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
103#[serde(rename_all = "snake_case")]
104pub enum ParticipantState {
105 Approved,
106 ChangesRequested,
107 #[serde(other)]
108 None,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct PullRequestLinks {
113 #[serde(rename = "self")]
114 pub self_link: Option<Link>,
115 pub html: Option<Link>,
116 pub commits: Option<Link>,
117 pub approve: Option<Link>,
118 pub diff: Option<Link>,
119 pub diffstat: Option<Link>,
120 pub comments: Option<Link>,
121 pub activity: Option<Link>,
122 pub merge: Option<Link>,
123 pub decline: Option<Link>,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
127#[non_exhaustive]
128pub struct CreatePullRequestRequest {
129 pub title: String,
130 pub source: PullRequestBranchRef,
131 pub destination: Option<PullRequestBranchRef>,
132 pub description: Option<String>,
133 pub close_source_branch: Option<bool>,
134 pub reviewers: Option<Vec<UserRef>>,
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct PullRequestBranchRef {
139 pub branch: BranchInfo,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct UserRef {
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub uuid: Option<String>,
151 #[serde(skip_serializing_if = "Option::is_none")]
152 pub username: Option<String>,
153}
154
155#[derive(Debug, Clone, Default, Serialize, Deserialize)]
158#[non_exhaustive]
159pub struct UpdatePullRequestRequest {
160 #[serde(skip_serializing_if = "Option::is_none")]
161 pub title: Option<String>,
162 #[serde(skip_serializing_if = "Option::is_none")]
163 pub description: Option<String>,
164 #[serde(skip_serializing_if = "Option::is_none")]
165 pub reviewers: Option<Vec<UserRef>>,
166 #[serde(skip_serializing_if = "Option::is_none")]
167 pub destination: Option<PullRequestBranchRef>,
168}
169
170impl UpdatePullRequestRequest {
171 pub fn is_empty(&self) -> bool {
173 self.title.is_none()
174 && self.description.is_none()
175 && self.reviewers.is_none()
176 && self.destination.is_none()
177 }
178}
179
180#[derive(Debug, Clone, Serialize, Deserialize)]
181#[non_exhaustive]
182pub struct MergePullRequestRequest {
183 #[serde(rename = "type")]
184 pub merge_type: Option<String>,
185 pub message: Option<String>,
186 pub close_source_branch: Option<bool>,
187 pub merge_strategy: Option<MergeStrategy>,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191#[serde(rename_all = "snake_case")]
192pub enum MergeStrategy {
193 MergeCommit,
194 Squash,
195 FastForward,
196}
197
198impl Default for MergePullRequestRequest {
199 fn default() -> Self {
200 Self {
201 merge_type: Some("pullrequest".to_string()),
202 message: None,
203 close_source_branch: Some(true),
204 merge_strategy: Some(MergeStrategy::MergeCommit),
205 }
206 }
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct PullRequestComment {
211 pub id: u64,
212 pub content: CommentContent,
213 pub user: User,
214 pub created_on: DateTime<Utc>,
215 pub updated_on: Option<DateTime<Utc>>,
216 pub deleted: Option<bool>,
217 pub inline: Option<InlineComment>,
218 pub parent: Option<CommentRef>,
219 pub links: Option<CommentLinks>,
220}
221
222#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct CommentContent {
224 pub raw: String,
225 pub markup: Option<String>,
226 pub html: Option<String>,
227}
228
229#[derive(Debug, Clone, Serialize, Deserialize)]
230pub struct InlineComment {
231 #[serde(skip_serializing_if = "Option::is_none")]
232 pub from: Option<u32>,
233 #[serde(skip_serializing_if = "Option::is_none")]
234 pub to: Option<u32>,
235 pub path: String,
236}
237
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct CommentRef {
240 pub id: u64,
241}
242
243#[derive(Debug, Clone, Serialize, Deserialize)]
244pub struct CommentLinks {
245 #[serde(rename = "self")]
246 pub self_link: Option<Link>,
247 pub html: Option<Link>,
248}
249
250#[derive(Debug, Clone, Serialize, Deserialize)]
252pub struct PrCommit {
253 pub hash: String,
254 pub message: Option<String>,
255 pub date: Option<DateTime<Utc>>,
256}
257
258#[derive(Debug, Clone, Serialize, Deserialize)]
260pub struct CommitStatus {
261 pub key: Option<String>,
262 pub name: Option<String>,
263 pub state: Option<String>,
264 pub url: Option<String>,
265 pub description: Option<String>,
266}
267
268#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct DiffStat {
271 pub status: Option<String>,
272 pub lines_added: Option<u64>,
273 pub lines_removed: Option<u64>,
274 pub old: Option<DiffStatFile>,
275 pub new: Option<DiffStatFile>,
276}
277
278#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct DiffStatFile {
281 pub path: Option<String>,
282}
283
284#[derive(Debug, Clone, Serialize, Deserialize)]
286pub struct PrTask {
287 pub id: Option<u64>,
288 pub state: Option<String>,
289 pub content: Option<PrTaskContent>,
290}
291
292#[derive(Debug, Clone, Serialize, Deserialize)]
294pub struct PrTaskContent {
295 pub raw: Option<String>,
296}
297
298#[derive(Debug, Clone, Serialize, Deserialize)]
303pub struct PrActivity {
304 pub update: Option<serde_json::Value>,
305 pub approval: Option<serde_json::Value>,
306 pub comment: Option<serde_json::Value>,
307 pub changes_requested: Option<serde_json::Value>,
308}
309
310#[cfg(test)]
311mod tests {
312 use super::{
313 BranchInfo, InlineComment, PullRequestBranchRef, UpdatePullRequestRequest, UserRef,
314 };
315
316 #[test]
317 fn update_request_is_empty_when_no_field_is_set() {
318 assert!(UpdatePullRequestRequest::default().is_empty());
319 }
320
321 #[test]
322 fn update_request_is_not_empty_when_any_field_is_set() {
323 let with_title = UpdatePullRequestRequest {
324 title: Some("t".into()),
325 ..Default::default()
326 };
327 let with_description = UpdatePullRequestRequest {
328 description: Some("d".into()),
329 ..Default::default()
330 };
331 let with_reviewers = UpdatePullRequestRequest {
332 reviewers: Some(vec![UserRef {
333 uuid: None,
334 username: Some("alice".into()),
335 }]),
336 ..Default::default()
337 };
338 let with_destination = UpdatePullRequestRequest {
339 destination: Some(PullRequestBranchRef {
340 branch: BranchInfo {
341 name: "main".into(),
342 },
343 }),
344 ..Default::default()
345 };
346
347 assert!(!with_title.is_empty());
348 assert!(!with_description.is_empty());
349 assert!(!with_reviewers.is_empty());
350 assert!(!with_destination.is_empty());
351 }
352
353 #[test]
354 fn update_request_serializes_only_set_fields() {
355 let request = UpdatePullRequestRequest {
356 title: Some("New title".into()),
357 destination: Some(PullRequestBranchRef {
358 branch: BranchInfo {
359 name: "develop".into(),
360 },
361 }),
362 ..Default::default()
363 };
364
365 let json = serde_json::to_value(&request).unwrap();
366 assert_eq!(
367 json,
368 serde_json::json!({
369 "title": "New title",
370 "destination": { "branch": { "name": "develop" } }
371 })
372 );
373 }
374
375 #[test]
376 fn inline_comment_omits_from_when_none() {
377 let inline = InlineComment {
378 from: None,
379 to: Some(3),
380 path: "src/main.rs".into(),
381 };
382
383 let json = serde_json::to_value(&inline).unwrap();
384 assert_eq!(json, serde_json::json!({ "to": 3, "path": "src/main.rs" }));
385 assert!(json.get("from").is_none());
386 }
387
388 #[test]
389 fn user_ref_with_only_uuid_omits_username() {
390 let user = UserRef {
391 uuid: Some("{account-uuid}".into()),
392 username: None,
393 };
394
395 let json = serde_json::to_value(&user).unwrap();
396 assert_eq!(json, serde_json::json!({ "uuid": "{account-uuid}" }));
397 assert!(json.get("username").is_none());
398 }
399}