blib/lib/Github/Fork/Parent.pm

filter: Statementstmt 87.7% 50 / 57 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 50.0% 2 / 4 totaltotal 79.1% 72 / 91 CC 11 SCAR 11.8
CC
11
Cov
78%
github_parent_author
5.4
github_parent
4.2
get_repo_data
3.6
CRAP 3.2
« Build ↑ Summary
1 package Github::Fork::Parent;
2
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 =head1 NAME
8
9 Github::Fork::Parent - Perl module to determine which repository stands in a root of GitHub forking hierarhy.
10
11 =head1 VERSION
12
13 Version 0.24
14
15 =cut
16
17 2 our $VERSION = '0.24';
18
19
20 =head1 SYNOPSIS
21
22 my $parent_url = github_parent('git://github.com/chorny/plagger.git');
23 #returns https://github.com/miyagawa/plagger
24
25 =head1 FUNCTIONS
26
27 =head2 github_parent
28
29 Takes link to repository (git://, git@ or http://) and returns http link to root repository.
30
31 =head2 github_parent_author
32
33 Takes link to repository (git://, git@ or http://) and returns owner of root repository.
34
35 =cut
36
37 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 #use YAML::Tiny 1.40;
39 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
40
41 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 our @EXPORT = qw(github_parent github_parent_author);
43
44 sub get_repo_data {
45 4 ▶ my ($author,$project)=@_;
Subroutine get_repo_data: called CC=3 SCAR=12.7
Pod undocumented
46 #my $url = "http://github.com/api/v2/yaml/repos/show/$author/$project/network";
47 4 my $url = "https://api.github.com/repos/$author/$project";
48
49 4 my $ua=LWP::UserAgent->new();
50 4 $ua->timeout(50);
51 4 my $response = $ua->get($url);
52 4 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 4 0 4
53 4 my $yaml = $response->content();
54 4 return $yaml;
55 } else {
56 0 ▶ if ($response->code eq '404') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code eq '404') { } 0 0 0
57 0 return undef;
58 } else {
59 0 die "Could not GET data (".$response->status_line.")";
60 }
61 }
62 }
63
64 sub parse_github_links {
65 5 ▶ my $link=shift;
Subroutine parse_github_links: called CC=2 SCAR=7.6
Pod undocumented
66 5 ▶ if ($link=~m#^(?:\Qgit://github.com/\E|git\@github\.com:|https?://github\.com/)([^/]+)/([^/.]+)(?:\.git)?$#) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ m[^(?:git\:\/\/github\.com\/|git\@github\.com:|https?://github\.com/)([^/]+)/([^/.]+)(?:\.git)?$]) { } 5 0 5
67 5 return ($1,$2);
68 } else {
69 0 return (undef,undef);
70 }
71
72 }
73
74 sub github_parent {
75 2 ▶ my $link=shift;
Subroutine github_parent: called CC=4 SCAR=14.5
Pod documented
76 2 my ($author,$project)=parse_github_links($link);
77 2 ▶ return $link unless $author;
Branch: 1 branch
BranchTrueFalseTotal
unless $author 0 2 2
78 2 my $yaml_content=get_repo_data($author,$project);
79 2 ▶ if ($yaml_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml_content) { } 2 0 2
80 #my $yaml=YAML::Tiny->read_string($yaml_content) or die;
81 2 my $yaml=decode_json($yaml_content);
82 2 my $source_url=$yaml->{source}{html_url};
83 2 ▶ die unless $source_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_url 0 2 2
84 2 return $source_url;
85 } else {
86 0 die "No content for $author/$project";
87 }
88 }
89
90 sub github_parent_author {
91 #my $link=shift;
92 #my $link1=github_parent($link);
93 #my ($author,$project)=parse_github_links($link1);
94 #die "Cannot get author from '$link1'" unless $author;
95 #return $author;
96 2 ▶ my $link=shift;
Subroutine github_parent_author: called CC=5 SCAR=16.8
Pod documented
97 2 my ($author,$project)=parse_github_links($link);
98 2 ▶ return $link unless $author;
Branch: 1 branch
BranchTrueFalseTotal
unless $author 0 2 2
99 2 my $yaml_content=get_repo_data($author,$project);
100 2 ▶ if ($yaml_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml_content) { } 2 0 2
101 #my $yaml=YAML::Tiny->read_string($yaml_content) or die;
102 2 my $yaml=decode_json($yaml_content);
103 2 ▶ return $author unless $yaml->{'fork'};
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml->{"fork"} 1 1 2
104 1 my $source=$yaml->{source}{owner}{login};
105 1 ▶ die "No login in YAML for $link" unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 1 1
106 1 return $source;
107 0 die;
108 } else {
109 0 die "No content";
110 }
111 }
112
113 =head1 AUTHOR
114
115 Alexandr Ciornii, C<< <alexchorny at gmail.com> >>
116
117 =head1 BUGS
118
119 Please report any bugs or feature requests to C<bug-github-fork-parent at rt.cpan.org>, or through
120 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Github-Fork-Parent>. I will be notified, and then you'll
121 automatically be notified of progress on your bug as I make changes.
122
123 =head1 SUPPORT
124
125 You can find documentation for this module with the perldoc command.
126
127 perldoc Github::Fork::Parent
128
129
130 You can also look for information at:
131
132 =over 4
133
134 =item * RT: CPAN's request tracker
135
136 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Github-Fork-Parent>
137
138 =item * AnnoCPAN: Annotated CPAN documentation
139
140 L<http://annocpan.org/dist/Github-Fork-Parent>
141
142 =item * CPAN Ratings
143
144 L<http://cpanratings.perl.org/d/Github-Fork-Parent>
145
146 =item * Search CPAN
147
148 L<http://search.cpan.org/dist/Github-Fork-Parent/>
149
150 =back
151
152
153 =head1 SEE ALSO
154
155 Net::GitHub
156
157 =head1 ACKNOWLEDGEMENTS
158
159
160 =head1 COPYRIGHT & LICENSE
161
162 Copyright 2009-2016 Alexandr Ciornii.
163
164 This program is free software; you can redistribute it and/or modify it
165 under the terms of either: the GNU General Public License as published
166 by the Free Software Foundation; or the Artistic License.
167
168 See http://dev.perl.org/licenses/ for more information.
169
170
171 =cut
172
173 2 1; # End of Github::Fork::Parent
« Build ↑ Summary