Skip to content
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
149 changes: 149 additions & 0 deletions Bugzilla/API/V1/Bugzilla.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::API::V1::Bugzilla;

use 5.10.1;
use Mojo::Base qw( Mojolicious::Controller );

use DateTime;
use Try::Tiny;

use Bugzilla::Constants;
use Bugzilla::Logging;
use Bugzilla::Util qw(datetime_from);

sub setup_routes {
my ($class, $r) = @_;

$r->get('/version')->to('V1::Bugzilla#version');
$r->get('/extensions')->to('V1::Bugzilla#extensions');
$r->get('/timezone')->to('V1::Bugzilla#timezone');
$r->get('/time')->to('V1::Bugzilla#time');
$r->get('/jobqueue_status')->to('V1::Bugzilla#jobqueue_status');

foreach my $path (qw(/version /extensions /timezone /time /jobqueue_status)) {
$r->options($path)->to('V1::Bugzilla#options');
}
}

sub options {
my ($self) = @_;

$self->res->headers->header('Allow' => 'GET');
$self->res->headers->header('Access-Control-Allow-Methods' => 'GET');

return $self->rendered(200);
}

sub version {
my ($self) = @_;
Bugzilla->usage_mode(USAGE_MODE_MOJO_REST);

return $self->render(json => {version => BUGZILLA_VERSION});
}

sub extensions {
my ($self) = @_;
Bugzilla->usage_mode(USAGE_MODE_MOJO_REST);
my $user = $self->bugzilla->login(LOGIN_REQUIRED);
$user->id || return $self->user_error('login_required');

my %extensions;
foreach my $extension (@{Bugzilla->extensions}) {
$extensions{$extension->NAME} = {version => '' . ($extension->VERSION || 0)};
}

return $self->render(json => {extensions => \%extensions});
}

sub timezone {
my ($self) = @_;
Bugzilla->usage_mode(USAGE_MODE_MOJO_REST);

# All Webservices return times in UTC; Use UTC here for backwards compat.
return $self->render(json => {timezone => '+0000'});
}

sub time {
my ($self) = @_;
Bugzilla->usage_mode(USAGE_MODE_MOJO_REST);
my $user = $self->bugzilla->login(LOGIN_REQUIRED);
$user->id || return $self->user_error('login_required');

# All Webservices return times in UTC; Use UTC here for backwards compat.
my $dbh = Bugzilla->dbh;
my $db_time = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
$db_time = datetime_from($db_time, 'UTC')->iso8601() . 'Z';
my $now_utc = DateTime->now()->iso8601() . 'Z';

return $self->render(
json => {
db_time => $db_time,
web_time => $now_utc,
web_time_utc => $now_utc,
tz_name => 'UTC',
tz_offset => '+0000',
tz_short_name => 'UTC',
}
);
}

sub jobqueue_status {
my ($self) = @_;

Bugzilla->usage_mode(USAGE_MODE_MOJO_REST);

my $user = $self->bugzilla->login;
$user->id || return $self->user_error('login_required');

my $dbh = Bugzilla->dbh;
my $query = q{
SELECT
COUNT(*) AS total,
COALESCE(
(SELECT COUNT(*)
FROM ts_error
WHERE ts_error.jobid = j.jobid
)
, 0) AS errors
FROM ts_job j
INNER JOIN ts_funcmap f
ON f.funcid = j.funcid
GROUP BY errors
};

my $status;
my $failed;
try {
$status = $dbh->selectrow_hashref($query);
}
catch {
ERROR($_);
$failed = 1;
};
return $self->code_error('jobqueue_status_error') if $failed;

return $self->render(
json => {
errors => 0 + ($status->{errors} // 0),
total => 0 + ($status->{total} // 0),
}
);
}

1;

__END__

=head1 NAME

Bugzilla::API::V1::Bugzilla - Global functions for the webservice interface.

=head1 DESCRIPTION

This provides functions that tell you about Bugzilla in general.
13 changes: 5 additions & 8 deletions Bugzilla/WebService.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ L<REST|Bugzilla::WebService::Server::REST>.

=head1 CALLING METHODS

Methods are grouped into "packages", like C<Bugzilla> for
L<Bugzilla::WebService::Bugzilla>. So, for example,
L<Bugzilla::WebService::Bugzilla/version>, is called as C<Bugzilla.version>.
Methods are grouped into "packages", like C<bug_modal> for
C<Bugzilla::Extension::BugModal::WebService>. So, for example,
its C<edit> method is called as C<bug_modal.edit>.

For REST, the "package" is more determined by the path
used to access the resource. See each relevant method
Expand Down Expand Up @@ -385,8 +385,5 @@ objects.

=head2 WebService Methods

=over

=item L<Bugzilla::WebService::Bugzilla>

=back
Only extensions still register methods here, through the C<webservice> hook.
The core resources are served by the native API (C<Bugzilla::API::V1::*>).
Loading
Loading