blib/lib/App/GHGen/Reporter.pm

filter: Statementstmt 100% 128 / 128 Branchbran 100% 30 / 30 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 9 / 9 Podpod 60.0% 3 / 5 totaltotal 96.7% 180 / 186 CC 31 SCAR 22.4
CC
31
Cov
99%
generate_github_comment
15.0
generate_markdown_report
9.0
estimate_savings
7.0
CRAP 9.4
« lib/App/GHGen/CostEstimator.pm ↑ Summary lib/App/GHGen/Analyzer.pm »
1 package App::GHGen::Reporter;
2  
3 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 our @EXPORT_OK = qw(
9 generate_markdown_report
10 generate_github_comment
11 estimate_savings
12 );
13  
14 6 our $VERSION = '0.13';
15  
16 =encoding utf-8
17  
18 =head1 NAME
19  
20 App::GHGen::Reporter - Generate reports for GitHub integration
21  
22 =head1 SYNOPSIS
23  
24 use App::GHGen::Reporter qw(generate_github_comment);
25  
26 my $comment = generate_github_comment(\@issues, \@fixes);
27  
28 =head1 FUNCTIONS
29  
30 =head2 generate_markdown_report($issues, $fixes)
31  
32 Produce a Markdown-formatted report of workflow issues and applied fixes.
33  
34 =head3 Purpose
35  
36 Render a structured Markdown document that summarises detected issues grouped
37 by category, includes suggested fixes, and appends an estimated savings
38 section when applicable.
39  
40 =head3 Arguments
41  
42 =over 4
43  
44 =item C<$issues> (ArrayRef[HashRef], required)
45  
46 Issues to report. Each must have C<type>, C<severity>, C<message>; an
47 optional C<fix> key renders a collapsible C<E<lt>detailsE<gt>> block.
48  
49 =item C<$fixes> (ArrayRef, optional, default C<[]>)
50  
51 List of fixes already applied (used only for the summary count).
52  
53 =back
54  
55 =head3 Returns
56  
57 A non-empty Markdown string beginning with C<# GHGen Workflow Analysis>.
58  
59 =head3 Side Effects
60  
61 None. Pure function.
62  
63 =head3 Usage Example
64  
65 my $md = generate_markdown_report(\@issues, \@fixes);
66 path('report.md')->spew_utf8($md);
67  
68 =head3 API SPECIFICATION
69  
70 =head4 Input
71  
72 {
73 issues => { type => 'arrayref', required => 1 },
74 fixes => { type => 'arrayref', default => [] },
75 }
76  
77 =head4 Output
78  
79 { type => 'scalar' } # Markdown string
80  
81 =head3 FORMAL SPECIFICATION
82  
83 generate_markdown_report : seq Issue × seq Fix → ℤ*
84  
85 result begins with "# GHGen Workflow Analysis"
86 |issues| > 0 ⇒ result contains "## Issues by Category"
87 |issues| = 0 ⇒ result does not contain "## Issues by Category"
88 |fixes| > 0 ⇒ result contains "Fixes applied"
89 savings.minutes > 0 ⇒ result contains "## 💰 Estimated Savings"
90  
91 =cut
92  
93 12 ▶sub generate_markdown_report($issues, $fixes = []) {
Subroutine generate_markdown_report: called CC=9 SCAR=22.0
Pod documented
94 12 my $report = "# GHGen Workflow Analysis\n\n";
95  
96 12 my $total_issues = scalar @$issues;
97 12 my $total_fixes = scalar @$fixes;
98  
99 12 $report .= "## Summary\n\n";
100 12 $report .= "- 📊 **Issues found:** $total_issues\n";
101 12 $report .= "- ✅ **Fixes applied:** $total_fixes\n\n";
102  
103 12 ▶ if (@$issues) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$issues) 9 3 12
104 # Group by type
105 9 my %by_type;
106 9 push @{$by_type{$_->{type}}}, $_ for @$issues;
107  
108 9 $report .= "## Issues by Category\n\n";
109  
110 9 for my $type (sort keys %by_type) {
111 16 my $count = scalar @{$by_type{$type}};
112 16 my $emoji = get_type_emoji($type);
113 16 $report .= "### $emoji " . ucfirst($type) . " ($count)\n\n";
114  
115 16 for my $issue (@{$by_type{$type}}) {
116 18 my $severity_badge = get_severity_badge($issue->{severity});
117 18 $report .= "**$severity_badge $issue->{message}**\n\n";
118  
119 18 ▶ if ($issue->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->{"fix"}) 15 3 18
120 15 $report .= "<details>\n";
121 15 $report .= "<summary>💡 Suggested Fix</summary>\n\n";
122 15 $report .= "```yaml\n";
123 15 $report .= "$issue->{fix}\n";
124 15 $report .= "```\n\n";
125 15 $report .= "</details>\n\n";
126 }
127 }
128 }
129 }
130  
131 # Add savings estimate if available
132 12 my $savings = estimate_savings($issues);
133 12 ▶ if ($savings->{minutes} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($savings->{"minutes"} > 0) 8 4 12
134 8 $report .= "## 💰 Estimated Savings\n\n";
135 8 $report .= "By fixing these issues, you could save:\n\n";
136 8 $report .= "- ⏱️ **~$savings->{minutes} CI minutes/month**\n";
137  
138 8 ▶ if ($savings->{cost} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($savings->{"cost"} > 0) 7 1 8
139 7 $report .= "- 💵 **~\$$savings->{cost}/month** (for private repos)\n";
140 }
141  
142 8 $report .= "\n";
143 }
144  
145 12 return $report;
146 }
147  
148 =head2 generate_github_comment($issues, $fixes, $options)
149  
150 Generate a GitHub Pull-Request comment summarising workflow issues.
151  
152 =head3 Purpose
153  
154 Produce a compact Markdown comment suitable for posting as a PR review
155 comment. Includes a summary table, a collapsible detail block, a how-to-fix
156 section when no fixes were applied, and a potential savings estimate.
157  
158 =head3 Arguments
159  
160 =over 4
161  
162 =item C<$issues> (ArrayRef[HashRef], required)
163  
164 Issues to display. Each must have C<type>, C<severity>, C<message>; an
165 optional C<file> key adds a file reference line.
166  
167 =item C<$fixes> (ArrayRef, optional, default C<[]>)
168  
169 Fixes already applied (used only for the applied-fix count in the header).
170  
171 =item C<$options> (HashRef, optional, default C<{}>)
172  
173 Reserved for future use; currently unused.
174  
175 =back
176  
177 =head3 Returns
178  
179 A non-empty Markdown string starting with C<## E<0x1F50D> GHGen Workflow Analysis>.
180  
181 When C<$issues> is empty, the comment contains the phrase
182 C<No issues found!> and is returned early without a table or details block.
183  
184 =head3 Side Effects
185  
186 None. Pure function.
187  
188 =head3 Usage Example
189  
190 my $comment = generate_github_comment(\@issues, \@fixes);
191 # Post $comment via GitHub API
192  
193 =head3 API SPECIFICATION
194  
195 =head4 Input
196  
197 {
198 issues => { type => 'arrayref', required => 1 },
199 fixes => { type => 'arrayref', default => [] },
200 options => { type => 'hashref', default => {} },
201 }
202  
203 =head4 Output
204  
205 { type => 'scalar' } # Markdown string
206  
207 =head3 FORMAL SPECIFICATION
208  
209 generate_github_comment : seq Issue × seq Fix × Options → ℤ*
210  
211 result begins with "## 🔍 GHGen Workflow Analysis"
212 |issues| = 0 ⇒ result contains "No issues found!" ∧ early return
213 |fixes| > 0 ⇒ result contains "Applied" ∧ fix count
214 |issues| > 0 ∧ |fixes| = 0 ⇒ result contains "How to Fix"
215 ∃ i ∈ issues: i.file defined ⇒ result contains i.file
216  
217 =cut
218  
219 15 ▶sub generate_github_comment($issues, $fixes = [], $options = {}) {
Subroutine generate_github_comment: called CC=15 SCAR=27.1
Pod documented
220 15 my $comment = "## 🔍 GHGen Workflow Analysis\n\n";
221  
222 15 my $total_issues = scalar @$issues;
223 15 my $total_fixes = scalar @$fixes;
224  
225 15 ▶ if ($total_fixes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_fixes > 0) 3 12 15
226 3 $comment .= "✅ **Applied $total_fixes automatic fix(es)**\n\n";
227 }
228  
229 15 ▶ if ($total_issues == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_issues == 0) 3 12 15
230 3 $comment .= "🎉 **No issues found!** Your workflows look great.\n\n";
231 3 return $comment;
232 }
233  
234 # Summary table
235 12 $comment .= "| Category | Count | Auto-fixable |\n";
236 12 $comment .= "|----------|-------|-------------|\n";
237  
238 12 my %by_type;
239 12 push @{$by_type{$_->{type}}}, $_ for @$issues;
240  
241 12 for my $type (sort keys %by_type) {
242 15 my $count = scalar @{$by_type{$type}};
243 15 ▶ my $fixable = grep { $_->{auto_fixable} // 1 } @{$by_type{$type}};
Condition50%1/2
$_->{"auto_fixable"} // 1
l!l
017
MC/DC0%0/1
$_->{"auto_fixable"} // 1
$_->{"auto_fixable"}
Truth table50%1/2
A
A= $_->{"auto_fixable"}
Aresult
11
00
244 15 my $emoji = get_type_emoji($type);
245 15 $comment .= "| $emoji " . ucfirst($type) . " | $count | $fixable |\n";
246 }
247  
248 12 $comment .= "\n";
249  
250 # Detailed issues
251 12 $comment .= "<details>\n";
252 12 $comment .= "<summary>📋 View Details</summary>\n\n";
253  
254 12 for my $type (sort keys %by_type) {
255 15 $comment .= "### " . get_type_emoji($type) . " " . ucfirst($type) . "\n\n";
256  
257 15 for my $issue (@{$by_type{$type}}) {
258 17 my $badge = get_severity_badge($issue->{severity});
259 17 $comment .= "- $badge **$issue->{message}**\n";
260  
261 17 ▶ if ($issue->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->{"file"}) 5 12 17
262 5 $comment .= " - File: `$issue->{file}`\n";
263 }
264 }
265  
266 15 $comment .= "\n";
267 }
268  
269 12 $comment .= "</details>\n\n";
270  
271 # Add recommendations
272 12 ▶ if ($total_fixes == 0 && $total_issues > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_fixes == 0 and $total_issues > 0) 9 3 12
Condition66%2/3
$total_fixes == 0 and $total_issues > 0
!ll&&!rl&&r
309
MC/DC50%1/2
$total_fixes == 0 and $total_issues > 0
$total_fixes == 0 $total_issues > 0
Truth table66%2/3
A and B
A= $total_fixes == 0B= $total_issues > 0
ABresult
0X0
100
111
273 9 $comment .= "### 💡 How to Fix\n\n";
274 9 $comment .= "Run these commands locally:\n\n";
275 9 $comment .= "```bash\n";
276 9 $comment .= "# Install ghgen\n";
277 9 $comment .= "cpanm App::GHGen\n\n";
278 9 $comment .= "# Analyze and fix\n";
279 9 $comment .= "ghgen analyze --fix\n";
280 9 $comment .= "```\n\n";
281  
282 9 $comment .= "Or enable auto-fix in this action:\n\n";
283 9 $comment .= "```yaml\n";
284 9 $comment .= "- uses: nigelhorne/ghgen-action\@v1\n";
285 9 $comment .= " with:\n";
286 9 $comment .= " auto-fix: true\n";
287 9 $comment .= " create-pr: true\n";
288 9 $comment .= "```\n\n";
289 }
290  
291 # Add savings estimate
292 12 my $savings = estimate_savings($issues);
293 12 ▶ if ($savings->{minutes} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($savings->{"minutes"} > 0) 9 3 12
294 9 $comment .= "### 💰 Potential Savings\n\n";
295 9 $comment .= "By fixing these issues:\n";
296 9 $comment .= "- ⏱️ Save **~$savings->{minutes} CI minutes/month**\n";
297  
298 9 ▶ if ($savings->{cost} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($savings->{"cost"} > 0) 8 1 9
299 8 $comment .= "- 💵 Save **~\$$savings->{cost}/month** (private repos)\n";
300 }
301  
302 9 $comment .= "\n";
303 }
304  
305 12 $comment .= "---\n";
306 12 $comment .= "*Analysis by [GHGen](https://github.com/your-org/ghgen)*\n";
307  
308 12 return $comment;
309 }
310  
311 =head2 estimate_savings($issues)
312  
313 Estimate CI-minute savings and associated cost reduction from fixing a set of issues.
314  
315 =head3 Purpose
316  
317 For each C<performance> (caching) or C<cost> (concurrency, triggers) issue,
318 add a fixed minute estimate to the running total and compute the equivalent
319 USD saving at the GitHub private-repo rate of $0.008/minute.
320  
321 =head3 Arguments
322  
323 =over 4
324  
325 =item C<$issues> (ArrayRef[HashRef], required)
326  
327 Issue hashes with at least C<type> and C<message>.
328  
329 =back
330  
331 =head3 Returns
332  
333 A hash reference:
334  
335 {
336 minutes => Int, # total estimated minutes saved per month; 0 when no savings
337 cost => Int, # floor(minutes * 0.008); 0 when no savings
338 }
339  
340 =head3 Side Effects
341  
342 None. Pure function.
343  
344 =head3 Usage Example
345  
346 my $s = estimate_savings(\@issues);
347 say "Save $s->{minutes} min/month";
348  
349 =head3 API SPECIFICATION
350  
351 =head4 Input
352  
353 { issues => { type => 'arrayref', required => 1 } }
354  
355 =head4 Output
356  
357 {
358 type => 'hashref',
359 keys => {
360 minutes => { type => 'scalar' },
361 cost => { type => 'scalar' },
362 },
363 }
364  
365 =head3 FORMAL SPECIFICATION
366  
367 estimate_savings : seq Issue → { minutes: ℕ, cost: ℕ }
368  
369 RATE ≔ 0.008
370 savings(i) ≔
371 i.type = performance ∧ i.message =~ /caching/ → 500
372 i.type = cost ∧ i.message =~ /concurrency/ → 50
373 i.type = cost ∧ i.message =~ /triggers/ → 100
374 otherwise → 0
375  
376 total ≔ ∑ { savings(i) ∣ i ∈ issues }
377 result ≔ { minutes ↦ total, cost ↦ floor(total × RATE) }
378  
379 =cut
380  
381 36 ▶sub estimate_savings($issues) {
Subroutine estimate_savings: called CC=7 SCAR=19.5
Pod documented
382 36 my %savings = (
383 minutes => 0,
384 cost => 0,
385 );
386  
387 36 for my $issue (@$issues) {
388 # Estimate savings by issue type
389 48 ▶ if ($issue->{type} eq 'performance') {
Branch: 2 branches
BranchTrueFalseTotal
if ($issue->{'type'} eq 'performance') { } 23 25 48
elsif ($issue->{'type'} eq 'cost') { } 14 11 25
390 # Caching saves ~5 minutes per workflow run
391 # Assume 100 runs/month
392 23 ▶ $savings{minutes} += 500 if $issue->{message} =~ /caching/;
Branch: 1 branch
BranchTrueFalseTotal
if $issue->{"message"} =~ /caching/u 20 3 23
393 } elsif ($issue->{type} eq 'cost') {
394 14 ▶ if ($issue->{message} =~ /concurrency/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->{"message"} =~ /concurrency/u) 9 5 14
395 # Concurrency saves ~50 minutes/month by canceling old runs
396 9 $savings{minutes} += 50;
397 }
398 14 ▶ if ($issue->{message} =~ /triggers/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issue->{"message"} =~ /triggers/u) 5 9 14
399 # Trigger filters save ~100 minutes/month
400 5 $savings{minutes} += 100;
401 }
402 }
403 }
404  
405 # Private repo pricing: ~$0.008 per minute
406 36 $savings{cost} = int($savings{minutes} * 0.008);
407  
408 36 return \%savings;
409 }
410  
411 54 ▶sub get_type_emoji($type) {
Subroutine get_type_emoji: called CC=2 SCAR=6.9
Pod undocumented
412 54 my %emojis = (
413 performance => '⚡',
414 security => '🔒',
415 cost => '💰',
416 maintenance => '🔧',
417 );
418  
419 54 ▶ return $emojis{$type} // '📌';
Condition100%2/2
$emojis{$type} // "\360\237\223\214"
l!l
513
MC/DC100%1/1
$emojis{$type} // "\360\237\223\214"
$emojis{$type}
Truth table100%2/2
A
A= $emojis{$type}
Aresult
11
00
420 }
421  
422 42 ▶sub get_severity_badge($severity) {
Subroutine get_severity_badge: called CC=2 SCAR=6.9
Pod undocumented
423 42 my %badges = (
424 high => '🔴',
425 medium => '🟡',
426 low => '🟢',
427 );
428  
429 42 ▶ return $badges{$severity} // '⚪';
Condition100%2/2
$badges{$severity} // "\342\232\252"
l!l
393
MC/DC100%1/1
$badges{$severity} // "\342\232\252"
$badges{$severity}
Truth table100%2/2
A
A= $badges{$severity}
Aresult
11
00
430 }
431  
432 =head1 AUTHOR
433  
434 Nigel Horne E<lt>njh@nigelhorne.comE<gt>
435  
436 L<https://github.com/nigelhorne>
437  
438 =head1 LICENSE
439  
440 This is free software; you can redistribute it and/or modify it under
441 the same terms as the Perl 5 programming language system itself.
442  
443 =cut
444  
445 6 1;
« lib/App/GHGen/CostEstimator.pm ↑ Summary lib/App/GHGen/Analyzer.pm »