-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_report.sh
executable file
·204 lines (180 loc) · 6.42 KB
/
generate_report.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# Define the input files and output json file
INPUT_FILE="test.json"
TIMES_FILE="test_times.json"
OUTPUT_FILE="slack_message.json"
WORKFLOW_URL=""
# Initialize variables
PASSED_TESTS=()
FAILED_TESTS=()
TEST_NAMES=()
TEST_TIMES=()
# Check if a URL parameter is passed
if [ "$#" -eq 1 ]; then
WORKFLOW_URL="$1"
fi
# Clear the output file if it already exists
> "$OUTPUT_FILE"
# Function to create the header block for the Slack message
create_header_block() {
cat <<EOF >> "$OUTPUT_FILE"
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "3L Monitoring Test Report :test_tube:",
"emoji": true
}
},
{
"type": "divider"
},
EOF
}
# Function to create the summary block for the Slack message
create_summary_block() {
total_passed=$1
total_failed=$2
cat <<EOF >> "$OUTPUT_FILE"
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Total Tests Passed:*\n$total_passed"
},
{
"type": "mrkdwn",
"text": "*Total Tests Failed:*\n$total_failed"
}
]
},
{
"type": "divider"
},
EOF
}
# Function to create the passed tests block
create_passed_tests_block() {
if [ ${#PASSED_TESTS[@]} -gt 0 ]; then
echo " {" >> "$OUTPUT_FILE"
echo ' "type": "section",' >> "$OUTPUT_FILE"
echo ' "text": {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "*Passed Tests:*"' >> "$OUTPUT_FILE"
echo " }," >> "$OUTPUT_FILE"
echo ' "fields": [' >> "$OUTPUT_FILE"
for test in "${PASSED_TESTS[@]}"; do
echo ' {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "`'$test'`"' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
done
echo ' ]' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
echo ' {' >> "$OUTPUT_FILE"
echo ' "type": "divider"' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
fi
}
# Function to create the failed tests block
create_failed_tests_block() {
if [ ${#FAILED_TESTS[@]} -gt 0 ]; then
echo " {" >> "$OUTPUT_FILE"
echo ' "type": "section",' >> "$OUTPUT_FILE"
echo ' "text": {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "*Failed Tests:*"' >> "$OUTPUT_FILE"
echo " }," >> "$OUTPUT_FILE"
echo ' "fields": [' >> "$OUTPUT_FILE"
for test in "${FAILED_TESTS[@]}"; do
test_name=$(echo "$test" | cut -d "|" -f 1)
# Add the test name as its own field
echo ' {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "`'$test_name'`"' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
done
echo ' ]' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
fi
}
# Function to create the test execution times block
create_test_times_block() {
if [ ${#TEST_NAMES[@]} -gt 0 ]; then
echo " {" >> "$OUTPUT_FILE"
echo ' "type": "section",' >> "$OUTPUT_FILE"
echo ' "text": {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "*Execution Times:*"' >> "$OUTPUT_FILE"
echo " }," >> "$OUTPUT_FILE"
echo ' "fields": [' >> "$OUTPUT_FILE"
for i in "${!TEST_NAMES[@]}"; do
echo ' {' >> "$OUTPUT_FILE"
echo ' "type": "mrkdwn",' >> "$OUTPUT_FILE"
echo ' "text": "`'${TEST_NAMES[$i]}'`: '${TEST_TIMES[$i]}' seconds"' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
done
echo ' ]' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
echo ' {' >> "$OUTPUT_FILE"
echo ' "type": "divider"' >> "$OUTPUT_FILE"
echo ' },' >> "$OUTPUT_FILE"
fi
}
# Function to create the workflow URL block
create_workflow_url_block() {
if [ -n "$WORKFLOW_URL" ]; then
cat <<EOF >> "$OUTPUT_FILE"
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Workflow URL:*\n<$WORKFLOW_URL>"
}
},
EOF
fi
}
# Function to end the JSON message block
end_json_block() {
create_workflow_url_block
echo " ]" >> "$OUTPUT_FILE"
echo "}" >> "$OUTPUT_FILE"
}
# Parse the test times JSON file and store in parallel arrays
while IFS= read -r line; do
test_name=$(echo "$line" | jq -r '.test')
time_seconds=$(echo "$line" | jq -r '.time_seconds')
# Store test names and execution times in parallel arrays
TEST_NAMES+=("$test_name")
TEST_TIMES+=("$time_seconds")
done < "$TIMES_FILE"
# Parse the test result JSON line by line
while IFS= read -r line; do
# Parse the type and event fields from the JSON line
type=$(echo "$line" | jq -r '.type')
event=$(echo "$line" | jq -r '.event')
if [[ "$type" == "test" ]]; then
test_name=$(echo "$line" | jq -r '.name')
if [[ "$event" == "ok" ]]; then
PASSED_TESTS+=("$test_name")
elif [[ "$event" == "failed" ]]; then
FAILED_TESTS+=("$test_name")
fi
elif [[ "$type" == "suite" && ("$event" == "ok" || "$event" == "failed")]]; then
total_passed=$(echo "$line" | jq -r '.passed')
total_failed=$(echo "$line" | jq -r '.failed')
# Create header and summary in the Slack message
create_header_block
create_summary_block "$total_passed" "$total_failed"
fi
done < "$INPUT_FILE"
# Write the Slack message
create_passed_tests_block
create_failed_tests_block
create_test_times_block
end_json_block
echo "Slack Block Kit JSON message generated in $OUTPUT_FILE"