Skip to content

Commit

Permalink
Add option to restart jobs upon comment submission
Browse files Browse the repository at this point in the history
This commit enhances the comment submission functionality with an integrated
job restart option, improving workflow efficiency for users managing multiple
jobs.

For this `restart_job` helper added in `Helpers.pm` and `_restart` in `Job.pm`
had to be refactored to encapsulate the logic and the reusability of the
feature(restart_jobs) in multiple places within the application.

https://progress.opensuse.org/issues/166559

Signed-off-by: Ioannis Bonatakis <[email protected]>
  • Loading branch information
b10n1k committed Sep 25, 2024
1 parent e88bdd3 commit d4080b2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 45 deletions.
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 wannaRestart = document.getElementById('wannaRestartCheck').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('wannaRestart', wannaRestart);

$.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
22 changes: 18 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 @@ sub create_many ($self) {
my $validation = $self->validation;
$validation->required('text')->like(qr/^(?!\s*$).+/);
$validation->required('job_id')->num(0, undef);
$validation->optional('wannaRestart')->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('wannaRestart');
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,24 @@ sub create_many ($self) {
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); };
if ($@) {
push @failed_restart, {job_id => $job_id, error => "Failed to restart job: $@"};
}
$self->emit_event(openqa_job_restart => {id => $single_job_id, result => $res, auto => $auto});
}
}

# 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
37 changes: 1 addition & 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,7 @@ 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);
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 $c->reply->validation_error({format => 'json'}) 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="wannaRestartCheck">
<label class="form-check-label" for="wannaRestartCheck">
Restart on Submit
</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

0 comments on commit d4080b2

Please sign in to comment.