Create a concise Slack message payload from consolidated data.
(consolidated_data)
| 566 | |
| 567 | |
| 568 | def create_slack_payload(consolidated_data): |
| 569 | """Create a concise Slack message payload from consolidated data.""" |
| 570 | total = consolidated_data["total_stats"] |
| 571 | success_rate = f"{(total['passed'] / total['tests'] * 100):.2f}%" if total["tests"] > 0 else "N/A" |
| 572 | |
| 573 | # Determine emoji based on success rate |
| 574 | if total["failed"] == 0: |
| 575 | emoji = "✅" |
| 576 | elif total["failed"] / total["tests"] < 0.1: |
| 577 | emoji = "⚠️" |
| 578 | else: |
| 579 | emoji = "❌" |
| 580 | |
| 581 | # Create a more compact summary section |
| 582 | summary = f"{emoji} *Diffusers Nightly Tests:* {success_rate} success ({total['passed']}/{total['tests']} tests" |
| 583 | if total["skipped"] > 0: |
| 584 | summary += f", {total['skipped']} skipped" |
| 585 | summary += ")" |
| 586 | |
| 587 | # Create the test suites table in markdown format |
| 588 | # Build the markdown table with proper alignment |
| 589 | table_lines = [] |
| 590 | table_lines.append("```") |
| 591 | |
| 592 | # Sort test suites by success rate (ascending - least successful first) |
| 593 | sorted_suites = sorted( |
| 594 | consolidated_data["test_suites"].items(), |
| 595 | key=lambda x: (x[1]["stats"]["passed"] / x[1]["stats"]["tests"] * 100) if x[1]["stats"]["tests"] > 0 else 0, |
| 596 | reverse=False, |
| 597 | ) |
| 598 | |
| 599 | # Calculate max widths for proper alignment |
| 600 | max_suite_name_len = max(len(suite_name) for suite_name, _ in sorted_suites) if sorted_suites else 10 |
| 601 | max_suite_name_len = max(max_suite_name_len, len("Test Suite")) # Ensure header fits |
| 602 | |
| 603 | # Create header with proper spacing (only Tests, Failed, Success Rate) |
| 604 | header = f"| {'Test Suite'.ljust(max_suite_name_len)} | {'Tests'.rjust(6)} | {'Failed'.rjust(6)} | {'Success Rate'.ljust(12)} |" |
| 605 | separator = f"|:{'-' * max_suite_name_len}|{'-' * 7}:|{'-' * 7}:|:{'-' * 11}|" |
| 606 | |
| 607 | table_lines.append(header) |
| 608 | table_lines.append(separator) |
| 609 | |
| 610 | # Add data rows with proper alignment |
| 611 | for suite_name, suite_data in sorted_suites: |
| 612 | stats = suite_data["stats"] |
| 613 | suite_success_rate = f"{(stats['passed'] / stats['tests'] * 100):.2f}%" if stats["tests"] > 0 else "N/A" |
| 614 | |
| 615 | row = f"| {suite_name.ljust(max_suite_name_len)} | {str(stats['tests']).rjust(6)} | {str(stats['failed']).rjust(6)} | {suite_success_rate.ljust(12)} |" |
| 616 | |
| 617 | table_lines.append(row) |
| 618 | |
| 619 | table_lines.append("```") |
| 620 | |
| 621 | # Create the Slack payload with character limit enforcement |
| 622 | payload = [ |
| 623 | {"type": "section", "text": {"type": "mrkdwn", "text": summary}}, |
| 624 | {"type": "section", "text": {"type": "mrkdwn", "text": "\n".join(table_lines)}}, |
| 625 | ] |