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

feat: evaluates a matrix expansion only once #274

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/audit/self_hosted_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl WorkflowAudit for SelfHostedRunner {
LoE::Expr(exp) => {
let matrix = Matrix::try_from(&job)?;

let expansions = matrix.expand_values();
let expansions = matrix.expanded_values;

let self_hosted = expansions.iter().any(|(path, expansion)| {
exp.as_bare() == path && expansion.contains("self-hosted")
Expand Down
36 changes: 20 additions & 16 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl<'w> Iterator for Jobs<'w> {
#[derive(Clone)]
pub(crate) struct Matrix<'w> {
inner: &'w LoE<job::Matrix>,
pub(crate) expanded_values: Vec<(String, String)>,
}

impl<'w> Deref for Matrix<'w> {
Expand All @@ -252,30 +253,43 @@ impl<'w> TryFrom<&'w Job<'w>> for Matrix<'w> {
};

let Some(Strategy {
matrix: Some(matrix),
matrix: Some(inner),
..
}) = &job.strategy
else {
bail!("job does not define a strategy or interior matrix")
};

Ok(Matrix { inner: matrix })
Ok(Matrix::new(inner))
}
}

impl<'w> Matrix<'w> {
pub(crate) fn new(inner: &'w LoE<job::Matrix>) -> Self {
Self { inner }
Self {
inner,
expanded_values: Matrix::expand_values(inner),
}
}

/// Checks whether some expanded path leads to an expression
pub(crate) fn is_static(&self) -> bool {
let expands_to_expression = self
.expanded_values
.iter()
.any(|(_, expansion)| expansion.starts_with("${{") && expansion.ends_with("}}"));

!expands_to_expression
}

/// Expands the current Matrix into all possible values
/// By default, the return is a pair (String, String), in which
/// the first component is the expanded path (e.g. 'matrix.os') and
/// the second component is the string representation for the expanded value
/// (eg ubuntu-latest)
/// (e.g. ubuntu-latest)
///
pub(crate) fn expand_values(&self) -> Vec<(String, String)> {
match &self.inner {
fn expand_values(inner: &LoE<job::Matrix>) -> Vec<(String, String)> {
match inner {
LoE::Expr(_) => vec![],
LoE::Literal(matrix) => {
let LoE::Literal(dimensions) = &matrix.dimensions else {
Expand Down Expand Up @@ -310,16 +324,6 @@ impl<'w> Matrix<'w> {
}
}

/// Checks whether some expanded path leads to an expression
pub(crate) fn is_static(&self) -> bool {
let expands_to_expression = self
.expand_values()
.iter()
.any(|(_, expansion)| expansion.starts_with("${{") && expansion.ends_with("}}"));

!expands_to_expression
}

fn expand_explicit_rows(
include: &IndexMap<String, serde_yaml::Value>,
) -> Vec<(String, String)> {
Expand Down
Loading