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}
180
181#[derive(Debug, Clone, Serialize, Deserialize)]
182pub struct TriggerPipelineTarget {
183 #[serde(rename = "type")]
184 pub target_type: String,
185 pub ref_type: String,
186 pub ref_name: String,
187 pub selector: Option<TriggerPipelineSelector>,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct TriggerPipelineSelector {
192 #[serde(rename = "type")]
193 pub selector_type: String,
194 pub pattern: String,
195}
196
197impl TriggerPipelineRequest {
198 pub fn for_branch(branch: &str) -> Self {
199 Self {
200 target: TriggerPipelineTarget {
201 target_type: "pipeline_ref_target".to_string(),
202 ref_type: "branch".to_string(),
203 ref_name: branch.to_string(),
204 selector: None,
205 },
206 }
207 }
208
209 pub fn for_branch_with_pipeline(branch: &str, pipeline: &str) -> Self {
210 Self {
211 target: TriggerPipelineTarget {
212 target_type: "pipeline_ref_target".to_string(),
213 ref_type: "branch".to_string(),
214 ref_name: branch.to_string(),
215 selector: Some(TriggerPipelineSelector {
216 selector_type: "custom".to_string(),
217 pattern: pipeline.to_string(),
218 }),
219 },
220 }
221 }
222}