diff --git a/scripts/validate-param.sh b/scripts/validate-param.sh index 028dffa..7fc191e 100755 --- a/scripts/validate-param.sh +++ b/scripts/validate-param.sh @@ -15,40 +15,55 @@ fi # Load the workflow file content WORKFLOW_CONTENT=$(cat "${WORKFLOW_FILE}") -# Parse the JSON input and iterate over the checks -echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r step; do - STEP_ID=$(echo "${step}" | jq -r '.key') - PARAMS=$(echo "${step}" | jq -r '.value') - - echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r param; do - PARAM=$(echo "${param}" | jq -r '.key') - EXPECTED_VALUE=$(echo "${param}" | jq -r '.value') +check_param() { + local PARAM_PATH=$1 + local EXPECTED_VALUE=$2 - # Check the parameter in the workflow file content - STEP_CONTENT=$(echo "${WORKFLOW_CONTENT}" | yq e ".jobs.test.steps[] | select(.id == \"${STEP_ID}\")" -) + ACTUAL_VALUE=$(echo "${WORKFLOW_CONTENT}" | yq e "${PARAM_PATH}" -) - if [ -z "${STEP_CONTENT}" ]; then - echo "Step with id ${STEP_ID} not found in the workflow file." - exit 1 - fi + if [ "${ACTUAL_VALUE}" == "null" ]; then + echo "The parameter at ${PARAM_PATH} is not set." + exit 1 + fi - echo "Checking step ${STEP_ID} for parameter ${PARAM} with expected value ${EXPECTED_VALUE}" - echo "Step content: ${STEP_CONTENT}" + if [ "${ACTUAL_VALUE}" != "${EXPECTED_VALUE}" ]; then + echo "Unexpected value at ${PARAM_PATH}" + echo " expected: ${EXPECTED_VALUE}" + echo " got: ${ACTUAL_VALUE}" + exit 1 + fi - ACTUAL_VALUE=$(echo "${STEP_CONTENT}" | yq e ".${PARAM}" -) + echo "Passed" +} - if [ "${ACTUAL_VALUE}" == "null" ]; then - echo "The parameter ${PARAM} for step ${STEP_ID} is not set." - exit 1 - fi - - if [ "${ACTUAL_VALUE}" != "${EXPECTED_VALUE}" ]; then - echo "Unexpected \"${PARAM}\" for step \"${STEP_ID}\"" - echo " expected: ${EXPECTED_VALUE}" - echo " got: ${ACTUAL_VALUE}" - exit 1 - fi +# Parse the JSON input and iterate over the checks +echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r entry; do + KEY=$(echo "${entry}" | jq -r '.key') + PARAMS=$(echo "${entry}" | jq -r '.value') - echo "Passed" - done + # Check if the key is for a workflow, job, or step + if [ "${KEY}" == "workflow" ]; then + echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r param; do + PARAM=$(echo "${param}" | jq -r '.key') + EXPECTED_VALUE=$(echo "${param}" | jq -r '.value') + check_param ".${PARAM}" "${EXPECTED_VALUE}" + done + elif [[ "${KEY}" == jobs.* ]]; then + JOB_ID=$(echo "${KEY}" | cut -d. -f2) + echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r param; do + PARAM=$(echo "${param}" | jq -r '.key') + EXPECTED_VALUE=$(echo "${param}" | jq -r '.value') + check_param ".jobs.${JOB_ID}.${PARAM}" "${EXPECTED_VALUE}" + done + elif [[ "${KEY}" == steps.* ]]; then + STEP_ID=$(echo "${KEY}" | cut -d. -f2) + echo "${PARAMS}" | jq -c 'to_entries | .[]' | while read -r param; do + PARAM=$(echo "${param}" | jq -r '.key') + EXPECTED_VALUE=$(echo "${param}" | jq -r '.value') + check_param ".jobs.test.steps[] | select(.id == \"${STEP_ID}\") | .${PARAM}" "${EXPECTED_VALUE}" + done + else + echo "Unknown key format: ${KEY}" + exit 1 + fi done