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 #[serde(rename = "IN_PROGRESS")]
69 InProgress,
70 Completed,
71 Halted,
72 Paused,
73}
74
75impl std::fmt::Display for PipelineStateName {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 match self {
78 PipelineStateName::Pending => write!(f, "PENDING"),
79 PipelineStateName::InProgress => write!(f, "IN_PROGRESS"),
80 PipelineStateName::Completed => write!(f, "COMPLETED"),
81 PipelineStateName::Halted => write!(f, "HALTED"),
82 PipelineStateName::Paused => write!(f, "PAUSED"),
83 }
84 }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct PipelineResult {
89 pub name: PipelineResultName,
90 #[serde(rename = "type")]
91 pub result_type: String,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
95#[serde(rename_all = "UPPERCASE")]
96pub enum PipelineResultName {
97 Successful,
98 Failed,
99 Error,
100 Stopped,
101 Expired,
102}
103
104impl std::fmt::Display for PipelineResultName {
105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106 match self {
107 PipelineResultName::Successful => write!(f, "SUCCESSFUL"),
108 PipelineResultName::Failed => write!(f, "FAILED"),
109 PipelineResultName::Error => write!(f, "ERROR"),
110 PipelineResultName::Stopped => write!(f, "STOPPED"),
111 PipelineResultName::Expired => write!(f, "EXPIRED"),
112 }
113 }
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct PipelineStage {
118 pub name: String,
119 #[serde(rename = "type")]
120 pub stage_type: String,
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct PipelineLinks {
125 #[serde(rename = "self")]
126 pub self_link: Option<Link>,
127 pub steps: Option<Link>,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct PipelineStep {
132 pub uuid: String,
133 pub name: Option<String>,
134 pub started_on: Option<DateTime<Utc>>,
135 pub completed_on: Option<DateTime<Utc>>,
136 pub state: Option<PipelineStepState>,
137 pub image: Option<PipelineImage>,
138 pub setup_commands: Option<Vec<PipelineCommand>>,
139 pub script_commands: Option<Vec<PipelineCommand>>,
140 pub links: Option<PipelineStepLinks>,
141}
142
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct PipelineStepState {
145 pub name: String,
146 #[serde(rename = "type")]
147 pub state_type: String,
148 pub result: Option<PipelineStepResult>,
149}
150
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct PipelineStepResult {
153 pub name: String,
154 #[serde(rename = "type")]
155 pub result_type: String,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct PipelineImage {
160 pub name: String,
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct PipelineCommand {
165 pub name: String,
166 pub command: Option<String>,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
170pub struct PipelineStepLinks {
171 #[serde(rename = "self")]
172 pub self_link: Option<Link>,
173 pub log: Option<Link>,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct TriggerPipelineRequest {
178 pub target: TriggerPipelineTarget,
179 #[serde(skip_serializing_if = "Option::is_none")]
181 pub variables: Option<Vec<PipelineVariableInput>>,
182}
183
184#[derive(Debug, Clone, Serialize, Deserialize)]
185pub struct TriggerPipelineTarget {
186 #[serde(rename = "type")]
187 pub target_type: String,
188 #[serde(skip_serializing_if = "Option::is_none")]
190 pub ref_type: Option<String>,
191 #[serde(skip_serializing_if = "Option::is_none")]
193 pub ref_name: Option<String>,
194 #[serde(skip_serializing_if = "Option::is_none")]
195 pub selector: Option<TriggerPipelineSelector>,
196 #[serde(skip_serializing_if = "Option::is_none")]
198 pub commit: Option<TriggerPipelineCommit>,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
202pub struct TriggerPipelineSelector {
203 #[serde(rename = "type")]
204 pub selector_type: String,
205 pub pattern: String,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct TriggerPipelineCommit {
210 pub hash: String,
211 #[serde(rename = "type")]
212 pub commit_type: String,
213}
214
215#[derive(Debug, Clone, Serialize, Deserialize)]
217pub struct PipelineVariableInput {
218 pub key: String,
219 pub value: String,
220 pub secured: bool,
221}
222
223impl TriggerPipelineRequest {
224 pub fn for_branch(branch: &str) -> Self {
226 Self {
227 target: TriggerPipelineTarget {
228 target_type: "pipeline_ref_target".to_string(),
229 ref_type: Some("branch".to_string()),
230 ref_name: Some(branch.to_string()),
231 selector: None,
232 commit: None,
233 },
234 variables: None,
235 }
236 }
237
238 pub fn for_commit(hash: &str) -> Self {
240 Self {
241 target: TriggerPipelineTarget {
242 target_type: "pipeline_commit_target".to_string(),
243 ref_type: None,
244 ref_name: None,
245 selector: None,
246 commit: Some(TriggerPipelineCommit {
247 hash: hash.to_string(),
248 commit_type: "commit".to_string(),
249 }),
250 },
251 variables: None,
252 }
253 }
254
255 pub fn with_pipeline(mut self, pipeline: &str) -> Self {
257 self.target.selector = Some(TriggerPipelineSelector {
258 selector_type: "custom".to_string(),
259 pattern: pipeline.to_string(),
260 });
261 self
262 }
263
264 pub fn with_variables(mut self, variables: Vec<PipelineVariableInput>) -> Self {
266 self.variables = if variables.is_empty() {
267 None
268 } else {
269 Some(variables)
270 };
271 self
272 }
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
279pub struct PipelineVariable {
280 pub uuid: Option<String>,
281 pub key: Option<String>,
282 pub value: Option<String>,
283 pub secured: Option<bool>,
284}
285
286#[derive(Debug, Clone, Serialize, Deserialize)]
288pub struct PipelineSchedule {
289 pub uuid: String,
290 pub enabled: Option<bool>,
291 pub cron_pattern: Option<String>,
292 pub target: Option<serde_json::Value>,
294}
295
296#[derive(Debug, Clone, Serialize, Deserialize)]
298pub struct PipelineCache {
299 pub uuid: String,
300 pub name: Option<String>,
301 pub path: Option<String>,
302 pub file_size_bytes: Option<u64>,
303}
304
305#[derive(Debug, Clone, Serialize, Deserialize)]
307pub struct PipelinesConfig {
308 pub enabled: Option<bool>,
309}
310
311#[cfg(test)]
312mod tests {
313 use serde_json::json;
314
315 use super::{PipelineVariableInput, TriggerPipelineRequest};
316
317 #[test]
318 fn for_branch_builds_ref_target() {
319 let req = TriggerPipelineRequest::for_branch("main");
320 assert_eq!(
321 serde_json::to_value(&req).unwrap(),
322 json!({
323 "target": {
324 "type": "pipeline_ref_target",
325 "ref_type": "branch",
326 "ref_name": "main"
327 }
328 })
329 );
330 }
331
332 #[test]
333 fn for_commit_builds_commit_target_without_ref() {
334 let req = TriggerPipelineRequest::for_commit("abc123");
335 assert_eq!(
336 serde_json::to_value(&req).unwrap(),
337 json!({
338 "target": {
339 "type": "pipeline_commit_target",
340 "commit": {"hash": "abc123", "type": "commit"}
341 }
342 })
343 );
344 }
345
346 #[test]
347 fn with_pipeline_adds_custom_selector() {
348 let req = TriggerPipelineRequest::for_branch("main").with_pipeline("nightly");
349 let value = serde_json::to_value(&req).unwrap();
350 assert_eq!(
351 value["target"]["selector"],
352 json!({"type": "custom", "pattern": "nightly"})
353 );
354 }
355
356 #[test]
357 fn with_variables_empty_leaves_field_absent() {
358 let req = TriggerPipelineRequest::for_branch("main").with_variables(vec![]);
359 assert!(req.variables.is_none());
360 let value = serde_json::to_value(&req).unwrap();
361 assert!(value.get("variables").is_none());
362 }
363
364 #[test]
365 fn with_variables_serializes_key_value_secured() {
366 let req = TriggerPipelineRequest::for_branch("main").with_variables(vec![
367 PipelineVariableInput {
368 key: "FOO".to_string(),
369 value: "bar".to_string(),
370 secured: true,
371 },
372 ]);
373 let value = serde_json::to_value(&req).unwrap();
374 assert_eq!(
375 value["variables"],
376 json!([{"key": "FOO", "value": "bar", "secured": true}])
377 );
378 }
379}