| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::gh::Command::Fork; | 
| 2 | 1 |  |  | 1 |  | 971 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 3 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use base qw(App::gh::Command); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 60 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use App::gh::Utils; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 768 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | App::gh::Command::Fork - fork current repository. | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 USAGE | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =pod | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | curl -F 'login=schacon' -F 'token=XXX' http://github.com/api/v2/yaml/repos/fork/dim/retrospectiva | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | $VAR1 = { | 
| 21 |  |  |  |  |  |  | 'repository' => { | 
| 22 |  |  |  |  |  |  | 'has_downloads' => bless( do{\(my $o = 1)}, 'JSON::Boolean' ), | 
| 23 |  |  |  |  |  |  | 'owner' => 'c9s', | 
| 24 |  |  |  |  |  |  | 'has_issues' => bless( do{\(my $o = 0)}, 'JSON::Boolean' ), | 
| 25 |  |  |  |  |  |  | 'name' => 'AnyMQ', | 
| 26 |  |  |  |  |  |  | 'private' => $VAR1->{'repository'}{'has_issues'}, | 
| 27 |  |  |  |  |  |  | 'has_wiki' => $VAR1->{'repository'}{'has_downloads'}, | 
| 28 |  |  |  |  |  |  | 'pushed_at' => '2010/04/06 00:40:45 -0700', | 
| 29 |  |  |  |  |  |  | 'description' => 'Simple message queue based on AnyEvent', | 
| 30 |  |  |  |  |  |  | 'watchers' => 1, | 
| 31 |  |  |  |  |  |  | 'forks' => 0, | 
| 32 |  |  |  |  |  |  | 'homepage' => '', | 
| 33 |  |  |  |  |  |  | 'created_at' => '2010/07/21 06:08:11 -0700', | 
| 34 |  |  |  |  |  |  | 'fork' => $VAR1->{'repository'}{'has_downloads'}, | 
| 35 |  |  |  |  |  |  | 'url' => 'http://github.com/c9s/AnyMQ', | 
| 36 |  |  |  |  |  |  | 'open_issues' => 0 | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | }; | 
| 39 |  |  |  |  |  |  | =cut | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub options { ( | 
| 42 | 0 |  |  | 0 | 0 |  | "verbose" => "verbose", | 
| 43 |  |  |  |  |  |  | "ssh" => "protocol_ssh",    # git@github.com:c9s/repo.git | 
| 44 |  |  |  |  |  |  | "http" => "protocol_http",  # http://github.com/c9s/repo.git | 
| 45 |  |  |  |  |  |  | "https" => "protocol_https",         # https://github.com/c9s/repo.git | 
| 46 |  |  |  |  |  |  | "git|ro"   => "protocol_git"         # git://github.com/c9s/repo.git | 
| 47 |  |  |  |  |  |  | ) } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub run { | 
| 51 | 0 |  |  | 0 | 0 |  | my $self=shift; | 
| 52 | 0 |  |  |  |  |  | my $user; | 
| 53 |  |  |  |  |  |  | my $repo; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 | 0 |  |  |  |  | if (@_) { | 
| 56 | 0 |  |  |  |  |  | $user = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | # copied from Github::Fork::Parent | 
| 59 | 0 | 0 | 0 |  |  |  | if ($user && $user=~m{^(?:\Qgit://github.com/\E|git\@github\.com:\E|\Qhttp://github.com/\E)([^/]+)/([^/]+)(?:\.git)?$}) { | 
|  |  | 0 | 0 |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | ($user,$repo)=($1,$2); | 
| 61 |  |  |  |  |  |  | } elsif( $user && $user =~ /\// ) { | 
| 62 | 0 |  |  |  |  |  | ($user,$repo) = split /\//,$user; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | else { | 
| 65 | 0 |  |  |  |  |  | $repo = shift; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | my $gh_id = App::gh->config->github_id; | 
| 71 | 0 |  | 0 |  |  |  | my $gh_token = App::gh->config->github_token | 
| 72 |  |  |  |  |  |  | || App::gh->config->github_password; | 
| 73 | 0 | 0 | 0 |  |  |  | unless( $gh_id && $gh_token ) { | 
| 74 | 0 |  |  |  |  |  | die "Github credentials not found. Cannot fork repository.\n"; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 | 0 |  |  |  |  | unless ( $repo ) { | 
| 79 |  |  |  |  |  |  | # detect .git directory | 
| 80 | 0 | 0 |  |  |  |  | if ( -e ".git/config" ) { | 
| 81 | 0 |  |  |  |  |  | my $config = App::gh->config->current(); | 
| 82 | 0 |  |  |  |  |  | for my $remote ( values %{ $config->{remote} } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | # git://github.com/miyagawa/Tatsumaki.git | 
| 84 |  |  |  |  |  |  | # http://github.com/miyagawa/Tatsumaki.git | 
| 85 | 0 | 0 | 0 |  |  |  | if ( $remote->{url} =~ m{(?:git|https?)://github.com/(.*?)/(.*?)\.git} | 
| 86 |  |  |  |  |  |  | || $remote->{url} =~ m{git\@github.com:(.*?)/(.*?)\.git} ) | 
| 87 |  |  |  |  |  |  | { | 
| 88 | 0 | 0 | 0 |  |  |  | die unless( $1 || $2 ); | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 |  |  |  |  |  | ($user,$repo) = ( $1 , $2 ); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  |  | info "Found GitHub repository of $user/$repo"; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  |  | my $_remotes = qx(git remote | grep @{[ $gh_id ]}); | 
|  | 0 |  |  |  |  |  |  | 
| 95 | 0 | 0 |  |  |  |  | if( $_remotes ) { | 
| 96 | 0 |  |  |  |  |  | die "Remote @{[ $gh_id ]} exists.\n"; | 
|  | 0 |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | my $remote_uri; | 
| 100 | 0 | 0 |  |  |  |  | if ( $self->{protocol_https} ) { | 
| 101 | 0 |  |  |  |  |  | $remote_uri = qq( https://@{[ $gh_id ]}\@github.com/@{[ $gh_id ]}/$repo.git); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | } else { | 
| 103 | 0 |  |  |  |  |  | $remote_uri = qq( git\@github.com:@{[ $gh_id ]}/$repo.git); | 
|  | 0 |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | }; | 
| 105 | 0 |  |  |  |  |  | info "Adding remote '@{[ $gh_id ]}' => $remote_uri"; | 
|  | 0 |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # url = git@github.com:c9s/App-gh.git | 
| 108 | 0 |  |  |  |  |  | my $cmd = qq( git remote add @{[ $gh_id ]} $remote_uri); | 
|  | 0 |  |  |  |  |  |  | 
| 109 | 0 |  |  |  |  |  | _debug $cmd; | 
| 110 | 0 |  |  |  |  |  | qx($cmd); | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | info "Remote added."; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | info "Forking..."; | 
| 119 | 0 |  |  |  |  |  | my $data = App::gh::API->fork($user, $repo); | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 1 |  |  | 1 |  | 1071 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 7313 |  | 
|  | 1 |  |  |  |  | 181 |  | 
| 122 | 0 |  |  |  |  |  | _debug Dumper( $data ); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | info "Repository forked:"; | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 0 |  |  |  |  |  | App::gh::Utils->print_repo_info($data->{repository}); | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # $data = $data->{repository}; | 
| 129 |  |  |  |  |  |  | # print "  Name:          " . $data->{name} . "\n"; | 
| 130 |  |  |  |  |  |  | # print "  Description:   " . $data->{description} . "\n"; | 
| 131 |  |  |  |  |  |  | # print "  Owner:         " . $data->{owner} . "\n"; | 
| 132 |  |  |  |  |  |  | # print "  Watchers:      " . $data->{watchers} . "\n"; | 
| 133 |  |  |  |  |  |  | # print "  Created at:    " . $data->{created_at} . "\n"; | 
| 134 |  |  |  |  |  |  | # print "  Pushed at:     " . $data->{pushed_at} . "\n"; | 
| 135 |  |  |  |  |  |  | # print "  Fork:          " . $data->{'fork'} . "\n"; | 
| 136 |  |  |  |  |  |  | # print "  URL:           " . $data->{url} . "\n"; | 
| 137 |  |  |  |  |  |  | # print "  Homepage:      " . ($data->{homepage}||'') . "\n"; | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | 1; |