Bug 25952: Skip invalid repositories when searching for plugins

This patch prevents the end-user to get the ugly 500 "malformed JSON
string" when something wrong happens with the plugin search. It happens
currently in ktd with the plugin repos listed in $KOHA_CONF

An error is reported to the UI

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2025-11-14 12:02:34 +01:00 committed by Lucas Gass
parent d51dc1fc69
commit 438e51351d
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 72 additions and 56 deletions

View File

@ -85,6 +85,15 @@
</div> </div>
[% END %] [% END %]
[% IF search_errors %]
<h2>Search errors</h2>
<div class="page-section">
[% FOREACH error IN search_errors %]
<div class="alert alert-warning">Cannot search repository [% error.repo.name | html %]: [% error.error | html %]</div>
[% END %]
</div>
[% END %]
[% IF search_results %] [% IF search_results %]
<h2>Search results</h2> <h2>Search results</h2>
<div class="page-section"> <div class="page-section">

View File

@ -23,6 +23,7 @@ use CGI qw ( -utf8 );
use JSON qw( from_json ); use JSON qw( from_json );
use LWP::Simple qw( get ); use LWP::Simple qw( get );
use Try::Tiny;
use Koha::Plugins; use Koha::Plugins;
use C4::Auth qw( get_template_and_user ); use C4::Auth qw( get_template_and_user );
@ -63,7 +64,7 @@ if ($plugins_enabled) {
$template->param( plugins_restricted => C4::Context->config('plugins_restricted') ); $template->param( plugins_restricted => C4::Context->config('plugins_restricted') );
$template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 ); $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
my @results; my ( @results, @errors );
if ($plugin_search) { if ($plugin_search) {
my $repos = C4::Context->config('plugin_repos'); my $repos = C4::Context->config('plugin_repos');
@ -73,68 +74,74 @@ if ($plugins_enabled) {
} }
foreach my $r ( @{ $repos->{repo} } ) { foreach my $r ( @{ $repos->{repo} } ) {
if ( $r->{service} eq 'github' ) { try {
my $url = if ( $r->{service} eq 'github' ) {
"https://api.github.com/search/repositories?q=$plugin_search+user:$r->{org_name}+in:name,description"; my $url =
my $response = from_json( get($url) ); "https://api.github.com/search/repositories?q=$plugin_search+user:$r->{org_name}+in:name,description";
foreach my $result ( @{ $response->{items} } ) { my $response = from_json( get($url) );
next unless $result->{name} =~ /^koha-plugin-/; foreach my $result ( @{ $response->{items} } ) {
my $releases = $result->{url} . "/releases/latest"; next unless $result->{name} =~ /^koha-plugin-/;
my $release_info = get($releases); my $releases = $result->{url} . "/releases/latest";
next unless $release_info; my $release_info = get($releases);
my $release = from_json($release_info); next unless $release_info;
my $tag_name = $release->{tag_name}; my $release = from_json($release_info);
for my $asset ( @{ $release->{assets} } ) { my $tag_name = $release->{tag_name};
if ( $asset->{browser_download_url} =~ m/\.kpz$/ ) { for my $asset ( @{ $release->{assets} } ) {
$result->{install_name} = $asset->{name}; if ( $asset->{browser_download_url} =~ m/\.kpz$/ ) {
$result->{install_url} = $asset->{browser_download_url}; $result->{install_name} = $asset->{name};
$result->{tag_name} = $tag_name; $result->{install_url} = $asset->{browser_download_url};
$result->{tag_name} = $tag_name;
}
}
push( @results, { repo => $r, result => $result } );
}
} elsif ( $r->{service} eq 'gitlab' ) {
my $org_name = $r->{org_name};
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";
my $response = from_json( get($url) );
foreach my $result ( @{$response} ) {
next unless $result->{name} =~ /^koha-plugin-/;
my $project_id = $result->{id};
my $description = $result->{description} // '';
my $web_url = $result->{web_url};
my $releases_url = "https://gitlab.com/api/v4/projects/$project_id/releases";
my $releases_info = get($releases_url);
next unless $releases_info;
my @releases = @{ from_json($releases_info) };
if ( scalar @releases > 0 ) {
# Pick the first one, the latest release
my $latest = $releases[0];
my $name = $latest->{name};
my $tag_name = $latest->{tag_name};
my @links = @{ $latest->{assets}->{links} };
my $url = $links[0]->{direct_asset_url};
my @parts = split( '/', $url );
my $filename = $parts[-1];
next unless $url =~ m/\.kpz$/;
my $result = {
description => $description,
install_name => $filename,
install_url => $url,
html_url => $web_url,
name => $name,
tag_name => $tag_name,
};
push @results, { repo => $r, result => $result };
} }
} }
push( @results, { repo => $r, result => $result } );
} }
} elsif ( $r->{service} eq 'gitlab' ) { } catch {
my $org_name = $r->{org_name}; warn $_;
my $url = push @errors, { repo => $r, error => $_ };
"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"; };
my $response = from_json( get($url) );
foreach my $result ( @{$response} ) {
next unless $result->{name} =~ /^koha-plugin-/;
my $project_id = $result->{id};
my $description = $result->{description} // '';
my $web_url = $result->{web_url};
my $releases_url = "https://gitlab.com/api/v4/projects/$project_id/releases";
my $releases_info = get($releases_url);
next unless $releases_info;
my @releases = @{ from_json($releases_info) };
if ( scalar @releases > 0 ) {
# Pick the first one, the latest release
my $latest = $releases[0];
my $name = $latest->{name};
my $tag_name = $latest->{tag_name};
my @links = @{ $latest->{assets}->{links} };
my $url = $links[0]->{direct_asset_url};
my @parts = split( '/', $url );
my $filename = $parts[-1];
next unless $url =~ m/\.kpz$/;
my $result = {
description => $description,
install_name => $filename,
install_url => $url,
html_url => $web_url,
name => $name,
tag_name => $tag_name,
};
push @results, { repo => $r, result => $result };
}
}
}
} }
$template->param( $template->param(
search_results => \@results, search_results => \@results,
search_errors => \@errors,
search_term => $plugin_search, search_term => $plugin_search,
); );
} }