Skip to content

Commit c163479

Browse files
Ivanna LisetskaIvanna Lisetska
authored andcommitted
resolved comments
1 parent 2c1b285 commit c163479

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ The number of concurrent file uploads to perform to the Buildkite analytics API.
9696

9797
Default value: `1`
9898

99+
## Requirements
100+
101+
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.
102+
103+
## Fallback Behavior
104+
105+
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.
106+
99107
## Examples
100108

101109
### Upload a JUnit file

hooks/pre-exit

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ save-report-url() {
7777
if which jq >/dev/null; then
7878
echo "Using jq to parse the report URL"
7979
report_url=$(jq -r '.run_url' "${json_file}" 2>&1) # Capture stderr for error reporting
80-
if [[ "$report_url" == "null" || "$report_url" == "" ]]; then
80+
if [[ "$report_url" == "null" || "$report_url" == "" || "$report_url" =~ "parse error" ]]; then
8181
echo "jq parsing failed with the message: $report_url"
8282
echo "Contents of $json_file:"
8383
cat "$json_file"
@@ -86,16 +86,23 @@ save-report-url() {
8686
else
8787
echo "jq not installed, attempting to parse with sed"
8888
report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}")
89+
if [[ "$report_url" == "null" || "$report_url" == "" ]]; then
90+
echo "sed parsing failed, no valid URL extracted."
91+
echo "Contents of $json_file:"
92+
cat "$json_file"
93+
return
94+
fi
8995
fi
9096

91-
if [[ "$report_url" == "null" || "$report_url" == "" ]]; then
92-
echo "Could not get the tests report URL from $json_file. 'run_url' property not found."
97+
if [ -z "$report_url" ]; then
98+
echo "No report URL found or extracted. Unable to save."
9399
return
94100
fi
95101

96102
echo "$report_url" >> "$REPORT_URLS_FILE"
97103
}
98104

105+
99106
# Uploads files to the Test Analytics API
100107
#
101108
# Upload failures should not fail the build, and should have a sensible timeout,
@@ -245,4 +252,4 @@ else
245252
fi
246253
if [ "$ANNOTATE" != "false" ]; then
247254
annotation-link "${REPORT_URLS_FILE}"
248-
fi
255+
fi

tests/pre-exit-report-link.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,20 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
176176

177177
unstub curl
178178
}
179+
180+
@test "Fallback to sed when jq is missing" {
181+
stub which "jq : exit 1"
182+
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
183+
stub buildkite-agent "annotate --style info --context \"test-collector\" --append : echo 'annotation success'"
184+
185+
run "$PWD/hooks/pre-exit"
186+
187+
assert_success
188+
assert_output --partial "jq not installed, attempting to parse with sed"
189+
assert_output --partial "curl success"
190+
assert_output --partial "annotation success"
191+
192+
unstub which
193+
unstub curl
194+
}
195+

0 commit comments

Comments
 (0)