diff --git a/README.md b/README.md index 6471229..db0b0cd 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,14 @@ The number of concurrent file uploads to perform to the Buildkite analytics API. Default value: `1` +## Requirements + +This plugin requires `jq` for parsing JSON data. If `jq` is not found on the agent, `sed` will be used as a fallback. Ensure that `sed` is also available to handle scenarios where `jq` cannot be used. + +## Fallback Behavior + +If `jq` is unavailable, the plugin will attempt to parse the results using `sed`. This ensures that the plugin remains functional even if the preferred JSON parser is missing. + ## Examples ### Upload a JUnit file diff --git a/hooks/pre-exit b/hooks/pre-exit index 08f97ad..de8bf7f 100755 --- a/hooks/pre-exit +++ b/hooks/pre-exit @@ -77,7 +77,7 @@ save-report-url() { if which jq >/dev/null; then echo "Using jq to parse the report URL" report_url=$(jq -r '.run_url' "${json_file}" 2>&1) # Capture stderr for error reporting - if [[ "$report_url" == "null" || "$report_url" == "" ]]; then + if [[ "$report_url" == "null" || "$report_url" == "" || "$report_url" =~ "parse error" ]]; then echo "jq parsing failed with the message: $report_url" echo "Contents of $json_file:" cat "$json_file" @@ -86,16 +86,23 @@ save-report-url() { else echo "jq not installed, attempting to parse with sed" report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}") + if [[ "$report_url" == "null" || "$report_url" == "" ]]; then + echo "sed parsing failed, no valid URL extracted." + echo "Contents of $json_file:" + cat "$json_file" + return + fi fi - if [[ "$report_url" == "null" || "$report_url" == "" ]]; then - echo "Could not get the tests report URL from $json_file. 'run_url' property not found." + if [ -z "$report_url" ]; then + echo "No report URL found or extracted. Unable to save." return fi echo "$report_url" >> "$REPORT_URLS_FILE" } + # Uploads files to the Test Analytics API # # Upload failures should not fail the build, and should have a sensible timeout, @@ -245,4 +252,4 @@ else fi if [ "$ANNOTATE" != "false" ]; then annotation-link "${REPORT_URLS_FILE}" -fi +fi \ No newline at end of file diff --git a/tests/pre-exit-report-link.bats b/tests/pre-exit-report-link.bats index 9eb05eb..b9b295b 100644 --- a/tests/pre-exit-report-link.bats +++ b/tests/pre-exit-report-link.bats @@ -176,3 +176,20 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \* unstub curl } + +@test "Fallback to sed when jq is missing" { + stub which "jq : exit 1" + stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'" + stub buildkite-agent "annotate --style info --context \"test-collector\" --append : echo 'annotation success'" + + run "$PWD/hooks/pre-exit" + + assert_success + assert_output --partial "jq not installed, attempting to parse with sed" + assert_output --partial "curl success" + assert_output --partial "annotation success" + + unstub which + unstub curl +} +