Skip to content

Commit

Permalink
openqa-load-templates: check existing job groups
Browse files Browse the repository at this point in the history
The script now checks if job groups are already present when
importing new template data. If a group already exists the data will only be written if the `--update` option was provided.

Related Issue: https://progress.opensuse.org/issues/174808
  • Loading branch information
Robert Richardson committed Jan 2, 2025
1 parent a28ab50 commit 528d903
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 13 additions & 0 deletions script/openqa-load-templates
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ sub post_entry ($table, $entry) {
if ($table eq 'JobGroups') {
# Create the group first
my $job_groups_url = $url->clone->path($options{apibase} . '/job_groups');
my $existing_group_res = $client->get($job_groups_url, form => {name => $entry->{group_name}})->res;

if ($existing_group_res->is_success) {
my $json = $existing_group_res->json;

for my $group (@$json) {
if ($group->{name} eq $entry->{group_name}) {
return 1 if $options{update};
die "Job group '$entry->{group_name}' already exists. Use --update to modify existing entries.";
}
}
}

my $res = $client->post($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error $res unless $res->is_success;

Expand Down
5 changes: 3 additions & 2 deletions t/40-script_load_dump_templates.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use OpenQA::Test::Utils qw(run_cmd test_cmd stop_service);
use Mojo::JSON; # booleans
use Cpanel::JSON::XS ();


sub test_once {
# Report failure at the callsite instead of the test function
local $Test::Builder::Level = $Test::Builder::Level + 1;
Expand Down Expand Up @@ -60,7 +59,9 @@ my $base_args = "--host $host --apikey $apikey --apisecret $apisecret";
$args = "$base_args $filename";
my $expected = qr/JobGroups.+=> \{ added => 1, of => 1 \}/;
test_once $args, $expected, 'Admin may load templates', 0, 'successfully loaded templates';
test_once $args, qr/group with existing name/, 'Duplicate job group', 255, 'failed on duplicate job group';
test_once $args, qr/Use --update to modify/, 'Duplicate job group', 255, 'failed on duplicate job group';
$args = "$base_args --update $filename";
test_once $args, $expected, 'Update with existing job group', 0, 'updated template with existing job group';

subtest 'test changing existing entries' => sub {
# delete job group so that we can load the template again without running into duplicate job group error
Expand Down

0 comments on commit 528d903

Please sign in to comment.