feb14cf4a1e780e585361c80b4bf97d4c558ad8d
[scpubgit/System-Introspector-Report.git] / lib / System / Introspector / Report / Builder / Repositories / Git / Locations.pm
1 package System::Introspector::Report::Builder::Repositories::Git::Locations;
2 use Moo;
3
4 extends 'System::Introspector::Report::Builder';
5
6 has collect_matching        => (is => 'ro', default => sub { [] });
7 has _locations_by_origin    => (is => 'lazy', default => sub { {} });
8
9 sub required_data {
10   return qw(
11     repositories/git
12     host
13   );
14 }
15
16 sub _match_origin {
17   my ($self, $origin) = @_;
18   my @match_rx = @{ $self->collect_matching };
19   for my $rx (@match_rx) {
20     if ($origin =~ qr{^$rx$}i) {
21       return $1;
22     }
23   }
24   return undef;
25 }
26
27 sub collect_from {
28   my ($self, $remote, $data) = @_;
29   my $git = $data->{'repositories/git'}{git} || {};
30   for my $location (keys %$git) {
31     my $origin  = $git->{$location}{config}{contents}{'remote.origin.url'}
32       or next;
33     my $matched = $self->_match_origin($origin)
34       or next;
35     push @{$self->_locations_by_origin->{$matched}}, {
36       remote    => $remote,
37       hostname  => $data->{host}{hostname},
38       location  => $location,
39       origin    => $origin,
40     };
41   }
42   return 1;
43 }
44
45 sub render_reports {
46   my ($self) = @_;
47   my @columns = (
48     { key => 'remote',   label => 'Remote Host' },
49     { key => 'hostname', label => 'Hostname' },
50     { key => 'location', label => 'Location' },
51   );
52   my $collected = $self->_locations_by_origin;
53   return map {
54     my $identifier  = $_;
55     my $rows        = $collected->{$identifier};
56     {
57       columns   => [@columns],
58       title     => "$identifier Checkouts",
59       id        => ['repositories-git-locations', $identifier],
60       rowid     => [qw( remote location )],
61       meta      => { repository => $identifier },
62       rows      => [
63         sort {
64           ($a->{remote} cmp $b->{remote})
65             ||
66           ($a->{location} cmp $b->{location})
67         } @$rows,
68       ],
69     };
70   } keys %$collected;
71 }
72
73 1;