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

Add option to restart jobs upon comment submission #5946

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion assets/javascripts/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ function showAddCommentsDialog() {

function addComments(form) {
const text = form.elements.text.value;
let restartRequested = document.getElementById('restartRequestedCheck').checked ? 1 : 0;
if (!text.length) {
return window.alert("The comment text mustn't be empty.");
}
Expand All @@ -301,10 +302,15 @@ function addComments(form) {
controls.style.display = 'inline';
window.addCommentsModal.hide();
};
const formData = new FormData(form);
formData.append('restartRequested', restartRequested);

$.ajax({
url: form.action,
method: 'POST',
data: $(form).serialize(),
data: formData,
processData: false, // Important to prevent jQuery from processing the data
contentType: false,
success: response => {
addFlash(
'info',
Expand Down
20 changes: 16 additions & 4 deletions lib/OpenQA/WebAPI/Controller/API/V1/Comment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@
my $validation = $self->validation;
$validation->required('text')->like(qr/^(?!\s*$).+/);
$validation->required('job_id')->num(0, undef);
$validation->optional('restartRequested')->in(0, 1);
return $self->reply->validation_error({format => 'json'}) if $validation->has_error;

my $text = $validation->param('text');
my $job_ids = $validation->every_param('job_id');
my $wanna_restart = $validation->param('restartRequested');
my $schema = $self->schema;
my $comments = $schema->resultset('Comments');
my (@created, @failed);
my (@created, @failed, @failed_restart);
for my $job_id (@$job_ids) {
my $txn_guard = $schema->txn_scope_guard;
eval {
Expand All @@ -191,12 +193,22 @@
push @failed, {job_id => $job_id} if $@;
}

if ($wanna_restart && $wanna_restart == 1) {
for my $job_id (@$job_ids) {
my ($res, $jobs, $auto, $single_job_id, $dup_route);
my %args = (jobs => $job_id);
$self->param('jobid', $job_id);
eval { ($res, $jobs, $auto, $single_job_id, $dup_route) = $self->restart_job(\%args); };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _restart method already supports multiple jobs as parameters.
The documentation for the /restart route says "Restart job(s)."

Calling this for every single job and validating the same request parameters again and again is not necessary.
Also you are passing something in %args but you never read that in the function.

And there is already one function that restarts one job, that could also be called, instead of calling the whole _restart thing:

my $res = OpenQA::Resource::Jobs::job_restart($jobs, @params);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Martchus and that's actually also what I meant earlier today: _restart is a complete endpoint:

sub restart ($self) { $self->_restart }

making a helper that implements a complete endpoint, doesn't make much sense to me.

push @failed_restart, {job_id => $job_id, error => "Failed to restart job: $@"} if ($@);
$self->emit_event(openqa_job_restart => {id => $single_job_id, result => $res, auto => $auto});

Check warning on line 203 in lib/OpenQA/WebAPI/Controller/API/V1/Comment.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/API/V1/Comment.pm#L197-L203

Added lines #L197 - L203 were not covered by tests
}
}

# create a single event containing all relevant IDs for this action
my %res = (created => \@created, failed => \@failed);
my %res = (created => \@created, failed => \@failed, failed_restart => \@failed_restart);
$self->emit_event('openqa_comments_create', \%res);

$res{error} = 'Not all comments could be created.' if @failed;
$self->render(json => \%res, status => (@failed ? 400 : 200));
$self->render(json => \%res, status => (@failed || @failed_restart ? 400 : 200));
}

=over 4
Expand Down
38 changes: 2 additions & 36 deletions lib/OpenQA/WebAPI/Controller/API/V1/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -808,42 +808,8 @@ sub done ($self) {
}

sub _restart ($self, %args) {
my $dup_route = $args{duplicate_route_compatibility};
my @flags = qw(force skip_aborting_jobs skip_parents skip_children skip_ok_result_children);
my $validation = $self->validation;
$validation->optional('clone')->num(0);
$validation->optional('prio')->num;
$validation->optional('dup_type_auto')->num(0); # recorded within the event; for informal purposes only
$validation->optional('jobid')->num(0);
$validation->optional('jobs');
$validation->optional('set')->like(qr/.+=.*/);
$validation->optional($_)->num(0) for @flags;
return $self->reply->validation_error({format => 'json'}) if $validation->has_error;

my $jobs = $self->param('jobid');
my $single_job_id;
if ($jobs) {
$self->app->log->debug("Restarting job $jobs");
$jobs = [$jobs];
$single_job_id = $jobs->[0];
}
else {
$jobs = $self->every_param('jobs');
$self->app->log->debug("Restarting jobs @$jobs");
}

my $auto = defined $validation->param('dup_type_auto') ? int($validation->param('dup_type_auto')) : 0;
my %settings = map { split('=', $_, 2) } @{$validation->every_param('set')};
my @params = map { $validation->param($_) ? ($_ => 1) : () } @flags;
push @params, clone => !defined $validation->param('clone') || $validation->param('clone');
push @params, prio => int($validation->param('prio')) if defined $validation->param('prio');
push @params, skip_aborting_jobs => 1 if $dup_route && !defined $validation->param('skip_aborting_jobs');
push @params, force => 1 if $dup_route && !defined $validation->param('force');
push @params, settings => \%settings;

my $res = OpenQA::Resource::Jobs::job_restart($jobs, @params);
OpenQA::Scheduler::Client->singleton->wakeup;

my ($res, $jobs, $auto, $single_job_id, $dup_route) = $self->restart_job(\%args);
return $self->reply->validation_error({format => 'json'}) unless defined $res;
my $duplicates = $res->{duplicates};
my @urls;
for (my $i = 0; $i < @$duplicates; $i++) {
Expand Down
41 changes: 41 additions & 0 deletions lib/OpenQA/WebAPI/Plugin/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use OpenQA::Schema;
use OpenQA::Utils qw(bugurl human_readable_size render_escaped_refs href_to_bugref);
use OpenQA::Events;
use OpenQA::Jobs::Constants qw(EXECUTION_STATES PRE_EXECUTION_STATES ABORTED_RESULTS FAILED NOT_COMPLETE_RESULTS);
use OpenQA::Resource::Jobs;
use OpenQA::Scheduler::Client;
use Text::Glob qw(glob_to_regex_string);
use List::Util qw(any);

Expand Down Expand Up @@ -224,6 +226,45 @@ sub register ($self, $app, $config) {
return $c->tag('span', title => $text, $text);
});

$app->helper(
restart_job => sub ($c, $args) {
my $dup_route = $args->{duplicate_route_compatibility};
my @flags = qw(force skip_aborting_jobs skip_parents skip_children skip_ok_result_children);
my $validation = $c->validation;
$validation->optional('clone')->num(0);
$validation->optional('prio')->num;
$validation->optional('dup_type_auto')->num(0); # recorded within the event; for informal purposes only
$validation->optional('jobid')->num(0);
$validation->optional('jobs');
$validation->optional('set')->like(qr/.+=.*/);
$validation->optional($_)->num(0) for @flags;
return undef if $validation->has_error;

my $jobs = $c->param('jobid');
my $single_job_id;
if ($jobs) {
$c->app->log->debug("Restarting job $jobs");
$jobs = [$jobs];
$single_job_id = $jobs->[0];
}
else {
$jobs = $c->every_param('jobs');
$c->app->log->debug("Restarting jobs @$jobs");
}

my $auto = defined $validation->param('dup_type_auto') ? int($validation->param('dup_type_auto')) : 0;
my %settings = map { split('=', $_, 2) } @{$validation->every_param('set')};
my @params = map { $validation->param($_) ? ($_ => 1) : () } @flags;
push @params, clone => !defined $validation->param('clone') || $validation->param('clone');
push @params, prio => int($validation->param('prio')) if defined $validation->param('prio');
push @params, skip_aborting_jobs => 1 if $dup_route && !defined $validation->param('skip_aborting_jobs');
push @params, force => 1 if $dup_route && !defined $validation->param('force');
push @params, settings => \%settings;
my $res = OpenQA::Resource::Jobs::job_restart($jobs, @params);
OpenQA::Scheduler::Client->singleton->wakeup;
return ($res, $jobs, $auto, $single_job_id, $dup_route);
});

my %progress_bar_query_by_key = (
unfinished => [state => [EXECUTION_STATES, PRE_EXECUTION_STATES]],
skipped => [result => [ABORTED_RESULTS]],
Expand Down
15 changes: 11 additions & 4 deletions templates/webapi/test/overview.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,17 @@
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
<div id="add-comments-controls">
<button type="submit" class="btn btn-danger">
<i class="fa fa-comment"></i> Submit comment on all <%= scalar @$job_ids %> jobs
</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Discard</button>
<div class="form-check my-1">
<input class="form-check-input"
type="checkbox" value="" id="restartRequestedCheck">
<label class="form-check-label" for="restartRequestedCheck">
Restart on Submit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Restart on Submit
Also restart job(s) on submit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I avoid also? Restart job(s) on submit sound sufficient, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the "also" as necessary to make it clear that the checkbox wouldn't "restart instead of commenting" but "restart and comment". But after the discussion we just had I suggest to actually have two buttons:

  1. Left, less-alerting button for "comment", e.g. in light orange
  2. Right, more-alerting button for "Comment and restart", e.g. the current red color

</label>
</div>
<button type="submit" class="btn btn-danger">
<i class="fa fa-comment"></i> Submit comment on all <%= scalar @$job_ids %> jobs
</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Discard</button>
</div>
</div>
</form>
Expand Down
Loading