blib/lib/App/Changelog.pm

filter: Statementstmt 43.2% 35 / 81 Branchbran 32.1% 9 / 28 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 2 totaltotal 43.6% 58 / 133 CC 24 SCAR 26.1
CC
24
Cov
43%
_build_changelog_content
24.0
generate_changelog
20.0
_filter_conventional_commits
12.0
CRAP 13.6
↑ Summary
1 package App::Changelog;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.0.2';
9  
10 3 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod undocumented
11 3 my ( $class, %args ) = @_;
12 my $self = {
13 output_file => $args{output_file} || 'CHANGELOG.md',
14 compact => $args{compact} // 1,
15 filter_tag => $args{filter_tag} || '',
16 3 ▶ conventional => $args{conventional} // 0,
Condition75%6/8
$args{'output_file'} || 'CHANGELOG.md'
l!l
30
$args{"compact"} // 1
l!l
12
$args{'filter_tag'} || ''
l!l
12
$args{"conventional"} // 0
l!l
03
MC/DC0%0/1
$args{'output_file'} || 'CHANGELOG.md'
$args{'output_file'}
MC/DC100%1/1
$args{"compact"} // 1
$args{"compact"}
MC/DC100%1/1
$args{'filter_tag'} || ''
$args{'filter_tag'}
MC/DC0%0/1
$args{"conventional"} // 0
$args{"conventional"}
Truth table50%1/2
A
A= $args{'output_file'}
Aresult
11
00
Truth table100%2/2
A
A= $args{"compact"}
Aresult
11
00
Truth table100%2/2
A
A= $args{'filter_tag'}
Aresult
11
00
Truth table50%1/2
A
A= $args{"conventional"}
Aresult
11
00
17 };
18 3 bless $self, $class;
19 3 return $self;
20 }
21  
22 0 ▶sub generate_changelog {
Subroutine generate_changelog: not called CC=4 SCAR=30.0
Pod undocumented
23 0 my ($self) = @_;
24  
25 0 say "Generating changelog from Git history...";
26  
27 my $git_log_format =
28 0 ▶ $self->{compact} ? '--pretty=format:"%h %s"' : '--pretty=fuller';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'compact'} ? : 0 0 0
29 0 ▶ if ( $self->{conventional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conventional"}) 0 0 0
30 0 $git_log_format = '--pretty=format:"%h %s (%an)"';
31 }
32  
33 0 my $git_log =
34 $self->_run_git_command("git log $git_log_format --abbrev-commit");
35 0 ▶ if ( !$git_log ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($git_log) 0 0 0
36 0 die
37 "Error: Could not retrieve Git history. Are you in a Git repository?\n";
38 }
39  
40 0 my @tags = $self->_get_tags();
41 0 my $changelog_content =
42 $self->_build_changelog_content( \@tags, $git_log_format );
43  
44 0 $self->_write_to_file($changelog_content);
45 0 say "Changelog generated successfully in $self->{output_file}.";
46 }
47  
48 1 ▶sub _build_changelog_content {
Subroutine _build_changelog_content: called CC=11 SCAR=31.8
49 1 my ( $self, $tags, $format ) = @_;
50 1 my $content = "# Changelog\n\n";
51  
52 1 for my $i ( 0 .. $#$tags ) {
53 3 my $current_tag = $tags->[$i];
54 3 ▶ my $previous_tag = $i == $#$tags ? '' : $tags->[ $i + 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$i == $tags->$#* ? : 1 2 3
55  
56 3 ▶ my $log_command =
Branch: 1 branch
BranchTrueFalseTotal
$previous_tag ? : 2 1 3
57 $previous_tag
58 ? "git log $previous_tag..$current_tag $format"
59 : "git log $current_tag $format";
60  
61 3 my $logs = $self->_run_git_command($log_command);
62 $logs = $self->_filter_conventional_commits($logs)
63 3 ▶ if $self->{conventional};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"conventional"} 0 3 3
64  
65 3 my $date =
66 $self->_run_git_command("git log -1 --format=%ai $current_tag");
67 3 $date =~ s/\s.*$//;
68  
69 3 $content .= "## [$current_tag] - $date\n\n";
70  
71 3 ▶ unless ( $self->{conventional} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'conventional'}) { } 0 3 3
72 3 ▶ $content .= "$logs\n" if $logs;
Branch: 1 branch
BranchTrueFalseTotal
if $logs 3 0 3
73 }
74 else {
75 0 my %grouped_commits;
76 0 my @log_lines = split( "\n", $logs );
77  
78 0 for my $log (@log_lines) {
79 0 ▶ if ( $log =~ /^[a-f0-9]+\s([a-z]+):\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log =~ /^[a-f0-9]+\s([a-z]+):\s*(.*)$/) 0 0 0
80 0 my $type = $1;
81 0 my $message = $2;
82  
83 0 push @{ $grouped_commits{$type} }, $message;
84 }
85 }
86  
87 0 for my $type ( sort keys %grouped_commits ) {
88 0 $content .= "### " . ucfirst($type) . "\n";
89 0 for my $message ( @{ $grouped_commits{$type} } ) {
90 0 $content .= "- $type: $message\n";
91 }
92 0 $content .= "\n";
93 }
94 }
95 }
96  
97 1 return $content;
98 }
99  
100 0 ▶sub _filter_conventional_commits {
Subroutine _filter_conventional_commits: not called CC=3 SCAR=24.8
101 0 my ( $self, $logs ) = @_;
102 0 my @lines = split( /\n/, $logs );
103 0 my @filtered;
104  
105 0 foreach my $line (@lines) {
106 0 ▶ if ( $line =~
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^feat|fix|chore|docs|style|refactor|test|perf|ci|build|revert\(.*?\): /) 0 0 0
107 /^feat|fix|chore|docs|style|refactor|test|perf|ci|build|revert\(.*?\): /
108 )
109 {
110 0 push @filtered, $line;
111 }
112 }
113  
114 0 return join( "\n", @filtered );
115 }
116  
117 1 ▶sub _get_tags {
Subroutine _get_tags: called CC=4 SCAR=18.5
118 1 my ($self) = @_;
119 1 my $git_tags = $self->_run_git_command('git tag --sort=creatordate');
120 1 my @tags = split( /\n/, $git_tags );
121 1 ▶ if ( !@tags ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tags) 0 1 1
122 0 die
123 "Error: No Git tags found. Use 'git tag' to create version tags first.\n";
124 }
125  
126 1 ▶ if ( $self->{filter_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter_tag"}) 0 1 1
127 0 @tags = grep { /^$self->{filter_tag}/ } @tags;
128 0 ▶ if ( !@tags ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tags) 0 0 0
129 0 die "Error: No tags matching the filter '$self->{filter_tag}'.\n";
130 }
131 }
132 1 return @tags;
133 }
134  
135 0 ▶sub _run_git_command {
Subroutine _run_git_command: not called CC=1 SCAR=6.9
136 0 my ( $self, $command ) = @_;
137 0 my $output = `$command`;
138 0 chomp $output;
139 0 return $output;
140 }
141  
142 0 ▶sub _write_to_file {
Subroutine _write_to_file: not called CC=2 SCAR=17.9
143 0 my ( $self, $content ) = @_;
144 open( my $fh, '>', $self->{output_file} )
145 0 ▶ or die "Could not open $self->{output_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->{"output_file"} or die qq[Could not open $self->{"output_file"}: $!] 0 0 0
146 0 print $fh $content;
147 0 close($fh);
148 }
149  
150 2 1;
151 __END__
↑ Summary