treeherder-cli 0.2.5

Fetch errors from a Firefox CI push on Treeherder, formatted as markdown
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use crate::models::*;
use colored::Colorize;
use comfy_table::{presets::UTF8_FULL, Attribute, Cell, Color, ContentArrangement, Table};

pub fn format_markdown_summary(
    revision: &str,
    push_id: u64,
    jobs: &[JobWithLogs],
    show_stack_traces: bool,
    fetch_logs: bool,
) -> String {
    let mut output = String::new();

    output.push_str(&format!(
        "{}\n\n",
        "Treeherder Test Results Summary".bold().underline()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Revision:".cyan().bold(),
        revision.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n\n",
        "Push ID:".cyan().bold(),
        push_id.to_string().yellow()
    ));

    if jobs.is_empty() {
        output.push_str(&format!(
            "{}\n",
            "✓ No jobs found matching criteria!".green().bold()
        ));
        return output;
    }

    let failed_count = jobs
        .iter()
        .filter(|j| {
            j.job.state == "completed" && (j.job.result == "testfailed" || j.job.result == "busted")
        })
        .count();

    let unknown_count = jobs.iter().filter(|j| j.job.result == "unknown").count();

    // Show header based on whether there are failures
    if failed_count > 0 {
        output.push_str(&format!(
            "{} ({} failures)\n\n",
            "Failed Jobs".red().bold(),
            failed_count
        ));
    } else if unknown_count > 0 {
        output.push_str(&format!(
            "{} ({} total, {} pending/running)\n\n",
            "Jobs".cyan().bold(),
            jobs.len(),
            unknown_count
        ));
    } else {
        output.push_str(&format!("{} ({})\n\n", "Jobs".cyan().bold(), jobs.len()));
    }

    // Always show the table when there are jobs
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Job ID").add_attribute(Attribute::Bold),
            Cell::new("Job Type").add_attribute(Attribute::Bold),
            Cell::new("Platform").add_attribute(Attribute::Bold),
            Cell::new("Result").add_attribute(Attribute::Bold),
            Cell::new("Errors").add_attribute(Attribute::Bold),
        ]);

    for job_with_logs in jobs {
        let job = &job_with_logs.job;
        let result_cell = match job.result.as_str() {
            "success" => Cell::new(&job.result).fg(Color::Green),
            "testfailed" | "busted" => Cell::new(&job.result).fg(Color::Red),
            _ => Cell::new(&job.result).fg(Color::Yellow),
        };

        table.add_row(vec![
            Cell::new(job.id),
            Cell::new(&job.job_type_name),
            Cell::new(&job.platform),
            result_cell,
            Cell::new(job_with_logs.errors.len()),
        ]);
    }

    output.push_str(&format!("{}\n\n", table));

    for job_with_logs in jobs {
        let job = &job_with_logs.job;
        let errors = &job_with_logs.errors;
        let log_matches = &job_with_logs.log_matches;

        let result_colored = match job.result.as_str() {
            "success" => job.result.green(),
            "testfailed" | "busted" => job.result.red(),
            _ => job.result.yellow(),
        };

        output.push_str(&format!(
            "{} {} - {}\n",
            "".cyan(),
            job.job_type_name.bold(),
            job.platform.dimmed()
        ));
        output.push_str(&format!(
            "  {} {} | {} {} | {} {}\n",
            "ID:".dimmed(),
            job.id.to_string().cyan(),
            "Symbol:".dimmed(),
            job.job_type_symbol.cyan(),
            "Result:".dimmed(),
            result_colored
        ));

        if let Some(log_dir) = &job_with_logs.log_dir {
            output.push_str(&format!("  {} {}\n", "Logs:".dimmed(), log_dir.blue()));
        }

        if !errors.is_empty() {
            output.push_str(&format!("\n  {}:\n", "Errors".red().bold()));

            let mut error_table = Table::new();
            error_table
                .load_preset(UTF8_FULL)
                .set_content_arrangement(ContentArrangement::Dynamic)
                .set_header(vec![
                    Cell::new("Test").add_attribute(Attribute::Bold),
                    Cell::new("Subtest").add_attribute(Attribute::Bold),
                    Cell::new("Status").add_attribute(Attribute::Bold),
                    Cell::new("Message").add_attribute(Attribute::Bold),
                ]);

            for error in errors {
                let test = error.test.as_deref().unwrap_or("-");
                let subtest = error.subtest.as_deref().unwrap_or("-");
                let status = error.status.as_deref().unwrap_or("-");
                let message = error
                    .message
                    .as_ref()
                    .map(|m| {
                        let msg_only = if let Some(pos) = m.find("Stack trace:") {
                            &m[..pos]
                        } else {
                            m
                        };
                        msg_only.trim().chars().take(60).collect::<String>()
                    })
                    .unwrap_or_else(|| "-".to_string());

                error_table.add_row(vec![
                    Cell::new(test),
                    Cell::new(subtest),
                    Cell::new(status).fg(Color::Red),
                    Cell::new(message),
                ]);
            }

            output.push_str(&format!("{}\n", error_table));

            if show_stack_traces {
                for error in errors {
                    let stack_trace = if let Some(stack) = &error.stack {
                        Some(stack.as_str())
                    } else if let Some(msg) = &error.message {
                        msg.find("Stack trace:")
                            .map(|pos| &msg[pos + "Stack trace:".len()..])
                    } else {
                        None
                    };

                    if let Some(stack) = stack_trace {
                        output.push_str(&format!(
                            "\n  {} for {}:\n",
                            "Stack trace".yellow().bold(),
                            error.test.as_deref().unwrap_or("unknown")
                        ));
                        for line in stack.lines() {
                            let trimmed = line.trim();
                            if !trimmed.is_empty() {
                                output.push_str(&format!("    {}\n", trimmed.dimmed()));
                            }
                        }
                        output.push('\n');
                    }
                }
            }
        } else if !fetch_logs {
            output.push_str(&format!("  {}\n", "No error summary available".dimmed()));
        }

        if fetch_logs && !log_matches.is_empty() {
            output.push_str(&format!(
                "\n  {} ({} matches):\n",
                "Pattern Matches".yellow().bold(),
                log_matches.len()
            ));
            let max_matches_to_show = 10;
            for log_match in log_matches.iter().take(max_matches_to_show) {
                output.push_str(&format!(
                    "    {}:{} {}\n",
                    log_match.log_name.cyan(),
                    log_match.line_number.to_string().yellow(),
                    log_match
                        .line_content
                        .chars()
                        .take(100)
                        .collect::<String>()
                        .dimmed()
                ));
            }
            if log_matches.len() > max_matches_to_show {
                output.push_str(&format!(
                    "    {} more matches (see log files)\n",
                    format!("... and {}", log_matches.len() - max_matches_to_show).dimmed()
                ));
            }
        }

        output.push('\n');
    }

    output
}

pub fn format_grouped_markdown_summary(
    revision: &str,
    push_id: u64,
    grouped: &[GroupedTestFailure],
) -> String {
    let mut output = String::new();

    output.push_str(&format!(
        "{}\n\n",
        "Treeherder Test Results - Grouped by Test"
            .bold()
            .underline()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Revision:".cyan().bold(),
        revision.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n\n",
        "Push ID:".cyan().bold(),
        push_id.to_string().yellow()
    ));

    if grouped.is_empty() {
        output.push_str(&format!("{}\n", "✓ No test failures found!".green().bold()));
        return output;
    }

    output.push_str(&format!(
        "{} ({} unique tests)\n\n",
        "Test Failures".red().bold(),
        grouped.len()
    ));

    for failure in grouped {
        output.push_str(&format!("{} {}\n", "".cyan(), failure.test_name.bold()));
        output.push_str(&format!(
            "  {} {} platforms: {}\n\n",
            "Affected on".dimmed(),
            failure.platforms.len().to_string().yellow(),
            failure.platforms.join(", ").cyan()
        ));

        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic)
            .set_header(vec![
                Cell::new("Platform").add_attribute(Attribute::Bold),
                Cell::new("Job").add_attribute(Attribute::Bold),
                Cell::new("Subtest").add_attribute(Attribute::Bold),
                Cell::new("Message").add_attribute(Attribute::Bold),
            ]);

        for job in &failure.jobs {
            let subtest = job.subtest.as_deref().unwrap_or("-");
            let message = job
                .message
                .as_ref()
                .map(|m| m.chars().take(50).collect::<String>())
                .unwrap_or_else(|| "-".to_string());

            table.add_row(vec![
                Cell::new(&job.platform),
                Cell::new(&job.job_type_name),
                Cell::new(subtest),
                Cell::new(message),
            ]);
        }

        output.push_str(&format!("{}\n\n", table));
    }

    output
}

pub fn format_comparison_markdown(result: &ComparisonResult) -> String {
    let mut output = String::new();

    output.push_str(&format!(
        "{}\n\n",
        "Treeherder Comparison Results".bold().underline()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Base revision:".cyan().bold(),
        result.base_revision.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n\n",
        "Comparing to:".cyan().bold(),
        result.compare_revision.yellow()
    ));

    let mut summary_table = Table::new();
    summary_table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Category").add_attribute(Attribute::Bold),
            Cell::new("Count").add_attribute(Attribute::Bold),
        ]);

    summary_table.add_row(vec![
        Cell::new("New Failures").fg(Color::Red),
        Cell::new(result.new_failures.len()).fg(if result.new_failures.is_empty() {
            Color::Green
        } else {
            Color::Red
        }),
    ]);
    summary_table.add_row(vec![
        Cell::new("Fixed").fg(Color::Green),
        Cell::new(result.fixed_failures.len()).fg(Color::Green),
    ]);
    summary_table.add_row(vec![
        Cell::new("Still Failing").fg(Color::Yellow),
        Cell::new(result.still_failing.len()).fg(Color::Yellow),
    ]);

    output.push_str(&format!("{}\n\n", summary_table));

    if result.new_failures.is_empty() {
        output.push_str(&format!(
            "{} {}\n\n",
            "New Failures:".red().bold(),
            "✓ None!".green()
        ));
    } else {
        output.push_str(&format!(
            "{} ({} tests)\n",
            "New Failures".red().bold(),
            result.new_failures.len()
        ));
        output.push_str(&format!(
            "{}\n\n",
            "These tests are now failing but passed in the comparison revision:".dimmed()
        ));

        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic)
            .set_header(vec![
                Cell::new("Test").add_attribute(Attribute::Bold),
                Cell::new("Platforms").add_attribute(Attribute::Bold),
            ]);

        for failure in &result.new_failures {
            table.add_row(vec![
                Cell::new(&failure.test_name).fg(Color::Red),
                Cell::new(failure.platforms.join(", ")),
            ]);
        }
        output.push_str(&format!("{}\n\n", table));
    }

    if result.fixed_failures.is_empty() {
        output.push_str(&format!(
            "{} {}\n\n",
            "Fixed Failures:".green().bold(),
            "None".dimmed()
        ));
    } else {
        output.push_str(&format!(
            "{} ({} tests)\n",
            "Fixed Failures".green().bold(),
            result.fixed_failures.len()
        ));
        output.push_str(&format!(
            "{}\n\n",
            "These tests were failing but now pass:".dimmed()
        ));

        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic)
            .set_header(vec![
                Cell::new("Test").add_attribute(Attribute::Bold),
                Cell::new("Platforms").add_attribute(Attribute::Bold),
            ]);

        for failure in &result.fixed_failures {
            table.add_row(vec![
                Cell::new(&failure.test_name).fg(Color::Green),
                Cell::new(failure.platforms.join(", ")),
            ]);
        }
        output.push_str(&format!("{}\n\n", table));
    }

    if !result.still_failing.is_empty() {
        output.push_str(&format!(
            "{} ({} tests)\n",
            "Still Failing".yellow().bold(),
            result.still_failing.len()
        ));
        output.push_str(&format!(
            "{}\n\n",
            "These tests fail in both revisions:".dimmed()
        ));

        let mut table = Table::new();
        table
            .load_preset(UTF8_FULL)
            .set_content_arrangement(ContentArrangement::Dynamic)
            .set_header(vec![
                Cell::new("Test").add_attribute(Attribute::Bold),
                Cell::new("Platforms").add_attribute(Attribute::Bold),
            ]);

        for failure in &result.still_failing {
            table.add_row(vec![
                Cell::new(&failure.test_name).fg(Color::Yellow),
                Cell::new(failure.platforms.join(", ")),
            ]);
        }
        output.push_str(&format!("{}\n\n", table));
    }

    output
}

pub fn format_perf_markdown(revision: &str, push_id: u64, perf_data: &[JobPerfData]) -> String {
    let mut output = String::new();

    output.push_str(&format!("{}\n\n", "Performance Data".bold().underline()));
    output.push_str(&format!(
        "{} {}\n",
        "Revision:".cyan().bold(),
        revision.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n\n",
        "Push ID:".cyan().bold(),
        push_id.to_string().yellow()
    ));

    let jobs_with_data: Vec<_> = perf_data.iter().filter(|j| j.perf_data.is_some()).collect();

    if jobs_with_data.is_empty() {
        output.push_str(&format!(
            "{}\n",
            "No performance data available for selected jobs".dimmed()
        ));
        return output;
    }

    for job_perf in jobs_with_data {
        output.push_str(&format!(
            "{} {}\n",
            "".cyan(),
            job_perf.job_type_name.bold()
        ));
        output.push_str(&format!(
            "  {} {} | {} {}\n",
            "Platform:".dimmed(),
            job_perf.platform.cyan(),
            "Job ID:".dimmed(),
            job_perf.job_id.to_string().cyan()
        ));

        if let Some(perf) = &job_perf.perf_data {
            output.push_str(&format!(
                "  {} {}\n\n",
                "Framework:".dimmed(),
                perf.framework.name.yellow()
            ));

            if !perf.suites.is_empty() {
                let mut table = Table::new();
                table
                    .load_preset(UTF8_FULL)
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec![
                        Cell::new("Suite").add_attribute(Attribute::Bold),
                        Cell::new("Metric").add_attribute(Attribute::Bold),
                        Cell::new("Value").add_attribute(Attribute::Bold),
                    ]);

                for suite in &perf.suites {
                    for subtest in &suite.subtests {
                        table.add_row(vec![
                            Cell::new(&suite.name),
                            Cell::new(&subtest.name),
                            Cell::new(format!("{:.2}", subtest.value)).fg(Color::Cyan),
                        ]);
                    }
                }

                output.push_str(&format!("{}\n", table));
            }
        }

        output.push('\n');
    }

    output
}

pub fn format_similar_history_markdown(history: &SimilarJobHistory) -> String {
    let mut output = String::new();

    output.push_str(&format!("{}\n\n", "Similar Job History".bold().underline()));
    output.push_str(&format!(
        "{} {}\n",
        "Job ID:".cyan().bold(),
        history.job_id.to_string().yellow()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Job Type:".cyan().bold(),
        history.job_type_name.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Repository:".cyan().bold(),
        history.repo.yellow()
    ));
    output.push_str(&format!(
        "{} {}\n",
        "Total Jobs:".cyan().bold(),
        history.total_jobs.to_string().yellow()
    ));

    let pass_rate_colored = if history.pass_rate >= 90.0 {
        format!("{:.1}%", history.pass_rate).green()
    } else if history.pass_rate >= 70.0 {
        format!("{:.1}%", history.pass_rate).yellow()
    } else {
        format!("{:.1}%", history.pass_rate).red()
    };

    output.push_str(&format!(
        "{} {} ({} pass, {} fail)\n\n",
        "Pass Rate:".cyan().bold(),
        pass_rate_colored,
        history.pass_count.to_string().green(),
        history.fail_count.to_string().red()
    ));

    output.push_str(&format!("{}\n\n", "Recent Results".bold()));

    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Push ID").add_attribute(Attribute::Bold),
            Cell::new("Result").add_attribute(Attribute::Bold),
            Cell::new("Platform").add_attribute(Attribute::Bold),
        ]);

    for job in &history.jobs {
        let (result_color, result_text) = match job.result.as_str() {
            "success" => (Color::Green, &job.result),
            "testfailed" | "busted" => (Color::Red, &job.result),
            _ => (Color::Yellow, &job.result),
        };

        table.add_row(vec![
            Cell::new(job.push_id),
            Cell::new(result_text).fg(result_color),
            Cell::new(&job.platform),
        ]);
    }

    output.push_str(&format!("{}\n", table));
    output
}