Skip to content

fix(plumber): improve job matrix validation - env. var dups #417

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
89 changes: 85 additions & 4 deletions plumber/ppl/lib/ppl/definition_reviser/job_matrix_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ defmodule Ppl.DefinitionReviser.JobMatrixValidator do
@moduledoc """
This module serves to validate that all job matrix values are provided
as a list of strings either explicitly or after evaluation by SPC command line tool.

It also validates that there are no duplicate environment variable names in the job matrix.
It also validates that the total product size of the matrix (product of number of values of each environment variable) is not too large.
"""

alias Util.ToTuple

@max_size 100

def validate(definition) do
with {:ok, definition} <- do_validate_job_matrix_values(definition, "blocks"),
{:ok, definition} <- do_validate_job_matrix_values(definition, "after_pipeline") do
ToTuple.ok(definition)
else
{:error, _} = error -> error
end
end

Expand Down Expand Up @@ -43,9 +50,29 @@ defmodule Ppl.DefinitionReviser.JobMatrixValidator do
jobs = get_in(block, ["build", "jobs"]) |> List.wrap()
block_name = get_in(block, ["name"])

case Enum.find_value(jobs, &validate_job_matrices(block_name, &1)) do
nil -> {:ok, [block] ++ block_acc}
{:error, error} -> {:error, error}
# Calculate total matrix size across all jobs in the block
total_result =
Enum.reduce_while(jobs, {:ok, 0}, fn job, {:ok, total_size} ->
case validate_job_matrices(block_name, job) do
nil -> {:cont, {:ok, total_size}}
{:ok, matrix_size} -> {:cont, {:ok, total_size + matrix_size}}
{:error, _} = error -> {:halt, error}
end
end)

case total_result do
{:ok, total_size} ->
if total_size > @max_size do
{:error,
{:malformed,
"Total matrix size exceeds maximum allowed size (#{@max_size}) in block '#{block_name}'. " <>
"The matrix product size is calculated as the product of the number of values for each environment variable."}}
else
{:ok, [block] ++ block_acc}
end

{:error, _} = error ->
error
end
end

Expand All @@ -56,10 +83,39 @@ defmodule Ppl.DefinitionReviser.JobMatrixValidator do
job_name = Map.get(job, "name")

if Map.has_key?(job, "matrix") and is_list(matrix_values) do
Enum.find_value(matrix_values, &check_matrix_values(block_name, job_name, &1))
case validate_job_matrix(block_name, job_name, matrix_values, job) do
{:ok, matrix_size} -> {:ok, matrix_size}
{:error, _} = error -> error
nil -> nil
end
end
end

defp validate_job_matrix(block_name, job_name, matrix_values, job) do
with nil <- check_for_duplicate_env_vars(block_name, job_name, matrix_values),
nil <- Enum.find_value(matrix_values, &check_matrix_values(block_name, job_name, &1)),
{:ok, matrix_size} <- check_matrix_product_size(block_name, job_name, matrix_values, job) do
{:ok, matrix_size}
else
{:error, _} = error -> error
end
end

defp check_for_duplicate_env_vars(block_name, job_name, matrix_values) do
env_var_names_count =
Enum.reduce(matrix_values, %{}, fn matrix_entry, acc ->
env_var = Map.get(matrix_entry, "env_var")
Map.update(acc, env_var, 1, &(&1 + 1))
end)

Enum.find_value(env_var_names_count, fn {env_var_name, count} ->
if count > 1 do
{:error,
{:malformed, duplicate_env_var_error_message(block_name, job_name, env_var_name)}}
end
end)
end

defp check_matrix_values(block_name, job_name, matrix_entry) do
env_var = get_in(matrix_entry, ["env_var"])
values = get_in(matrix_entry, ["values"])
Expand All @@ -72,4 +128,29 @@ defmodule Ppl.DefinitionReviser.JobMatrixValidator do
defp error_mesasge(block_name, job_name, env_var) do
"Matrix values for env_var '#{env_var}' (block '#{block_name}', job '#{job_name}' must be a non-empty list of strings."
end

defp duplicate_env_var_error_message(block_name, job_name, env_var_name) do
"Duplicate environment variable(s): '#{env_var_name}' in job matrix (block '#{block_name}', job '#{job_name}')."
end

def check_matrix_product_size(block_name, job_name, matrix_values, _job) do
matrix_size =
Enum.reduce(matrix_values, 1, fn matrix_entry, acc ->
values = get_in(matrix_entry, ["values"])
if is_list(values), do: min(acc * length(values), @max_size + 1), else: acc
end)

if matrix_size > @max_size do
{:error,
{:malformed,
matrix_product_size_error_message(block_name, job_name, matrix_size, @max_size)}}
else
{:ok, matrix_size}
end
end

defp matrix_product_size_error_message(block_name, job_name, size, max_size) do
"Matrix product size exceeds maximum allowed size (#{max_size}) in job matrix (block '#{block_name}', job '#{job_name}'). " <>
"The matrix product size is calculated as the product of the number of values for each environment variable."
end
end
Loading