Skip to content

Commit b2146d3

Browse files
committed
Allow tags to be specified on the upload
These tags will propagate onto all the executions within the upload.
1 parent c8ef9e1 commit b2146d3

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ This should allow you to use a special exit code to soft-fail on when no files t
7878

7979
Default value: `1`
8080

81+
#### `tags` (array of strings)
82+
83+
A list of tags to apply to all test results in the upload, in the format `key=value`.
84+
85+
For example:
86+
87+
```yaml
88+
tags:
89+
- "arch=arm64"
90+
- "language.version=1.2.3"
91+
```
92+
8193
#### `timeout` (number)
8294

8395
Maximum number of seconds to wait for each file to upload before timing out.

hooks/pre-exit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ upload() {
131131
"--form" "run_env[collector]=test-collector-buildkite-plugin"
132132
)
133133

134+
if [ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS:-}" ]; then
135+
curl_args+=("--form" "$(build_tag_form_field "${BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS}")")
136+
elif [ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS_0:-}" ]; then
137+
prefix="BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS"
138+
parameter="${prefix}_0"
139+
if [ -n "${!parameter:-}" ]; then
140+
i=0
141+
parameter="${prefix}_${i}"
142+
while [ -n "${!parameter:-}" ]; do
143+
curl_args+=("--form" "$(build_tag_form_field "${!parameter}")")
144+
i=$((i+1))
145+
parameter="${prefix}_${i}"
146+
done
147+
fi
148+
fi
149+
134150
if [[ "$DEBUG" == "true" ]]; then
135151
curl_args+=("--form" "run_env[debug]=\"$DEBUG\"")
136152
fi
@@ -154,6 +170,14 @@ upload() {
154170
curl "${curl_args[@]}" -H @<(printf 'Authorization: Token token=\"%s\"\n' "${TOKEN_VALUE}")
155171
}
156172

173+
# input: "key=value"
174+
# output: "tags[key]=value"
175+
build_tag_form_field() {
176+
local key="${1%%=*}" # longest matching trailing pattern deleted; keep the part before the first "="
177+
local value="${1#*=}" # shortest matching leading pattern deleted; keep the part after the first "="
178+
echo "tags[$key]=$value"
179+
}
180+
157181
# Runs the whole plugin logic for a particular find pattern
158182
find_and_upload() {
159183
FILES_PATTERN="$1"

tests/pre-exit-success.bats

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,38 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \*
135135
unstub curl
136136
}
137137

138+
@test "Single tag from string" {
139+
export BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS="hello=world"
140+
141+
stub curl \
142+
"-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} --form tags[hello]=world \* -H \* : echo 'curl success'"
143+
144+
run "$PWD/hooks/pre-exit"
145+
146+
assert_success
147+
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
148+
assert_output --partial "curl success"
149+
150+
unstub curl
151+
}
152+
153+
@test "Multiple tags from array" {
154+
export BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS_0="hello=world"
155+
export BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS_1="foo=bar=baz"
156+
unset BUILDKITE_PLUGIN_TEST_COLLECTOR_TAGS
157+
158+
stub curl \
159+
"-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} --form tags[hello]=world --form tags[foo]=bar=baz \* -H \* : echo 'curl success'"
160+
161+
run "$PWD/hooks/pre-exit"
162+
163+
assert_success
164+
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
165+
assert_output --partial "curl success"
166+
167+
unstub curl
168+
}
169+
138170
@test "Debug true prints the curl info w/o token" {
139171
export BUILDKITE_PLUGIN_TEST_COLLECTOR_DEBUG="true"
140172

0 commit comments

Comments
 (0)