From bbba88cb095d69635b381168271f067fcccd8d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Alberto=20Monta=C3=B1o=20Fetecua?= Date: Thu, 5 Dec 2024 10:15:38 -0500 Subject: [PATCH] feat: implemented checks of internal run commands in yml files --- .circleci/config.yml | 4 +-- src/@orb.yml | 2 +- src/scripts/review.bats | 74 ++++++++++++++++++++++------------------- src/scripts/review.sh | 4 +-- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 82b1c89..4876961 100755 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,8 +4,8 @@ orbs: # For the orb-tools-orb it must call itself within the first workflow, # meaning this dev version will need to be published every 90 days. # No other orbs will need to import themselves here. - orb-tools-alpha: circleci/orb-tools@12.1.0 - shellcheck: circleci/shellcheck@3.1 + orb-tools-alpha: circleci/orb-tools@12.2.0 + shellcheck: circleci/shellcheck@3.2.0 filters: &filters tags: diff --git a/src/@orb.yml b/src/@orb.yml index 52f9ce2..c5c938f 100644 --- a/src/@orb.yml +++ b/src/@orb.yml @@ -11,4 +11,4 @@ display: orbs: cli: circleci/circleci-cli@0.1.9 - bats: circleci/bats@1.0.0 + bats: circleci/bats@1.1.0 diff --git a/src/scripts/review.bats b/src/scripts/review.bats index aa9a6a2..5371c63 100644 --- a/src/scripts/review.bats +++ b/src/scripts/review.bats @@ -106,14 +106,14 @@ setup() { fi ERROR_COUNT=0 for i in $(find "${ORB_SOURCE_DIR}/jobs" "${ORB_SOURCE_DIR}/commands" -name "*.yml" 2>/dev/null); do - ORB_COMPONENT_STEPS_COUNT=$(yq '[.steps.[] | .run | select(. != null)] | length' "$i") - j=0 - while [ "$j" -lt "$ORB_COMPONENT_STEPS_COUNT" ]; do - ORB_COMPONENT_STEP=$(yq "[.steps.[] | .run | select(. != null)][$j]" "$i") - ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq -o=json '.' | jq 'type') - ORB_COMPONENT_LINE_NUMBER=$(yq "[.steps.[] | .run | select(. != null)][$j] | line" "$i") - ORB_COMPONENT_STEP_NAME=$(yq "[.steps.[] | .run | select(. != null)][$j] | .name" "$i") - if [[ "$ORB_COMPONENT_STEP_TYPE" == '"string"' ]]; then + ORB_COMPONENT_STEPS_COUNT=$(yq -o json '.' "$i" | jq '[. | paths ] | map(select(last=="run")) | length') + RUN_ENTRIES=$(yq -o json '.' "$i" | jq -r '[paths] | map(select(last == "run") | join(".")) | .[]') + + for ENTRY in $(echo "$RUN_ENTRIES"); do + ORB_COMPONENT_STEP=$(yq ".$ENTRY" "$i") + ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq 'type') + ORB_COMPONENT_LINE_NUMBER=$(yq ".${ENTRY} | line" "$i") + if [[ "$ORB_COMPONENT_STEP_TYPE" == '!!str' ]]; then echo "File: \"${i}\"" echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}" echo "It appears this 'run' step is using 'string' formatting." @@ -122,15 +122,18 @@ setup() { echo "$ORB_COMPONENT_STEP" echo --- ERROR_COUNT=$((ERROR_COUNT + 1)) - elif [[ "$ORB_COMPONENT_STEP_NAME" == null || "$ORB_COMPONENT_STEP_NAME" == '""' ]]; then - echo "File: \"${i}\"" - echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}" - echo --- - yq "[.steps.[] | .run | select(. != null)][$j]" "$i" - echo --- - ERROR_COUNT=$((ERROR_COUNT + 1)) + else + ORB_COMPONENT_STEP_NAME=$(echo "${ORB_COMPONENT_STEP}" | yq '.name') + ORB_COMPONENT_LINE_NUMBER=$(yq ".${ENTRY}.command | line" "$i") + if [[ "$ORB_COMPONENT_STEP_NAME" == null || "$ORB_COMPONENT_STEP_NAME" == '""' ]]; then + echo "File: \"${i}\"" + echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}" + echo --- + echo "$ORB_COMPONENT_STEP" + echo --- + ERROR_COUNT=$((ERROR_COUNT + 1)) + fi fi - j=$((j + 1)) done done if [[ "$ERROR_COUNT" -gt 0 ]]; then @@ -149,14 +152,14 @@ setup() { fi ERROR_COUNT=0 for i in $(find ${ORB_SOURCE_DIR}/jobs ${ORB_SOURCE_DIR}/commands -name "*.yml" 2>/dev/null); do - ORB_COMPONENT_STEPS_COUNT=$(yq '[.steps.[] | .run | select(. != null)] | length' "$i") - j=0 - while [ "$j" -lt "$ORB_COMPONENT_STEPS_COUNT" ]; do - ORB_COMPONENT_STEP=$(yq "[.steps.[] | .run | select(. != null)][$j]" "$i") - ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq -o=json '.' | jq 'type') - ORB_COMPONENT_LINE_NUMBER=$(yq "[.steps.[] | .run | select(. != null)][$j] | line" "$i") - ORB_COMPONENT_STEP_COMMAND=$(yq "[.steps.[] | .run | select(. != null)][$j] | .command" "$i") - if [[ "$ORB_COMPONENT_STEP_TYPE" == '"string"' ]]; then + ORB_COMPONENT_STEPS_COUNT=$(yq -o json '.' "$i" | jq '[. | paths ] | map(select(last=="run")) | length') + RUN_ENTRIES=$(yq -o json '.' "$i" | jq -r '[paths] | map(select(last == "run") | join(".")) | .[]') + + for ENTRY in $(echo "$RUN_ENTRIES"); do + ORB_COMPONENT_STEP=$(yq ".$ENTRY" "$i") + ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq 'type') + ORB_COMPONENT_LINE_NUMBER=$(yq ".${ENTRY} | line" "$i") + if [[ "$ORB_COMPONENT_STEP_TYPE" == '!!str' ]]; then echo "File: \"${i}\"" echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}" echo "It appears this 'run' step is using 'string' formatting." @@ -165,18 +168,21 @@ setup() { echo "$ORB_COMPONENT_STEP" echo --- ERROR_COUNT=$((ERROR_COUNT + 1)) - elif [[ "${#ORB_COMPONENT_STEP_COMMAND}" -gt "${ORB_VAL_MAX_COMMAND_LENGTH}" ]]; then - if [[ ! "$ORB_COMPONENT_STEP_COMMAND" =~ \<\/dev/null; then echo 'The "yq" package must be installed to execute review testing.' echo 'Installing "yq" automatically...' - YQ_VERSION=v4.20.1 + YQ_VERSION=v4.44.6 YQ_BIN=yq_linux_amd64 wget https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/${YQ_BIN}.tar.gz -O - | tar xz && mv ${YQ_BIN} /usr/bin/yq fi mkdir -p /tmp/orb_dev_kit/review/ -echo "$ORB_VAL_REVIEW_BATS_FILE" >review.bats +echo "$ORB_VAL_REVIEW_BATS_FILE" > review.bats echo "Reviewing orb best practices" echo "If required, tests can be skipped via their \"RCXXX\" code with the \"exclude\" parameter." bats -T --pretty --report-formatter junit --output /tmp/orb_dev_kit/review ./review.bats