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
use serde_derive::*;
use serde_yaml::{Mapping, Value};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use tracing::{debug, info};
use yaml_merge_keys::merge_keys_serde;

pub type DynErr = Box<dyn std::error::Error + 'static>;

pub type StageName = String;
pub type JobName = String;
pub type VarName = String;
pub type VarValue = String;
pub type Script = String;

/// All Jobs in the same stage tend to be run at once.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Job {
    pub stage: Option<StageName>,
    pub before_script: Option<Vec<Script>>,
    pub script: Option<Vec<Script>>,

    /// Even though variables could be None,
    /// they could be defined in extends_job.variables
    /// (or globally)
    pub variables: Option<BTreeMap<VarName, VarValue>>,

    pub extends: Option<JobName>,

    #[serde(skip)]
    pub extends_job: Option<Rc<Job>>,
}

impl Job {
    /// Returns the consolidated local variables based on all extends.
    pub fn get_merged_variables(&self) -> BTreeMap<String, String> {
        let mut results = BTreeMap::new();
        self.calculate_variables(&mut results);
        results
    }

    fn calculate_variables(&self, mut variables: &mut BTreeMap<String, String>) {
        if let Some(ref parent) = self.extends_job {
            parent.calculate_variables(&mut variables);
        }
        if let Some(ref var) = self.variables {
            for (k, v) in var.iter() {
                variables.insert(k.clone(), v.clone());
            }
        }
    }
}

#[derive(Debug)]
pub struct GitlabCIConfig {
    pub file: PathBuf,

    /// Based on include orderings, what's the parent of this gitlab config.
    pub parent: Option<Box<GitlabCIConfig>>,

    /// Global variables
    pub variables: BTreeMap<VarName, VarValue>,

    /// Stages group jobs that run in parallel. The ordering is important
    pub stages: Vec<StageName>,

    /// Targets that gitlab can run.
    pub jobs: BTreeMap<JobName, Rc<Job>>,
}

impl GitlabCIConfig {
    /// Returns the consolidated global variables based on all imports.
    pub fn get_merged_variables(&self) -> BTreeMap<String, String> {
        let mut results = BTreeMap::new();
        self.calculate_variables(&mut results);
        results
    }

    pub fn lookup_job(&self, job_name: &str) -> Option<Rc<Job>> {
        if let Some(job) = self.jobs.get(job_name) {
            Some(job.clone())
        } else {
            if let Some(parent) = &self.parent {
                parent.lookup_job(job_name)
            } else {
                None
            }
        }
    }

    fn calculate_variables(&self, mut variables: &mut BTreeMap<String, String>) {
        if let Some(ref parent) = self.parent {
            parent.calculate_variables(&mut variables);
        }
        variables.extend(self.variables.clone());
    }
}

//#[tracing::instrument]
fn parse_includes(
    context: &Path,
    include: &Value,
    parent: Option<GitlabCIConfig>,
) -> Option<GitlabCIConfig> {
    match include {
        Value::String(include_filename) => {
            // Remove leading '/' - join (correctly) won't concat them if filename starts from root.
            let ch = include_filename.chars().next().unwrap();
            let include_filename = if ch == '/' || ch == '\\' {
                include_filename[1..].to_owned()
            } else {
                include_filename.to_owned()
            };
            let include_filename = context.join(&include_filename);
            parse_aux(&context.join(&Path::new(&include_filename)), parent).ok()
        }
        Value::Sequence(includes) => {
            let mut parent = parent;
            for include in includes {
                parent = parse_includes(context, include, parent);
                debug!("parent returned {:?}", parent.as_ref().unwrap().file);
            }
            parent
        }
        Value::Mapping(map) => {
            if let Some(Value::String(local)) = map.get(&Value::String("local".to_owned())) {
                let local = context.join(local);
                parse_aux(&local, parent).ok()
            } else if let Some(Value::String(project)) =
                map.get(&Value::String("project".to_owned()))
            {
                // We assume that the included project is checked out in a sister directory.
                let parts = project.split('/');
                let project_name = parts.last().expect("project name should contain '/'");

                if let Value::String(file) = map
                    .get(&Value::String("file".to_owned()))
                    .unwrap_or(&Value::String(".gitlab-ci.yml".to_owned()))
                {
                    let path = context.join(
                        Path::new("..")
                            .join(Path::new(project_name))
                            .join(Path::new(file)),
                    );
                    parse_aux(&path, parent).ok()
                } else {
                    parent
                }
            } else {
                parent
            }
        }
        _ => parent,
    }
}

///
/// Taking a path to a .gitlab-ci.yml file will read it and parse it where possible.
/// Anything unknown will be silently skipped. Jobs will be linked up with their parents.
///
pub fn parse(gitlab_file: &Path) -> Result<GitlabCIConfig, DynErr> {
    parse_aux(gitlab_file, None)
}

//#[tracing::instrument]
fn parse_aux(gitlab_file: &Path, parent: Option<GitlabCIConfig>) -> Result<GitlabCIConfig, DynErr> {
    debug!(
        "Parsing file {:?}, parent: {:?}",
        gitlab_file,
        parent.as_ref().map(|c| c.file.clone())
    );
    let f = std::fs::File::open(&gitlab_file)?;
    let raw_yaml = serde_yaml::from_reader(f)?;

    let val: serde_yaml::Value = merge_keys_serde(raw_yaml).expect("Couldn't merge yaml :<<");
    let mut config = GitlabCIConfig {
        file: gitlab_file.to_path_buf(),
        parent: None,
        stages: Vec::new(),
        variables: BTreeMap::new(),
        jobs: BTreeMap::new(),
    };

    if let serde_yaml::Value::Mapping(map) = val {
        info!("Parsing {:?} succesful.", gitlab_file);

        if let Some(includes) = map.get(&Value::String("include".to_owned())) {
            config.parent = parse_includes(
                gitlab_file
                    .parent()
                    .expect("gitlab-ci file wasn't in a dir??"),
                includes,
                parent,
            )
            .map(Box::new);
        } else {
            config.parent = parent.map(Box::new)
        }

        debug!(
            "All includes loaded for {:?}. {:?}",
            gitlab_file,
            config.parent.as_ref().map(|p| p.file.clone())
        );

        for (k, v) in map.iter() {
            if let Value::String(key) = k {
                if !config.jobs.contains_key(key) {
                    match (key.as_ref(), v) {
                        ("variables", _) => {
                            let global_var_map: Mapping = serde_yaml::from_value(v.clone())?;
                            for (key, value) in global_var_map {
                                if let (Value::String(key), Value::String(value)) = (key, value) {
                                    config.variables.insert(key, value);
                                }
                            }
                        }
                        ("stages", Value::Sequence(seq)) => {
                            for stage in seq {
                                if let Value::String(stage_name) = stage {
                                    config.stages.push(stage_name.to_owned());
                                }
                            }
                        }
                        (k, _) => {
                            let job_def = parse_job(&config, k, &map);
                            if let Ok(job) = job_def {
                                config.jobs.insert(k.to_owned(), job);
                            }
                        }
                    };
                }
            }
        }
    }

    Ok(config)
}

// When a file is loaded, all includes are imported, then all jobs, then
// only then do we load the jobs of the file that included us.
#[tracing::instrument]
fn parse_job(config: &GitlabCIConfig, job_name: &str, top: &Mapping) -> Result<Rc<Job>, DynErr> {
    let job_nm = Value::String(job_name.to_owned());
    if let Some(job) = top.get(&job_nm) {
        let j: Result<Job, _> = serde_yaml::from_value(job.clone());
        if let Ok(mut j) = j {
            if let Some(ref parent_job_name) = j.extends {
                // Parse parents first so we don't get wicked fun with Rc<>...

                let job: Option<Rc<Job>> = if job_name != parent_job_name
                    && top.contains_key(&Value::String(parent_job_name.clone()))
                {
                    parse_job(config, parent_job_name, top).ok()
                } else {
                    config.lookup_job(parent_job_name)
                };
                j.extends_job = job;
            }
            Ok(Rc::new(j)) //TODO: maybe push rc outside here
        } else {
            Err(Box::new(j.unwrap_err()))
        }
    } else {
        Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "Job not found",
        )))
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use std::path::PathBuf;
    use tracing::Level;
    use tracing_subscriber;

    #[test]
    pub fn parse_example() -> Result<(), DynErr> {
        let example_file: PathBuf = PathBuf::from(file!())
            .parent()
            .unwrap()
            .join("../examples/simple/.gitlab-ci.yml");

        // let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
        // let p = &PathBuf::from(Path::join(&root, "examples/simple/.gitlab-ci.yml"));
        let config = parse(&example_file)?;
        assert_eq!(
            config.variables["GLOBAL_VAR"],
            "this GLOBAL_VAR should mostly always be set.",
        );

        assert_eq!(config.stages.len(), 1);

        // Check jobs are linked up to their parents
        let parent = config
            .jobs
            .get("tired_starlings")
            .unwrap()
            .extends_job
            .as_ref()
            .unwrap();
        assert!(parent
            .variables
            .as_ref()
            .unwrap()
            .contains_key("AN_INHERITED_VARIABLE"));
        Ok(())
    }

    #[test]
    pub fn parse_include() -> Result<(), DynErr> {
        let example_file: PathBuf = PathBuf::from(file!())
            .parent()
            .unwrap()
            .join("../.gitlab-ci.yml");

        let config = parse(&example_file)?;
        assert!(config.parent.is_some());

        let globals = config.get_merged_variables();
        assert!(globals.contains_key("GLOBAL_VAR"));
        Ok(())
    }

    #[test]
    pub fn consolidated_global_vars() -> Result<(), DynErr> {
        let example_file: PathBuf = PathBuf::from(file!())
            .parent()
            .unwrap()
            .join("../examples/simple/.gitlab-ci.yml");
        let config = parse(&example_file)?;
        let vars = config.get_merged_variables();
        assert!(vars.contains_key("GLOBAL_VAR"));
        Ok(())
    }

    #[test]
    pub fn imports() -> Result<(), DynErr> {
        let subscriber = tracing_subscriber::fmt()
            // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
            // will be written to stdout.
            .with_max_level(Level::TRACE)
            // builds the subscriber.
            .finish();

        tracing::subscriber::with_default(subscriber, || {
            let example_file: PathBuf = PathBuf::from(file!())
                .parent()
                .unwrap()
                .join("../examples/imports/a.yml");
            let config = parse(&example_file).unwrap();
            let vars = config.get_merged_variables();

            let mut parent = config.parent;
            println!("file {:?}", config.file);
            while let Some(par) = parent {
                println!("parent {:?}", par.file);
                parent = par.parent;
            }

            assert!(vars.contains_key("A"));
            assert!(vars.contains_key("B"));
            assert!(vars.contains_key("C"));
            assert!(vars.contains_key("D"));
            assert!(vars.contains_key("E"));
        });
        Ok(())
    }
}