| 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 hierarchy. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 1.0 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '1.0'; | |||||||||
| 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
| |||||||||||
| 53 | 4 | my $yaml = $response->content(); | |||||||||
| 54 | 4 | return $yaml; | |||||||||
| 55 | } else { | ||||||||||
| 56 | 0 | ▶ | if ($response->code eq '404') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 10 | ▶ | my $link=shift; | ||||||||
|
Subroutine
parse_github_links: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||
| 66 | 10 | $link =~ s/\.git$//; #github does not allow repositories ending in .git, so we can safely remove extension | |||||||||
| 67 | 10 | ▶ | if ($link=~m{^ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | (?:\Qgit://github.com/\E|git\@github\.com:|https?://github\.com/) | ||||||||||
| 69 | ([^/]+)/([^/]+) #repository name can contain dots | ||||||||||
| 70 | $ }x | ||||||||||
| 71 | ) { | ||||||||||
| 72 | 10 | return ($1,$2); | |||||||||
| 73 | } else { | ||||||||||
| 74 | 0 | return (undef,undef); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub github_parent { | ||||||||||
| 80 | 2 | ▶ | my $link=shift; | ||||||||
|
Subroutine
github_parent: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||
| 81 | 2 | my ($author,$project)=parse_github_links($link); | |||||||||
| 82 | 2 | ▶ | return $link unless $author; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 2 | my $yaml_content=get_repo_data($author,$project); | |||||||||
| 84 | 2 | ▶ | if ($yaml_content) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | #my $yaml=YAML::Tiny->read_string($yaml_content) or die; | ||||||||||
| 86 | 2 | my $yaml=decode_json($yaml_content); | |||||||||
| 87 | 2 | my $source_url=$yaml->{source}{html_url}; | |||||||||
| 88 | 2 | ▶ | die unless $source_url; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 2 | return $source_url; | |||||||||
| 90 | } else { | ||||||||||
| 91 | 0 | die "No content for $author/$project"; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | sub github_parent_author { | ||||||||||
| 96 | #my $link=shift; | ||||||||||
| 97 | #my $link1=github_parent($link); | ||||||||||
| 98 | #my ($author,$project)=parse_github_links($link1); | ||||||||||
| 99 | #die "Cannot get author from '$link1'" unless $author; | ||||||||||
| 100 | #return $author; | ||||||||||
| 101 | 2 | ▶ | my $link=shift; | ||||||||
|
Subroutine
github_parent_author: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||
| 102 | 2 | my ($author,$project)=parse_github_links($link); | |||||||||
| 103 | 2 | ▶ | return $link unless $author; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 2 | my $yaml_content=get_repo_data($author,$project); | |||||||||
| 105 | 2 | ▶ | if ($yaml_content) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | #my $yaml=YAML::Tiny->read_string($yaml_content) or die; | ||||||||||
| 107 | 2 | my $yaml=decode_json($yaml_content); | |||||||||
| 108 | 2 | ▶ | return $author unless $yaml->{'fork'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 1 | my $source=$yaml->{source}{owner}{login}; | |||||||||
| 110 | 1 | ▶ | die "No login in YAML for $link" unless $source; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | return $source; | |||||||||
| 112 | 0 | die; | |||||||||
| 113 | } else { | ||||||||||
| 114 | 0 | die "No content"; | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | =head1 AUTHOR | ||||||||||
| 119 | |||||||||||
| 120 | Alexandr Ciornii, C<< <alexchorny at gmail.com> >> | ||||||||||
| 121 | |||||||||||
| 122 | =head1 BUGS | ||||||||||
| 123 | |||||||||||
| 124 | Please report any bugs or feature requests to C<bug-github-fork-parent at rt.cpan.org>, or through | ||||||||||
| 125 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Github-Fork-Parent>. I will be notified, and then you'll | ||||||||||
| 126 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 127 | |||||||||||
| 128 | =head1 SUPPORT | ||||||||||
| 129 | |||||||||||
| 130 | You can find documentation for this module with the perldoc command. | ||||||||||
| 131 | |||||||||||
| 132 | perldoc Github::Fork::Parent | ||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | You can also look for information at: | ||||||||||
| 136 | |||||||||||
| 137 | =over 4 | ||||||||||
| 138 | |||||||||||
| 139 | =item * RT: CPAN's request tracker | ||||||||||
| 140 | |||||||||||
| 141 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Github-Fork-Parent> | ||||||||||
| 142 | |||||||||||
| 143 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 144 | |||||||||||
| 145 | L<http://annocpan.org/dist/Github-Fork-Parent> | ||||||||||
| 146 | |||||||||||
| 147 | =item * CPAN Ratings | ||||||||||
| 148 | |||||||||||
| 149 | L<http://cpanratings.perl.org/d/Github-Fork-Parent> | ||||||||||
| 150 | |||||||||||
| 151 | =item * Search CPAN | ||||||||||
| 152 | |||||||||||
| 153 | L<http://search.cpan.org/dist/Github-Fork-Parent/> | ||||||||||
| 154 | |||||||||||
| 155 | =back | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =head1 SEE ALSO | ||||||||||
| 159 | |||||||||||
| 160 | Net::GitHub | ||||||||||
| 161 | |||||||||||
| 162 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 166 | |||||||||||
| 167 | Copyright 2009-2017 Alexandr Ciornii. | ||||||||||
| 168 | |||||||||||
| 169 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 170 | under the terms of either: the GNU General Public License as published | ||||||||||
| 171 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 172 | |||||||||||
| 173 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 174 | |||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | |||||||||||
| 178 | 2 | 1; # End of Github::Fork::Parent | |||||||||