change evaluation of conditions
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
declare -A context
|
||||
declare -A outputs
|
||||
declare -a output_names
|
||||
|
||||
matched_any=false
|
||||
|
||||
echo "=== CONTEXT ==="
|
||||
printf '%s\n' "$CONTEXT"
|
||||
printf '%s\n' "${CONTEXT:-}"
|
||||
|
||||
echo
|
||||
echo "=== RULES ==="
|
||||
printf '%s\n' "$RULES"
|
||||
#
|
||||
# Remove leading/trailing whitespace
|
||||
#
|
||||
printf '%s\n' "${RULES:-}"
|
||||
|
||||
trim() {
|
||||
local var="$*"
|
||||
var="${var#"${var%%[![:space:]]*}"}"
|
||||
@@ -16,13 +18,11 @@ trim() {
|
||||
printf '%s' "$var"
|
||||
}
|
||||
|
||||
#
|
||||
# Load key=value pairs into context array
|
||||
#
|
||||
load_context() {
|
||||
local key
|
||||
local value
|
||||
|
||||
while IFS='=' read -r key value; do
|
||||
|
||||
key="$(trim "${key:-}")"
|
||||
value="$(trim "${value:-}")"
|
||||
|
||||
@@ -30,20 +30,10 @@ load_context() {
|
||||
[[ "$key" =~ ^# ]] && continue
|
||||
|
||||
context["$key"]="$value"
|
||||
|
||||
done <<< "$CONTEXT"
|
||||
done <<< "${CONTEXT:-}"
|
||||
}
|
||||
|
||||
#
|
||||
# Evaluate a single condition
|
||||
#
|
||||
# Examples:
|
||||
# branch == 9.0
|
||||
# event != push
|
||||
# count > 10
|
||||
#
|
||||
evaluate_condition() {
|
||||
|
||||
local condition="$1"
|
||||
|
||||
local variable
|
||||
@@ -65,8 +55,9 @@ evaluate_condition() {
|
||||
|
||||
actual="${context[$variable]}"
|
||||
|
||||
case "$operator" in
|
||||
echo "Evaluating: '$variable' actual='$actual' operator='$operator' expected='$expected'"
|
||||
|
||||
case "$operator" in
|
||||
"==")
|
||||
[[ "$actual" == "$expected" ]]
|
||||
;;
|
||||
@@ -88,7 +79,6 @@ evaluate_condition() {
|
||||
;;
|
||||
|
||||
">"|">="|"<"|"<=")
|
||||
|
||||
if [[ ! "$actual" =~ ^-?[0-9]+$ ]]; then
|
||||
echo "Numeric operator '$operator' requires integer value. Actual: '$actual'"
|
||||
return 1
|
||||
@@ -122,74 +112,102 @@ evaluate_condition() {
|
||||
esac
|
||||
}
|
||||
|
||||
#
|
||||
# Evaluate list of conditions
|
||||
#
|
||||
# Conditions are OR'd together
|
||||
#
|
||||
evaluate_conditions() {
|
||||
|
||||
local matched=false
|
||||
local conditions="$1"
|
||||
local condition
|
||||
|
||||
while IFS= read -r condition; do
|
||||
|
||||
condition="$(trim "${condition:-}")"
|
||||
|
||||
[[ -z "$condition" ]] && continue
|
||||
[[ "$condition" =~ ^# ]] && continue
|
||||
|
||||
if evaluate_condition "$condition"; then
|
||||
matched=true
|
||||
break
|
||||
return 0
|
||||
fi
|
||||
done <<< "$conditions"
|
||||
|
||||
done <<< "$CONDITIONS"
|
||||
|
||||
[[ "$matched" == true ]]
|
||||
return 1
|
||||
}
|
||||
|
||||
#
|
||||
# Write outputs
|
||||
#
|
||||
apply_variables() {
|
||||
|
||||
while IFS= read -r output; do
|
||||
store_output() {
|
||||
local output="$1"
|
||||
local name
|
||||
local value
|
||||
|
||||
output="$(trim "${output:-}")"
|
||||
|
||||
[[ -z "$output" ]] && continue
|
||||
[[ "$output" =~ ^# ]] && continue
|
||||
[[ -z "$output" ]] && return 0
|
||||
[[ "$output" =~ ^# ]] && return 0
|
||||
|
||||
if [[ ! "$output" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
|
||||
echo "Invalid variable definition: $output"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "$output" >> "$GITHUB_OUTPUT"
|
||||
name="${output%%=*}"
|
||||
value="${output#*=}"
|
||||
|
||||
done <<< "$VARIABLES"
|
||||
}
|
||||
|
||||
#
|
||||
# Main
|
||||
#
|
||||
main() {
|
||||
|
||||
load_context
|
||||
|
||||
local matched=false
|
||||
|
||||
if evaluate_conditions; then
|
||||
matched=true
|
||||
apply_variables
|
||||
if [[ ! -v outputs["$name"] ]]; then
|
||||
output_names+=("$name")
|
||||
fi
|
||||
|
||||
if [[ "$matched" == false && "$FAIL_ON_NO_MATCH" == "true" ]]; then
|
||||
echo "No conditions matched"
|
||||
exit 1
|
||||
fi
|
||||
outputs["$name"]="$value"
|
||||
|
||||
echo "matched=$matched" >> "$GITHUB_OUTPUT"
|
||||
echo "Set output candidate: $name=$value"
|
||||
}
|
||||
|
||||
main
|
||||
apply_variables() {
|
||||
local variables="$1"
|
||||
local output
|
||||
|
||||
while IFS= read -r output; do
|
||||
store_output "$output"
|
||||
done <<< "$variables"
|
||||
}
|
||||
|
||||
finalize_rule() {
|
||||
local conditions="$1"
|
||||
local variables="$2"
|
||||
|
||||
conditions="$(trim "$conditions")"
|
||||
variables="$(trim "$variables")"
|
||||
|
||||
[[ -z "$conditions" && -z "$variables" ]] && return 0
|
||||
|
||||
echo
|
||||
echo "=== PARSED RULE ==="
|
||||
echo "--- conditions ---"
|
||||
printf '%s\n' "$conditions"
|
||||
echo "--- set ---"
|
||||
printf '%s\n' "$variables"
|
||||
|
||||
if [[ -z "$conditions" ]]; then
|
||||
echo "Rule has no conditions, skipping"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -z "$variables" ]]; then
|
||||
echo "Rule has no variables to set, skipping"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if evaluate_conditions "$conditions"; then
|
||||
echo "Rule matched"
|
||||
matched_any=true
|
||||
apply_variables "$variables"
|
||||
else
|
||||
echo "Rule did not match"
|
||||
fi
|
||||
}
|
||||
|
||||
parse_rules() {
|
||||
local line
|
||||
local current_conditions=""
|
||||
local current_variables=""
|
||||
local mode=""
|
||||
|
||||
while IFS= read -r line; do
|
||||
line="$(trim "${line:-}")"
|
||||
|
||||
[[ -z "$line" ]] && continue
|
||||
Reference in New Issue
Block a user