Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanna Lisetska authored and Ivanna Lisetska committed May 10, 2024
1 parent 2c1b285 commit c163479
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -245,4 +252,4 @@ else
fi
if [ "$ANNOTATE" != "false" ]; then
annotation-link "${REPORT_URLS_FILE}"
fi
fi
17 changes: 17 additions & 0 deletions tests/pre-exit-report-link.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c163479

Please sign in to comment.