bitbucket_cli/models/
pipeline.rs

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 Pipeline {
9    pub uuid: String,
10    pub build_number: u64,
11    pub creator: Option<User>,
12    pub repository: Option<Repository>,
13    pub target: PipelineTarget,
14    pub trigger: Option<PipelineTrigger>,
15    pub state: PipelineState,
16    pub created_on: DateTime<Utc>,
17    pub completed_on: Option<DateTime<Utc>>,
18    pub build_seconds_used: Option<u64>,
19    pub links: Option<PipelineLinks>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct PipelineTarget {
24    #[serde(rename = "type")]
25    pub target_type: String,
26    pub ref_type: Option<String>,
27    pub ref_name: Option<String>,
28    pub selector: Option<PipelineSelector>,
29    pub commit: Option<PipelineCommit>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct PipelineSelector {
34    #[serde(rename = "type")]
35    pub selector_type: String,
36    pub pattern: Option<String>,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct PipelineCommit {
41    pub hash: String,
42    pub message: Option<String>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct PipelineTrigger {
47    #[serde(rename = "type")]
48    pub trigger_type: String,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct PipelineState {
53    pub name: PipelineStateName,
54    #[serde(rename = "type")]
55    pub state_type: String,
56    pub result: Option<PipelineResult>,
57    pub stage: Option<PipelineStage>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
61#[serde(rename_all = "UPPERCASE")]
62pub enum PipelineStateName {
63    Pending,
64    Building,
65    Completed,
66    Halted,
67    Paused,
68}
69
70impl std::fmt::Display for PipelineStateName {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            PipelineStateName::Pending => write!(f, "PENDING"),
74            PipelineStateName::Building => write!(f, "BUILDING"),
75            PipelineStateName::Completed => write!(f, "COMPLETED"),
76            PipelineStateName::Halted => write!(f, "HALTED"),
77            PipelineStateName::Paused => write!(f, "PAUSED"),
78        }
79    }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct PipelineResult {
84    pub name: PipelineResultName,
85    #[serde(rename = "type")]
86    pub result_type: String,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
90#[serde(rename_all = "UPPERCASE")]
91pub enum PipelineResultName {
92    Successful,
93    Failed,
94    Error,
95    Stopped,
96    Expired,
97}
98
99impl std::fmt::Display for PipelineResultName {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        match self {
102            PipelineResultName::Successful => write!(f, "SUCCESSFUL"),
103            PipelineResultName::Failed => write!(f, "FAILED"),
104            PipelineResultName::Error => write!(f, "ERROR"),
105            PipelineResultName::Stopped => write!(f, "STOPPED"),
106            PipelineResultName::Expired => write!(f, "EXPIRED"),
107        }
108    }
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct PipelineStage {
113    pub name: String,
114    #[serde(rename = "type")]
115    pub stage_type: String,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct PipelineLinks {
120    #[serde(rename = "self")]
121    pub self_link: Option<Link>,
122    pub steps: Option<Link>,
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct PipelineStep {
127    pub uuid: String,
128    pub name: Option<String>,
129    pub started_on: Option<DateTime<Utc>>,
130    pub completed_on: Option<DateTime<Utc>>,
131    pub state: Option<PipelineStepState>,
132    pub image: Option<PipelineImage>,
133    pub setup_commands: Option<Vec<PipelineCommand>>,
134    pub script_commands: Option<Vec<PipelineCommand>>,
135    pub links: Option<PipelineStepLinks>,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct PipelineStepState {
140    pub name: String,
141    #[serde(rename = "type")]
142    pub state_type: String,
143    pub result: Option<PipelineStepResult>,
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct PipelineStepResult {
148    pub name: String,
149    #[serde(rename = "type")]
150    pub result_type: String,
151}
152
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct PipelineImage {
155    pub name: String,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct PipelineCommand {
160    pub name: String,
161    pub command: Option<String>,
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize)]
165pub struct PipelineStepLinks {
166    #[serde(rename = "self")]
167    pub self_link: Option<Link>,
168    pub log: Option<Link>,
169}
170
171#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct TriggerPipelineRequest {
173    pub target: TriggerPipelineTarget,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct TriggerPipelineTarget {
178    #[serde(rename = "type")]
179    pub target_type: String,
180    pub ref_type: String,
181    pub ref_name: String,
182    pub selector: Option<TriggerPipelineSelector>,
183}
184
185#[derive(Debug, Clone, Serialize, Deserialize)]
186pub struct TriggerPipelineSelector {
187    #[serde(rename = "type")]
188    pub selector_type: String,
189    pub pattern: String,
190}
191
192impl TriggerPipelineRequest {
193    pub fn for_branch(branch: &str) -> Self {
194        Self {
195            target: TriggerPipelineTarget {
196                target_type: "pipeline_ref_target".to_string(),
197                ref_type: "branch".to_string(),
198                ref_name: branch.to_string(),
199                selector: None,
200            },
201        }
202    }
203
204    pub fn for_branch_with_pipeline(branch: &str, pipeline: &str) -> Self {
205        Self {
206            target: TriggerPipelineTarget {
207                target_type: "pipeline_ref_target".to_string(),
208                ref_type: "branch".to_string(),
209                ref_name: branch.to_string(),
210                selector: Some(TriggerPipelineSelector {
211                    selector_type: "custom".to_string(),
212                    pattern: pipeline.to_string(),
213                }),
214            },
215        }
216    }
217}