Skip to content

Commit 8ebff14

Browse files
Merge pull request #74 from buildkite-plugins/ivannalisetska/SUP-1821
Test Collector Plugin Issue - error parsing report url
2 parents 6d4447d + c163479 commit 8ebff14

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
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: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,36 @@ save-report-url() {
7373
echo "Could not get the tests report URL from $json_file. File not found."
7474
return
7575
fi
76-
# Could be easier with jq, but we don't want to require it
76+
7777
if which jq >/dev/null; then
78-
echo "Using jq to parse the report URL"
79-
report_url=$(jq -r '.run_url' "${json_file}")
78+
echo "Using jq to parse the report URL"
79+
report_url=$(jq -r '.run_url' "${json_file}" 2>&1) # Capture stderr for error reporting
80+
if [[ "$report_url" == "null" || "$report_url" == "" || "$report_url" =~ "parse error" ]]; then
81+
echo "jq parsing failed with the message: $report_url"
82+
echo "Contents of $json_file:"
83+
cat "$json_file"
84+
return
85+
fi
8086
else
81-
echo "Not using jq to parse the report URL"
82-
report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}")
87+
echo "jq not installed, attempting to parse with sed"
88+
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
8395
fi
8496

85-
if [ "$report_url" == "null" ]; then
86-
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."
8799
return
88100
fi
89101

90102
echo "$report_url" >> "$REPORT_URLS_FILE"
91103
}
92104

105+
93106
# Uploads files to the Test Analytics API
94107
#
95108
# Upload failures should not fail the build, and should have a sensible timeout,
@@ -239,4 +252,4 @@ else
239252
fi
240253
if [ "$ANNOTATE" != "false" ]; then
241254
annotation-link "${REPORT_URLS_FILE}"
242-
fi
255+
fi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id": "2","run_id": "2","queued": 130,"skipped": 0,"errors": []}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id": "4","run_id": "4","queued": 150,"skipped": 0,"errors": [],"run_url": null}

tests/pre-exit-report-link.bats

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bats
22

33
# To debug stubs, uncomment these lines:
4-
# export CURL_STUB_DEBUG=/dev/tty
5-
# export GIT_STUB_DEBUG=/dev/tty
4+
#export CURL_STUB_DEBUG=/dev/tty
5+
#export GIT_STUB_DEBUG=/dev/tty
66

77
setup() {
88
load "$BATS_PLUGIN_PATH/load.bash"
@@ -53,7 +53,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
5353
assert_success
5454
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
5555
assert_output --partial "curl success"
56-
assert_output --partial "Not using jq"
56+
assert_output --partial "jq not installed, attempting to parse with sed"
5757
assert_output --partial "annotation success"
5858

5959
unstub which
@@ -117,6 +117,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
117117
unstub jq
118118
unstub curl
119119
}
120+
120121
@test "Annotates report link absorbs empty file error" {
121122
export CURL_RESP_FILE="response.json"
122123
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
@@ -141,8 +142,54 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
141142
assert_success
142143
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
143144
assert_output --partial "curl success"
144-
assert_output --partial "'run_url' property not found"
145+
assert_output --partial "jq parsing failed with the message: "
146+
assert_output --partial "Contents of ./tests/fixtures/response_no_url.json:"
147+
assert_output --partial "There are no report URLs to annotate"
148+
149+
unstub curl
150+
}
151+
152+
@test "No annotation when 'run_url' property is missing in JSON response" {
153+
export CURL_RESP_FILE="./tests/fixtures/response_missing_url.json"
154+
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
155+
156+
run "$PWD/hooks/pre-exit"
157+
158+
assert_success
159+
assert_output --partial "jq parsing failed with the message:"
160+
assert_output --partial "Contents of ./tests/fixtures/response_missing_url.json:"
161+
assert_output --partial "There are no report URLs to annotate"
162+
163+
unstub curl
164+
}
165+
166+
@test "No annotation when 'run_url' is null in JSON response" {
167+
export CURL_RESP_FILE="./tests/fixtures/response_null_url.json"
168+
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'"
169+
170+
run "$PWD/hooks/pre-exit"
171+
172+
assert_success
173+
assert_output --partial "jq parsing failed with the message: null"
174+
assert_output --partial "Contents of ./tests/fixtures/response_null_url.json:"
145175
assert_output --partial "There are no report URLs to annotate"
146176

147177
unstub curl
148178
}
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)