Bug 23975: Add 'Install' support for github results
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 12 Feb 2020 14:43:01 +0000 (14:43 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 3 Mar 2020 09:18:56 +0000 (09:18 +0000)
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-home.tt
plugins/plugins-home.pl
plugins/plugins-upload.pl

index 3ae2cd5..09204f5 100644 (file)
@@ -71,7 +71,7 @@
                                     <td><a href="[% sr.result.html_url | url %]" target="_new">[% sr.result.name %]</a></td>
                                     <td>[% sr.result.description %]</td>
                                     <td>[% sr.repo.name %]</td>
-                                    <td><button class="btn btn-install-plugin"><i class="fa fa-download"></i> Install</button></td>
+                                    <td><a class="btn btn-install-plugin" href="/cgi-bin/koha/plugins/plugins-upload.pl?op=Upload&uploadfile=[% sr.result.install_name | uri %]&uploadlocation=[% sr.result.install_url | uri %]"><i class="fa fa-download"></i> Install</a></td>
                                 </tr>
                             [% END %]
                         </table>
     [% INCLUDE 'calendar.inc' %]
     <script>
         $(document).ready(function(){
-            $(".btn-install-plugin").on("click", function() { alert("Sorry, this functionality doesn't exist yet!"); });
             $(".uninstall_plugin").on("click", function(){
                 $(".dropdown").removeClass("open");
                 var plugin_name = $(this).data("plugin-name");
index 0c17d9b..0e7de02 100755 (executable)
@@ -69,6 +69,14 @@ if ($plugins_enabled) {
                 my $response = from_json( get($url) );
                 foreach my $result ( @{ $response->{items} } ) {
                     next unless $result->{name} =~ /^koha-plugin-/;
+                    my $releases = $result->{url} . "/releases/latest";
+                    my $release = from_json( get($releases) );
+                    for my $asset ( @{$release->{assets}} ) {
+                        if ($asset->{browser_download_url} =~ m/\.kpz$/) {
+                            $result->{install_name} = $asset->{name};
+                            $result->{install_url} = $asset->{browser_download_url};
+                        }
+                    }
                     push( @results, { repo => $r, result => $result } );
                 }
             }
index c849942..7f873fa 100755 (executable)
@@ -20,6 +20,7 @@ use Modern::Perl;
 
 use Archive::Extract;
 use CGI qw ( -utf8 );
+use Mojo::UserAgent;
 use File::Copy;
 use File::Temp;
 
@@ -46,6 +47,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 
 my $uploadfilename = $input->param('uploadfile');
 my $uploadfile     = $input->upload('uploadfile');
+my $uploadlocation = $input->param('uploadlocation');
 my $op             = $input->param('op') || q{};
 
 my ( $total, $handled, @counts, $tempfile, $tfh );
@@ -53,7 +55,7 @@ my ( $total, $handled, @counts, $tempfile, $tfh );
 my %errors;
 
 if ($plugins_enabled) {
-    if ( ( $op eq 'Upload' ) && $uploadfile ) {
+    if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
         my $plugins_dir = C4::Context->config("pluginsdir");
         $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
 
@@ -69,15 +71,24 @@ if ($plugins_enabled) {
         $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
         $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
         $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
-        $errors{'EMPTYUPLOAD'}    = 1 unless ( length($uploadfile) > 0 );
+
+        if ( $uploadlocation ) {
+            my $ua = Mojo::UserAgent->new(max_redirects => 5);
+            my $tx = $ua->get($uploadlocation);
+            $tx->result->save_to($tempfile);
+        } else {
+            $errors{'EMPTYUPLOAD'}    = 1 unless ( length($uploadfile) > 0 );
+        }
 
         if (%errors) {
             $template->param( ERRORS => [ \%errors ] );
         } else {
-            while (<$uploadfile>) {
-                print $tfh $_;
+            if ( $uploadfile ) {
+                while (<$uploadfile>) {
+                    print $tfh $_;
+                }
+                close $tfh;
             }
-            close $tfh;
 
             my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
             unless ( $ae->extract( to => $plugins_dir ) ) {
@@ -90,11 +101,11 @@ if ($plugins_enabled) {
 
             Koha::Plugins->new()->InstallPlugins();
         }
-    } elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
+    } elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
         warn "Problem uploading file or no file uploaded.";
     }
 
-    if ( $uploadfile && !%errors && !$template->param('ERRORS') ) {
+    if ( ($uploadfile || $uploadlocation) && !%errors && !$template->param('ERRORS') ) {
         print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
     } else {
         output_html_with_http_headers $input, $cookie, $template->output;