File Coverage

lib/App/gh/Command/Pullreq/Send.pm
Criterion Covered Total %
statement 15 81 18.5
branch 0 16 0.0
condition 0 11 0.0
subroutine 5 8 62.5
pod 0 3 0.0
total 20 119 16.8


line stmt bran cond sub pod time code
1             package App::gh::Command::Pullreq::Send;
2 1     1   1686 use base qw(App::gh::Command);
  1         2  
  1         110  
3 1     1   16 use v5.10;
  1         4  
  1         60  
4 1     1   6 use App::gh::Utils;
  1         3  
  1         91  
5 1     1   6 use File::stat;
  1         8  
  1         12  
6 1     1   55 use File::Temp;
  1         3  
  1         1169  
7             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
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             sub parse_uri {
48 0     0 0   my ($uri) = @_;
49 0 0         if ( $uri =~ m{(git|https?)://github.com/(.*?)/(.*?).git} ) {
    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             sub get_remote {
58 0     0 0   my $self = shift;
59 0           my $config = App::gh->config->current();
60 0           my %remotes = %{ $config->{remote} };
  0            
61             # try to get origin remote
62 0   0       return $remotes{origin} || (values( %remotes ))[0];
63             }
64              
65             sub run {
66 0     0 0   my $self = shift;
67              
68 0 0         unless ( $ENV{EDITOR} ) {
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   0       my $remote_branch = shift ||'master';
85 0           my $base = shift;
86 0           my ($user,$repo,$uri_type);
87 0 0         if( $base ) {
88 0           ($user,$repo) = split m{[/:]},$base;
89              
90             } else {
91 0           my $remote = $self->get_remote();
92 0 0         die "Remote not found\n." unless $remote;
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 0 0       unless( $gh_id && $gh_token ) {
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 0         if ($t == stat($f->filename)->mtime) {
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> };
  0            
  0            
131 0           close $fh;
132 0           my ($title, $body) = split("\n", $content, 2);
133 0           chomp( $title );
134 0           chomp( $body );
135              
136 0 0 0       if (length($title) == 0 || length($body) == 0) {
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;