Bug 34959: Remove unused sub
[koha.git] / plugins / plugins-home.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
24 use JSON qw( from_json );
25 use LWP::Simple qw( get );
26
27 use Koha::Plugins;
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::Context;
31
32 my $plugins_enabled = C4::Context->config("enable_plugins");
33
34 my $input  = CGI->new;
35 my $method = $input->param('method');
36 my $plugin_search = $input->param('plugin-search');
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {   template_name => ($plugins_enabled) ? "plugins/plugins-home.tt" : "plugins/plugins-disabled.tt",
40         query         => $input,
41         type          => "intranet",
42         flagsrequired   => { plugins => '*' },
43     }
44 );
45
46 if ($plugins_enabled) {
47
48     $template->param(
49         koha_version => C4::Context->preference("Version"),
50         method       => $method,
51     );
52
53     my @plugins = Koha::Plugins->new()->GetPlugins(
54         {
55             method => $method,
56             all    => 1,
57             errors => 1
58         }
59     );
60
61     $template->param( plugins => \@plugins, );
62
63     $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
64     my @results;
65     if ($plugin_search) {
66         my $repos = C4::Context->config('plugin_repos');
67
68         # Fix data structure if only one repo defined
69         if ( ref($repos->{repo}) eq 'HASH' ) {
70             $repos = { repo => [ $repos->{repo} ] };
71         }
72
73         foreach my $r ( @{ $repos->{repo} } ) {
74             if ( $r->{service} eq 'github' ) {
75                 my $url = "https://api.github.com/search/repositories?q=$plugin_search+user:$r->{org_name}+in:name,description";
76                 my $response = from_json( get($url) );
77                 foreach my $result ( @{ $response->{items} } ) {
78                     next unless $result->{name} =~ /^koha-plugin-/;
79                     my $releases     = $result->{url} . "/releases/latest";
80                     my $release_info = get($releases);
81                     next unless $release_info;
82                     my $release  = from_json( $release_info );
83                     my $tag_name = $release->{tag_name};
84                     for my $asset ( @{$release->{assets}} ) {
85                         if ($asset->{browser_download_url} =~ m/\.kpz$/) {
86                             $result->{install_name} = $asset->{name};
87                             $result->{install_url}  = $asset->{browser_download_url};
88                             $result->{tag_name}     = $tag_name;
89                         }
90                     }
91                     push( @results, { repo => $r, result => $result } );
92                 }
93             }
94             elsif ( $r->{service} eq 'gitlab' ) {
95                 my $org_name = $r->{org_name};
96                 my $url = "https://gitlab.com/api/v4/groups/$org_name/projects?with_issues_enabled=no\&with_merge_requests_enabled=no\&with_shared=no\&include_subgroups=yes\&search=koha-plugin+$plugin_search";
97                 my $response = from_json( get($url) );
98                 foreach my $result ( @{ $response } ) {
99                     next unless $result->{name} =~ /^koha-plugin-/;
100                     my $project_id   = $result->{id};
101                     my $description  = $result->{description} // '';
102                     my $web_url      = $result->{web_url};
103                     my $releases_url  = "https://gitlab.com/api/v4/projects/$project_id/releases";
104                     my $releases_info = get($releases_url);
105                     next unless $releases_info;
106                     my @releases = @{ from_json($releases_info) };
107
108                     if ( scalar @releases > 0 ) {
109
110                         # Pick the first one, the latest release
111                         my $latest   = $releases[0];
112                         my $name     = $latest->{name};
113                         my $tag_name = $latest->{tag_name};
114                         my @links    = @{ $latest->{assets}->{links} };
115                         my $url      = $links[0]->{direct_asset_url};
116                         my @parts    = split( '/', $url );
117                         my $filename = $parts[-1];
118                         next unless $url =~ m/\.kpz$/;
119                         my $result = {
120                             description  => $description,
121                             install_name => $filename,
122                             install_url  => $url,
123                             html_url     => $web_url,
124                             name         => $name,
125                             tag_name     => $tag_name,
126                         };
127                         push @results, { repo => $r, result => $result };
128                     }
129                 }
130             }
131         }
132
133         $template->param(
134             search_results => \@results,
135             search_term    => $plugin_search,
136         );
137     }
138 }
139
140 output_html_with_http_headers( $input, $cookie, $template->output );