Skip to content

Commit

Permalink
Merge pull request os-autoinst#5288 from Martchus/cancelled-force-result
Browse files Browse the repository at this point in the history
Allow using force-result label with all final job states
  • Loading branch information
mergify[bot] authored Aug 23, 2023
2 parents 5bfccc7 + c74c8fc commit 62b015d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/OpenQA/Schema/Result/Comments.pm
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ sub _control_job_result ($self, $c) {
die "force_result description '$description' does not match pattern '$force_result_re'\n"
unless ($description // '') =~ /$force_result_re/;
my $job = $self->job;
die "force_result only allowed on finished jobs\n" unless $job->state eq OpenQA::Jobs::Constants::DONE;
$job->update_result($new_result);
die "force_result only allowed on finished jobs\n"
unless OpenQA::Jobs::Constants::meta_state($job->state) eq OpenQA::Jobs::Constants::FINAL;
$job->update_result($new_result, OpenQA::Jobs::Constants::DONE);
return undef;
}

Expand Down
8 changes: 5 additions & 3 deletions lib/OpenQA/Schema/Result/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,11 @@ sub update_backend ($self, $backend_info) {
$self->update({backend => $backend_info->{backend}});
}

sub update_result ($self, $result) {
my $res = $self->update({result => $result});
OpenQA::App->singleton->emit_event('openqa_job_update_result', {id => $self->id, result => $result}) if $res;
sub update_result ($self, $result, $state = undef) {
my %values = (result => $result);
$values{state} = $state if defined $state;
my $res = $self->update(\%values);
OpenQA::App->singleton->emit_event('openqa_job_update_result', {id => $self->id, %values}) if $res;
return $res;
}

Expand Down
9 changes: 9 additions & 0 deletions t/api/09-comments.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Test::Warnings ':report_warnings';
use OpenQA::Test::TimeLimit '8';
use OpenQA::Test::Case;
use OpenQA::Test::Client 'client';
use OpenQA::Jobs::Constants;
use OpenQA::Client;
use Mojo::IOLoop;

Expand Down Expand Up @@ -253,6 +254,14 @@ subtest 'can update job result with special label comment' => sub {
->status_is(400, 'comment can not be created when job is unfinished')
->json_like('/error' => qr/only allowed on finished/);
is $jobs->find($job_id)->result, 'none', 'unfinished job will not be updated';
$job_id = 99981;
$route = "/api/v1/jobs/$job_id/comments";
is $jobs->find($job_id)->state, CANCELLED, 'job initially is cancelled';
$t->post_ok($route => form => {text => 'label:force_result:passed:foo'})
->status_is(200, 'comment can be created when job is cancelled');
my $job = $jobs->find($job_id);
is $job->state, DONE, 'cancelled job is now considered done';
is $job->result, PASSED, 'result of cancelled job has been updated';
};

subtest 'unauthorized users can only read' => sub {
Expand Down

0 comments on commit 62b015d

Please sign in to comment.