File Coverage

blib/lib/Data/CodeRepos/CommitPing.pm
Criterion Covered Total %
statement 85 90 94.4
branch 24 30 80.0
condition 11 13 84.6
subroutine 14 18 77.7
pod 0 9 0.0
total 134 160 83.7


line stmt bran cond sub pod time code
1             package Data::CodeRepos::CommitPing;
2              
3 5     5   12820 use strict;
  5         10  
  5         267  
4 5     5   52 use warnings;
  5         12  
  5         264  
5             our $VERSION = '0.03';
6              
7 5     5   30 use Carp;
  5         11  
  5         476  
8 5     5   8449 use DateTime;
  5         49850  
  5         247  
9 5     5   5101 use DateTime::Format::HTTP;
  5         37154  
  5         210  
10 5     5   2585 use YAML;
  5         14727  
  5         3770  
11              
12             sub new {
13 28     28 0 255715 my($class, $stuff) = @_;
14 28 50       104 croak 'usage: Data::CodeRepos::CommitPing->new($coderepos_commit_data)' unless $stuff;
15            
16 28 100       98 unless (ref $stuff eq 'HASH') {
17 22 100       53 if (ref $stuff) {
18             # if CGI object
19 6         12 $stuff = eval { $stuff->param('yaml') };
  6         23  
20 6 50       40 croak "bad object: $@" if $@;
21             }
22 22         116 $stuff = Load($stuff);
23             }
24              
25 28         583084 for my $key (qw/ author comment date files rev /) {
26 140 50       475 croak 'invalid CodeRepos commit ping format' unless defined $stuff->{$key};
27             }
28              
29 28         579 $stuff->{date} = DateTime::Format::HTTP->parse_datetime($stuff->{date});
30              
31 28         22414 for (@{ $stuff->{files} }) {
  28         99  
32 570         2989 $_->{path_list} = [ split '/', $_->{path} ];
33             }
34              
35 28         378 bless { %$stuff }, $class;
36             }
37              
38 18     18 0 94 sub revision { shift->{rev} }
39             *rev = \&revision;
40 0     0 0 0 sub comment { shift->{comment} }
41 0     0 0 0 sub date { shift->{date} }
42 0     0 0 0 sub author { shift->{author} }
43 0     0 0 0 sub files { shift->{files} }
44              
45             sub changes_base {
46 10     10 0 18 my $self = shift;
47              
48 10         22 my $changes_projs = {};
49 10         29 for my $file (@{ $self->{files} }) {
  10         29  
50 147 100       408 next unless $file->{path_list}->[0];
51 146         282 my $proj = 'proj_' . $file->{path_list}->[0];
52 146         341 my $path = $self->$proj($file);
53 146         458 $changes_projs->{$path}++;
54             }
55              
56 10         39 for my $top ($self->toplevel_updates) {
57 7 100       23 next unless $top->{status} eq '_U';
58 6         10 my $path = join '/', @{ $top->{path_list} };
  6         16  
59 6         21 delete $changes_projs->{$path};
60             }
61              
62 10         17 my @ret;
63 10         18 for my $path ( sort { $changes_projs->{$b} <=> $changes_projs->{$a} } keys %{ $changes_projs } ) {
  0         0  
  10         41  
64 10         28 push @ret, $path;
65             }
66 10 50       33 return unless @ret;
67 10 50       358 return @ret > 1 ? [ @ret ] : $ret[0];
68             }
69              
70             sub toplevel_updates {
71 10     10 0 21 my $self = shift;
72              
73 10         14 my @ret;
74 10         13 for my $file (@{ $self->{files} }) {
  10         28  
75 147         155 my @list = @{ $file->{path_list} };
  147         430  
76 147 100 100     1305 unless (($list[0] || '') eq 'lang' && ($list[2] || '') eq 'misc') {
      100        
      100        
77 145 100       550 next if @list > 2;
78             }
79 7         19 push @ret, $file;
80             }
81 10         33 @ret;
82             }
83              
84              
85             # projects
86             # book config corp dan docs dotfiles lang platform poem websites
87             our %PROJECT_BASE;
88             for my $proj (qw/ platform docs dan corp /) {
89             $PROJECT_BASE{$proj} = 3;
90             }
91             for my $proj (qw/ config websites dotfiles poem book /) {
92             $PROJECT_BASE{$proj} = 2;
93             }
94              
95             for my $proj (keys %PROJECT_BASE) {
96 5     5   45 no strict 'refs';
  5         8  
  5         244  
97             *{"proj_$proj"} = sub {
98 5     5   31 use strict;
  5         24  
  5         1286  
99 1     1   3 my($self, $file) = @_;
100 1         2 my @list = @{ $file->{path_list} };
  1         4  
101 1         3 my $max = $PROJECT_BASE{$proj};
102 1 50       4 $max = @list < $max ? scalar(@list) : $max;
103 1         1 $max--;
104 1         6 join '/', @list[0..$max];
105             }
106             }
107              
108             sub proj_lang {
109 145     145 0 405 my($self, $file) = @_;
110 145         185 my @list = @{ $file->{path_list} };
  145         694  
111 145         222 my $max = scalar(@list) - 1;
112 145 100       302 if ($list[2]) {
113 141 100 66     723 if ($list[2] eq 'misc' || ($list[1] eq 'javascript' && $list[2] eq 'userscripts')) {
      66        
114 3 100       9 $max = $list[3] ? 3 : 2;
115             } else {
116 138         288 $max = 2;
117             }
118             }
119 145         625 join '/', @list[0..$max];
120             }
121              
122              
123             1;
124             __END__