Simplify jobset cloning

We can just show the normal "edit jobset" page for the original jobset
and then do a PUT request to create a new jobset.

Also simplified updating the jobset inputs.  We can just delete all of
them and recreate them from the user parameters.  That's safe because
it's done in a transaction.
This commit is contained in:
Eelco Dolstra 2013-10-03 17:26:17 +00:00
parent 232f46c750
commit f32077b5e8
5 changed files with 27 additions and 111 deletions

View file

@ -149,6 +149,7 @@ sub edit : Chained('jobsetChain') PathPart Args(0) {
$c->stash->{template} = 'edit-jobset.tt'; $c->stash->{template} = 'edit-jobset.tt';
$c->stash->{edit} = 1; $c->stash->{edit} = 1;
$c->stash->{clone} = defined $c->stash->{params}->{clone};
$c->stash->{totalShares} = getTotalShares($c->model('DB')->schema); $c->stash->{totalShares} = getTotalShares($c->model('DB')->schema);
} }
@ -209,102 +210,41 @@ sub updateJobset {
, schedulingshares => int($c->stash->{params}->{schedulingshares}) , schedulingshares => int($c->stash->{params}->{schedulingshares})
}); });
# Process the inputs of this jobset. # Set the inputs of this jobset.
unless (defined $c->stash->{params}->{inputs}) { $jobset->jobsetinputs->delete;
$c->stash->{params}->{inputs} = {};
foreach my $param (keys %{$c->stash->{params}}) { foreach my $param (keys %{$c->stash->{params}}) {
next unless $param =~ /^input-(\w+)-name$/; next unless $param =~ /^input-(\w+)-name$/;
my $baseName = $1; my $baseName = $1;
next if $baseName eq "template"; next if $baseName eq "template";
$c->stash->{params}->{inputs}->{$c->stash->{params}->{$param}} = { type => $c->stash->{params}->{"input-$baseName-type"}, values => $c->stash->{params}->{"input-$baseName-values"} }; my $name = $c->stash->{params}->{$param};
unless ($baseName =~ /^\d+$/) { # non-numeric base name is an existing entry my $type = $c->stash->{params}->{"input-$baseName-type"};
$c->stash->{params}->{inputs}->{$c->stash->{params}->{$param}}->{oldName} = $baseName; my $values = $c->stash->{params}->{"input-$baseName-values"};
}
}
}
foreach my $inputName (keys %{$c->stash->{params}->{inputs}}) { error($c, "Invalid input name $name.") unless $name =~ /^[[:alpha:]][\w-]*$/;
my $inputData = $c->stash->{params}->{inputs}->{$inputName}; error($c, "Invalid input type $type.") unless defined $c->stash->{inputTypes}->{$type};
error($c, "Invalid input name $inputName.") unless $inputName =~ /^[[:alpha:]][\w-]*$/;
my $inputType = $inputData->{type}; my $input = $jobset->jobsetinputs->create({ name => $name, type => $type });
error($c, "Invalid input type $inputType.") unless defined $c->stash->{inputTypes}->{$inputType}; # Set the values for this input.
my @values = ref($values) eq 'ARRAY' ? @{$values} : ($values);
my $input;
unless (defined $inputData->{oldName}) {
$input = $jobset->jobsetinputs->update_or_create(
{ name => $inputName
, type => $inputType
});
} else { # it's an existing input
$input = ($jobset->jobsetinputs->search({name => $inputData->{oldName}}))[0];
die unless defined $input;
$input->update({name => $inputName, type => $inputType});
}
# Update the values for this input. Just delete all the
# current ones, then create the new values.
$input->jobsetinputalts->delete_all;
my $values = $inputData->{values};
$values = [] unless defined $values;
$values = [$values] unless ref($values) eq 'ARRAY';
my $altnr = 0; my $altnr = 0;
foreach my $value (@{$values}) { foreach my $value (@values) {
$value = checkInputValue($c, $inputType, $value); $value = checkInputValue($c, $type, $value);
$input->jobsetinputalts->create({altnr => $altnr++, value => $value}); $input->jobsetinputalts->create({altnr => $altnr++, value => $value});
} }
} }
# Get rid of deleted inputs.
my @inputs = $jobset->jobsetinputs->all;
foreach my $input (@inputs) {
$input->delete unless defined $c->stash->{params}->{inputs}->{$input->name};
}
} }
sub clone : Chained('jobsetChain') PathPart('clone') Args(0) { sub clone : Chained('jobsetChain') PathPart('clone') Args(0) {
my ($self, $c) = @_; my ($self, $c) = @_;
my $jobset = $c->stash->{jobset}; requireProjectOwner($c, $c->stash->{project});
requireProjectOwner($c, $jobset->project);
$c->stash->{template} = 'clone-jobset.tt'; $c->stash->{template} = 'edit-jobset.tt';
} $c->stash->{clone} = 1;
$c->stash->{totalShares} = getTotalShares($c->model('DB')->schema);
sub clone_submit : Chained('jobsetChain') PathPart('clone/submit') Args(0) {
my ($self, $c) = @_;
my $jobset = $c->stash->{jobset};
requireProjectOwner($c, $jobset->project);
requirePost($c);
my $newJobsetName = trim $c->stash->{params}->{"newjobset"};
error($c, "Invalid jobset name: $newJobsetName") if $newJobsetName !~ /^$jobsetNameRE$/;
my $newJobset;
txn_do($c->model('DB')->schema, sub {
$newJobset = $jobset->project->jobsets->create(
{ name => $newJobsetName
, description => $jobset->description
, nixexprpath => $jobset->nixexprpath
, nixexprinput => $jobset->nixexprinput
, enabled => 0
, enableemail => $jobset->enableemail
, emailoverride => $jobset->emailoverride || ""
});
foreach my $input ($jobset->jobsetinputs) {
my $newinput = $newJobset->jobsetinputs->create({name => $input->name, type => $input->type});
foreach my $inputalt ($input->jobsetinputalts) {
$newinput->jobsetinputalts->create({altnr => $inputalt->altnr, value => $inputalt->value});
}
}
});
$c->res->redirect($c->uri_for($c->controller('Jobset')->action_for("edit"), [$jobset->project->name, $newJobsetName]));
} }

View file

@ -1,24 +0,0 @@
[% WRAPPER layout.tt title="Clone jobset $jobset.project.name:$jobset.name" %]
[% PROCESS common.tt %]
[% USE HTML %]
[% edit=1 %]
<form class="form-horizontal" action="[% c.uri_for('/jobset' jobset.project.name jobset.name 'clone' 'submit') %]" method="post">
<fieldset>
<div class="control-group">
<label class="control-label">New name</label>
<div class="controls">
<input type="text" class="span3" name="newjobset" value=""/>
</div>
</div>
<div class="form-actions">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</fieldset>
</form>
[% END %]

View file

@ -1,4 +1,4 @@
[% WRAPPER layout.tt title=(create ? "Create jobset in project $project.name" : "Editing jobset $project.name:$jobset.name") %] [% WRAPPER layout.tt title=(create ? "Create jobset in project $project.name" : clone ? "Cloning jobset $project.name:$jobset.name" : "Editing jobset $project.name:$jobset.name") %]
[% PROCESS common.tt %] [% PROCESS common.tt %]
[% USE format %] [% USE format %]
@ -64,7 +64,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label">Identifier</label> <label class="control-label">Identifier</label>
<div class="controls"> <div class="controls">
<input type="text" class="span3" name="name" [% HTML.attributes(value => jobset.name) %]/> <input type="text" class="span3" name="name" [% HTML.attributes(value => clone ? "" : jobset.name) %]/>
</div> </div>
</div> </div>
@ -132,7 +132,7 @@
[% INCLUDE renderJobsetInputs %] [% INCLUDE renderJobsetInputs %]
<div class="form-actions"> <div class="form-actions">
<button id="submit-jobset" type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> [%IF create %]Create[% ELSE %]Apply changes[% END %]</button> <button id="submit-jobset" type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> [%IF create || clone %]Create jobset[% ELSE %]Apply changes[% END %]</button>
</div> </div>
</fieldset> </fieldset>
@ -168,7 +168,7 @@
$("#submit-jobset").click(function() { $("#submit-jobset").click(function() {
requestJSON({ requestJSON({
[% IF create %] [% IF create || clone %]
url: "[% c.uri_for('/jobset' project.name '.new') %]", url: "[% c.uri_for('/jobset' project.name '.new') %]",
[% ELSE %] [% ELSE %]
url: "[% c.uri_for('/jobset' project.name jobset.name) %]", url: "[% c.uri_for('/jobset' project.name jobset.name) %]",

View file

@ -56,7 +56,7 @@
<div class="form-actions"> <div class="form-actions">
<button id="submit-project" type="submit" class="btn btn-primary"> <button id="submit-project" type="submit" class="btn btn-primary">
<i class="icon-ok icon-white"></i> <i class="icon-ok icon-white"></i>
[%IF create %]Create[% ELSE %]Apply changes[% END %] [%IF create %]Create project[% ELSE %]Apply changes[% END %]
</button> </button>
</div> </div>

View file

@ -51,7 +51,7 @@
<ul class="dropdown-menu"> <ul class="dropdown-menu">
[% INCLUDE menuItem title="Edit configuration" icon="icon-edit" uri=c.uri_for(c.controller('Jobset').action_for('edit'), [project.name, jobset.name]) %] [% INCLUDE menuItem title="Edit configuration" icon="icon-edit" uri=c.uri_for(c.controller('Jobset').action_for('edit'), [project.name, jobset.name]) %]
[% INCLUDE menuItem title="Delete this jobset" icon="icon-trash" uri="javascript:deleteJobset()" %] [% INCLUDE menuItem title="Delete this jobset" icon="icon-trash" uri="javascript:deleteJobset()" %]
[% INCLUDE menuItem title="Clone this jobset" uri=c.uri_for('/jobset' project.name jobset.name 'clone') %] [% INCLUDE menuItem title="Clone this jobset" uri=c.uri_for(c.controller('Jobset').action_for('edit'), [project.name, jobset.name], { clone => 1 }) %]
[% INCLUDE menuItem title="Evaluate this jobset" uri="javascript:confirmEvaluateJobset()" %] [% INCLUDE menuItem title="Evaluate this jobset" uri="javascript:confirmEvaluateJobset()" %]
</ul> </ul>
</li> </li>