azure_rust/pull_requests/
mod.rs

1use std::fmt;
2
3use serde::Serialize;
4
5pub use pr_create_options::PROption;
6pub use pr_create_response::PullRequestCreateResponse;
7pub use pr_get_response::PullRequestResponse;
8pub use pull_request_list_options::{PullListOptions, PullListOptionsBuilder};
9pub use pull_requests_reponse::PullRequestsResponse;
10pub use pull_update_options::{PullUpdateOptions, PullUpdateOptionsBuilder};
11
12pub mod pull;
13pub mod pulls;
14
15pub use pull::PullRequest;
16pub use pulls::PullRequests;
17
18mod pr_get_response {
19    // use super::pr_refs::LinksRefs;
20    use serde::*;
21
22    #[derive(Debug, Deserialize)]
23    pub struct PullRequestResponse {
24        // #[serde(flatten)]
25        // pub repository: Repository,
26        #[serde(rename = "pullRequestId")]
27        pub pull_request_id: i64,
28        // #[serde(rename = "codeReviewId")]
29        // pub code_review_id: Option<i64>,
30        // pub status: Option<String>,
31        #[serde(rename = "createdBy")]
32        pub created_by: CreatedBy,
33        // #[serde(rename = "creationDate")]
34        // pub creation_date: String,
35        // pub title: String,
36        // pub description: Option<String>,
37        // #[serde(rename = "sourceRefName")]
38        // pub source_ref_name: String,
39        // #[serde(rename = "targetRefName")]
40        // pub target_ref_name: String,
41        // #[serde(rename = "mergeStatus")]
42        // pub merge_status: String,
43        // #[serde(rename = "isDraft")]
44        // pub is_draft: bool,
45        // #[serde(rename = "mergeId")]
46        // pub merge_id: String,
47        // #[serde(rename = "lastMergeSourceCommit")]
48        // pub last_merge_source_commit: LastMergeSourceCommit,
49        // #[serde(rename = "lastMergeTargetCommit")]
50        // pub last_merge_target_commit: LastMergeTargetCommit,
51        // #[serde(rename = "lastMergeCommit")]
52        // pub last_merge_commit: LastMergeCommit,
53        // pub reviewers: Vec<::serde_json::Value>,
54        // pub url: String,
55        // #[serde(rename = "_links")]
56        // pub links: LinksRefs,
57        // #[serde(rename = "supportsIterations")]
58        // pub supports_iterations: bool,
59        // #[serde(rename = "artifactId")]
60        // pub artifact_id: String,
61    }
62    #[derive(Debug, Deserialize)]
63    pub struct Repository {
64        pub id: String,
65        pub name: String,
66        pub url: String,
67        pub project: Project,
68        pub size: i64,
69        #[serde(rename = "remoteUrl")]
70        pub remote_url: String,
71        #[serde(rename = "sshUrl")]
72        pub ssh_url: String,
73        #[serde(rename = "webUrl")]
74        pub web_url: String,
75    }
76    #[derive(Debug, Deserialize)]
77    pub struct Project {
78        pub id: String,
79        pub name: String,
80        pub url: String,
81        pub state: String,
82        pub revision: i64,
83        pub visibility: String,
84        #[serde(rename = "lastUpdateTime")]
85        pub last_update_time: String,
86    }
87    #[derive(Debug, Deserialize)]
88    pub struct CreatedBy {
89        #[serde(rename = "displayName")]
90        pub display_name: String,
91        pub url: String,
92        // #[serde(rename = "_links")]
93        // pub links: Links,
94        pub id: String,
95        #[serde(rename = "uniqueName")]
96        pub unique_name: String,
97        #[serde(rename = "imageUrl")]
98        pub image_url: String,
99        pub descriptor: String,
100    }
101    #[derive(Debug, Deserialize)]
102    pub struct Links {
103        pub avatar: Avatar,
104    }
105    #[derive(Debug, Deserialize)]
106    pub struct Avatar {
107        pub href: String,
108    }
109    #[derive(Debug, Deserialize)]
110    pub struct LastMergeSourceCommit {
111        #[serde(rename = "commitId")]
112        pub commit_id: String,
113        pub url: String,
114    }
115    #[derive(Debug, Deserialize)]
116    pub struct LastMergeTargetCommit {
117        #[serde(rename = "commitId")]
118        pub commit_id: String,
119        pub url: String,
120    }
121    #[derive(Debug, Deserialize)]
122    pub struct LastMergeCommit {
123        #[serde(rename = "commitId")]
124        pub commit_id: String,
125        pub author: Author,
126        pub committer: Committer,
127        pub comment: String,
128        pub url: String,
129    }
130    #[derive(Debug, Deserialize)]
131    pub struct Author {
132        pub name: String,
133        pub email: String,
134        pub date: String,
135    }
136    #[derive(Debug, Deserialize)]
137    pub struct Committer {
138        pub name: String,
139        pub email: String,
140        pub date: String,
141    }
142}
143
144mod pr_create_response {
145    use super::pr_refs::LinksRefs;
146    use serde::*;
147
148    #[derive(Debug, Deserialize)]
149    pub struct PullRequestCreateResponse {
150        pub repository: Repository,
151        #[serde(rename = "pullRequestId")]
152        pub pull_request_id: i64,
153        #[serde(rename = "codeReviewId")]
154        pub code_review_id: i64,
155        pub status: String,
156        #[serde(rename = "createdBy")]
157        pub created_by: CreatedBy,
158        #[serde(rename = "creationDate")]
159        pub creation_date: String,
160        pub title: String,
161        pub description: String,
162        #[serde(rename = "sourceRefName")]
163        pub source_ref_name: String,
164        #[serde(rename = "targetRefName")]
165        pub target_ref_name: String,
166        #[serde(rename = "mergeStatus")]
167        pub merge_status: String,
168        #[serde(rename = "isDraft")]
169        pub is_draft: bool,
170        #[serde(rename = "mergeId")]
171        pub merge_id: String,
172        #[serde(rename = "lastMergeSourceCommit")]
173        pub last_merge_source_commit: LastMergeSourceCommit,
174        #[serde(rename = "lastMergeTargetCommit")]
175        pub last_merge_target_commit: LastMergeTargetCommit,
176        pub reviewers: Vec<::serde_json::Value>,
177        pub labels: Vec<::serde_json::Value>,
178        pub url: String,
179        #[serde(rename = "_links")]
180        pub links: LinksRefs,
181        #[serde(rename = "supportsIterations")]
182        pub supports_iterations: bool,
183        #[serde(rename = "artifactId")]
184        pub artifact_id: String,
185    }
186
187    #[derive(Debug, Deserialize)]
188    pub struct Repository {
189        pub id: String,
190        pub name: String,
191        pub url: String,
192        pub project: Project,
193        pub size: i64,
194        #[serde(rename = "remoteUrl")]
195        pub remote_url: String,
196        #[serde(rename = "sshUrl")]
197        pub ssh_url: String,
198        #[serde(rename = "webUrl")]
199        pub web_url: String,
200    }
201
202    #[derive(Debug, Deserialize)]
203    pub struct Project {
204        pub id: String,
205        pub name: String,
206        pub url: String,
207        pub state: String,
208        pub revision: i64,
209        pub visibility: String,
210        #[serde(rename = "lastUpdateTime")]
211        pub last_update_time: String,
212    }
213
214    #[derive(Debug, Deserialize)]
215    pub struct CreatedBy {
216        #[serde(rename = "displayName")]
217        pub display_name: String,
218        pub url: String,
219        #[serde(rename = "_links")]
220        pub links: Links,
221        pub id: String,
222        #[serde(rename = "uniqueName")]
223        pub unique_name: String,
224        #[serde(rename = "imageUrl")]
225        pub image_url: String,
226        pub descriptor: String,
227    }
228
229    #[derive(Debug, Deserialize)]
230    pub struct Links {
231        pub avatar: Avatar,
232    }
233
234    #[derive(Debug, Deserialize)]
235    pub struct Avatar {
236        pub href: String,
237    }
238
239    #[derive(Debug, Deserialize)]
240    pub struct LastMergeSourceCommit {
241        #[serde(rename = "commitId")]
242        pub commit_id: String,
243        pub url: String,
244    }
245
246    #[derive(Debug, Deserialize)]
247    pub struct LastMergeTargetCommit {
248        #[serde(rename = "commitId")]
249        pub commit_id: String,
250        pub url: String,
251    }
252}
253
254mod pr_refs {
255    use serde::*;
256    #[derive(Debug, Deserialize)]
257    pub struct LinksRefs {
258        #[serde(rename = "self")]
259        pub self_field: Href,
260        pub repository: Option<RepositoryRef>,
261        #[serde(rename = "workItems")]
262        pub work_items: Option<WorkItemsRef>,
263        #[serde(rename = "sourceBranch")]
264        pub source_branch: Option<SourceBranchRef>,
265        #[serde(rename = "targetBranch")]
266        pub target_branch: Option<TargetBranchRef>,
267        pub statuses: Option<StatusesRef>,
268        #[serde(rename = "sourceCommit")]
269        pub source_commit: Option<SourceCommitRef>,
270        #[serde(rename = "targetCommit")]
271        pub target_commit: Option<TargetCommitRef>,
272        #[serde(rename = "createdBy")]
273        pub created_by: Option<CreatedByRef>,
274        pub iterations: Option<IterationsRef>,
275    }
276    #[derive(Debug, Deserialize)]
277    pub struct Href {
278        pub href: String,
279    }
280    #[derive(Debug, Deserialize)]
281    pub struct RepositoryRef {
282        pub href: String,
283    }
284    #[derive(Debug, Deserialize)]
285    pub struct WorkItemsRef {
286        pub href: String,
287    }
288    #[derive(Debug, Deserialize)]
289    pub struct SourceBranchRef {
290        pub href: String,
291    }
292    #[derive(Debug, Deserialize)]
293    pub struct TargetBranchRef {
294        pub href: String,
295    }
296    #[derive(Debug, Deserialize)]
297    pub struct StatusesRef {
298        pub href: String,
299    }
300    #[derive(Debug, Deserialize)]
301    pub struct SourceCommitRef {
302        pub href: String,
303    }
304    #[derive(Debug, Deserialize)]
305    pub struct TargetCommitRef {
306        pub href: String,
307    }
308    #[derive(Debug, Deserialize)]
309    pub struct CreatedByRef {
310        pub href: String,
311    }
312    #[derive(Debug, Deserialize)]
313    pub struct IterationsRef {
314        pub href: String,
315    }
316}
317
318mod pull_request_list_options {
319
320    use super::PullStatus;
321    use std::collections::HashMap;
322    use url::form_urlencoded;
323    #[derive(Default)]
324    pub struct PullListOptions {
325        pub params: HashMap<&'static str, String>,
326    }
327
328    impl PullListOptions {
329        pub fn builder() -> PullListOptionsBuilder {
330            PullListOptionsBuilder::default()
331        }
332
333        /// serialize options as a string. returns None if no options are defined
334        pub fn serialize(&self) -> Option<String> {
335            if self.params.is_empty() {
336                None
337            } else {
338                let encoded: String = form_urlencoded::Serializer::new(String::new())
339                    .extend_pairs(&self.params)
340                    .finish();
341                Some(encoded)
342            }
343        }
344    }
345
346    #[derive(Default)]
347    pub struct PullListOptionsBuilder(PullListOptions);
348
349    impl PullListOptionsBuilder {
350        pub fn skip(&mut self, skip: u32) -> &mut Self {
351            self.0.params.insert("skip", skip.to_string());
352            self
353        }
354
355        pub fn top(&mut self, top: u32) -> &mut Self {
356            self.0.params.insert("top", top.to_string());
357            self
358        }
359
360        pub fn direction<D>(&mut self, direction: D) -> &mut Self
361        where
362            D: Into<String>,
363        {
364            self.0.params.insert("direction", direction.into());
365            self
366        }
367
368        pub fn status(&mut self, status: PullStatus) -> &mut Self {
369            self.0
370                .params
371                .insert("searchCriteria.status", status.to_string());
372            self
373        }
374
375        pub fn repository_id<R>(&mut self, repo_id: R) -> &mut Self
376        where
377            R: Into<String>,
378        {
379            self.0
380                .params
381                .insert("searchCriteria.repositoryId", repo_id.into());
382            self
383        }
384
385        pub fn source_ref_name<R>(&mut self, ref_name: R) -> &mut Self
386        where
387            R: Into<String>,
388        {
389            self.0
390                .params
391                .insert("searchCriteria.sourceRefName", ref_name.into());
392            self
393        }
394        pub fn source_ref_repo_id<R>(&mut self, ref_name: R) -> &mut Self
395        where
396            R: Into<String>,
397        {
398            self.0
399                .params
400                .insert("searchCriteria.sourceRepositoryId", ref_name.into());
401            self
402        }
403
404        pub fn target_ref_name<R>(&mut self, ref_name: R) -> &mut Self
405        where
406            R: Into<String>,
407        {
408            self.0
409                .params
410                .insert("searchCriteria.targetRefName", ref_name.into());
411            self
412        }
413
414        pub fn reviewer_id<R>(&mut self, reviewer_id: R) -> &mut Self
415        where
416            R: Into<String>,
417        {
418            self.0
419                .params
420                .insert("searchCriteria.reviewerId", reviewer_id.into());
421            self
422        }
423
424        pub fn include_links(&mut self, include_links: bool) -> &mut Self {
425            self.0
426                .params
427                .insert("searchCriteria.includeLinks", include_links.to_string());
428            self
429        }
430
431        pub fn build(&self) -> PullListOptions {
432            PullListOptions {
433                params: self.0.params.clone(),
434            }
435        }
436    }
437}
438
439mod pull_requests_reponse {
440    use serde::*;
441    #[derive(Debug, Deserialize)]
442    pub struct PullRequestsResponse {
443        pub value: Vec<Value>,
444        pub count: i64,
445    }
446
447    #[derive(Debug, Deserialize)]
448    pub struct Value {
449        pub repository: Repository,
450        #[serde(rename = "pullRequestId")]
451        pub pull_request_id: u64,
452        #[serde(rename = "codeReviewId")]
453        pub code_review_id: i64,
454        pub status: String,
455        #[serde(rename = "createdBy")]
456        pub created_by: CreatedBy,
457        #[serde(rename = "creationDate")]
458        pub creation_date: String,
459        pub title: String,
460        pub description: String,
461        #[serde(rename = "sourceRefName")]
462        pub source_ref_name: String,
463        #[serde(rename = "targetRefName")]
464        pub target_ref_name: String,
465        #[serde(rename = "mergeStatus")]
466        pub merge_status: String,
467        #[serde(rename = "isDraft")]
468        pub is_draft: bool,
469        #[serde(rename = "mergeId")]
470        pub merge_id: String,
471        #[serde(rename = "lastMergeSourceCommit")]
472        pub last_merge_source_commit: LastMergeSourceCommit,
473        #[serde(rename = "lastMergeTargetCommit")]
474        pub last_merge_target_commit: LastMergeTargetCommit,
475        #[serde(rename = "lastMergeCommit")]
476        pub last_merge_commit: LastMergeCommit,
477        pub reviewers: Vec<::serde_json::Value>,
478        pub url: String,
479        #[serde(rename = "supportsIterations")]
480        pub supports_iterations: bool,
481    }
482
483    #[derive(Debug, Deserialize)]
484    pub struct Repository {
485        pub id: String,
486        pub name: String,
487        pub url: String,
488        pub project: Project,
489    }
490
491    #[derive(Debug, Deserialize)]
492    pub struct Project {
493        pub id: String,
494        pub name: String,
495        pub state: String,
496        pub visibility: String,
497        #[serde(rename = "lastUpdateTime")]
498        pub last_update_time: String,
499    }
500
501    #[derive(Debug, Deserialize)]
502    pub struct CreatedBy {
503        #[serde(rename = "displayName")]
504        pub display_name: String,
505        pub url: String,
506        #[serde(rename = "_links")]
507        pub links: Links,
508        pub id: String,
509        #[serde(rename = "uniqueName")]
510        pub unique_name: String,
511        #[serde(rename = "imageUrl")]
512        pub image_url: String,
513        pub descriptor: String,
514    }
515
516    #[derive(Debug, Deserialize)]
517    pub struct Links {
518        pub avatar: Avatar,
519    }
520
521    #[derive(Debug, Deserialize)]
522    pub struct Avatar {
523        pub href: String,
524    }
525
526    #[derive(Debug, Deserialize)]
527    pub struct LastMergeSourceCommit {
528        #[serde(rename = "commitId")]
529        pub commit_id: String,
530        pub url: String,
531    }
532
533    #[derive(Debug, Deserialize)]
534    pub struct LastMergeTargetCommit {
535        #[serde(rename = "commitId")]
536        pub commit_id: String,
537        pub url: String,
538    }
539
540    #[derive(Debug, Deserialize)]
541    pub struct LastMergeCommit {
542        #[serde(rename = "commitId")]
543        pub commit_id: String,
544        pub url: String,
545    }
546}
547
548mod pr_create_options {
549    use serde::*;
550    #[derive(Debug, Serialize)]
551    pub struct PROption {
552        #[serde(rename = "sourceRefName")]
553        pub source_ref_name: String,
554        #[serde(rename = "targetRefName")]
555        pub target_ref_name: String,
556        pub title: String,
557        #[serde(skip_serializing_if = "Option::is_none")]
558        pub description: Option<String>,
559        #[serde(skip_serializing_if = "Option::is_none")]
560        pub reviewers: Option<Vec<Reviewer>>,
561    }
562
563    #[derive(Debug, Serialize)]
564    pub struct Reviewer {
565        pub id: String,
566    }
567}
568
569mod pull_update_options {
570    use super::{MergeStrategy, PullStatus};
571    use serde::*;
572
573    #[derive(Debug, Default, Serialize)]
574    pub struct PullUpdateOptions {
575        #[serde(skip_serializing_if = "Option::is_none")]
576        title: Option<String>,
577        #[serde(skip_serializing_if = "Option::is_none")]
578        description: Option<String>,
579        #[serde(skip_serializing_if = "Option::is_none")]
580        status: Option<PullStatus>,
581        #[serde(skip_serializing_if = "Option::is_none")]
582        #[serde(rename = "mergeStrategy")]
583        merge_strategy: Option<MergeStrategy>,
584        #[serde(skip_serializing_if = "Option::is_none")]
585        #[serde(rename = "lastMergeSourceCommit")]
586        source_commit: Option<LastMergeSourceCommit>,
587        #[serde(skip_serializing_if = "Option::is_none")]
588        #[serde(rename = "completionOptions")]
589        completion_options: Option<CompletionOptions>,
590        #[serde(skip_serializing_if = "Option::is_none")]
591        #[serde(rename = "isDraft")]
592        draft:Option<bool>
593    }
594
595    #[derive(Debug, Default, Serialize, Clone)]
596    pub struct LastMergeSourceCommit {
597        #[serde(skip_serializing_if = "Option::is_none")]
598        commit_id: Option<String>,
599    }
600
601    #[derive(Debug, Default, Serialize, Clone)]
602    pub struct CompletionOptions {
603        #[serde(skip_serializing_if = "Option::is_none")]
604        #[serde(rename = "bypassPolicy")]
605        bypass_policy: Option<bool>,
606        #[serde(skip_serializing_if = "Option::is_none")]
607        #[serde(rename = "bypassReason")]
608        bypass_reason: Option<String>,
609        #[serde(skip_serializing_if = "Option::is_none")]
610        #[serde(rename = "deleteSourceBranch")]
611        delete_source_branch: Option<bool>,
612    }
613
614    impl PullUpdateOptions {
615        pub fn builder() -> PullUpdateOptionsBuilder {
616            PullUpdateOptionsBuilder::default()
617        }
618    }
619    #[derive(Default)]
620    pub struct PullUpdateOptionsBuilder(PullUpdateOptions);
621
622    impl PullUpdateOptionsBuilder {
623        /// set the title of the pull
624        pub fn title<T>(&mut self, title: T) -> &mut Self
625        where
626            T: Into<String>,
627        {
628            self.0.title = Some(title.into());
629            self
630        }
631  
632        /// set the description of the pull
633        pub fn description<B>(&mut self, description: B) -> &mut Self
634        where
635            B: Into<String>,
636        {
637            self.0.description = Some(description.into());
638            self
639        }
640
641        /// set the status of the pull
642        pub fn status(&mut self, status: PullStatus) -> &mut Self {
643            self.0.status = Some(status.into());
644            self
645        }
646
647        /// set the merge strategy of the pull
648        pub fn merge_strategy(&mut self, merge_strategy: MergeStrategy) -> &mut Self {
649            self.0.merge_strategy = Some(merge_strategy.into());
650            self
651        }
652
653         pub fn draft(&mut self, draft: bool) -> &mut Self {
654            self.0.draft = Some(draft);
655            self
656        }
657        // /// set the source commit of the merge
658        // pub fn source_commit<C>(&mut self, source_commit: C) -> &mut Self
659        // where
660        //     C: Into<String>,
661        // {
662        //     self.0.source_commit = LastMergeSourceCommit {
663        //         commit_id: Some(source_commit.into()),
664        //     };
665        //     self
666        // }
667
668        // pub fn bypass_policy(&mut self, bypass_policy: bool) -> &mut Self {
669        //     self.0.completion_options.bypass_policy = Some(bypass_policy);
670        //     self
671        // }
672
673        // /// set the bypass reason of the pull
674        // pub fn bypass_reason<B>(&mut self, bypass_reason: B) -> &mut Self
675        // where
676        //     B: Into<String>,
677        // {
678        //     self.0.completion_options.bypass_reason = Some(bypass_reason.into());
679        //     self
680        // }
681        // pub fn delete_source_branch(&mut self, delete_source_branch: bool) -> &mut Self {
682        //     self.0.completion_options.delete_source_branch = Some(delete_source_branch);
683        //     self
684        // }
685
686        /// create a new set of pull edit options
687        pub fn build(&self) -> PullUpdateOptions {
688            PullUpdateOptions {
689                title: self.0.title.clone(),
690                description: self.0.description.clone(),
691                status: self.0.status.clone(),
692                draft: self.0.draft.clone(),
693                merge_strategy: self.0.merge_strategy.clone(),
694                source_commit: self.0.source_commit.clone(),
695                completion_options: self.0.completion_options.clone(),
696            }
697        }
698    }
699}
700/// enum representation of Azure Pull Request Status
701#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
702pub enum PullStatus {
703    Abandoned,
704    Active,
705    Draft,
706    All,
707    Completed,
708    NotSet,
709}
710
711impl fmt::Display for PullStatus {
712    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713        match *self {
714            PullStatus::Abandoned => "abandoned",
715            PullStatus::Active => "active",
716            PullStatus::Draft => "draft",
717            PullStatus::All => "all",
718            PullStatus::Completed => "completed",
719            PullStatus::NotSet => "notSet",
720        }
721        .fmt(f)
722    }
723}
724
725impl Default for PullStatus {
726    fn default() -> PullStatus {
727        PullStatus::NotSet
728    }
729}
730
731/// enum representation of Azure Pull Request Merge Strategies, by default will squash merge
732#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
733pub enum MergeStrategy {
734    NoFastForward,
735    Rebase,
736    RebaseMerge,
737    Squash,
738}
739
740impl fmt::Display for MergeStrategy {
741    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
742        match *self {
743            MergeStrategy::NoFastForward => "noFastForward",
744            MergeStrategy::Rebase => "rebase",
745            MergeStrategy::RebaseMerge => "rebaseMerge",
746            MergeStrategy::Squash => "squash",
747        }
748        .fmt(f)
749    }
750}
751
752// Should the default be squash?
753impl Default for MergeStrategy {
754    fn default() -> MergeStrategy {
755        MergeStrategy::Squash
756    }
757}