|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +from datetime import date |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from slack_sdk import WebClient |
| 8 | +from tabulate import tabulate |
| 9 | + |
| 10 | + |
| 11 | +MAX_LEN_MESSAGE = 2900 # slack endpoint has a limit of 3001 characters |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser() |
| 14 | +parser.add_argument("--slack_channel_name", default="diffusers-ci-nightly") |
| 15 | + |
| 16 | + |
| 17 | +def main(slack_channel_name=None): |
| 18 | + failed = [] |
| 19 | + passed = [] |
| 20 | + |
| 21 | + group_info = [] |
| 22 | + |
| 23 | + total_num_failed = 0 |
| 24 | + empty_file = False or len(list(Path().glob("*.log"))) == 0 |
| 25 | + |
| 26 | + total_empty_files = [] |
| 27 | + |
| 28 | + for log in Path().glob("*.log"): |
| 29 | + section_num_failed = 0 |
| 30 | + i = 0 |
| 31 | + with open(log) as f: |
| 32 | + for line in f: |
| 33 | + line = json.loads(line) |
| 34 | + i += 1 |
| 35 | + if line.get("nodeid", "") != "": |
| 36 | + test = line["nodeid"] |
| 37 | + if line.get("duration", None) is not None: |
| 38 | + duration = f'{line["duration"]:.4f}' |
| 39 | + if line.get("outcome", "") == "failed": |
| 40 | + section_num_failed += 1 |
| 41 | + failed.append([test, duration, log.name.split("_")[0]]) |
| 42 | + total_num_failed += 1 |
| 43 | + else: |
| 44 | + passed.append([test, duration, log.name.split("_")[0]]) |
| 45 | + empty_file = i == 0 |
| 46 | + group_info.append([str(log), section_num_failed, failed]) |
| 47 | + total_empty_files.append(empty_file) |
| 48 | + os.remove(log) |
| 49 | + failed = [] |
| 50 | + text = ( |
| 51 | + "🌞 There were no failures!" |
| 52 | + if not any(total_empty_files) |
| 53 | + else "Something went wrong there is at least one empty file - please check GH action results." |
| 54 | + ) |
| 55 | + no_error_payload = { |
| 56 | + "type": "section", |
| 57 | + "text": { |
| 58 | + "type": "plain_text", |
| 59 | + "text": text, |
| 60 | + "emoji": True, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + message = "" |
| 65 | + payload = [ |
| 66 | + { |
| 67 | + "type": "header", |
| 68 | + "text": { |
| 69 | + "type": "plain_text", |
| 70 | + "text": "🤗 Results of the Diffusers scheduled nightly tests.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + ] |
| 74 | + if total_num_failed > 0: |
| 75 | + for i, (name, num_failed, failed_tests) in enumerate(group_info): |
| 76 | + if num_failed > 0: |
| 77 | + if num_failed == 1: |
| 78 | + message += f"*{name}: {num_failed} failed test*\n" |
| 79 | + else: |
| 80 | + message += f"*{name}: {num_failed} failed tests*\n" |
| 81 | + failed_table = [] |
| 82 | + for test in failed_tests: |
| 83 | + failed_table.append(test[0].split("::")) |
| 84 | + failed_table = tabulate( |
| 85 | + failed_table, |
| 86 | + headers=["Test Location", "Test Case", "Test Name"], |
| 87 | + showindex="always", |
| 88 | + tablefmt="grid", |
| 89 | + maxcolwidths=[12, 12, 12], |
| 90 | + ) |
| 91 | + message += "\n```\n" + failed_table + "\n```" |
| 92 | + |
| 93 | + if total_empty_files[i]: |
| 94 | + message += f"\n*{name}: Warning! Empty file - please check the GitHub action job *\n" |
| 95 | + print(f"### {message}") |
| 96 | + else: |
| 97 | + payload.append(no_error_payload) |
| 98 | + |
| 99 | + if len(message) > MAX_LEN_MESSAGE: |
| 100 | + print(f"Truncating long message from {len(message)} to {MAX_LEN_MESSAGE}") |
| 101 | + message = message[:MAX_LEN_MESSAGE] + "..." |
| 102 | + |
| 103 | + if len(message) != 0: |
| 104 | + md_report = { |
| 105 | + "type": "section", |
| 106 | + "text": {"type": "mrkdwn", "text": message}, |
| 107 | + } |
| 108 | + payload.append(md_report) |
| 109 | + action_button = { |
| 110 | + "type": "section", |
| 111 | + "text": {"type": "mrkdwn", "text": "*For more details:*"}, |
| 112 | + "accessory": { |
| 113 | + "type": "button", |
| 114 | + "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, |
| 115 | + "url": f"https://github.com/huggingface/diffusers/actions/runs/{os.environ['GITHUB_RUN_ID']}", |
| 116 | + }, |
| 117 | + } |
| 118 | + payload.append(action_button) |
| 119 | + |
| 120 | + date_report = { |
| 121 | + "type": "context", |
| 122 | + "elements": [ |
| 123 | + { |
| 124 | + "type": "plain_text", |
| 125 | + "text": f"Nightly test results for {date.today()}", |
| 126 | + }, |
| 127 | + ], |
| 128 | + } |
| 129 | + payload.append(date_report) |
| 130 | + |
| 131 | + print(payload) |
| 132 | + |
| 133 | + client = WebClient(token=os.environ.get("SLACK_API_TOKEN")) |
| 134 | + client.chat_postMessage(channel=f"#{slack_channel_name}", text=message, blocks=payload) |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + args = parser.parse_args() |
| 139 | + main(args.slack_channel_name) |
0 commit comments