| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package App::Nopaste::Service::Gist; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Service provider for GitHub gist - http://gist.github.com/ | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '1.013'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use parent 'App::Nopaste::Service'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean 0.19; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub available { 1 } | ||||||||||||||||||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub forbid_in_default { 0 } | ||||||||||||||||||||||||||
|
Subroutine
forbid_in_default: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub nopaste { | ||||||||||||||||||||||||||
|
Subroutine
nopaste: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 0 | $self->run(@_); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($self, %arg) = @_; | |||||||||||||||||||||||||||
| 26 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | my $content = { | ||||||||||||||||||||||||||||
| 29 | public => defined $arg{private} ? JSON->false : JSON->true, | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | defined $arg{desc} ? (description => $arg{desc}) : (), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $filename = defined $arg{filename} | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | ? path($arg{filename})->basename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | : 'nopaste'; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $content->{files} = { | ||||||||||||||||||||||||||||
| 38 | $filename => { | ||||||||||||||||||||||||||||
| 39 | content => $arg{text} | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 0 | }; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | $content = encode_json($content); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my %auth = $self->_get_auth; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $url = 'https://api.github.com/gists'; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $res = do { | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($auth{oauth_token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | $ua->post( | |||||||||||||||||||||||||||
| 52 | $url, | ||||||||||||||||||||||||||||
| 53 | 'Authorization' => "token $auth{oauth_token}", | ||||||||||||||||||||||||||||
| 54 | Content => $content, | ||||||||||||||||||||||||||||
| 55 | Content_Type => 'application/json', | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 0 | use_module('HTTP::Request::Common'); | |||||||||||||||||||||||||||
| 60 | 0 | my $req = HTTP::Request::Common::POST($url, Content => $content); | |||||||||||||||||||||||||||
| 61 | 0 | $req->authorization_basic(@auth{qw/username password/}); | |||||||||||||||||||||||||||
| 62 | 0 | $ua->request($req); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return $self->return($res); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub _get_auth { | ||||||||||||||||||||||||||
|
Subroutine
_get_auth: called CC=7 SCAR=23.2
| |||||||||||||||||||||||||||||
| 70 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if (my $oauth_token = $ENV{GITHUB_OAUTH_TOKEN}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | 1 | return (oauth_token => $oauth_token); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | ▶ | elsif ($ENV{GITHUB_USER} && $ENV{GITHUB_PASSWORD}) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$ENV{"GITHUB_USER"} and $ENV{"GITHUB_PASSWORD"}
MC/DC100%2/2
$ENV{"GITHUB_USER"} and $ENV{"GITHUB_PASSWORD"}
$ENV{"GITHUB_USER"}
$ENV{"GITHUB_PASSWORD"}
Truth table100%3/3
A and B
A= $ENV{"GITHUB_USER"}B= $ENV{"GITHUB_PASSWORD"}
| |||||||||||||||||||||||||||||
| 76 | return ( | ||||||||||||||||||||||||||||
| 77 | username => $ENV{GITHUB_USER}, | ||||||||||||||||||||||||||||
| 78 | password => $ENV{GITHUB_PASSWORD}, | ||||||||||||||||||||||||||||
| 79 | 1 | ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | # this is also done by the fallback mechanism in Config::Identity::GitHub. | ||||||||||||||||||||||||||||
| 83 | 2 | my $github_config = path('~', '.github'); | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | if (-f $github_config) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | my $content = $github_config->slurp_utf8; | |||||||||||||||||||||||||||
| 86 | 0 | my ($username) = $content =~ m/\blogin (.+)(?:$|#)/m; | |||||||||||||||||||||||||||
| 87 | 0 | my ($password) = $content =~ m/\bpassword (.+)(?:$|#)/m; | |||||||||||||||||||||||||||
| 88 | return ( | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | username => $username, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$username and $password
MC/DC0%0/2
$username and $password
$username
$password
Truth table0%0/3
A and B
A= $usernameB= $password
| |||||||||||||||||||||||||||||
| 90 | password => $password, | ||||||||||||||||||||||||||||
| 91 | ) if $username and $password; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | die join("\n", | |||||||||||||||||||||||||||
| 96 | "Export GITHUB_OAUTH_TOKEN first. For example:", | ||||||||||||||||||||||||||||
| 97 | " perl -MApp::Nopaste::Service::Gist -e 'App::Nopaste::Service::Gist->create_token'", | ||||||||||||||||||||||||||||
| 98 | "", | ||||||||||||||||||||||||||||
| 99 | "OR you can export GITHUB_USER and GITHUB_PASSWORD.", | ||||||||||||||||||||||||||||
| 100 | "OR you can set 'login' and 'password' in ~/.github.", | ||||||||||||||||||||||||||||
| 101 | ) . "\n"; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub create_token { | ||||||||||||||||||||||||||
|
Subroutine
create_token: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | local $| = 1; | |||||||||||||||||||||||||||
| 108 | 0 | print "Username: "; | |||||||||||||||||||||||||||
| 109 | 0 | chomp(my $username = <>); | |||||||||||||||||||||||||||
| 110 | 0 | print "Password: "; | |||||||||||||||||||||||||||
| 111 | 0 | chomp(my $password = <>); | |||||||||||||||||||||||||||
| 112 | 0 | print "\n\n"; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | exit unless $username && $password; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$username and $password
MC/DC0%0/2
$username and $password
$username
$password
Truth table0%0/3
A and B
A= $usernameB= $password
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $parameters = { | |||||||||||||||||||||||||||
| 117 | scopes => ["gist"], | ||||||||||||||||||||||||||||
| 118 | note => "App::Nopaste", | ||||||||||||||||||||||||||||
| 119 | note_url => "https://metacpan.org/module/App::Nopaste", | ||||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $request = HTTP::Request->new(POST => 'https://api.github.com/authorizations'); | |||||||||||||||||||||||||||
| 125 | 0 | $request->authorization_basic($username, $password); | |||||||||||||||||||||||||||
| 126 | 0 | $request->content(encode_json($parameters)); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $response = $ua->request($request); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $response_content = decode_json($response->decoded_content); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($response_content->{token} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | print "GITHUB_OAUTH_TOKEN=$response_content->{token}\n"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | print $response_content->{message} || "Unspecified error", "\n"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$response_content->{'message'} || 'Unspecified error'
MC/DC0%0/1
$response_content->{'message'} || 'Unspecified error'
$response_content->{'message'}
Truth table0%0/2
A
A= $response_content->{'message'}
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub return { | ||||||||||||||||||||||||||
|
Subroutine
return: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my ($self, $res) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | my $text = $res->status_line; | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($res->code == 401) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $text .= "\nYou may need to authorize $0. See `perldoc " . __PACKAGE__ . "`"; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | return (0, "Failed: " . $text); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (($res->header('Client-Warning') || '') eq 'Internal response') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->header('Client-Warning') || ''
MC/DC0%0/1
$res->header('Client-Warning') || ''
$res->header('Client-Warning')
Truth table0%0/2
A
A= $res->header('Client-Warning')
| |||||||||||||||||||||||||||||
| 152 | 0 | return (0, "LWP Error: " . $res->content); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $id = decode_json($res->content)->{id}; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return (0, "Could not find paste link.") if !$id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | return (1, "https://gist.github.com/$id"); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||