Nopaste/Service/GitLab.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 107 CC 9 SCAR 24.4
CC
9
Cov
0%
return
30.0
config
12.0
run
12.0
CRAP 11.5
1 use strict;
2 use warnings;
3 package App::Nopaste::Service::GitLab;
4 # ABSTRACT: Service provider for GitLab snippets
5  
6 our $VERSION = '1.013';
7  
8 use parent 'App::Nopaste::Service';
9  
10 use JSON::MaybeXS;
11 use LWP::UserAgent;
12 use Path::Tiny;
13 use namespace::clean 0.19;
14  
15 my $config;
16 sub config {
17 return $config if $config;
18  
19 local *STDERR;
20  
21 for my $key (qw(host token)) {
22 my $value = `git config gitlab.$key`;
23 return ($config = {}) unless $value && ! $?;
24  
25 chomp $value;
26 $config->{$key} = $value;
27 }
28  
29 return $config;
30 }
31  
32 sub available { !! keys %{ __PACKAGE__->config } }
33 sub forbid_in_default { 0 }
34  
35 sub nopaste {
36 my $self = shift;
37 $self->run(@_);
38 }
39  
40 sub run {
41 my ($self, %arg) = @_;
42 my $ua = LWP::UserAgent->new;
43  
44 my $desc = $arg{desc} || $arg{filename} || "a gist from nopaste";
45  
46 my $filename = defined $arg{filename}
47 ? path($arg{filename})->basename
48 : 'nopaste';
49  
50 my $json = encode_json({
51 title => $desc,
52 file_name => $filename,
53 visibility => $arg{private} ? "private" : "internal",
54 content => $arg{text},
55 });
56  
57 my $url = sprintf 'https://%s/api/v4/snippets', $self->config->{host};
58  
59 my $res = $ua->post(
60 $url,
61 'PRIVATE-TOKEN' => $self->config->{token},
62 Content => $json,
63 Content_Type => 'application/json',
64 );
65  
66 return $self->return($res);
67 }
68  
69 sub return {
70 my ($self, $res) = @_;
71  
72 if ($res->is_error) {
73 my $text = $res->status_line;
74 if ($res->code == 401) {
75 $text .= "\nYou may need set gitlab.token and gitlab.host in your .gitconfig";
76 }
77 return (0, "Failed: " . $text);
78 }
79  
80 if (($res->header('Client-Warning') || '') eq 'Internal response') {
81 return (0, "LWP Error: " . $res->content);
82 }
83  
84 my $url = decode_json($res->content)->{web_url};
85  
86 return (0, "Could not find paste link.") unless $url;
87 return (1, $url);
88 }
89  
90 1;
91  
92 __END__