Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1446236 - Add simpler method to check if an extension is present #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Bugzilla.pm
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,19 @@ sub template_inner {
||= Bugzilla::Template->create(%options);
}

our $in_extensions = 0;

sub extensions {
state $extensions;
return $extensions if $extensions;

# Guard against extensions querying the extension list during initialization
# (through this method or has_extension).
# The extension list is not fully populated at that point,
# so the results would not be meaningful.
die "Recursive attempt to load/query extensions" if $in_extensions > 0;
local $in_extensions = $in_extensions + 1;

my $extension_packages = Bugzilla::Extension->load_all();
$extensions = [];
foreach my $package (@$extension_packages) {
Expand All @@ -136,6 +146,16 @@ sub extensions {
return $extensions;
}

sub has_extension {
my ($class, $name) = @_;
my $cache = $class->request_cache;
if (!$cache->{extensions_hash}) {
my %extensions = map { $_->NAME => 1 } @{Bugzilla->extensions};
$cache->{extensions_hash} = \%extensions;
}
return exists $cache->{extensions_hash}{$name};
}

sub cgi {
return request_cache->{cgi} ||= Bugzilla::CGI->new;
}
Expand Down
3 changes: 1 addition & 2 deletions votes.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ use lib qw(. lib local/lib/perl5);
use Bugzilla;
use Bugzilla::Error;

my $is_enabled = grep { $_->NAME eq 'Voting' } @{Bugzilla->extensions};
$is_enabled || ThrowCodeError('extension_disabled', {name => 'Voting'});
Bugzilla->has_extension('Voting') || ThrowCodeError('extension_disabled', { name => 'Voting' });

my $cgi = Bugzilla->cgi;
my $action = $cgi->param('action') || 'show_user';
Expand Down