Skip to content

Commit c36ff9b

Browse files
committed
🎨 Use printf and correct MD outputting
1 parent c87ed8b commit c36ff9b

1 file changed

Lines changed: 61 additions & 20 deletions

File tree

lib/bazel-bep.bash

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ create_annotation() {
3232
# Check if we're running in Buildkite
3333
if [ -n "${BUILDKITE:-}" ] && command -v buildkite-agent >/dev/null 2>&1; then
3434
echo "Creating Buildkite annotation..."
35-
buildkite-agent annotate "$content" --style "$style" --context "$context_id"
35+
# Use printf to ensure newlines are properly interpreted
36+
printf "%s" "$content" | buildkite-agent annotate --style "$style" --context "$context_id"
3637
else
3738
# We're not in Buildkite, just display the content on stdout
3839
echo "Not running in Buildkite. Would create annotation with style '$style':"
39-
echo "$content"
40+
# Use printf to properly display the markdown
41+
printf "%s" "$content"
4042
fi
4143
}
4244

@@ -117,13 +119,19 @@ process_bep() {
117119
((fail_count++))
118120
# Get failure details with proper highlighting
119121
local errors=$(echo "$line" | jq -r '.completed.failureDetail.message // "Unknown error"')
120-
failure_details+="### ❌ Failed: $label\n\`\`\`diff\n- ERROR: $errors\n\`\`\`\n\n"
122+
failure_details+="### ❌ Failed: $label
123+
\`\`\`diff
124+
- ERROR: $errors
125+
\`\`\`
121126
127+
"
122128
# Check for missing dependency or deleted package errors
123129
if echo "$errors" | grep -q "no such target\|no such package\|Package is considered deleted"; then
124130
# Extract relevant part of the error message
125131
local error_detail=$(echo "$errors" | grep -o "'[^']*'\|Package [^:]*" | head -1)
126-
failure_details+="**🔍 Possible Fix:** $error_detail might be missing, renamed, or deleted. Add it to --deleted_packages flag if it's intentionally deleted.\n\n"
132+
failure_details+="**🔍 Possible Fix:** $error_detail might be missing, renamed, or deleted. Add it to --deleted_packages flag if it's intentionally deleted.
133+
134+
"
127135
fi
128136
fi
129137
fi
@@ -188,13 +196,20 @@ process_bep() {
188196
status_emoji="⏱️"
189197
fi
190198

191-
failure_details+="### $status_emoji Failed Test: $test_label ($test_status in ${test_time}s)\n\`\`\`diff\n- $test_errors\n\`\`\`\n\n"
199+
failure_details+="### $status_emoji Failed Test: $test_label ($test_status in ${test_time}s)
200+
\`\`\`diff
201+
- $test_errors
202+
\`\`\`
203+
204+
"
192205

193206
# Add stack trace if available
194207
if echo "$line" | jq -e 'has("testResult") and .testResult | has("testActionOutput") and .testResult.testActionOutput != null' > /dev/null 2>&1; then
195208
if echo "$line" | jq -e '.testResult.testActionOutput[] | select(.name == "test.log") | .uri' > /dev/null 2>&1; then
196209
local log_uri=$(echo "$line" | jq -r '.testResult.testActionOutput[] | select(.name == "test.log") | .uri')
197-
failure_details+="[View Full Test Log]($log_uri)\n\n"
210+
failure_details+="[View Full Test Log]($log_uri)
211+
212+
"
198213
fi
199214
fi
200215
fi
@@ -222,11 +237,14 @@ process_bep() {
222237
fi
223238

224239
# Clean header for the output
225-
local summary="## 🚀 Bazel Results\n\n"
240+
local summary="## 🚀 Bazel Results
226241
242+
"
227243
# Add command used if running in Buildkite
228244
if [ -n "${BUILDKITE_COMMAND:-}" ]; then
229-
summary+="**🏃 Command:** \`${BUILDKITE_COMMAND}\`\n\n"
245+
summary+="**🏃 Command:** \`${BUILDKITE_COMMAND}\`
246+
247+
"
230248
fi
231249

232250
if [ $total_build_time -gt 0 ]; then
@@ -245,11 +263,16 @@ process_bep() {
245263
if [ "$skip_count" -gt 0 ]; then
246264
summary+="| ⏭️ $skip_count skipped "
247265
fi
248-
summary+="\n\n"
266+
summary+="
267+
268+
"
249269

250270
# Add performance section with test timings in a collapsible section
251271
if [ ${#slowest_tests[@]} -gt 0 ]; then
252-
summary+="\n<details>\n<summary><strong>⏱️ Test Durations</strong> (${#slowest_tests[@]} tests)</summary>\n\n"
272+
summary+="<details>
273+
<summary><strong>⏱️ Test Durations</strong> (${#slowest_tests[@]} tests)</summary>
274+
275+
"
253276

254277
# First sort the tests by duration (longest first)
255278
local sorted_indexes=()
@@ -275,42 +298,60 @@ process_bep() {
275298

276299
# Output the sorted results
277300
for i in "${!sorted_tests[@]}"; do
278-
summary+="- \`${sorted_tests[$i]}\`: ${sorted_times[$i]}s\n"
301+
summary+="- \`${sorted_tests[$i]}\`: ${sorted_times[$i]}s
302+
"
279303
if [ $i -ge 9 ]; then # Show only top 10 slowest tests
280304
if [ ${#sorted_tests[@]} -gt 10 ]; then
281-
summary+="- _...and $((${#sorted_tests[@]} - 10)) more_\n"
305+
summary+="- _...and $((${#sorted_tests[@]} - 10)) more_
306+
"
282307
fi
283308
break
284309
fi
285310
done
286-
summary+="</details>\n"
311+
summary+="</details>
312+
"
287313
fi
288314

289315
# Add list of successful targets in a collapsible section
290316
if [ ${#successful_targets[@]} -gt 0 ]; then
291-
summary+="\n<details>\n<summary><strong>✅ Successfully Built</strong> (${#successful_targets[@]} targets)</summary>\n\n"
317+
summary+="
318+
<details>
319+
<summary><strong>✅ Successfully Built</strong> (${#successful_targets[@]} targets)</summary>
320+
321+
"
292322

293323
# Sort the targets for better readability
294324
IFS=$'\n' successful_targets_sorted=($(sort <<<"${successful_targets[*]}"))
295325
unset IFS
296326

297327
# Show all targets
298328
for target in "${successful_targets_sorted[@]}"; do
299-
summary+="- \`$target\`\n"
329+
summary+="- \`$target\`
330+
"
300331
done
301332

302-
summary+="</details>\n"
333+
summary+="</details>
334+
"
303335
fi
304336

305337
# Add details for failures if any in a collapsible section, but auto-expanded
306338
if [ -n "$failure_details" ]; then
307-
summary+="\n<details open>\n<summary><strong>❌ Failure Details</strong> ($fail_count failures)</summary>\n\n"
308-
summary+="$failure_details"
309-
summary+="</details>\n"
339+
summary+="
340+
<details open>
341+
<summary><strong>❌ Failure Details</strong> ($fail_count failures)</summary>
342+
343+
$failure_details</details>
344+
"
310345
fi
311346

312347
# Add random inspirational quote
313-
summary+="\n---\n\n💡 **Random Dev Wisdom:**\n\n_$(get_random_quote)_\n"
348+
summary+="
349+
---
350+
351+
💡 **Random Dev Wisdom:**
352+
353+
_$(get_random_quote)_
354+
"
314355

315356
# Create the annotation
316357
create_annotation "$style" "$summary"

0 commit comments

Comments
 (0)