change evaluation of conditions

This commit is contained in:
2026-07-25 13:15:14 +02:00
parent b2e16ac1e0
commit d4a9a85fbf
+83 -65
View File
@@ -1,14 +1,16 @@
declare -A context declare -A context
declare -A outputs
declare -a output_names
matched_any=false
echo "=== CONTEXT ===" echo "=== CONTEXT ==="
printf '%s\n' "$CONTEXT" printf '%s\n' "${CONTEXT:-}"
echo echo
echo "=== RULES ===" echo "=== RULES ==="
printf '%s\n' "$RULES" printf '%s\n' "${RULES:-}"
#
# Remove leading/trailing whitespace
#
trim() { trim() {
local var="$*" local var="$*"
var="${var#"${var%%[![:space:]]*}"}" var="${var#"${var%%[![:space:]]*}"}"
@@ -16,13 +18,11 @@ trim() {
printf '%s' "$var" printf '%s' "$var"
} }
#
# Load key=value pairs into context array
#
load_context() { load_context() {
local key
local value
while IFS='=' read -r key value; do while IFS='=' read -r key value; do
key="$(trim "${key:-}")" key="$(trim "${key:-}")"
value="$(trim "${value:-}")" value="$(trim "${value:-}")"
@@ -30,20 +30,10 @@ load_context() {
[[ "$key" =~ ^# ]] && continue [[ "$key" =~ ^# ]] && continue
context["$key"]="$value" context["$key"]="$value"
done <<< "${CONTEXT:-}"
done <<< "$CONTEXT"
} }
#
# Evaluate a single condition
#
# Examples:
# branch == 9.0
# event != push
# count > 10
#
evaluate_condition() { evaluate_condition() {
local condition="$1" local condition="$1"
local variable local variable
@@ -65,8 +55,9 @@ evaluate_condition() {
actual="${context[$variable]}" actual="${context[$variable]}"
case "$operator" in echo "Evaluating: '$variable' actual='$actual' operator='$operator' expected='$expected'"
case "$operator" in
"==") "==")
[[ "$actual" == "$expected" ]] [[ "$actual" == "$expected" ]]
;; ;;
@@ -88,7 +79,6 @@ evaluate_condition() {
;; ;;
">"|">="|"<"|"<=") ">"|">="|"<"|"<=")
if [[ ! "$actual" =~ ^-?[0-9]+$ ]]; then if [[ ! "$actual" =~ ^-?[0-9]+$ ]]; then
echo "Numeric operator '$operator' requires integer value. Actual: '$actual'" echo "Numeric operator '$operator' requires integer value. Actual: '$actual'"
return 1 return 1
@@ -122,74 +112,102 @@ evaluate_condition() {
esac esac
} }
#
# Evaluate list of conditions
#
# Conditions are OR'd together
#
evaluate_conditions() { evaluate_conditions() {
local conditions="$1"
local matched=false local condition
while IFS= read -r condition; do while IFS= read -r condition; do
condition="$(trim "${condition:-}")" condition="$(trim "${condition:-}")"
[[ -z "$condition" ]] && continue [[ -z "$condition" ]] && continue
[[ "$condition" =~ ^# ]] && continue [[ "$condition" =~ ^# ]] && continue
if evaluate_condition "$condition"; then if evaluate_condition "$condition"; then
matched=true return 0
break
fi fi
done <<< "$conditions"
done <<< "$CONDITIONS" return 1
[[ "$matched" == true ]]
} }
# store_output() {
# Write outputs local output="$1"
# local name
apply_variables() { local value
while IFS= read -r output; do
output="$(trim "${output:-}")" output="$(trim "${output:-}")"
[[ -z "$output" ]] && continue [[ -z "$output" ]] && return 0
[[ "$output" =~ ^# ]] && continue [[ "$output" =~ ^# ]] && return 0
if [[ ! "$output" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then if [[ ! "$output" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
echo "Invalid variable definition: $output" echo "Invalid variable definition: $output"
return 1 return 1
fi fi
echo "$output" >> "$GITHUB_OUTPUT" name="${output%%=*}"
value="${output#*=}"
done <<< "$VARIABLES" if [[ ! -v outputs["$name"] ]]; then
} output_names+=("$name")
#
# Main
#
main() {
load_context
local matched=false
if evaluate_conditions; then
matched=true
apply_variables
fi fi
if [[ "$matched" == false && "$FAIL_ON_NO_MATCH" == "true" ]]; then outputs["$name"]="$value"
echo "No conditions matched"
exit 1
fi
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