add stats to the changes

releases
Yanick Champoux 2022-07-20 14:27:53 -04:00
parent d68f49668d
commit 1ad1e9810c
6 changed files with 87 additions and 0 deletions

View File

@ -16,6 +16,7 @@ with 'App::Changelord::Role::ChangeTypes';
has changelog => ( is => 'lazy' );
with 'App::Changelord::Role::Versions';
with 'App::Changelord::Role::Stats';
sub _build_changelog ($self) { $self->parent_command->changelog }
@ -50,6 +51,12 @@ sub run ($self) {
$self->changelog->{releases}[0]{date} = sprintf "%d-%02d-%02d",
$time[5] + 1900, $time[4], $time[3];
if( $self->changelog->{project}{with_stats} ) {
push $self->changelog->{releases}[0]{changes}->@*, {
type => 'stats', desc => $self->stats
};
}
my $change = $self->changelog;
Bless($change)->keys(
[ uniq qw/

View File

@ -49,6 +49,8 @@ sub run ($self) {
project => {
name => undef,
homepage => undef,
with_stats => 'true',
ticket_url => undef,
},
change_types => $self->change_types,
releases => [

View File

@ -21,6 +21,8 @@ properties:
examples:
- s!GH(\d+)!https://github.com/yanick/App-Changelord/issue/$1/
- /^\d+$/ ? "https://.../$_" : undef
with_stats:
description: if true, add git statistics when bumping the version.
change_types:
type: array
items:

View File

@ -18,6 +18,7 @@ sub _build_change_types($self) {
{ title => 'Features' , level => 'minor', keywords => [ 'feat' ] } ,
{ title => 'Bug fixes' , level => 'patch', keywords => [ 'fix' ] },
{ title => 'Package maintenance' , level => 'patch', keywords => [ 'chore', 'maint', 'refactor' ] },
{ title => 'Statistics' , level => 'patch', keywords => [ 'stats' ] },
]
}

View File

@ -0,0 +1,48 @@
package App::Changelord::Role::Stats;
use v5.36.0;
use Git::Repository;
use Moo::Role;
use feature 'try'; no warnings qw/ experimental /;
requires 'changelog';
# stolen from Dist::Zilla::Plugin::ChangeStats::Git
has repo => (
is => 'ro',
default => sub { Git::Repository->new( work_tree => '.' ) },
);
has stats => (
is => 'lazy' );
sub _build_stats ($self) {
my $comparison_data = $self->_get_comparison_data or return;
my $stats = 'code churn: ' . $comparison_data;
return $stats =~ s/\s+/ /gr;
}
sub _get_comparison_data($self) {
# HEAD versus previous release
# What are we diffing against? :)
my $previous = $self->changelog->{releases}->@* > 1
? $self->changelog->{releases}[1]{version}
: '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; # empty tree
my $output = eval {
$self->repo->run( 'diff', '--shortstat', $previous, 'HEAD')
};
warn "could not gather stats: $@\n" if $@;
return $output;
}
1;

27
t/stats.t Normal file
View File

@ -0,0 +1,27 @@
use Test2::V0;
use v5.36.0;
package TestMe {
use Moo;
has changelog => (
is => 'ro',
default => sub {{
project => { ticket_url => undef },
releases => [
{ },
]
}}
);
with 'App::Changelord::Role::Stats';
with 'App::Changelord::Role::ChangeTypes';
}
my $test = TestMe->new;
like $test->stats => qr/code churn: /;
done_testing;