Skip to content

Commit

Permalink
fix: RC008 RC009 fixes (#131)
Browse files Browse the repository at this point in the history
* fix: RC008 properly checks run steps

* fix:  RC008 proper name check

* refactor: remove unnecessary 'cat'

* chore: lint missing quotes
  • Loading branch information
KyleTryon authored Mar 15, 2022
1 parent 69f2c34 commit 3e07cc9
Showing 1 changed file with 68 additions and 42 deletions.
110 changes: 68 additions & 42 deletions src/scripts/review.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ setup() {
}

@test "RC001: Include source_url in @orb.yml" {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC001" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC001" ]]; then
skip
fi
result=$(cat ${REVIEW_TEST_DIR}src/@orb.yml | yq '.display.source_url')
result=$(yq '.display.source_url' "${REVIEW_TEST_DIR}src/@orb.yml")

echo 'Set a value for "source_url" under the "display" key in "@orb.yml"'
[[ ! $result = null ]]
}

@test "RC002: All components (jobs, commands, executors, examples) must have descriptions" {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC002" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC002" ]]; then
skip
fi
for i in $(find ${REVIEW_TEST_DIR}src/jobs ${REVIEW_TEST_DIR}src/examples ${REVIEW_TEST_DIR}src/commands ${REVIEW_TEST_DIR}src/executors -name "*.yml" 2>/dev/null); do
ORB_ELEMENT_DESCRIPTION=$(cat $i | yq '.description')
if [[ $ORB_ELEMENT_DESCRIPTION == null || $ORB_ELEMENT_DESCRIPTION == '""' ]]; then
for i in $(find "${REVIEW_TEST_DIR}src/jobs" "${REVIEW_TEST_DIR}src/examples" "${REVIEW_TEST_DIR}src/commands" "${REVIEW_TEST_DIR}src/executors" -name "*.yml" 2>/dev/null); do
ORB_ELEMENT_DESCRIPTION=$(yq '.description' "$i")
if [[ "$ORB_ELEMENT_DESCRIPTION" == null || "$ORB_ELEMENT_DESCRIPTION" == '""' ]]; then
echo
echo "Orb component ${i} is missing a description"
echo "Orb components are not invalid without descriptions, but these descriptions appear on the Orb Registry for documentation and provide a better experience."
Expand All @@ -30,7 +30,7 @@ setup() {
}

@test "RC003: All production-ready orbs should contain at least one usage example." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC003" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC003" ]]; then
skip
fi
ORB_ELEMENT_EXAMPLE_COUNT=$(find ${REVIEW_TEST_DIR}src/examples/*.yml -type f 2>/dev/null | wc -l | xargs)
Expand All @@ -43,10 +43,10 @@ setup() {
}

@test "RC004: Usage example names shoud be descriptive." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC004" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC004" ]]; then
skip
fi
for i in $(find ${REVIEW_TEST_DIR}/src/examples/*.yml -type f >/dev/null 2>&1); do
for i in $(find "${REVIEW_TEST_DIR}/src/examples/*.yml" -type f >/dev/null 2>&1); do
if [[ $i =~ "example" ]]; then
echo
echo "Usage example file name ${i} contains the word 'example'."
Expand All @@ -57,11 +57,11 @@ setup() {
}

@test "RC005: Write a detailed orb description." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC005" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC005" ]]; then
skip
fi
ORB_ELEMENT_DESCRIPTION=$(cat ${REVIEW_TEST_DIR}src/@orb.yml | yq '.description')
if [[ ${#ORB_ELEMENT_DESCRIPTION} -lt 64 ]]; then
ORB_ELEMENT_DESCRIPTION=$(yq '.description' "${REVIEW_TEST_DIR}src/@orb.yml")
if [[ "${#ORB_ELEMENT_DESCRIPTION}" -lt 64 ]]; then
echo
echo "Orb description appears short (under 64 characters)."
echo "Update the description in ${REVIEW_TEST_DIR}src/@orb.yml to provide a detailed description of the orb."
Expand All @@ -71,12 +71,12 @@ setup() {
}

@test "RC006: Source URL should be valid." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC006" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC006" ]]; then
skip
fi
SOURCE_URL=$(cat ${REVIEW_TEST_DIR}/src/@orb.yml | yq '.display.source_url')
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --retry 5 --retry-delay 5 $SOURCE_URL)
if [[ $HTTP_RESPONSE -ne 200 ]]; then
SOURCE_URL=$(yq '.display.source_url' "${REVIEW_TEST_DIR}/src/@orb.yml")
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --retry 5 --retry-delay 5 "$SOURCE_URL")
if [[ "$HTTP_RESPONSE" -ne 200 ]]; then
echo
echo "Source URL: \"$SOURCE_URL\" is not reachable."
echo "Check the Source URL for this orb."
Expand All @@ -85,12 +85,12 @@ setup() {
}

@test "RC007: Home URL should be valid." {
HOME_URL=$(cat ${REVIEW_TEST_DIR}/src/@orb.yml | yq '.display.home_url')
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC007" || "$HOME_URL" == "null" ]]; then
HOME_URL=$(yq '.display.home_url' "${REVIEW_TEST_DIR}/src/@orb.yml")
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC007" || "$HOME_URL" == "null" ]]; then
skip
fi
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --retry 5 --retry-delay 5 $HOME_URL)
if [[ $HTTP_RESPONSE -ne 200 ]]; then
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --retry 5 --retry-delay 5 "$HOME_URL")
if [[ "$HTTP_RESPONSE" -ne 200 ]]; then
echo
echo "Home URL: \"$HOME_URL\" is not reachable."
echo "Check the Home URL for this orb."
Expand All @@ -99,28 +99,39 @@ setup() {
}

@test "RC008: All Run steps should contain a name." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC008" ]]; then
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC008" ]]; then
skip
fi
ERROR_COUNT=0
for i in $(find ${REVIEW_TEST_DIR}src/jobs ${REVIEW_TEST_DIR}src/commands -name "*.yml" 2>/dev/null); do
ORB_COMPONENT_STEPS_COUNT=$(cat $i | yq '[.steps.[] | .run] | length - 1')
for j in $(seq 0 $ORB_COMPONENT_STEPS_COUNT); do

ORB_COMPONENT_STEP=$(cat $i | yq "[.steps.[] | .run][$j]")
ORB_COMPONENT_LINE_NUMBER=$(cat $i | yq "[.steps.[] | .run][$j] | line")
ORB_COMPONENT_STEP_NAME=$(cat $i | yq '.steps[$j].run.name')
if [[ $ORB_COMPONENT_STEP_NAME == null || $ORB_COMPONENT_STEP_NAME == '""' ]]; then
for i in $(find "${REVIEW_TEST_DIR}src/jobs" "${REVIEW_TEST_DIR}src/commands" -name "*.yml" 2>/dev/null); do
ORB_COMPONENT_STEPS_COUNT=$(yq '[.steps.[] | .run] | length' "$i")
j=0
while [ "$j" -lt "$ORB_COMPONENT_STEPS_COUNT" ]; do
ORB_COMPONENT_STEP=$(yq "[.steps.[] | .run][$j]" "$i")
ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq -o=json '.' | jq 'type')
ORB_COMPONENT_LINE_NUMBER=$(yq "[.steps.[] | .run][$j] | line" "$i")
ORB_COMPONENT_STEP_NAME=$(yq "[.steps.[] | .run][$j] | .name" "$i")
if [[ "$ORB_COMPONENT_STEP_TYPE" == '"string"' ]]; then
echo "File: \"${i}\""
echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}"
echo "It appears this 'run' step is using 'string' formatting."
echo "Consider converting this step into an object with a \"name\" and \"command\" property."
echo ---
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 ---
cat $i | yq "[.steps.[] | .run][$j]"
yq "[.steps.[] | .run][$j]" "$i"
echo ---
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
j=$((j + 1))
done
done
if [[ $ERROR_COUNT -gt 0 ]]; then
if [[ "$ERROR_COUNT" -gt 0 ]]; then
echo
echo "Components were found to contain \"run\" steps without a name."
echo "Steps are not invalid without names, but the default used will be the command code, which can be long and confusing."
Expand All @@ -130,30 +141,45 @@ setup() {
fi
}

@test "RC009: All Run step's commands should be imported." {
if [[ " ${SKIPPED_REVIEW_CHECKS[@]} " =~ "RC009" ]]; then
@test "RC009: Complex Run step's commands should be imported." {
if [[ "${SKIPPED_REVIEW_CHECKS[*]}" =~ "RC009" ]]; then
skip
fi
ERROR_COUNT=0
for i in $(find ${REVIEW_TEST_DIR}src/jobs ${REVIEW_TEST_DIR}src/commands -name "*.yml" 2>/dev/null); do
ORB_COMPONENT_STEPS_COUNT=$(cat $i | yq '[.steps.[] | .run] | length - 1')
for j in $(seq 0 $ORB_COMPONENT_STEPS_COUNT); do
ORB_COMPONENT_STEP=$(cat $i | yq "[.steps.[] | .run][$j]")
ORB_COMPONENT_LINE_NUMBER=$(cat $i | yq "[.steps.[] | .run][$j] | line")
ORB_COMPONENT_STEP_COMMAND=$(cat $i | yq '.steps[$j].run.command')
if [[ ! $ORB_COMPONENT_STEP_COMMAND =~ \<\<include\(* ]]; then
ORB_COMPONENT_STEPS_COUNT=$(yq '[.steps.[] | .run] | length' "$i")
j=0
while [ "$j" -lt "$ORB_COMPONENT_STEPS_COUNT" ]; do
ORB_COMPONENT_STEP=$(yq "[.steps.[] | .run][$j]" "$i")
ORB_COMPONENT_STEP_TYPE=$(echo "$ORB_COMPONENT_STEP" | yq -o=json '.' | jq 'type')
ORB_COMPONENT_LINE_NUMBER=$(yq "[.steps.[] | .run][$j] | line" "$i")
ORB_COMPONENT_STEP_COMMAND=$(yq "[.steps.[] | .run][$j] | .command" "$i")
if [[ "$ORB_COMPONENT_STEP_TYPE" == '"string"' ]]; then
echo "File: \"${i}\""
echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}"
echo "It appears this 'run' step is using 'string' formatting."
echo "Consider converting this step into an object with a \"name\" and \"command\" property."
echo ---
echo $ORB_COMPONENT_STEP_COMMAND
echo "$ORB_COMPONENT_STEP"
echo ---
ERROR_COUNT=$((ERROR_COUNT + 1))
elif [[ "${#ORB_COMPONENT_STEP_COMMAND}" -gt 64 ]]; then
if [[ ! "$ORB_COMPONENT_STEP_COMMAND" =~ \<\<include\(* ]]; then
echo "File: \"${i}\""
echo "Line number: ${ORB_COMPONENT_LINE_NUMBER}"
echo "This command appears longer than 64 characters. Consider using the 'include' syntax."
echo ---
echo "$ORB_COMPONENT_STEP_COMMAND"
echo ---
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
fi
j=$((j + 1))
done
done
if [[ $ERROR_COUNT -gt 0 ]]; then
if [[ "$ERROR_COUNT" -gt 0 ]]; then
echo
echo "Components were found to contain \"run\" steps with a command that is not imported."
echo "Components were found to contain \"run\" steps with a long command that is not imported."
echo "Did you know you can write your shell scripts and other commands in external files and import them here?"
echo "Writing your scripts externally will allow you to take advantage of syntax hilighting and avoid mixing code and markup."
echo "https://circleci.com/docs/2.0/using-orbs/#file-include-syntax"
Expand Down

0 comments on commit 3e07cc9

Please sign in to comment.