Plugin/CheckIssues.pm

filter: Statementstmt 73.9% 85 / 115 Branchbran 45.6% 21 / 46 Conditioncond 60.7% 17 / 28 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 3 totaltotal 63.5% 141 / 222 CC 38 SCAR 23.8
CC
38
Cov
65%
_fetch
30.0
get_issues
15.9
__ANON__
12.0
CRAP 10.8
↑ Summary App/Command/issues.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Dist::Zilla::Plugin::CheckIssues; # git description: v0.010-8-gf6e9be7
4 # vim: set ts=8 sts=4 sw=4 tw=115 et :
5 # ABSTRACT: Retrieve count of outstanding RT and github issues for your distribution
6 # KEYWORDS: plugin bugs issues rt github
7  
8 4 our $VERSION = '0.011';
9  
10 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 with 'Dist::Zilla::Role::BeforeRelease';
12 4 ▶use List::Util 1.33 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Term::ANSIColor 3.00 'colored';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 has [qw(rt github colour)] => (
18 is => 'ro', isa => 'Bool',
19 default => 1,
20 );
21  
22 has repo_url => (
23 is => 'rw', isa => 'Str',
24 lazy => 1,
25 3 ▶ default => sub {
Subroutine __ANON__: called CC=11 SCAR=24.3
26 3 my $self = shift;
27  
28 3 my $url;
29 3 ▶ if ($self->zilla->built_in)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zilla->built_in) { } 2 1 3
30 {
31 # we've already done a build, so distmeta is available
32 2 my $distmeta = $self->zilla->distmeta;
33 2 ▶ $url = (($distmeta->{resources} || {})->{repository} || {})->{url} || '';
Condition100%6/6
+(+($distmeta->{'resources'} || {})->{'repository'} || {})->{'url'} || ''
l!l
11
+($distmeta->{'resources'} || {})->{'repository'} || {}
l!l
11
$distmeta->{'resources'} || {}
l!l
11
MC/DC100%1/1
+(+($distmeta->{'resources'} || {})->{'repository'} || {})->{'url'} || ''
$distmeta->{'resources'}
Truth table100%2/2
A
A= $distmeta->{'resources'}
Aresult
11
00
34 }
35 else
36 {
37 # no build (we're probably running the command): we cannot simply
38 # call ->distmeta because various plugins will cause side-effects
39 # with invalid assumptions (no files have been gathered, etc) --
40 # so we just query a short list of plugins that we know can
41 # provide repository resource metadata
42 my @plugins = grep {
43 2 my $plugin = $_;
44 6 ▶ any { $plugin->isa('Dist::Zilla::Plugin::' . $_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 qw(MetaResources AutoMetaResources GithubMeta GitHub::Meta Repository)
46 1 } @{ $self->zilla->plugins_with(-MetaProvider) };
47  
48 1 ▶ $self->log('Cannot find any resource metadata-providing plugins to run!')
Branch: 1 branch
BranchTrueFalseTotal
unless @plugins 0 1 1
49 if not @plugins;
50  
51 1 foreach my $plugin (@plugins)
52 {
53 1 $self->log_debug([ 'calling metadata for %s', $plugin->plugin_name ]);
54 1 my $plugin_meta = $plugin->metadata;
55 1 ▶ $url = (($plugin_meta->{resources} || {})->{repository} || {})->{url} || '';
Condition50%3/6
+(+($plugin_meta->{'resources'} || {})->{'repository'} || {})->{'url'} || ''
l!l
10
+($plugin_meta->{'resources'} || {})->{'repository'} || {}
l!l
10
$plugin_meta->{'resources'} || {}
l!l
10
MC/DC0%0/1
+(+($plugin_meta->{'resources'} || {})->{'repository'} || {})->{'url'} || ''
$plugin_meta->{'resources'}
Truth table50%1/2
A
A= $plugin_meta->{'resources'}
Aresult
11
00
56 1 ▶ last if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 1 0 1
57 }
58 }
59 3 $url;
60 },
61 4 );
62  
63 # owner_name, repo_name
64 has _github_owner_repo => (
65 isa => 'ArrayRef[Str]',
66 init_arg => undef,
67 lazy => 1,
68 4 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
69 4 my $self = shift;
70  
71 4 ▶ if (my $url = $self->repo_url)
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $self->repo_url) 3 1 4
72 {
73 3 $self->log_debug([ 'getting issue data for %s...', $url ]);
74 3 my ($owner_name, $repo_name) = $url =~ m{github\.com[:/]([^/]+)/([^/]+?)(?:/|\.git|$)};
75 3 ▶ return [ $owner_name, $repo_name ] if $owner_name and $repo_name;
Branch: 1 branch
BranchTrueFalseTotal
if $owner_name and $repo_name 3 0 3
Condition33%1/3
$owner_name and $repo_name
!ll&&!rl&&r
003
MC/DC0%0/2
$owner_name and $repo_name
$owner_name $repo_name
Truth table33%1/3
A and B
A= $owner_nameB= $repo_name
ABresult
0X0
100
111
76 }
77  
78 1 $self->log('failed to find a github repo in metadata');
79 1 [];
80 },
81 4 traits => ['Array'],
82 handles => { _github_owner_repo => 'elements' },
83 );
84  
85 3 ▶sub mvp_aliases { +{ color => 'colour' } }
Subroutine mvp_aliases: called CC=1 SCAR=0.0
Pod undocumented
86  
87 # metaconfig is unimportant for this distribution since it does not alter the
88 # built distribution in any way
89 around dump_config => sub
90 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my ($orig, $self) = @_;
92 0 my $config = $self->$orig;
93  
94 0 ▶ my $data = {
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ne __PACKAGE__ ? : 0 0 0
95 blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
96 };
97 0 ▶ $config->{+__PACKAGE__} = $data if keys %$data;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$data 0 0 0
98  
99 0 return $config;
100 4 };
101  
102 sub get_issues
103 5 ▶{
Subroutine get_issues: called CC=14 SCAR=27.7
Pod undocumented
104 5 my $self = shift;
105  
106 5 my $dist_name = $self->zilla->name;
107  
108 5 my @issues;
109  
110 5 ▶ if ($self->rt)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rt) 4 1 5
111 {
112 4 my %rt_data = $self->_rt_data_for_dist($dist_name);
113 4 ▶ if (defined $rt_data{open} and defined $rt_data{stalled}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rt_data{"open"} and defined $rt_data{"stalled"}) 3 1 4
Condition66%2/3
defined $rt_data{"open"} and defined $rt_data{"stalled"}
!ll&&!rl&&r
103
MC/DC50%1/2
defined $rt_data{"open"} and defined $rt_data{"stalled"}
defined $rt_data{"open"} defined $rt_data{"stalled"}
Truth table66%2/3
A and B
A= defined $rt_data{"open"}B= defined $rt_data{"stalled"}
ABresult
0X0
100
111
114 my $colour = $rt_data{open} ? 'bright_red'
115 3 ▶ : $rt_data{stalled} ? 'yellow'
Branch: 2 branches
BranchTrueFalseTotal
$rt_data{'open'} ? : 3 0 3
$rt_data{'stalled'} ? : 0 0 0
116 : 'green';
117  
118 my @text = (
119 'Issues on RT (https://rt.cpan.org/Public/Dist/Display.html?Name=' . $dist_name . '):',
120 3 ▶ ' open: ' . ($rt_data{open} || 0) . ' stalled: ' . ($rt_data{stalled} || 0),
Condition50%2/4
$rt_data{'open'} || 0
l!l
30
$rt_data{'stalled'} || 0
l!l
30
MC/DC0%0/1
$rt_data{'open'} || 0
$rt_data{'open'}
MC/DC0%0/1
$rt_data{'stalled'} || 0
$rt_data{'stalled'}
Truth table50%1/2
A
A= $rt_data{'open'}
Aresult
11
00
Truth table50%1/2
A
A= $rt_data{'stalled'}
Aresult
11
00
121 );
122  
123 3 ▶ @text = map colored($_, $colour), @text if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 3 3
124 3 push @issues, @text;
125 }
126 }
127  
128 5 ▶ if ($self->github
Branch: 1 branch
BranchTrueFalseTotal
if ($self->github and () = $self->_github_owner_repo) 3 2 5
Condition100%3/3
$self->github and () = $self->_github_owner_repo
!ll&&!rl&&r
113
MC/DC100%2/2
$self->github and () = $self->_github_owner_repo
$self->github () = $self->_github_owner_repo
Truth table100%3/3
A and B
A= $self->githubB= () = $self->_github_owner_repo
ABresult
0X0
100
111
129 and my ($owner_name, $repo_name) = $self->_github_owner_repo)
130 {
131 3 my $issue_count = $self->_github_issue_count($owner_name, $repo_name);
132 3 ▶ if (defined $issue_count)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $issue_count) 3 0 3
133 {
134 3 ▶ my $colour = $issue_count ? 'bright_red' : 'green';
Branch: 1 branch
BranchTrueFalseTotal
$issue_count ? : 3 0 3
135  
136 3 my $url = 'https://github.com/'.$owner_name.'/'.$repo_name;
137 3 my @text = (
138 'Issues and/or pull requests on github ('.$url.'/issues and '.$url.'/pulls):',
139 ' open: ' . $issue_count,
140 );
141  
142 3 ▶ @text = map colored($_, $colour), @text if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 3 3
143 3 push @issues, @text;
144 }
145 }
146  
147 5 return @issues;
148 }
149  
150 sub before_release
151 3 ▶{
Subroutine before_release: called CC=2 SCAR=6.9
Pod undocumented
152 3 my $self = shift;
153  
154 3 $self->log($_) foreach $self->get_issues;
155 }
156  
157 sub _rt_data_for_dist
158 4 ▶{
Subroutine _rt_data_for_dist: called CC=3 SCAR=11.0
159 4 my ($self, $dist_name) = @_;
160  
161 4 my $json = $self->_rt_data_raw;
162 4 ▶ return if not $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 4 4
163  
164 4 require JSON::MaybeXS; JSON::MaybeXS->VERSION('1.001000');
165 4 my $all_data = JSON::MaybeXS->new(utf8 => 0)->decode($json);
166 4 ▶ return if not $all_data->{$dist_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $all_data->{$dist_name} 1 3 4
167  
168 3 my %rt_data;
169 $rt_data{open} = $all_data->{$dist_name}{counts}{active}
170 3 - $all_data->{$dist_name}{counts}{stalled};
171 3 $rt_data{stalled} = $all_data->{$dist_name}{counts}{stalled};
172 3 return %rt_data;
173 }
174  
175 sub _rt_data_raw
176 0 ▶{
Subroutine _rt_data_raw: not called CC=2 SCAR=17.9
177 0 my $self = shift;
178  
179 0 $self->log_debug('fetching RT bug data...');
180 0 my $data = $self->_fetch('https://rt.cpan.org/Public/bugs-per-dist.json');
181 0 ▶ return if not $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
182 0 return $data;
183 }
184  
185 sub _github_issue_count
186 0 ▶{
Subroutine _github_issue_count: not called CC=2 SCAR=17.9
187 0 my ($self, $owner_name, $repo_name) = @_;
188  
189 0 $self->log_debug('fetching github issues data...');
190  
191 0 my $json = $self->_fetch('https://api.github.com/repos/' . $owner_name . '/' . $repo_name);
192 0 ▶ return if not $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
193  
194 0 require JSON::MaybeXS; JSON::MaybeXS->VERSION('1.001000');
195 0 my $data = JSON::MaybeXS->new(utf8 => 0)->decode($json);
196 0 $data->{open_issues_count};
197 }
198  
199 sub _fetch
200 0 ▶{
Subroutine _fetch: not called CC=5 SCAR=34.0
201 0 my ($self, $url) = @_;
202  
203 0 require HTTP::Tiny;
204 0 my $res = HTTP::Tiny->new->get($url);
205 0 ▶ if (not $res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
206 $self->log('could not fetch from '.$url.': got '
207 0 ▶ .($res->{status} && $res->{content} ? $res->{status}.' '.$res->{content} : 'unknown'));
Branch: 1 branch
BranchTrueFalseTotal
$res->{'status'} && $res->{'content'} ? : 0 0 0
Condition0%0/3
$res->{"status"} and $res->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"status"} and $res->{"content"}
$res->{"status"} $res->{"content"}
Truth table0%0/3
A and B
A= $res->{"status"}B= $res->{"content"}
ABresult
0X0
100
111
208 0 return;
209 }
210  
211 0 my $data = $res->{content};
212  
213 0 require HTTP::Headers;
214 0 ▶ if (my $charset = HTTP::Headers->new(%{ $res->{headers} })->content_type_charset)
Branch: 1 branch
BranchTrueFalseTotal
if (my $charset = "HTTP::Headers"->new(%{$$res{"headers"};})->content_type_charset) 0 0 0
215 {
216 0 $data = Encode::decode($charset, $data, Encode::FB_CROAK);
217 }
218  
219 0 return $data;
220 }
221  
222 4 __PACKAGE__->meta->make_immutable;
223  
224 __END__
↑ Summary App/Command/issues.pm »