Skip to content

Commit 32f2c5c

Browse files
Create plugin to upload test results
1 parent 54afba4 commit 32f2c5c

File tree

6 files changed

+82
-258
lines changed

6 files changed

+82
-258
lines changed

LICENSE

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
MIT License
22

3-
Copyright (c) 2021 Kim Dowling
3+
Copyright (c) 2021
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
1111

1212
The above copyright notice and this permission notice shall be included in all
1313
copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+2-50
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,6 @@
1-
# Annotate From File Buildkite Plugin
1+
# Test Analytics from file Plugin
22

3-
Annotates the build with the contents of a file
4-
5-
This is intended to be used with other plugins like docker-compose.
6-
7-
## Example
8-
9-
Add the following to your `pipeline.yml`:
10-
11-
```yml
12-
steps:
13-
- command: ls > /app/output/output.md
14-
plugins:
15-
- docker-compose#v3.7.0:
16-
run: app
17-
volumes:
18-
- "./output:/app/output"
19-
- phcyso/annotate-from-file#v1.0.0:
20-
path: "./output/output.md"
21-
```
22-
23-
## Configuration
24-
25-
### `path` (Required, string)
26-
27-
The path to the file to show as an annotation. Each files that match this
28-
pattern will create an annotation.
29-
30-
### `must_exist` (Optional, boolean, default: false)
31-
32-
If `must_exist` is set to true then the plugin will exit non zero if the files
33-
given in `path` does not exist
34-
35-
### `style` (Optional, string, default: "info")
36-
37-
One of the allowed annotation
38-
[styles](https://buildkite.com/docs/agent/v3/cli-annotate#annotation-styles)
39-
40-
### `context` (Optional, string, default: it will use the default context)
41-
42-
The context of the annotation used to differentiate this annotation from others,
43-
if no defined it will use default context.
44-
45-
Use `file_name`as context value if you want to specify context value same as
46-
file name.
47-
48-
### `append` (Optional, boolean, default: false)
49-
50-
Append to the body of an existing annotation depending on the given annotation
51-
context.
3+
< WIP >
524

535
## Developing
546

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
lint:
33
image: buildkite/plugin-linter
4-
command: ["--id", "phcyso/annotate-from-file"]
4+
command: ["--id", "sergiocabreraoria/test-analytics-from-file"]
55
volumes:
66
- ".:/plugin:ro"
77
tests:

hooks/post-command

+54-35
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,64 @@
11
#!/bin/bash
2+
23
# set -euo pipefail
34

4-
FILE_PATH="${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH}"
5-
STYLE="${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE:-"info"}"
6-
MUST_EXIST="${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST:-"false"}"
7-
CONTEXT="${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT:-""}"
8-
APPEND="${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND:-"false"}"
5+
TEST_RESULTS_PATH="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_TEST_RESULTS_PATH}"
6+
TEST_RESULTS_FORMAT="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_TEST_RESULTS_FORMAT}"
7+
TEST_RUN_LINK="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_TEST_RUN_LINK:-false}"
8+
CONTEXT="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_ANNOTATION_CONTEXT:-bkta}"
9+
STYLE="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_ANNOTATION_STYLE:-info}"
10+
APPEND_FLAG="${BUILDKITE_PLUGIN_TEST_ANALYTICS_FROM_FILE_ANNOTATION_APPEND:-false}"
11+
12+
bk_annotate_test_analytic_link(){
13+
echo "--- :spiral_note_pad: Annotate Test Analytics link"
14+
test_analytics_run_url=$(echo "$1" | grep -o '"run_url": *"[^"]*"' | cut -d'"' -f4)
15+
echo "Test Analytic url: ${test_analytics_run_url}"
916

17+
ANNOTATION="
18+
<p>See <a href=\"$test_analytics_run_url\">the global test run</a> for more information.</p>
19+
<p>The test run includes results from <a href=\"$BUILDKITE_JOB_URL\">global run job</a></p>
20+
"
21+
buildkite-agent annotate "$ANNOTATION" --style "$STYLE" --context "$CONTEXT" $APPEND_FLAG
22+
}
1023

11-
echo "--- :spiral_note_pad: Annotating build from file"
12-
echo "FILE_PATH:$FILE_PATH"
1324

14-
if find "$(dirname $FILE_PATH)" -name "$(basename $FILE_PATH)*" -print -quit | grep .; then
15-
echo "Annotation file exists, continuing"
16-
else
17-
echo "Annotation file does not exist, Exiting"
18-
[ "${MUST_EXIST}" == "true" ] && exit 1 || exit 0
25+
if [[ -n "$BUILDKITE_ANALYTICS_TOKEN" ]]; then
26+
echo "Missing Buildkite Test Analytic Token at BUILDKITE_ANALYTICS_TOKEN env variable"
27+
exit 1
1928
fi
2029

21-
# Check style values https://buildkite.com/docs/agent/v3/cli-annotate#style
22-
if [[ ! $STYLE =~ ^(success|info|warning|error)$ ]]; then
23-
echo "'$STYLE' is a non valid style."
30+
if [[ -n "$TEST_RESULTS_PATH" ]]; then
31+
echo "'TEST_RESULTS_PATH' not received"
2432
exit 1
2533
fi
26-
[[ "$APPEND" == "true" ]] && APPEND_FLAG="--append" || APPEND_FLAG=""
27-
[[ -n "$CONTEXT" ]] && CONTEXT_FLAG="--context $CONTEXT" || CONTEXT_FLAG=""
28-
29-
echo "FILE_PATH: $FILE_PATH"
30-
echo "STYLE: $STYLE"
31-
echo "MUST_EXIST: $MUST_EXIST"
32-
echo "CONTEXT_FLAG: $CONTEXT_FLAG"
33-
echo "APPEND_FLAG: $APPEND_FLAG"
34-
35-
# Use find to search for all files that match the pattern specified by FILE_PATH
36-
ANNOTATION_COUNTER=0
37-
38-
find "$(dirname $FILE_PATH)" -name "$(basename $FILE_PATH)*" -type f | while read -r file; do
39-
((ANNOTATION_COUNTER++))
40-
[[ "$CONTEXT" == "file_name" ]] && CONTEXT_FLAG="--context $(basename $file)"
41-
cat "$file" | buildkite-agent annotate --style "$STYLE" $CONTEXT_FLAG $APPEND_FLAG
42-
echo "$ANNOTATION_COUNTER Annotation created for $FILE_PATH file $CONTEXT_FLAG"
43-
done
44-
45-
echo "Annotation created - Done"
34+
35+
if [[ ! $TEST_RESULTS_FORMAT =~ ^(json|junit)$ ]]; then
36+
echo "'$TEST_RESULTS_FORMAT' is a non valid format <json | junit>."
37+
exit 1
38+
fi
39+
40+
if [[ ! $TEST_RUN_LINK =~ ^(true|false)$ ]]; then
41+
echo "'$TEST_RUN_LINK' is a non valid format <true | false"
42+
exit 1
43+
fi
44+
45+
BK_RUN="${BUILDKITE_TRIGGERED_FROM_BUILD_PIPELINE_SLUG:-BUILDKITE_PIPELINE_SLUG}_${BUILDKITE_TRIGGERED_FROM_BUILD_NUMBER:-BUILDKITE_BUILD_NUMBER}"
46+
47+
test_analytics_response=$(curl -X POST \
48+
-H "Authorization: Token token=\"${BK_TEST_ANALYTIC_TOKEN}\"" \
49+
-F "data=@$TEST_RESULTS_PATH" \
50+
-F "format=$TEST_RESULTS_FORMAT" \
51+
-F "run_env[CI]=buildkite" \
52+
-F "run_env[key]=${BUILDKITE_TRIGGERED_FROM_BUILD_ID:-$BUILDKITE_BUILD_ID}" \
53+
-F "run_env[url]=$BUILDKITE_JOB_URL" \
54+
-F "run_env[branch]=$BUILDKITE_BRANCH" \
55+
-F "run_env[commit_sha]=$BUILDKITE_COMMIT" \
56+
-F "run_env[number]=${BUILDKITE_TRIGGERED_FROM_BUILD_NUMBER:-$BUILDKITE_BUILD_NUMBER}" \
57+
-F "run_env[job_id]=$BUILDKITE_JOB_ID" \
58+
-F "run_env[message]=$BK_RUN" \
59+
https://analytics-api.buildkite.com/v1/uploads
60+
)
61+
62+
[[ "$TEST_RUN_LINK" == "true" ]] && bk_annotate_test_analytic_link "$test_analytics_response"
63+
64+
echo "Test results sent to Buildkite Test Analytics - Done"

plugin.yml

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
name: Annotate From file
2-
description: Annotates the build with the contents of a file
3-
author: https://github.com/phcyso
1+
name: Test Analytics From File
2+
description:
3+
Send tests results to Buildkite Test Analytics and create a shourcut as
4+
annotation
5+
author: https://github.com/sergiocabreraoria
46
requirements: []
57
configuration:
68
properties:
7-
path:
9+
test_results_path:
810
type: string
9-
style:
11+
test_results_format:
1012
type: string
11-
must_exist:
13+
test_run_link:
1214
type: boolean
13-
context:
15+
annotation_context:
1416
type: string
15-
append:
17+
annotation_style:
18+
type: string
19+
annotation_append:
1620
type: boolean
1721
additionalProperties: false

tests/post-command.bats

+1-151
Original file line numberDiff line numberDiff line change
@@ -8,155 +8,5 @@ setup() {
88
# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty
99
}
1010

11-
@test "Creates an annotation from a given file" {
12-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="test_file.test"
13-
14-
echo "hello world" >> test_file.test
15-
16-
stub buildkite-agent 'annotate --style "info" : echo Annotation created'
17-
18-
run "$PWD/hooks/post-command"
19-
20-
unstub buildkite-agent
21-
rm test_file.test
22-
23-
assert_output --partial "Annotation created"
24-
assert_success
25-
}
26-
27-
@test "Exits cleanly when file doesnt exist " {
28-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="NOFILE"
29-
30-
run "$PWD/hooks/post-command"
31-
32-
assert_output --partial "Annotation file does not exist, Exiting"
33-
assert_success
34-
}
35-
36-
@test "Exits with 1 when file doesnt exist and must_exist is true " {
37-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="*999.bats"
38-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST="true"
39-
40-
run "$PWD/hooks/post-command"
41-
42-
assert_output --partial "Annotation file does not exist, Exiting"
43-
assert_failure
44-
}
45-
46-
@test "Valid case with append and context" {
47-
test_file="test_file_context.test"
48-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="${test_file}"
49-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE=success
50-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST=false
51-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT=test-context
52-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND=true
53-
54-
echo "hello world" >> $test_file
55-
56-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT" --append : echo Annotation created'
57-
58-
run "$PWD/hooks/post-command"
59-
60-
unstub buildkite-agent
61-
rm "$test_file"
62-
63-
assert_output --partial "Annotation created"
64-
assert_success
65-
}
66-
67-
@test "Invalid style case" {
68-
test_file="test_file_context.test"
69-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="${test_file}"
70-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE=invalid_style
71-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST=false
72-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT=test-context
73-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND=false
74-
75-
echo "hello world" >> $test_file
76-
77-
run "$PWD/hooks/post-command"
78-
rm "$test_file"
79-
80-
assert_output --partial "is a non valid style"
81-
assert_failure
82-
}
83-
84-
@test "Annotation Append without context " {
85-
test_file="test_file_append.test"
86-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="${test_file}"
87-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE=info
88-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST=false
89-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT=""
90-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND=true
91-
92-
echo "hello world" >> $test_file
93-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --append : echo Annotation created'
94-
95-
run "$PWD/hooks/post-command"
96-
97-
unstub buildkite-agent
98-
rm "$test_file"
99-
100-
assert_output --partial "Annotation created"
101-
assert_success
102-
}
103-
104-
@test "Annotation Append with 'file_name' as context' " {
105-
test_file="test_file_context_file_name.test"
106-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="${test_file}"
107-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE=info
108-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST=false
109-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT="file_name"
110-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND=true
111-
112-
echo "hello world" >> $test_file
113-
echo "hello world" >> "diff_${test_file}_asdfa.txt"
114-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH" --append : echo Annotation created with context $BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH'
115-
116-
run "$PWD/hooks/post-command"
117-
118-
unstub buildkite-agent
119-
rm "$test_file"
120-
rm "diff_${test_file}_asdfa.txt"
121-
122-
assert_output --partial "Annotation created with context $BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH"
123-
assert_success
124-
}
125-
126-
@test "Multiple files: Annotation Append with 'file_name' as context' " {
127-
test_file="test_file_context_file_name.test"
128-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH="${test_file}"
129-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE=info
130-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_MUST_EXIST=false
131-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_CONTEXT="file_name"
132-
export BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_APPEND=true
133-
134-
echo "hello world" >> "${test_file}_1.txt"
135-
echo "hello world" >> "${test_file}_2.txt"
136-
echo "hello world" >> "${test_file}_3.txt"
137-
echo "hello world" >> "${test_file}_4.txt"
138-
echo "hello world" >> "Not_include_${test_file}_5.txt"
139-
140-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH}_1.txt" --append : echo Annotation created'
141-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH}_2.txt" --append : echo Annotation created'
142-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH}_3.txt" --append : echo Annotation created'
143-
stub buildkite-agent 'annotate --style "$BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_STYLE" --context "${BUILDKITE_PLUGIN_ANNOTATE_FROM_FILE_PATH}_4.txt" --append : echo Annotation created'
144-
145-
run "$PWD/hooks/post-command"
146-
147-
unstub buildkite-agent
148-
149-
rm "${test_file}_1.txt"
150-
rm "${test_file}_2.txt"
151-
rm "${test_file}_3.txt"
152-
rm "${test_file}_4.txt"
153-
rm "Not_include_${test_file}_5.txt"
154-
155-
assert_output --partial "1 Annotation created for"
156-
assert_output --partial "2 Annotation created for"
157-
assert_output --partial "3 Annotation created for"
158-
assert_output --partial "4 Annotation created for"
159-
refute_output "Not_include_"
160-
assert_output --partial "Annotation created - Done"
161-
assert_success
11+
@test "" {
16212
}

0 commit comments

Comments
 (0)