Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for pipeline and environment variables #47

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading