Skip to content

Commit

Permalink
refactor: move pipeline export to pipeline project
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 2, 2021
1 parent 5b80760 commit b66d251
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
4 changes: 2 additions & 2 deletions plugins/coco_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub fn plugin() -> Box<dyn PluginInterface> {

#[cfg(test)]
mod tests {
use crate::pipeline::Pipeline;
use crate::pipeline_plugin::execute;
use core_model::{CocoConfig, RepoConfig};
use jenkinsfile::Jenkinsfile;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
Expand Down Expand Up @@ -81,7 +81,7 @@ mod tests {
let mut file = File::open(output_dir).unwrap();
let mut code = String::new();
file.read_to_string(&mut code).unwrap();
let pipelines: Vec<Jenkinsfile> = serde_json::from_str(&code).unwrap();
let pipelines: Vec<Pipeline> = serde_json::from_str(&code).unwrap();

assert_eq!(1, pipelines.len());
assert_eq!(1, pipelines[0].stages.len());
Expand Down
62 changes: 60 additions & 2 deletions plugins/coco_pipeline/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
use jenkinsfile::Jenkinsfile;
use serde::{Deserialize, Serialize};

/// common pipeline visualize model
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Pipeline {}
pub struct Pipeline {
pub name: String,
pub stages: Vec<PipelineStage>,
}

impl Pipeline {
pub fn from(jenkinsfile: Jenkinsfile) -> Pipeline {
let mut pipeline = Pipeline {
name: jenkinsfile.name,
stages: vec![],
};

for main_stage in &jenkinsfile.stages {
let mut stage = PipelineStage::new(main_stage.name.clone());
for sub_stage in &main_stage.sub_stages {
stage.sub_stages.push(PipelineStage {
name: sub_stage.name.clone(),
steps: sub_stage.steps.clone(),
is_parallel: false,
sub_stages: vec![],
})
}

pipeline.stages.push(stage);
}

pipeline
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PipelineStage {
pub name: String,
pub steps: Vec<String>,
pub is_parallel: bool,
pub sub_stages: Vec<PipelineStage>,
}

impl PipelineStage {
pub fn new(name: String) -> PipelineStage {
Self {
name,
steps: vec![],
is_parallel: false,
sub_stages: vec![],
}
}
}

impl Default for PipelineStage {
fn default() -> Self {
Self {
name: "".to_string(),
steps: vec![],
is_parallel: false,
sub_stages: vec![],
}
}
}
9 changes: 5 additions & 4 deletions plugins/coco_pipeline/src/pipeline_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::pipeline::Pipeline;
use core_model::url_format::uri_to_path;
use core_model::{url_format, CocoConfig, Settings};
use ignore::Walk;
Expand All @@ -14,7 +15,7 @@ pub fn execute(config: CocoConfig) {
for path in origin_files {
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
if let Some(jenkinsfile) = Jenkinsfile::from_str(contents.as_str()) {
results.push(jenkinsfile);
results.push(Pipeline::from(jenkinsfile));
}
}

Expand All @@ -31,18 +32,18 @@ fn write_to_json_file(url_str: &str, result: &String) {

fn lookup_jenkinsfile(url_str: &str) -> Vec<PathBuf> {
let path = uri_to_path(url_str);
let mut origin_files = vec![];
let mut pipeline_files = vec![];
for result in Walk::new(path) {
if let Ok(entry) = result {
if !entry.file_type().unwrap().is_file() {
continue;
}

if entry.file_name().to_str().unwrap() == "Jenkinsfile" {
origin_files.push(entry.into_path());
pipeline_files.push(entry.into_path());
}
}
}

origin_files
pipeline_files
}

0 comments on commit b66d251

Please sign in to comment.