Skip to content

Commit 79de773

Browse files
authored
Merge pull request #81 from buildkite-plugins/tags
Introduce `tags` option
2 parents cc06d4a + b2146d3 commit 79de773

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ These are all the options available to configure this plugin's behaviour.
1212

1313
One or more patterns of files to upload to Test Analytics, relative to the root of the searching path (`./` by default). May contain `*` to match any number of characters of any type (unlike shell expansions, it will match `/` and `.` if necessary). Can be either a single pattern in a string or any number of them in an array.
1414

15-
#### `format` (string)
15+
#### `format` (string)
1616

1717
Format of the file.
1818

1919
Only the following values are allowed: `junit`, `json`
2020

2121
### Optional
2222

23-
#### `api-token-env-name` (string)
23+
#### `api-token-env-name` (string)
2424

2525
Name of the environment variable that contains the Test Analytics API token.
2626

@@ -45,7 +45,7 @@ For example:
4545

4646
Important: you may have to be careful to escape special characters like `$` during pipeline upload
4747

48-
#### `debug` (boolean)
48+
#### `debug` (boolean)
4949

5050
Print debug information to the build output.
5151

@@ -78,7 +78,19 @@ 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-
#### `timeout`(number)
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+
93+
#### `timeout` (number)
8294

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

@@ -115,7 +127,7 @@ steps:
115127
- label: "🔨 Test"
116128
command: "make test"
117129
plugins:
118-
- test-collector#v1.10.1:
130+
- test-collector#v1.10.2:
119131
files: "test/junit-*.xml"
120132
format: "junit"
121133
```
@@ -129,7 +141,7 @@ steps:
129141
- label: "🔨 Test"
130142
command: "make test"
131143
plugins:
132-
- test-collector#v1.10.1:
144+
- test-collector#v1.10.2:
133145
files:
134146
- "test-data-*.json"
135147
format: "json"
@@ -151,7 +163,7 @@ steps:
151163
- label: "🔍 Test Analytics"
152164
command: buildkite-agent artifact download "tests-*.xml" .
153165
plugins:
154-
- test-collector#v1.10.1:
166+
- test-collector#v1.10.2:
155167
files: "tests-*.xml"
156168
format: "junit"
157169
```
@@ -165,7 +177,7 @@ steps:
165177
- label: "🔨 Test"
166178
command: "make test"
167179
plugins:
168-
- test-collector#v1.10.1:
180+
- test-collector#v1.10.2:
169181
files: "test-data-*.json"
170182
format: "json"
171183
branches: "-qa$"
@@ -178,7 +190,7 @@ steps:
178190
- label: "🔨 Test"
179191
command: "make test"
180192
plugins:
181-
- test-collector#v1.10.1:
193+
- test-collector#v1.10.2:
182194
files: "test-data-*.json"
183195
format: "json"
184196
exclude-branches: "^legacy$"
@@ -191,7 +203,7 @@ steps:
191203
- label: "🔨 Test"
192204
command: "make test"
193205
plugins:
194-
- test-collector#v1.10.1:
206+
- test-collector#v1.10.2:
195207
files: "test-data-*.json"
196208
format: "json"
197209
branches: "^stage-"

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)