| 1 | package App::gh::Command::Issue::Edit; | ||
| 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 - create or edit issue. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 USAGE | ||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | $ gh issue edit ([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 ($data, $title, $body); | ||
| 61 | |||
| 62 | my $f = File::Temp->new(SUFFIX => ".mkd"); | ||
| 63 | |||
| 64 | if ($number) { | ||
| 65 | $data = App::gh->api->issue_get($user, $repo, $number); | ||
| 66 | open my $fh, '>', $f->filename; | ||
| 67 | printf $fh "%s\n%s", $data->{issue}->{title}, $data->{issue}->{body}; | ||
| 68 | close $fh; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $t = stat($f->filename)->mtime; | ||
| 72 | system $ENV{EDITOR}, $f->filename; | ||
| 73 | if ($t == stat($f->filename)->mtime) { | ||
| 74 | info "No changes. Issue was not sent."; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | open my $fh, '<', $f->filename; | ||
| 79 | my $content = do { local $/; <$fh> }; | ||
| 80 | close $fh; | ||
| 81 | ($title, $body) = split("\n", $content, 2); | ||
| 82 | chomp( $title ); | ||
| 83 | chomp( $body ); | ||
| 84 | |||
| 85 | if (length($title) == 0 || length($body) == 0) { | ||
| 86 | info "Message should two lines at least."; | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | info "Editing issue..."; | ||
| 91 | $data = App::gh->api->issue_edit($user, $repo, $number, $title, $body); | ||
| 92 | |||
| 93 | info "sent: " . $data->{issue}->{html_url}; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 1; |