-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_coverage_report.php
92 lines (75 loc) · 3.05 KB
/
generate_coverage_report.php
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
<?php
// This script generates JSON report for coverage badge github action and generates markdown report for coverage-diff github action
$outputPath = __DIR__.'/cov/json/';
$markdownPath = __DIR__.'/cov/markdown/';
$contents = file_get_contents(__DIR__.'/cov/xml/clover.xml');
$xml = simplexml_load_string($contents);
$arr = json_decode(json_encode($xml), true);
$metrics = $arr['project']['metrics']['@attributes'];
// Calculate coverage metrics
$statementsCovered = (int) $metrics['coveredstatements'];
$statementsTotal = (int) $metrics['statements'];
$statementsPct = $statementsTotal > 0 ? ($statementsCovered / $statementsTotal) * 100 : 0;
$functionsCovered = (int) $metrics['coveredmethods'];
$functionsTotal = (int) $metrics['methods'];
$functionsPct = $functionsTotal > 0 ? ($functionsCovered / $functionsTotal) * 100 : 0;
// Function to return color based on percentage
function getCoverageStatus($percentage)
{
if ($percentage >= 80) {
return ':green_circle:';
}
if ($percentage >= 50) {
return ':yellow_circle:';
}
return ':red_circle:';
}
// Generate JSON report
$map = [
'total' => [
'statements' => ['pct' => number_format($statementsPct, 2)],
],
];
file_put_contents($outputPath.'index.json', json_encode($map));
// Generate Markdown report
$markdown = "# Code Coverage Report\n\n";
$markdown .= "## Coverage Report\n\n";
$markdown .= "| St. | Category | Percentage | Covered / Total |\n";
$markdown .= "|-----|----------------|------------|-----------------|\n";
$markdown .= sprintf(
"| %s | Statements | %.2f%% | %d / %d |\n",
getCoverageStatus($statementsPct),
$statementsPct,
$statementsCovered,
$statementsTotal
);
$markdown .= sprintf(
"| %s | Functions | %.2f%% | %d / %d |\n",
getCoverageStatus($functionsPct),
$functionsPct,
$functionsCovered,
$functionsTotal
);
$markdown .= "\n<details>\n";
$markdown .= "<summary>Files Coverage</summary>\n\n";
$markdown .= "| St. | File | Methods | Statements | Total Coverage |\n";
$markdown .= "|-----|------|---------|------------|----------------|\n";
foreach ($arr['project']['file'] as $file) {
$filePath = 'src/'.explode('src/', $file['@attributes']['name'])[1];
$fileMetrics = $file['metrics']['@attributes'];
$methodsPct = $fileMetrics['methods'] > 0 ? ($fileMetrics['coveredmethods'] / $fileMetrics['methods']) * 100 : 0;
$statementsPct = $fileMetrics['statements'] > 0 ? ($fileMetrics['coveredstatements'] / $fileMetrics['statements']) * 100 : 0;
$fileElements = (int) $fileMetrics['elements'];
$fileCoveredElements = (int) $fileMetrics['coveredelements'];
$totalCoveragePct = $fileElements > 0 ? ($fileCoveredElements / $fileElements) * 100 : 0;
$markdown .= sprintf(
"| %s | %s | %.2f%% | %.2f%% | %.2f%% |\n",
getCoverageStatus($totalCoveragePct),
$filePath,
$methodsPct,
$statementsPct,
$totalCoveragePct
);
}
$markdown .= "\n</details>\n";
file_put_contents($markdownPath.'coverage_report.md', $markdown);