| 1 | package Dist::Zilla::Plugin::GitHub::UploadRelease; | ||
| 2 | |||
| 3 | # ABSTRACT: Upload the package to GitHub after release | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '0.002'; # VERSION | ||
| 9 | |||
| 10 | use JSON qw(encode_json); | ||
| 11 | use Moose; | ||
| 12 | use Try::Tiny; | ||
| 13 | use Git::Wrapper; | ||
| 14 | use File::Basename; | ||
| 15 | use File::Slurp qw(read_file); | ||
| 16 | use MIME::Base64 qw(encode_base64); | ||
| 17 | |||
| 18 | use Dist::Zilla::Plugin::GitHub (); | ||
| 19 | use Dist::Zilla::Plugin::Git::Tag (); | ||
| 20 | |||
| 21 | extends 'Dist::Zilla::Plugin::GitHub'; | ||
| 22 | |||
| 23 | with 'Dist::Zilla::Role::AfterRelease'; | ||
| 24 | |||
| 25 | |||
| 26 | has _last_status => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'Int', | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub _api_request { | ||
| 32 | my ($self, $method, $path, $payload, $headers) = @_; | ||
| 33 | |||
| 34 | my $url = $path =~ m{^https?://}i ? $path : $self->api . $path; | ||
| 35 | |||
| 36 | $self->log_debug("$method $url"); | ||
| 37 | |||
| 38 | $headers //= {}; | ||
| 39 | |||
| 40 | my ($login, $pass, $otp) = $self->_get_credentials(0); | ||
| 41 | |||
| 42 | if ($pass) { | ||
| 43 | my $basic = encode_base64("$login:$pass", ''); | ||
| 44 | $headers->{Authorization} = "Basic $basic"; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ($self->prompt_2fa) { | ||
| 48 | $headers->{'X-GitHub-OTP'} = $otp; | ||
| 49 | $self->log("Using two-factor authentication"); | ||
| 50 | } | ||
| 51 | |||
| 52 | my $response = HTTP::Tiny->new->request('POST', $url, { | ||
| 53 | content => (ref($payload) ? encode_json($payload) : $payload), | ||
| 54 | headers => $headers, | ||
| 55 | }); | ||
| 56 | |||
| 57 | $self->_last_status($response->{status}); | ||
| 58 | |||
| 59 | my $result = $self->_check_response($response); | ||
| 60 | |||
| 61 | return unless $result; | ||
| 62 | |||
| 63 | if ($result eq 'redo') { | ||
| 64 | $self->log("Retrying with two-factor authentication"); | ||
| 65 | $self->prompt_2fa(1); | ||
| 66 | return __CODE__->(@_); | ||
| 67 | } | ||
| 68 | |||
| 69 | return $result; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub after_release { | ||
| 74 | my ($self, $archive) = @_; | ||
| 75 | |||
| 76 | my $dist_name = $self->zilla->name; | ||
| 77 | |||
| 78 | my ($login) = $self->_get_credentials(1); | ||
| 79 | return unless $login; | ||
| 80 | |||
| 81 | my $repo_name = $self->_get_repo_name($login); | ||
| 82 | |||
| 83 | my $git_tag_plugin = $self->zilla->plugin_named('Git::Tag') // $self->log_fatal('Plugin Git::Tag not found'); | ||
| 84 | |||
| 85 | my $tag = $git_tag_plugin->tag; | ||
| 86 | |||
| 87 | my ($result, $status); | ||
| 88 | |||
| 89 | $self->log("Create release for $tag in $repo_name"); | ||
| 90 | $result = $self->_api_request(POST => "/repos/$repo_name/releases", { tag_name => $tag }); | ||
| 91 | |||
| 92 | $status = $self->_last_status; | ||
| 93 | if ($status ne 201) { | ||
| 94 | $self->log_fatal("Release NOT created: $status"); | ||
| 95 | } | ||
| 96 | |||
| 97 | my $upload_url = $result->{upload_url}; | ||
| 98 | $upload_url =~ s{\{.*?\}$}{}; | ||
| 99 | $upload_url .= '?name='.$archive; | ||
| 100 | |||
| 101 | my $payload = read_file($archive); | ||
| 102 | |||
| 103 | $self->log("Upload $archive to GitHub"); | ||
| 104 | $result = $self->_api_request(POST => $upload_url, $payload, { 'Content-Type' => 'application/gzip' }); | ||
| 105 | |||
| 106 | $status = $self->_last_status; | ||
| 107 | if ($status ne 201) { | ||
| 108 | $self->log_fatal("Release NOT uploaded: $status"); | ||
| 109 | } | ||
| 110 | $self->log('Done'); | ||
| 111 | } | ||
| 112 | |||
| 113 | no Moose; | ||
| 114 | |||
| 115 | __PACKAGE__->meta->make_immutable; | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ |