Skip to content

Commit

Permalink
Add jobs API option "follow" to return latest clone of queried job
Browse files Browse the repository at this point in the history
  • Loading branch information
asdil12 committed Jan 17, 2025
1 parent b917c95 commit 17b05ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
43 changes: 39 additions & 4 deletions lib/OpenQA/WebAPI/Controller/API/V1/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,25 @@ sub show ($self) {
my $job_id = int($self->stash('jobid'));
my $details = $self->stash('details') || 0;
my $check_assets = !!$self->param('check_assets');
my $job = $self->schema->resultset('Jobs')->find($job_id, {prefetch => 'settings'});
return $self->reply->not_found unless $job;
my $follow = $self->param('follow'); # follow job clones and report most recent result for given id

my @cloned_as;
my $job;
while (1) {
return unless $job = $self->find_job_or_render_not_found($job_id, {prefetch => 'settings'});
if ($follow && $job->clone_id) {
push(@cloned_as, $job->clone_id);
$job_id = $job->clone_id;
next;

Check warning on line 465 in lib/OpenQA/WebAPI/Controller/API/V1/Job.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/API/V1/Job.pm#L463-L465

Added lines #L463 - L465 were not covered by tests
}
last;
}
$job = $job->to_hash(assets => 1, check_assets => $check_assets, deps => 1, details => $details, parent_group => 1);
if ($job_id != $self->stash('jobid')) {
# if follow=1 return cloned job with original id
# but indicate clone chain in cloned_as
$job->{cloned_as} = \@cloned_as;

Check warning on line 473 in lib/OpenQA/WebAPI/Controller/API/V1/Job.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/API/V1/Job.pm#L473

Added line #L473 was not covered by tests
}
$self->render(json => {job => $job});
}

Expand Down Expand Up @@ -597,9 +613,28 @@ interested in the status.
=cut

sub get_status ($self) {
my $follow = $self->param('follow'); # follow job clones and report most recent result for given id
my @fields = qw(id state result blocked_by_id);
return unless my $job = $self->find_job_or_render_not_found($self->stash('jobid'));
$self->render(json => {map { $_ => $job->$_ } @fields});
my $query_jobid = $self->stash('jobid');
my @cloned_as;
my $job;
while (1) {
return unless $job = $self->find_job_or_render_not_found($query_jobid);
if ($follow && $job->clone_id) {
push(@cloned_as, $job->clone_id);
$query_jobid = $job->clone_id;
next;

Check warning on line 626 in lib/OpenQA/WebAPI/Controller/API/V1/Job.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/API/V1/Job.pm#L624-L626

Added lines #L624 - L626 were not covered by tests
}
last;
}
my $json = {map { $_ => $job->$_ } @fields};
if ($query_jobid != $self->stash('jobid')) {
# if follow=1 return cloned job with original id
# but indicate clone chain in cloned_as
$json->{id} = $self->stash('jobid');
$json->{cloned_as} = \@cloned_as;

Check warning on line 635 in lib/OpenQA/WebAPI/Controller/API/V1/Job.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/API/V1/Job.pm#L634-L635

Added lines #L634 - L635 were not covered by tests
}
$self->render(json => $json);
}

=over 4
Expand Down
6 changes: 2 additions & 4 deletions lib/OpenQA/WebAPI/Plugin/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,8 @@ sub _param_hash ($c, $name) {
return @$values ? {map { $_ => 1 } @$values} : undef;
}

sub _find_job_or_render_not_found {
my ($c, $job_id) = @_;

my $job = $c->schema->resultset('Jobs')->find(int($job_id));
sub _find_job_or_render_not_found ($c, $job_id, $query_settings = {}) {
my $job = $c->schema->resultset('Jobs')->find(int($job_id), $query_settings);
return $job if $job;
$c->render(json => {error => 'Job does not exist'}, status => 404);
return undef;
Expand Down

0 comments on commit 17b05ac

Please sign in to comment.