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 1676743 - Convert all REST API code to run under native Mojolicious routing #1657

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c05319a
Bump version to 20200117.1
dklawren Jan 17, 2020
f66659c
Merge remote-tracking branch 'upstream/master'
dklawren Jan 23, 2020
1457930
Merge remote-tracking branch 'upstream/master'
dklawren Feb 4, 2020
879c02a
Merge remote-tracking branch 'upstream/master'
dklawren Feb 11, 2020
cf08e61
Merge remote-tracking branch 'upstream/master'
dklawren Feb 26, 2020
9c9e237
Merge remote-tracking branch 'upstream/master'
dklawren Feb 26, 2020
0d8cb41
Merge remote-tracking branch 'upstream/master'
dklawren Feb 26, 2020
c48e749
Merge remote-tracking branch 'upstream/master'
dklawren Mar 5, 2020
44f1149
Merge remote-tracking branch 'upstream/master'
dklawren Mar 6, 2020
db980c8
Merge remote-tracking branch 'upstream/master'
dklawren Mar 9, 2020
9bce0ae
Merge remote-tracking branch 'upstream/master'
dklawren Mar 9, 2020
f8194dc
Merge remote-tracking branch 'upstream/master'
Mar 11, 2020
f5e8ed5
Merge remote-tracking branch 'upstream/master'
dklawren Mar 20, 2020
be21ec1
Merge branch 'master' of https://github.com/dklawren/bmo
dklawren Mar 20, 2020
698f56f
Merge remote-tracking branch 'upstream/master'
dklawren Mar 25, 2020
d96a196
Merge remote-tracking branch 'upstream/master'
dklawren Apr 10, 2020
a4ccc14
Merge remote-tracking branch 'upstream/master'
dklawren Apr 27, 2020
e83671b
Merge remote-tracking branch 'upstream/master'
dklawren Apr 29, 2020
52fd38a
Merge remote-tracking branch 'upstream/master'
dklawren May 4, 2020
4175907
Merge remote-tracking branch 'upstream/master'
dklawren May 5, 2020
32293eb
Merge remote-tracking branch 'upstream/master'
dklawren May 22, 2020
b332a70
Merge remote-tracking branch 'upstream/master'
dklawren Jun 9, 2020
61b5a62
Merge remote-tracking branch 'upstream/master'
dklawren Jul 2, 2020
3310020
Merge remote-tracking branch 'upstream/master'
dklawren Jul 8, 2020
1089d75
Merge remote-tracking branch 'upstream/master'
dklawren Jul 20, 2020
a2a2e82
Merge remote-tracking branch 'upstream/master'
dklawren Aug 20, 2020
00d6e6a
Merge remote-tracking branch 'upstream/master'
dklawren Sep 2, 2020
2adfd4c
Merge remote-tracking branch 'upstream/master'
dklawren Sep 16, 2020
96b7356
Merge remote-tracking branch 'upstream/master' into master
dklawren Oct 13, 2020
bb3076f
Merge remote-tracking branch 'upstream/master'
dklawren Oct 16, 2020
e1b2175
Merge remote-tracking branch 'upstream/master'
dklawren Oct 23, 2020
2000ac8
Merge remote-tracking branch 'upstream/master'
dklawren Oct 26, 2020
27acdda
Merge remote-tracking branch 'upstream/master'
dklawren Oct 30, 2020
5bf03d7
Merge remote-tracking branch 'upstream/master'
dklawren Nov 9, 2020
1c2eb68
Bug 1676743 - Convert all REST API code to run under native Mojolicio…
dklawren Nov 11, 2020
e809da4
Merge remote-tracking branch 'upstream/master' into master
dklawren Nov 18, 2020
1f943e5
Merge branch 'mojo-rest' of https://github.com/dklawren/bmo into mojo…
dklawren Nov 18, 2020
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
1,834 changes: 1,834 additions & 0 deletions Bugzilla/API/V1/Bug.pm

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions Bugzilla/API/V1/BugUserLastVisit.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 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::BugUserLastVisit;

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

use Bugzilla::Bug;
use Bugzilla::Error;
use Bugzilla::API::V1::Util qw( validate filter );
use Bugzilla::Constants;

sub update {
my ($self, $params) = validate(@_, 'ids');
my $user = Bugzilla->user;
my $dbh = Bugzilla->dbh;

$user->login(LOGIN_REQUIRED);

my $ids = $params->{ids} // [];
ThrowCodeError('param_required', {param => 'ids'}) unless @$ids;

# Cache permissions for bugs. This highly reduces the number of calls to the
# DB. visible_bugs() is only able to handle bug IDs, so we have to skip
# aliases.
$user->visible_bugs([grep /^[0-9]+$/, @$ids]);

$dbh->bz_start_transaction();
my @results;
my $last_visit_ts = $dbh->selectrow_array('SELECT NOW()');
foreach my $bug_id (@$ids) {
my $bug = Bugzilla::Bug->check({id => $bug_id, cache => 1});

next unless $user->can_see_bug($bug->id);

$bug->update_user_last_visit($user, $last_visit_ts);

push(@results,
$self->_bug_user_last_visit_to_hash($bug_id, $last_visit_ts, $params));
}
$dbh->bz_commit_transaction();

return \@results;
}

sub get {
my ($self, $params) = validate(@_, 'ids');
my $user = Bugzilla->user;
my $ids = $params->{ids};

$user->login(LOGIN_REQUIRED);

if ($ids) {

# Cache permissions for bugs. This highly reduces the number of calls to
# the DB. visible_bugs() is only able to handle bug IDs, so we have to
# skip aliases.
$user->visible_bugs([grep /^[0-9]+$/, @$ids]);
}

my @last_visits = @{$user->last_visited};

if ($ids) {

# remove bugs that we are not interested in if ids is passed in.
my %id_set = map { ($_ => 1) } @$ids;
@last_visits = grep { $id_set{$_->bug_id} } @last_visits;
}

return [
map {
$self->_bug_user_last_visit_to_hash($_->bug_id, $_->last_visit_ts, $params)
} @last_visits
];
}

sub _bug_user_last_visit_to_hash {
my ($self, $bug_id, $last_visit_ts, $params) = @_;

my %result = (
id => $self->type('int', $bug_id),
last_visit_ts => $self->type('dateTime', $last_visit_ts)
);

return filter($params, \%result);
}

1;
106 changes: 106 additions & 0 deletions Bugzilla/API/V1/Bugzilla.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 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 Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Logging;
use Bugzilla::Util qw(datetime_from);
use JSON::XS;
use Try::Tiny;

use DateTime;

# Basic info that is needed before logins
use constant LOGIN_EXEMPT => {timezone => 1, version => 1,};

sub version {
my $self = shift;
return {version => $self->type('string', BUGZILLA_VERSION)};
}

sub extensions {
my $self = shift;

my %retval;
foreach my $extension (@{Bugzilla->extensions}) {
my $version = $extension->VERSION || 0;
my $name = $extension->NAME;
$retval{$name}->{version} = $self->type('string', $version);
}
return {extensions => \%retval};
}

sub timezone {
my $self = shift;

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

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

# All Webservices return times in UTC; Use UTC here for backwards compat.
# Hardcode values where appropriate
my $dbh = Bugzilla->dbh;

my $db_time = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
$db_time = datetime_from($db_time, 'UTC');
my $now_utc = DateTime->now();

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

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

Bugzilla->login(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;
try {
$status = $dbh->selectrow_hashref($query);
$status->{errors} = 0 + $status->{errors};
$status->{total} = 0 + $status->{total};
}
catch {
ERROR($_);
ThrowCodeError('jobqueue_status_error');
};

return $status;
}

1;

82 changes: 82 additions & 0 deletions Bugzilla/API/V1/Classification.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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::Classification;

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

use Bugzilla::Classification;
use Bugzilla::Error;
use Bugzilla::API::V1::Util qw(filter validate params_to_objects);

sub get {
my ($self, $params) = validate(@_, 'names', 'ids');

defined $params->{names}
|| defined $params->{ids}
|| ThrowCodeError('params_required',
{function => 'Classification.get', params => ['names', 'ids']});

my $user = Bugzilla->user;

Bugzilla->params->{'useclassification'}
|| $user->in_group('editclassifications')
|| ThrowUserError('auth_classification_not_enabled');

Bugzilla->switch_to_shadow_db;

my @classification_objs
= @{params_to_objects($params, 'Bugzilla::Classification')};
unless ($user->in_group('editclassifications')) {
my %selectable_class
= map { $_->id => 1 } @{$user->get_selectable_classifications};
@classification_objs = grep { $selectable_class{$_->id} } @classification_objs;
}

my @classifications
= map { $self->_classification_to_hash($_, $params) } @classification_objs;

return {classifications => \@classifications};
}

sub _classification_to_hash {
my ($self, $classification, $params) = @_;

my $user = Bugzilla->user;
return
unless (Bugzilla->params->{'useclassification'}
|| $user->in_group('editclassifications'));

my $products
= $user->in_group('editclassifications')
? $classification->products
: $user->get_selectable_products($classification->id);

return filter $params,
{
id => $self->type('int', $classification->id),
name => $self->type('string', $classification->name),
description => $self->type('string', $classification->description),
sort_key => $self->type('int', $classification->sortkey),
products => [map { $self->_product_to_hash($_, $params) } @$products],
};
}

sub _product_to_hash {
my ($self, $product, $params) = @_;

return filter $params,
{
id => $self->type('int', $product->id),
name => $self->type('string', $product->name),
description => $self->type('string', $product->description),
},
undef, 'products';
}

1;
Loading