1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! CircleCI parser - converts CircleCI config YAML to common Pipeline model
//!
//! Parses CircleCI configuration files into the common Pipeline representation.
//! Supports:
//! - `version` field
//! - `workflows` with job orchestration
//! - `jobs` definitions with `steps`
//! - `executors` for Docker images
//! - Environment variables at workflow, job, and step levels
//! - Docker executor image references
use crate::error::Result;
use crate::models::{EnvVar, Job, Pipeline, Provider, Step};
use serde_yaml::Value;
/// Parse CircleCI configuration YAML content
///
/// Converts CircleCI syntax to the common Pipeline model.
pub fn parse(content: &str) -> Result<Pipeline> {
let yaml: Value = serde_yaml::from_str(content)?;
let mapping = yaml.as_mapping().ok_or_else(|| {
crate::error::PipecheckError::InvalidPipeline("Expected YAML mapping".to_string())
})?;
// Parse global env vars (from top-level environment key)
let mut env = Vec::new();
if let Some(env_val) = mapping.get("environment") {
if let Some(env_map) = env_val.as_mapping() {
for (k, v) in env_map {
if let Some(key) = k.as_str() {
let value = match v {
Value::String(s) => s.clone(),
other => format!("{:?}", other),
};
env.push(EnvVar {
key: key.to_string(),
value,
is_secret: false,
});
}
}
}
}
// Parse workflows to understand job ordering
let mut workflow_deps: std::collections::HashMap<String, Vec<String>> =
std::collections::HashMap::new();
if let Some(workflows_val) = mapping.get("workflows") {
if let Some(workflows_map) = workflows_val.as_mapping() {
for (_wf_name, wf_val) in workflows_map {
if let Some(Value::Sequence(job_list)) = wf_val.get("jobs") {
for job_entry in job_list {
match job_entry {
Value::String(s) => {
workflow_deps.entry(s.clone()).or_default();
}
Value::Mapping(m) => {
// Could be job with requires or job name as key
if let Some(Value::String(job_name)) = m.keys().next() {
workflow_deps.entry(job_name.clone()).or_default();
if let Some(config_map) = m
.get(Value::String(job_name.clone()))
.and_then(|v| v.as_mapping())
{
if let Some(Value::Sequence(reqs)) =
config_map.get("requires")
{
let deps: Vec<String> = reqs
.iter()
.filter_map(|r| {
if let Value::String(s) = r {
Some(s.clone())
} else {
None
}
})
.collect();
workflow_deps.insert(job_name.clone(), deps);
}
}
}
}
_ => {}
}
}
}
}
}
}
// Parse jobs
let mut jobs = Vec::new();
if let Some(jobs_val) = mapping.get("jobs") {
if let Some(jobs_map) = jobs_val.as_mapping() {
for (key, value) in jobs_map {
let id = match key.as_str() {
Some(s) => s.to_string(),
None => continue,
};
let job_map = match value.as_mapping() {
Some(m) => m,
None => continue,
};
let job = parse_job(&id, job_map, &workflow_deps)?;
jobs.push(job);
}
}
}
Ok(Pipeline {
provider: Provider::CircleCI,
jobs,
env,
source: content.to_string(),
})
}
fn parse_job(
id: &str,
map: &serde_yaml::Mapping,
workflow_deps: &std::collections::HashMap<String, Vec<String>>,
) -> Result<Job> {
let mut steps = Vec::new();
let mut env = Vec::new();
let mut container_image: Option<String> = None;
let mut service_images: Vec<String> = Vec::new();
// Parse executor (Docker image)
if let Some(executor_val) = map.get("executor") {
if let Value::Mapping(m) = executor_val {
if let Some(Value::String(s)) = m.get(Value::String("name".to_string())) {
container_image = Some(s.clone());
}
} else if let Value::String(s) = executor_val {
// Named executor reference - check top-level executors
container_image = Some(format!("executor:{}", s));
}
}
// Parse docker executor (inline)
if let Some(Value::Sequence(docker_list)) = map.get("docker") {
for (idx, docker_entry) in docker_list.iter().enumerate() {
if let Value::Mapping(m) = docker_entry {
if let Some(Value::String(s)) = m.get(Value::String("image".to_string())) {
if idx == 0 {
container_image = Some(s.clone());
} else {
service_images.push(s.clone());
}
}
}
}
}
// Parse job-level environment
if let Some(env_val) = map.get("environment") {
if let Some(env_map) = env_val.as_mapping() {
for (k, v) in env_map {
if let Some(key) = k.as_str() {
let value = match v {
Value::String(s) => s.clone(),
other => format!("{:?}", other),
};
env.push(EnvVar {
key: key.to_string(),
value,
is_secret: false,
});
}
}
}
}
// Parse steps
if let Value::Sequence(step_list) = map.get("steps").unwrap_or(&Value::Null) {
for (idx, step_val) in step_list.iter().enumerate() {
match step_val {
Value::String(s) => {
steps.push(Step {
name: Some(format!("step-{}", idx)),
uses: None,
run: Some(s.clone()),
env: Vec::new(),
with_inputs: None,
});
}
Value::Mapping(m) => {
if let Some(Value::String(step_name)) = m.keys().next() {
// Check for special step types
if step_name == "run" {
let run = match m.get(Value::String(step_name.clone())) {
Some(Value::String(s)) => s.clone(),
Some(Value::Mapping(run_map)) => {
if let Some(cmd) =
run_map.get(Value::String("command".to_string()))
{
match cmd {
Value::String(s) => s.clone(),
other => format!("{:?}", other),
}
} else {
format!("{:?}", m)
}
}
Some(other) => format!("{:?}", other),
None => format!("{:?}", m),
};
steps.push(Step {
name: Some(format!("step-{}", idx)),
uses: None,
run: Some(run),
env: Vec::new(),
with_inputs: None,
});
} else if step_name == "checkout" {
steps.push(Step {
name: Some("checkout".to_string()),
uses: Some("circleci/checkout".to_string()),
run: None,
env: Vec::new(),
with_inputs: None,
});
} else if step_name == "save_cache"
|| step_name == "restore_cache"
|| step_name == "store_artifacts"
|| step_name == "store_test_results"
{
steps.push(Step {
name: Some(step_name.clone()),
uses: Some(format!("circleci/{}", step_name)),
run: None,
env: Vec::new(),
with_inputs: Some(step_val.clone()),
});
} else {
// Custom orb command or step
let step_config = m.get(Value::String(step_name.clone()));
steps.push(Step {
name: Some(step_name.clone()),
uses: Some(step_name.clone()),
run: step_config.map(|v| format!("{:?}", v)),
env: Vec::new(),
with_inputs: step_config.cloned(),
});
}
}
}
_ => {}
}
}
}
// Get dependencies from workflow definition
let depends_on = workflow_deps.get(id).cloned().unwrap_or_default();
// Parse CircleCI's `max_time` (equivalent to timeout-minutes)
let timeout_minutes = map.get("max_time").and_then(|v| v.as_u64());
Ok(Job {
id: id.to_string(),
name: None,
depends_on,
steps,
env,
container_image,
service_images,
timeout_minutes,
})
}