Skip to content

Commit

Permalink
Merge pull request #47 from wcm-io-devops/feature/validate-env-and-pi…
Browse files Browse the repository at this point in the history
…peline-vars

Add validation for pipeline and environment variables
  • Loading branch information
BerSomBen authored Oct 24, 2024
2 parents 2f0ce53 + 8c096a9 commit 21d123f
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::pipelines::get_pipeline;
use crate::HOST_NAME;
use colored::*;
use reqwest::{Method, StatusCode};
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::process;
use std::thread::sleep;
use std::time::Duration;
Expand All @@ -23,13 +25,72 @@ impl PartialEq for EnvironmentVariable {
}
}

impl Eq for EnvironmentVariable {}

impl Hash for EnvironmentVariable {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.service.hash(state);
}
}

impl Hash for EnvironmentVariableServiceType {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
EnvironmentVariableServiceType::All => {
state.write_u8(1);
}
EnvironmentVariableServiceType::Author => {
state.write_u8(2);
}
EnvironmentVariableServiceType::Publish => {
state.write_u8(3);
}
EnvironmentVariableServiceType::Preview => {
state.write_u8(4);
}
EnvironmentVariableServiceType::Invalid => {
state.write_u8(5);
}
}
}
}

// Make pipeline variables comparable - if they have the same name and same service they are the same.
impl PartialEq for PipelineVariable {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.service == other.service
}
}

impl Eq for PipelineVariable {}

impl Hash for PipelineVariable {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.service.hash(state);
}
}

impl Hash for PipelineVariableServiceType {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
PipelineVariableServiceType::Build => {
state.write_u8(1);
}
PipelineVariableServiceType::FunctionalTest => {
state.write_u8(2);
}
PipelineVariableServiceType::UiTest => {
state.write_u8(3);
}
PipelineVariableServiceType::Invalid => {
state.write_u8(4);
}
}
}
}

/// Retrieves environment variables for the specified environment.
///
/// # Arguments
Expand Down Expand Up @@ -134,6 +195,21 @@ pub async fn set_env_vars_from_file(

println!("{:>4} Environment: {} ({})", "⬛", e.id, env.name);

// ensure there are no duplicate environment variables
let duplicates = find_duplicates(e.variables.clone());
if duplicates.len() > 0 {
for dv in &duplicates {
eprintln!(
"{:>8} {} name: '{}' service: {}",
"❌".red(),
"ERROR, duplicate variable definition found, please check your file!".red(),
dv.name,
dv.service
);
}
process::exit(4);
}

// The vector that holds the final variables that will be set or deleted. Will be constructed
// by comparing the variables that are currently set in Cloud Manager and those in the local
// YAML config file.
Expand Down Expand Up @@ -375,6 +451,21 @@ pub async fn set_pipeline_vars_from_file(

println!("{:>4} Pipeline: {} ({})", "⬛", l.id, pipeline.name);

// ensure there are no duplicate environment variables
let duplicates = find_duplicates(l.variables.clone());
if duplicates.len() > 0 {
for dv in &duplicates {
eprintln!(
"{:>8} {} name: '{}' service: {}",
"❌".red(),
"ERROR, duplicate variable definition found, please check your file!".red(),
dv.name,
dv.service
);
}
process::exit(4);
}

// The vector that holds the final variables that will be set or deleted. Will be constructed
// by comparing the variables that are currently set in Cloud Manager and those in the local
// YAML config file.
Expand Down Expand Up @@ -515,3 +606,16 @@ pub async fn set_pipeline_vars_from_file(
process::exit(2);
}
}

fn find_duplicates<T: Eq + Hash + Clone>(vec: Vec<T>) -> Vec<T> {
let mut seen = HashSet::new();
let mut duplicates = Vec::new();

for item in vec {
if !seen.insert(item.clone()) {
duplicates.push(item);
}
}

duplicates
}

0 comments on commit 21d123f

Please sign in to comment.