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