diff --git a/lib/OpenQA/Schema/ResultSet/Jobs.pm b/lib/OpenQA/Schema/ResultSet/Jobs.pm index 26cf07c92ac6..53e22d5a9648 100644 --- a/lib/OpenQA/Schema/ResultSet/Jobs.pm +++ b/lib/OpenQA/Schema/ResultSet/Jobs.pm @@ -274,6 +274,15 @@ sub _prepare_complex_query_search_args ($self, $args) { push @conds, {'me.group_id' => {-in => $subquery}}; } + if (defined $args->{not_groupid}) { + my $id = $args->{not_groupid}; + if ($id) { + push @conds, {-or => [{'me.group_id' => {-not_in => $id}}, {'me.group_id' => undef},]}; + } + else { + push @conds, {'me.group_id' => {-not => undef}}; + } + } if ($args->{ids}) { push @conds, {'me.id' => {-in => $args->{ids}}}; } diff --git a/lib/OpenQA/WebAPI/Controller/API/V1/Job.pm b/lib/OpenQA/WebAPI/Controller/API/V1/Job.pm index 6f7dffe73e82..8b82eec35648 100644 --- a/lib/OpenQA/WebAPI/Controller/API/V1/Job.pm +++ b/lib/OpenQA/WebAPI/Controller/API/V1/Job.pm @@ -85,6 +85,7 @@ sub list ($self) { $validation->optional('limit')->num; $validation->optional('offset')->num; $validation->optional('groupid')->num; + $validation->optional('not_groupid')->num; my $limits = OpenQA::App->singleton->config->{misc_limits}; my $limit = min($limits->{generic_max_limit}, $validation->param('limit') // $limits->{generic_default_limit}); @@ -99,7 +100,7 @@ sub list ($self) { $args{limit} = $limit + 1; $args{offset} = $offset; my @args = qw(build iso distri version flavor scope group groupid - before after arch hdd_1 test machine worker_class + not_groupid before after arch hdd_1 test machine worker_class modules modules_result); for my $arg (@args) { next unless defined(my $value = $self->param($arg)); diff --git a/t/api/04-jobs.t b/t/api/04-jobs.t index 347e63ca980b..44bc010153ec 100644 --- a/t/api/04-jobs.t +++ b/t/api/04-jobs.t @@ -123,6 +123,21 @@ subtest 'check job group' => sub { is(scalar(@{$t->tx->res->json->{jobs}}), 0); }; +subtest 'exclude groupless jobs' => sub { + my %jobs = map { $_->{id} => $_ } @jobs; + is($jobs{99928}->{state}, 'scheduled', 'groupless job is listed'); + $t->get_ok('/api/v1/jobs?not_groupid=0')->status_is(200); + @jobs = @{$t->tx->res->json->{jobs}}; + is scalar @jobs, 15, 'groupless jobs are excluded'; +}; + +subtest 'exclude specific group' => sub { + my %jobs = map { $_->{id} => $_ } @jobs; + $t->get_ok('/api/v1/jobs?not_groupid=1001')->status_is(200); + @jobs = @{$t->tx->res->json->{jobs}}; + is scalar @jobs, 4, 'jobs of specified groups are excluded'; +}; + subtest 'restricted query' => sub { $t->get_ok('/api/v1/jobs?iso=openSUSE-13.1-DVD-i586-Build0091-Media.iso'); is(scalar(@{$t->tx->res->json->{jobs}}), 6, 'query for existing jobs by iso');