lib/App/gh/Command/Pullreq/Send.pm

filter: Statementstmt 19.5% 16 / 82 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 16.5% 21 / 127 CC 13 SCAR 40.5
CC
13
Cov
15%
run
110.0
parse_uri
12.0
get_remote
6.0
CRAP 57.6
« lib/App/gh/Command/Pull.pm ↑ Summary blib/lib/App/gh/Command/Commit.pm »
1 package App::gh::Command::Pullreq::Send;
2 1 ▶use base qw(App::gh::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use App::gh::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require App::gh::Git;
8  
9  
10 =head1 NAME
11  
12 App::gh::Command::PullReq::Send - pull request of current branch.
13  
14 =head1 DESCRIPTION
15  
16 =head1 USAGE
17  
18 $ gh pullreq send ([base branch]) ([owner]/[repo])
19  
20 Base branch: the branch you fork from.
21  
22 Owner/Repo: the repository you fork from.
23  
24 Example:
25  
26 $ gh pullreq send
27  
28 $ gh pullreq send master c9s/App-gh
29  
30 =head1 API
31  
32 API Spec is from L<http://develop.github.com/p/pulls.html>
33  
34 pull[base] - A String of the branch or commit SHA that you want your
35 changes to be pulled to.
36  
37 pull[head] - A String of the branch or commit SHA of your changes.
38 Typically this will be a branch. If the branch is in a fork of the original
39 repository, specify the username first: "my-user:some-branch".
40  
41 pull[title] - The String title of the Pull Request (and the related Issue).
42  
43 pull[body] - The String body of the Pull Request.
44  
45 =cut
46  
47 0 ▶sub parse_uri {
Subroutine parse_uri: not called CC=3 SCAR=24.8
Pod undocumented
48 0 my ($uri) = @_;
49 0 ▶ if ( $uri =~ m{(git|https?)://github.com/(.*?)/(.*?).git} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uri =~ m[(git|https?)://github.com/(.*?)/(.*?).git]) { } 0 0 0
elsif ($uri =~ m[git\@github.com:(.*?)/(.*?).git]) { } 0 0 0
50 0 return ($2,$3,$1);
51 } elsif ( $uri =~ m{git\@github.com:(.*?)/(.*?).git} ) {
52 0 return ($1,$2,'git');
53 }
54 0 return undef;
55 }
56  
57 0 ▶sub get_remote {
Subroutine get_remote: not called CC=2 SCAR=17.9
Pod undocumented
58 0 my $self = shift;
59 0 my $config = App::gh->config->current();
60 0 my %remotes = %{ $config->{remote} };
61 # try to get origin remote
62 0 ▶ return $remotes{origin} || (values( %remotes ))[0];
Condition0%0/3
$remotes{'origin'} || (values %remotes)[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$remotes{'origin'} || (values %remotes)[0]
$remotes{'origin'} (values %remotes)[0]
Truth table0%0/3
A || B
A= $remotes{'origin'}B= (values %remotes)[0]
ABresult
1X1
011
000
63 }
64  
65 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
66 0 my $self = shift;
67  
68 0 ▶ unless ( $ENV{EDITOR} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"EDITOR"}) 0 0 0
69 0 say "\$EDITOR is not set. please set the EDITOR environment variable for editing.";
70 0 say "\nUnix-like system users: \n";
71 0 say "\tNano text editor, a simple text editor";
72 0 say "\t\t export EDITOR=nano";
73 0 say "\tVIM users please run:";
74 0 say "\t\t export EDITOR=vim";
75 0 say "\tEmacs users please run:";
76 0 say "\t\t export EDITOR=emacs";
77  
78 0 say "\nWindows users please run:\n";
79 0 say "\t\t set EDITOR=notepad.txt";
80 0 say "";
81 0 die;
82 }
83  
84 0 ▶ my $remote_branch = shift ||'master';
Condition0%0/2
shift() || 'master'
l!l
00
MC/DC0%0/1
shift() || 'master'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
85 0 my $base = shift;
86 0 my ($user,$repo,$uri_type);
87 0 ▶ if( $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 0 0 0
88 0 ($user,$repo) = split m{[/:]},$base;
89  
90 } else {
91 0 my $remote = $self->get_remote();
92 0 ▶ die "Remote not found\n." unless $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote 0 0 0
93 0 ($user, $repo, $uri_type ) = parse_uri( $remote->{url} );
94 }
95  
96  
97  
98 0 my $gh_id = App::gh->config->github_id;
99 0 my $gh_token = App::gh->config->github_token;
100 0 ▶ unless( $gh_id && $gh_token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gh_id and $gh_token) 0 0 0
Condition0%0/3
$gh_id and $gh_token
!ll&&!rl&&r
000
MC/DC0%0/2
$gh_id and $gh_token
$gh_id $gh_token
Truth table0%0/3
A and B
A= $gh_idB= $gh_token
ABresult
0X0
100
111
101 0 die "Github authtoken not found. Can not send pull request.\n";
102 }
103  
104  
105 0 my $local_repo = App::gh->git;
106 0 open my $fh, '<', $local_repo->wc_path()."/.git/HEAD";
107 0 my $ref = <$fh>;
108 0 close $fh;
109 0 chomp( $ref );
110  
111 0 my ($branch) = ( $ref =~ m{ref:\s\S+?/\S+?/(\S+)} );
112  
113 0 my $f = File::Temp->new(SUFFIX => ".md");
114 0 my $t = stat($f->filename)->mtime;
115  
116 0 open $fh , ">" , $f->filename;
117 0 print $fh "Title\n";
118 0 print $fh "Body (markdown format)\n";
119 0 close $fh;
120  
121 # launch editor
122 0 system $ENV{EDITOR}, $f->filename;
123  
124 0 ▶ if ($t == stat($f->filename)->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == &App::gh::Command::Pullreq::Send::stat(scalar $f->filename)->mtime) 0 0 0
125 0 info "No changes. Pull request was not sent.";
126 0 return;
127 }
128  
129 0 open $fh, '<', $f->filename;
130 0 my $content = do { local $/; <$fh> };
131 0 close $fh;
132 0 my ($title, $body) = split("\n", $content, 2);
133 0 chomp( $title );
134 0 chomp( $body );
135  
136 0 ▶ if (length($title) == 0 || length($body) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title == 0 or length $body == 0) 0 0 0
Condition0%0/3
length $title == 0 or length $body == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
length $title == 0 or length $body == 0
length $title == 0 length $body == 0
Truth table0%0/3
A or B
A= length $title == 0B= length $body == 0
ABresult
1X1
011
000
137 0 info "Message should two lines at least.";
138 0 return;
139 }
140  
141 0 info "Sending pull request for $branch...";
142 # XXX: make arguments into hash format
143 0 my $data = App::gh->api->pullreq_send($user, $repo, $branch, $remote_branch, $title, $body);
144  
145 0 info "Sent: " . $data->{pull}->{html_url};
146 }
147  
148 1 1;
« lib/App/gh/Command/Pull.pm ↑ Summary blib/lib/App/gh/Command/Commit.pm »