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
|
#!/usr/bin/env python3
"""
Collect step summaries from all matrix jobs and generate a combined markdown table.
This script:
1. Collects GITHUB_STEP_SUMMARY files from all matrix jobs (via artifacts)
2. Parses test statistics from each summary
3. Generates a combined markdown table with branches as rows and build_presets as columns
4. Writes the combined summary to GITHUB_STEP_SUMMARY
"""
import argparse
import json
import os
import re
import sys
from pathlib import Path
from typing import Dict, List, Optional, Tuple
def parse_all_attempts(summary_text: str) -> List[Dict]:
"""
Parse all attempt tables from markdown summary.
Returns: list of dicts, each containing stats and URL for one attempt (try_1, try_2, try_3, etc.)
"""
if not summary_text:
return []
# Pattern to match markdown table with test statistics
pattern = r'\|\s*TESTS\s*\|\s*PASSED\s*\|\s*ERRORS\s*\|\s*FAILED\s*\|\s*SKIPPED\s*\|\s*MUTED'
url_pattern = r'https://storage\.yandexcloud\.net/[^\s\)]+ya-test\.html[^\s\)]*'
lines = summary_text.split('\n')
# Find all table headers
table_starts = []
for i, line in enumerate(lines):
if re.search(pattern, line, re.IGNORECASE):
table_starts.append(i)
if not table_starts:
return []
attempts = []
def extract_number(s):
match = re.search(r'\[?(\d+)\]?', s)
return int(match.group(1)) if match else 0
# Parse each table
for table_idx, start_idx in enumerate(table_starts):
attempt_num = table_idx + 1
stats = {
'tests': 0,
'passed': 0,
'errors': 0,
'failed': 0,
'skipped': 0,
'muted': 0
}
attempt_url = None
# Find data row for this table
i = start_idx + 1
while i < len(lines):
line = lines[i].strip()
if '---' in line:
i += 1
continue
if line.startswith('|') and line.count('|') >= 6:
parts = [p.strip() for p in line.split('|')]
parts = [p for p in parts if p]
if len(parts) >= 6:
try:
stats['tests'] = extract_number(parts[0])
stats['passed'] = extract_number(parts[1])
stats['errors'] = extract_number(parts[2])
stats['failed'] = extract_number(parts[3])
stats['skipped'] = extract_number(parts[4])
stats['muted'] = extract_number(parts[5])
# Extract URL from FAILED or TESTS column
failed_cell = parts[3] if len(parts) > 3 else ''
tests_cell = parts[0] if len(parts) > 0 else ''
match = re.search(url_pattern, failed_cell)
if match:
attempt_url = match.group(0)
else:
match = re.search(url_pattern, tests_cell)
if match:
attempt_url = match.group(0)
break
except (ValueError, IndexError):
pass
# Check if next line is a new table header
if i + 1 < len(lines) and re.search(pattern, lines[i + 1], re.IGNORECASE):
break
i += 1
if stats['tests'] > 0 or stats['failed'] > 0 or stats['errors'] > 0 or attempt_url:
attempts.append({
'attempt': attempt_num,
'stats': stats,
'url': attempt_url
})
return attempts
def parse_summary_markdown(summary_text: str) -> Optional[Dict]:
"""
Parse test statistics from markdown summary.
Looks for markdown table format:
| TESTS | PASSED | ERRORS | FAILED | SKIPPED | MUTED |
There can be multiple tables (for retries: try_1, try_2, try_3).
We only take the LAST table (last attempt) for combined summary.
Returns: dict with test statistics from last attempt or None if not found
"""
if not summary_text:
return None
# Pattern to match markdown table with test statistics
pattern = r'\|\s*TESTS\s*\|\s*PASSED\s*\|\s*ERRORS\s*\|\s*FAILED\s*\|\s*SKIPPED\s*\|\s*MUTED'
lines = summary_text.split('\n')
# Find all table headers
table_starts = []
for i, line in enumerate(lines):
if re.search(pattern, line, re.IGNORECASE):
table_starts.append(i)
if not table_starts:
return None
# Use the LAST table (last attempt)
start_idx = table_starts[-1]
stats = {
'tests': 0,
'passed': 0,
'errors': 0,
'failed': 0,
'skipped': 0,
'muted': 0
}
def extract_number(s):
# Handle format: [624](url) or just 624
match = re.search(r'\[?(\d+)\]?', s)
return int(match.group(1)) if match else 0
# Look for data row after the header (only from last table)
i = start_idx + 1
while i < len(lines):
line = lines[i].strip()
# Skip separator line
if '---' in line:
i += 1
continue
# Check if this is a data row
if line.startswith('|') and line.count('|') >= 6:
# Parse the row - this is the data from last attempt
parts = [p.strip() for p in line.split('|')]
parts = [p for p in parts if p] # Remove empty
if len(parts) >= 6:
try:
# Take values from last attempt only (don't sum)
stats['tests'] = extract_number(parts[0])
stats['passed'] = extract_number(parts[1])
stats['errors'] = extract_number(parts[2])
stats['failed'] = extract_number(parts[3])
stats['skipped'] = extract_number(parts[4])
stats['muted'] = extract_number(parts[5])
# Found the data row, break
break
except (ValueError, IndexError):
pass
# Check if next line is a new table header (shouldn't happen for last table, but just in case)
if i + 1 < len(lines) and re.search(pattern, lines[i + 1], re.IGNORECASE):
# This shouldn't happen since we're using last table, but break anyway
break
i += 1
# Only return stats if we found data
if stats['tests'] > 0 or stats['failed'] > 0 or stats['errors'] > 0:
return stats
return None
def extract_test_report_url(summary_text: str) -> Optional[str]:
"""
Extract test report URL from summary.
Takes URL from the LAST attempt (last table) - from FAILED column if available, otherwise from TESTS column.
"""
if not summary_text:
return None
# Pattern to match markdown table with test statistics
table_pattern = r'\|\s*TESTS\s*\|\s*PASSED\s*\|\s*ERRORS\s*\|\s*FAILED\s*\|\s*SKIPPED\s*\|\s*MUTED'
lines = summary_text.split('\n')
# Find all table headers
table_starts = []
for i, line in enumerate(lines):
if re.search(table_pattern, line, re.IGNORECASE):
table_starts.append(i)
if not table_starts:
return None
# Use the LAST table (last attempt)
start_idx = table_starts[-1]
# Pattern for storage.yandexcloud.net URLs with ya-test.html
url_pattern = r'https://storage\.yandexcloud\.net/[^\s\)]+ya-test\.html[^\s\)]*'
# Look for data row after the header and extract URL from FAILED column (preferred) or TESTS column
i = start_idx + 1
while i < len(lines):
line = lines[i].strip()
# Skip separator line
if '---' in line:
i += 1
continue
# Check if this is a data row
if line.startswith('|') and line.count('|') >= 6:
# Parse the row
parts = [p.strip() for p in line.split('|')]
parts = [p for p in parts if p] # Remove empty
if len(parts) >= 6:
# Try FAILED column first (column index 3, but parts[0] is TESTS, parts[1] is PASSED, etc.)
# Actually, parts array: [TESTS, PASSED, ERRORS, FAILED, SKIPPED, MUTED]
# So FAILED is at index 3
failed_cell = parts[3] if len(parts) > 3 else ''
tests_cell = parts[0] if len(parts) > 0 else ''
# Try to extract URL from FAILED column first
match = re.search(url_pattern, failed_cell)
if match:
return match.group(0)
# Fallback to TESTS column
match = re.search(url_pattern, tests_cell)
if match:
return match.group(0)
# Found the data row, break
break
i += 1
# Fallback: search in entire summary (but prefer last attempt)
# Search backwards from end to find last URL
all_matches = list(re.finditer(url_pattern, summary_text))
if all_matches:
return all_matches[-1].group(0)
return None
def extract_job_info_from_name(job_name: str) -> Tuple[Optional[str], Optional[str]]:
"""
Extract branch and build_preset from job name.
Format: "Regression-run_... (build_preset) / branch:build_preset"
Returns: (branch, build_preset)
"""
branch = None
build_preset = None
if ':' in job_name:
# Format: "branch:build_preset"
parts = job_name.split(':')
if len(parts) >= 2:
branch = parts[0].strip()
build_preset = parts[1].strip()
elif ' / ' in job_name:
# Format: "Regression-run_... (build_preset) / branch:build_preset"
parts = job_name.split(' / ')
if len(parts) >= 2:
branch_preset = parts[-1].strip()
if ':' in branch_preset:
branch, build_preset = branch_preset.split(':', 1)
branch = branch.strip()
build_preset = build_preset.strip()
return branch, build_preset
def collect_summaries_from_artifacts(artifacts_dir: str) -> Dict[str, Dict]:
"""
Collect summaries from artifact files.
Artifacts are downloaded with pattern "job-summary-*" and contain files like:
"summary-{branch}:{build_preset}.md"
Returns: dict mapping "branch:build_preset" to summary data
"""
summaries = {}
artifacts_path = Path(artifacts_dir)
if not artifacts_path.exists():
print(f"Warning: Artifacts directory {artifacts_dir} does not exist", file=sys.stderr)
return summaries
# Look for summary files - they might be in subdirectories (one per artifact)
# Files can be named: "summary-*.md" or "{branch}:{build_preset} summary"
summary_files = list(artifacts_path.rglob("summary-*.md"))
# Also look for files ending with " summary" (without .md extension)
summary_files.extend(artifacts_path.rglob("* summary"))
for summary_file in summary_files:
try:
with open(summary_file, 'r', encoding='utf-8') as f:
summary_text = f.read()
# Extract job name from filename
# Format: "summary-{branch}:{build_preset}.md" or " {branch}:{build_preset} summary"
filename = summary_file.name.strip() # Remove leading/trailing whitespace
# Handle format: "summary-{branch}:{build_preset}.md"
if filename.startswith('summary-') and filename.endswith('.md'):
job_name = filename[8:-3].strip() # Remove "summary-" prefix and ".md" suffix
# Handle format: "{branch}:{build_preset} summary" (without .md extension)
elif filename.endswith(' summary'):
job_name = filename[:-8].strip() # Remove " summary" suffix
else:
job_name = summary_file.stem.replace('summary-', '').strip()
branch, build_preset = extract_job_info_from_name(job_name)
if not branch or not build_preset:
# Try to extract from filename directly
if ':' in job_name:
parts = job_name.split(':', 1)
branch = parts[0].strip()
build_preset = parts[1].strip()
if not branch or not build_preset:
print(f"Warning: Could not extract branch/build_preset from {job_name} (file: {summary_file})", file=sys.stderr)
continue
key = f"{branch}:{build_preset}"
# Parse statistics from last attempt
stats = parse_summary_markdown(summary_text)
test_report_url = extract_test_report_url(summary_text)
# Parse all attempts for detailed table
all_attempts = parse_all_attempts(summary_text)
summaries[key] = {
'branch': branch,
'build_preset': build_preset,
'job_name': job_name,
'stats': stats or {},
'test_report_url': test_report_url,
'all_attempts': all_attempts, # List of all attempts with stats and URLs
}
print(f"Collected summary for {key}", file=sys.stderr)
except Exception as e:
print(f"Warning: Error processing {summary_file}: {e}", file=sys.stderr)
continue
return summaries
def generate_combined_markdown_table(summaries: Dict[str, Dict]) -> str:
"""
Generate combined markdown table.
Rows: branches
Columns: build_presets
Cells: error counts with links (always show link, even if no errors)
Plus detailed table with all attempts below.
"""
if not summaries:
return "No job summaries available.\n"
# Collect all branches and build_presets
branches = sorted(set(s['branch'] for s in summaries.values()))
presets = sorted(set(s['build_preset'] for s in summaries.values()))
if not branches or not presets:
return "No valid job summaries found.\n"
lines = []
lines.append("## 📊 Combined Test Summary\n")
lines.append("| Branch | " + " | ".join(presets) + " |")
lines.append("|" + "---|" * (len(presets) + 1))
for branch in branches:
row = [branch]
for preset in presets:
key = f"{branch}:{preset}"
if key in summaries:
summary = summaries[key]
stats = summary.get('stats', {})
failed = stats.get('failed', 0)
errors = stats.get('errors', 0)
total_errors = failed + errors
test_report_url = summary.get('test_report_url', '')
if test_report_url:
if total_errors > 0:
cell = f"[{total_errors}]({test_report_url})"
if failed > 0 and errors > 0:
cell += f" ({failed} failed, {errors} errors)"
elif failed > 0:
cell += f" ({failed} failed)"
else:
cell += f" ({errors} errors)"
else:
# No errors, but still show link
cell = f"[✓]({test_report_url})"
else:
# No URL available
if total_errors > 0:
cell = str(total_errors)
if failed > 0 and errors > 0:
cell += f" ({failed} failed, {errors} errors)"
elif failed > 0:
cell += f" ({failed} failed)"
else:
cell += f" ({errors} errors)"
else:
cell = "✓"
row.append(cell)
else:
row.append("—")
lines.append("| " + " | ".join(row) + " |")
# Add detailed table with all attempts
# Same structure as main table: branches as rows, build_presets as columns
lines.append("\n### Detailed Results (All Attempts)\n")
lines.append("| Branch | " + " | ".join(presets) + " |")
lines.append("|" + "---|" * (len(presets) + 1))
for branch in branches:
row = [branch]
for preset in presets:
key = f"{branch}:{preset}"
if key in summaries:
summary = summaries[key]
all_attempts = summary.get('all_attempts', [])
if all_attempts:
# Format all attempts in this cell
attempt_parts = []
for attempt in all_attempts:
attempt_num = attempt.get('attempt', 0)
attempt_stats = attempt.get('stats', {})
failed = attempt_stats.get('failed', 0)
errors = attempt_stats.get('errors', 0)
attempt_url = attempt.get('url', '')
total_errors = failed + errors
if attempt_url:
if total_errors > 0:
attempt_text = f"try_{attempt_num}: [{total_errors}]({attempt_url})"
if failed > 0 and errors > 0:
attempt_text += f" ({failed}f, {errors}e)"
elif failed > 0:
attempt_text += f" ({failed}f)"
else:
attempt_text += f" ({errors}e)"
else:
attempt_text = f"try_{attempt_num}: [✓]({attempt_url})"
else:
if total_errors > 0:
attempt_text = f"try_{attempt_num}: {total_errors}"
if failed > 0 and errors > 0:
attempt_text += f" ({failed}f, {errors}e)"
elif failed > 0:
attempt_text += f" ({failed}f)"
else:
attempt_text += f" ({errors}e)"
else:
attempt_text = f"try_{attempt_num}: ✓"
attempt_parts.append(attempt_text)
cell = "<br>".join(attempt_parts)
else:
# Fallback if all_attempts not parsed
stats = summary.get('stats', {})
failed = stats.get('failed', 0)
errors = stats.get('errors', 0)
test_report_url = summary.get('test_report_url', '')
total_errors = failed + errors
if test_report_url:
if total_errors > 0:
cell = f"last: [{total_errors}]({test_report_url})"
if failed > 0 and errors > 0:
cell += f" ({failed}f, {errors}e)"
elif failed > 0:
cell += f" ({failed}f)"
else:
cell += f" ({errors}e)"
else:
cell = f"last: [✓]({test_report_url})"
else:
if total_errors > 0:
cell = f"last: {total_errors}"
if failed > 0 and errors > 0:
cell += f" ({failed}f, {errors}e)"
elif failed > 0:
cell += f" ({failed}f)"
else:
cell += f" ({errors}e)"
else:
cell = "last: ✓"
row.append(cell)
else:
row.append("—")
lines.append("| " + " | ".join(row) + " |")
return "\n".join(lines) + "\n"
def main():
parser = argparse.ArgumentParser(
description='Collect step summaries from matrix jobs and generate combined markdown table'
)
parser.add_argument(
'--artifacts-dir',
required=True,
help='Directory containing summary artifacts from matrix jobs'
)
parser.add_argument(
'--output',
help='Output file for combined summary (default: GITHUB_STEP_SUMMARY)'
)
args = parser.parse_args()
# Collect summaries
print("Collecting summaries from artifacts...", file=sys.stderr)
summaries = collect_summaries_from_artifacts(args.artifacts_dir)
print(f"Found {len(summaries)} job summaries", file=sys.stderr)
# Generate combined markdown table
combined_markdown = generate_combined_markdown_table(summaries)
# Write output
output_file = args.output or os.environ.get('GITHUB_STEP_SUMMARY')
if output_file:
with open(output_file, 'w', encoding='utf-8') as f:
f.write(combined_markdown)
print(f"Combined summary written to {output_file}", file=sys.stderr)
else:
# Write to stdout if no output file specified
print(combined_markdown)
return 0
if __name__ == '__main__':
sys.exit(main())
|