blib/lib/App/gh/Command/Issue/Comment.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 98 CC 8 SCAR 28.8
CC
8
Cov
0%
run
42.0
parse_uri
12.0
get_remote
2.0
CRAP 17.9
1 package App::gh::Command::Issue::Comment;
2 use warnings;
3 use strict;
4 use base qw(App::gh::Command);
5 use App::gh::Utils;
6 use File::stat;
7 use File::Temp;
8 require App::gh::Git;
9  
10  
11 =head1 NAME
12  
13 App::gh::Command::Issue::Edit - comment to the issue.
14  
15 =head1 DESCRIPTION
16  
17 =head1 USAGE
18  
19 =pod
20  
21 $ gh issue comment [number]
22  
23 =cut
24  
25 sub parse_uri {
26 my ($uri) = @_;
27 if ( $uri =~ m{(git|https?)://github.com/(.*?)/(.*?).git} ) {
28 return ($2,$3,$1);
29 } elsif ( $uri =~ m{git\@github.com:(.*?)/(.*?).git} ) {
30 return ($1,$2,'git');
31 }
32 return undef;
33 }
34  
35 sub get_remote {
36 my $self = shift;
37 my $config = App::gh->config->current();
38 my %remotes = %{ $config->{remote} };
39 # try to get origin remote
40 return $remotes{origin} || (values( %remotes ))[0];
41 }
42  
43 sub run {
44 my $self = shift;
45  
46 die "\$EDITOR is not set." unless $ENV{EDITOR};
47  
48 my $number = shift||'';
49 my $remote = $self->get_remote();
50  
51 die "Remote not found\n." unless $remote;
52 my ( $user, $repo, $uri_type ) = parse_uri( $remote->{url} );
53  
54 my $gh_id = App::gh->config->github_id;
55 my $gh_token = App::gh->config->github_token;
56 unless( $gh_id && $gh_token ) {
57 die "Github authtoken not found. Can not edit issue.\n";
58 }
59  
60 my $f = File::Temp->new(SUFFIX => ".mkd");
61 my $t = stat($f->filename)->mtime;
62 system $ENV{EDITOR}, $f->filename;
63 if ($t == stat($f->filename)->mtime) {
64 info "No changes. Issue was not sent.";
65 return;
66 }
67  
68 open my $fh, '<', $f->filename;
69 my $body = do { local $/; <$fh> };
70 close $fh;
71  
72 if (length($body) == 0) {
73 info "Message should have lines at least.";
74 return;
75 }
76  
77 info "Sending comment to issue #$number...";
78 my $data = App::gh->api->issue_comment($user, $repo, $number, $body);
79  
80 info "sent";
81 }
82  
83  
84 1;