pipechecker 0.3.2

CI/CD Pipeline Auditor - Catch errors before you push
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! GitLab CI parser - converts GitLab CI YAML to common Pipeline model
//!
//! Parses GitLab CI configuration files into the common Pipeline representation.
//! Supports:
//! - `stages` definitions
//! - Job definitions with `script`, `before_script`, `after_script`
//! - Dependency chains via `needs` or `dependencies` keywords
//! - Environment variables at global and job levels
//! - Docker image references via `image` keyword
//! - Service definitions
//! - `include` block parsing (local + remote detection)
//! - `extends:` support with shallow merge semantics
//! - Hidden job filtering (keys starting with `.`)
//! - `rules:` block parsing per job
//! - Top-level `workflow:rules:` parsing

use crate::error::Result;
use crate::models::{EnvVar, Job, Pipeline, Provider, RuleCondition, Step};
use serde_yaml::Value;

/// Information about included files from `include:` blocks
#[derive(Debug, Clone, Default)]
pub struct IncludeInfo {
    /// Local file paths included
    pub local: Vec<String>,
    /// Remote URLs included
    pub remote: Vec<String>,
    /// Project-based includes (project::path)
    pub project: Vec<String>,
}

/// Resolve `extends:` — merge parent job fields into child job (task 2.1)
///
/// Handles both `extends: .base` (single string) and `extends: [.base, .deploy]` (list).
/// Performs shallow merge per GitLab semantics: child fields override parent fields.
fn resolve_extends(mapping: &serde_yaml::Mapping) -> serde_yaml::Mapping {
    let mut resolved = serde_yaml::Mapping::new();

    // First pass: clone all entries
    for (k, v) in mapping {
        resolved.insert(k.clone(), v.clone());
    }

    // Second pass: resolve extends for each job
    for (key, value) in mapping.clone() {
        if let Value::Mapping(job_map) = value {
            let mut merged = job_map.clone();
            resolve_extends_for_job(&mut merged, mapping);
            resolved.insert(key, Value::Mapping(merged));
        }
    }

    resolved
}

/// Resolve extends for a single job by merging parent fields
fn resolve_extends_for_job(job_map: &mut serde_yaml::Mapping, all_entries: &serde_yaml::Mapping) {
    let extends_val = match job_map.get(Value::String("extends".to_string())) {
        Some(v) => v.clone(),
        None => return,
    };

    // Remove extends from the child
    job_map.remove(Value::String("extends".to_string()));

    // Get the list of parent names
    let parent_names: Vec<String> = match &extends_val {
        Value::String(s) => vec![s.clone()],
        Value::Sequence(seq) => seq
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect(),
        _ => return,
    };

    // Merge each parent (later parents override earlier ones, child overrides all)
    for parent_name in &parent_names {
        let parent_val = match all_entries.get(Value::String(parent_name.clone())) {
            Some(Value::Mapping(m)) => m.clone(),
            _ => continue,
        };

        // Insert parent fields only if child doesn't already have them
        for (pk, pv) in parent_val {
            if !job_map.contains_key(&pk) {
                job_map.insert(pk, pv);
            }
        }
    }
}

/// Parse top-level `workflow:rules:` block (task 2.4)
fn parse_workflow_rules(mapping: &serde_yaml::Mapping) -> Vec<RuleCondition> {
    let workflow_val = match mapping.get(Value::String("workflow".to_string())) {
        Some(v) => v,
        None => return Vec::new(),
    };

    let workflow_map = match workflow_val.as_mapping() {
        Some(m) => m,
        None => return Vec::new(),
    };

    let rules_val = match workflow_map.get(Value::String("rules".to_string())) {
        Some(Value::Sequence(seq)) => seq,
        _ => return Vec::new(),
    };

    rules_val.iter().filter_map(parse_rule_condition).collect()
}

/// Parse a `rules:` block for a job (task 2.3)
fn parse_rules(mapping: &serde_yaml::Mapping) -> Vec<RuleCondition> {
    let rules_val = match mapping.get(Value::String("rules".to_string())) {
        Some(Value::Sequence(seq)) => seq,
        _ => return Vec::new(),
    };

    rules_val.iter().filter_map(parse_rule_condition).collect()
}

/// Parse a single rule condition from a YAML mapping
fn parse_rule_condition(rule_val: &Value) -> Option<RuleCondition> {
    let rule_map = rule_val.as_mapping()?;

    let when = rule_map
        .get(Value::String("when".to_string()))
        .and_then(|v| v.as_str())
        .map(String::from);

    let if_condition = rule_map
        .get(Value::String("if".to_string()))
        .and_then(|v| v.as_str())
        .map(String::from);

    let exists = rule_map
        .get(Value::String("exists".to_string()))
        .and_then(|v| {
            if let Value::Sequence(seq) = v {
                let paths: Vec<String> = seq
                    .iter()
                    .filter_map(|p| p.as_str().map(String::from))
                    .collect();
                if paths.is_empty() {
                    None
                } else {
                    Some(paths)
                }
            } else {
                None
            }
        });

    let allow_failure = rule_map
        .get(Value::String("allow_failure".to_string()))
        .and_then(|v| v.as_bool());

    if when.is_some() || if_condition.is_some() || exists.is_some() || allow_failure.is_some() {
        Some(RuleCondition {
            when,
            if_condition,
            exists,
            allow_failure,
        })
    } else {
        None
    }
}

/// Parse GitLab CI configuration YAML content
///
/// Converts GitLab CI syntax to the common Pipeline model.
/// Note: `include:` blocks are parsed for detection but external files are NOT loaded.
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
    let mut env = Vec::new();
    if let Some(env_val) = mapping.get("variables") {
        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 global image
    let global_image = mapping.get("image").and_then(|v| {
        if let Value::String(s) = v {
            Some(s.clone())
        } else if let Value::Mapping(m) = v {
            if let Some(Value::String(s)) = m.get(Value::String("name".to_string())) {
                Some(s.clone())
            } else {
                None
            }
        } else {
            None
        }
    });

    // Parse top-level workflow:rules (task 2.4)
    let workflow_rules = parse_workflow_rules(mapping);

    // Resolve extends: merge parent fields into child jobs (task 2.1)
    let resolved = resolve_extends(mapping);

    // Filter out hidden jobs (task 2.2) — keys starting with '.'
    let reserved = vec![
        "stages",
        "variables",
        "image",
        "before_script",
        "after_script",
        "cache",
        "services",
        "include",
        "default",
    ];

    let mut jobs = Vec::new();

    for (key, value) in &resolved {
        let key_str = match key.as_str() {
            Some(s) => s,
            None => continue,
        };

        // Skip reserved top-level keys
        if reserved.contains(&key_str) || key_str == "workflow" {
            continue;
        }

        // Skip hidden jobs (task 2.2) — keys starting with '.'
        if key_str.starts_with('.') {
            continue;
        }

        // Each other top-level key is a job
        if let Some(job_map) = value.as_mapping() {
            let mut job = parse_job(key_str, job_map)?;
            // If job doesn't have an image, use global default
            if job.container_image.is_none() {
                job.container_image = global_image.clone();
            }
            jobs.push(job);
        }
    }

    Ok(Pipeline {
        provider: Provider::GitLabCI,
        jobs,
        env,
        source: content.to_string(),
        is_reusable: false,
        workflow_call_inputs: Vec::new(),
        workflow_call_secrets: Vec::new(),
        workflow_rules,
    })
}

/// Parse include blocks to extract referenced files (without loading external content)
///
/// Returns IncludeInfo with detected include references.
/// This does NOT resolve/load external files - just extracts paths for auditing.
pub fn parse_includes(content: &str) -> Result<IncludeInfo> {
    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())
    });

    let mut info = IncludeInfo::default();

    let include_val = match mapping {
        Ok(m) => m.get("include"),
        Err(e) => return Err(e),
    };

    let Some(include_block) = include_val else {
        return Ok(info);
    };

    let Value::Sequence(includes) = include_block else {
        if let Value::String(s) = include_block {
            classify_include(s, &mut info);
        }
        return Ok(info);
    };

    for item in includes {
        match item {
            Value::String(s) => {
                classify_include(s, &mut info);
            }
            Value::Mapping(m) => {
                if let Some(Value::String(s)) = m.get("local") {
                    info.local.push(s.clone());
                }
                if let Some(Value::String(s)) = m.get("remote") {
                    info.remote.push(s.clone());
                }
                if let Some(Value::String(s)) = m.get("project") {
                    if let Some(Value::String(path)) = m.get("file") {
                        info.project.push(format!("{}:{}", s, path));
                    } else {
                        info.project.push(s.clone());
                    }
                }
            }
            _ => {}
        }
    }

    Ok(info)
}

fn classify_include(s: &str, info: &mut IncludeInfo) {
    if s.starts_with("https://") || s.starts_with("http://") {
        info.remote.push(s.to_string());
    } else if s.contains("::") {
        info.project.push(s.to_string());
    } else {
        info.local.push(s.to_string());
    }
}

fn parse_job(id: &str, map: &serde_yaml::Mapping) -> Result<Job> {
    let mut steps = Vec::new();
    let mut depends_on = Vec::new();
    let mut env = Vec::new();
    let mut container_image: Option<String> = None;
    let mut service_images: Vec<String> = Vec::new();
    let mut timeout_minutes: Option<u64> = None;

    // Parse image
    if let Some(image_val) = map.get("image") {
        if let Value::String(s) = image_val {
            container_image = Some(s.clone());
        } else if let Value::Mapping(m) = image_val {
            if let Some(Value::String(s)) = m.get(Value::String("name".to_string())) {
                container_image = Some(s.clone());
            }
        }
    }

    // Parse timeout
    if let Some(timeout_val) = map.get("timeout") {
        if let Some(n) = timeout_val.as_u64() {
            timeout_minutes = Some(n);
        }
    }

    // Parse services
    if let Some(Value::Sequence(services)) = map.get("services") {
        for svc in services {
            if let Value::String(s) = svc {
                service_images.push(s.clone());
            } else if let Value::Mapping(m) = svc {
                if let Some(Value::String(s)) = m.get(Value::String("name".to_string())) {
                    service_images.push(s.clone());
                }
            }
        }
    }

    // Parse job-level variables
    if let Some(vars_val) = map.get("variables") {
        if let Some(vars_map) = vars_val.as_mapping() {
            for (k, v) in vars_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 dependencies / needs
    if let Some(needs_val) = map.get("needs") {
        if let Value::Sequence(seq) = needs_val {
            for item in seq {
                match item {
                    Value::String(s) => depends_on.push(s.clone()),
                    Value::Mapping(m) => {
                        if let Some(Value::String(s)) = m.get(Value::String("job".to_string())) {
                            depends_on.push(s.clone());
                        }
                    }
                    _ => {}
                }
            }
        }
    } else if let Value::Sequence(seq) = map.get("dependencies").unwrap_or(&Value::Null) {
        for item in seq {
            if let Value::String(s) = item {
                depends_on.push(s.clone());
            }
        }
    }

    // Parse script steps
    for script_key in &["before_script", "script", "after_script"] {
        if let Some(script_val) = map.get(*script_key) {
            let run = match script_val {
                Value::String(s) => s.clone(),
                Value::Sequence(seq) => seq
                    .iter()
                    .map(|v| match v {
                        Value::String(s) => s.clone(),
                        other => format!("{:?}", other),
                    })
                    .collect::<Vec<_>>()
                    .join("\n"),
                other => format!("{:?}", other),
            };

            steps.push(Step {
                name: Some(script_key.to_string()),
                uses: None,
                run: Some(run),
                env: Vec::new(),
                with_inputs: None,
            });
        }
    }

    // Parse trigger
    if let Some(trigger_val) = map.get("trigger") {
        let trigger_str = match trigger_val {
            Value::String(s) => s.clone(),
            Value::Mapping(m) => {
                if let Some(Value::String(s)) = m.get(Value::String("project".to_string())) {
                    format!("project: {}", s)
                } else {
                    format!("{:?}", trigger_val)
                }
            }
            other => format!("{:?}", other),
        };
        steps.push(Step {
            name: Some("trigger".to_string()),
            uses: None,
            run: Some(format!("trigger: {}", trigger_str)),
            env: Vec::new(),
            with_inputs: None,
        });
    }

    // Parse rules (task 2.3)
    let rules = parse_rules(map);

    Ok(Job {
        id: id.to_string(),
        name: None,
        depends_on,
        steps,
        env,
        container_image,
        service_images,
        timeout_minutes,
        rules,
    })
}