From b66d251441975dee626d5c76ab1a0e92c2c72142 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 2 Mar 2021 14:34:48 +0800 Subject: [PATCH] refactor: move pipeline export to pipeline project --- plugins/coco_pipeline/src/lib.rs | 4 +- plugins/coco_pipeline/src/pipeline.rs | 62 +++++++++++++++++++- plugins/coco_pipeline/src/pipeline_plugin.rs | 9 +-- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/plugins/coco_pipeline/src/lib.rs b/plugins/coco_pipeline/src/lib.rs index 802686d6..1ee2a7c9 100644 --- a/plugins/coco_pipeline/src/lib.rs +++ b/plugins/coco_pipeline/src/lib.rs @@ -35,9 +35,9 @@ pub fn plugin() -> Box { #[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; @@ -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 = serde_json::from_str(&code).unwrap(); + let pipelines: Vec = serde_json::from_str(&code).unwrap(); assert_eq!(1, pipelines.len()); assert_eq!(1, pipelines[0].stages.len()); diff --git a/plugins/coco_pipeline/src/pipeline.rs b/plugins/coco_pipeline/src/pipeline.rs index 071980c4..4211ede9 100644 --- a/plugins/coco_pipeline/src/pipeline.rs +++ b/plugins/coco_pipeline/src/pipeline.rs @@ -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, +} + +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, + pub is_parallel: bool, + pub sub_stages: Vec, +} + +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![], + } + } +} diff --git a/plugins/coco_pipeline/src/pipeline_plugin.rs b/plugins/coco_pipeline/src/pipeline_plugin.rs index 359a0941..b9a862f8 100644 --- a/plugins/coco_pipeline/src/pipeline_plugin.rs +++ b/plugins/coco_pipeline/src/pipeline_plugin.rs @@ -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; @@ -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)); } } @@ -31,7 +32,7 @@ fn write_to_json_file(url_str: &str, result: &String) { fn lookup_jenkinsfile(url_str: &str) -> Vec { 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() { @@ -39,10 +40,10 @@ fn lookup_jenkinsfile(url_str: &str) -> Vec { } if entry.file_name().to_str().unwrap() == "Jenkinsfile" { - origin_files.push(entry.into_path()); + pipeline_files.push(entry.into_path()); } } } - origin_files + pipeline_files }