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.
ZFEN2MAOJRRQEIORSK2N6PNAWGBEN2WNAWA6IWUVOHP5I5PQC6JAC
HNUZZHS4I762NGDMXOSQFPMU5BEKCFYFG64F6CJGXFOIZVTLDACQC
4NTIBJ74F2QUMISJPB4DGM7VPYFVZDS6ZDITVIAQCSWO6C3UIQ6QC
TQVKZQUGCFYNH5P56LXMXRXZNTD56MH5T5GX2BMQ5YSRPGHAUYMAC
ODNCGFQ5FPKFI624BVMLW7PJ2EFJOR3TY66OCZM42UNNTWBCF2TQC
GEADFVZ5LXXFIE3VIP4UJ4AEI2VX57DXER47JA4IHH5BG3QNPAEAC
3HZY24CX4U2TO74HOY4YX3LBJIYF4DLXHCIY7J2RASAC4COMSMZAC
UMFB2767NMAG46NAG3XHMYQMCLF5TMVN46SST4AM2CFUEMNZH3WQC
LZVO64YG43JD7YMZSCTZNOBS5ROZA4FMPKJW2YOMHX2V5PTGBVWQC
ZCTQCN3EQDFYIKRKM3SJIGLTZ6QVKNPCIR3V6CRZ3W4PP2V2YQ2AC
OOQ2D3KCLFPYNAN253PHWLBQMB6OMO2KYQWQXLTP65SQAYZWQ5LAC
RSEGBU6CIEDRNTL7R6Q7RF2IZGCN3HH6VTPGFBKD77LSHEUD6EKAC
TNMOG2ZQTXJJ6TEPEDB6D4XPIIGMAV3DUDWO33WD23CGDVT4LO2QC
HQGXL4MXGHICQ3IRRQUR6KQHS2RKVOTDKQ5ZETXHQ56DANR2YADQC
VS3OUK7DARCUQWR6NC2MIKHBLOVPXDUGITCNYS5VHYYGHS3OQBNQC
HFJPPMTU45LWGNLX2V3JXBP2VPK2W6PPONF4TUSQAAH7ARDAD5HAC
KJC7RKIEGIR62HPUILVCORKOFYTIAVNPHU3U2CDS36ZEQA7T7UBAC
KG5AMUMGZA53FMA35K3KYOPJTNG3BKWDFWDPVXJ6XGG2GJJRI2LAC
RWFCW6ZNIE24XTEQUK5Y37K42BQER7EDGSSD4GQNFTGO5V4TY72QC
U4TD3AIQXBJFFUORTMIC4IHZTVBORRKL2TZ2FSP4G665ECZOEMNAC
D5QIOJGPKQJIYBUCSC3MFJ3TXLPNZ2XMI37GXMFRVRFWWR2VMTFAC
PZL3SZM3U3BYJX2RGYXC6NMBG7WQHFWHSYDYXZ7Q5VZA3EDYVPIQC
2G63HKCHG7S6DGWDOHSDF7PXFPD6H4TRKDKIIFCXXAKET6FCWN2AC
OX6NYJDVCDX2UCJ4QQMTRJVZ535NOMCJGYCHDLCADJPQI7RMCXPQC
A6XVP6FE3CO7BVZR23HQS2GFDNF3FK7KBDSOB4YIPRISZHEWQJKAC
JY7BXXOP3EZCDT5RSMVE4Y6IECXGYL4GEBJOZHR7H3Z35XZ3NIVQC
DAPOSS44WRFQZ7XUWSMYCCORY4POKD42DGAIXMGP4ZRBOEPNNRGAC
# Process the inputs of this jobset.
unless (defined $c->stash->{params}->{inputs}) {
$c->stash->{params}->{inputs} = {};
foreach my $param (keys %{$c->stash->{params}}) {
next unless $param =~ /^input-(\w+)-name$/;
my $baseName = $1;
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"} };
unless ($baseName =~ /^\d+$/) { # non-numeric base name is an existing entry
$c->stash->{params}->{inputs}->{$c->stash->{params}->{$param}}->{oldName} = $baseName;
}
}
}
# Set the inputs of this jobset.
$jobset->jobsetinputs->delete;
foreach my $inputName (keys %{$c->stash->{params}->{inputs}}) {
my $inputData = $c->stash->{params}->{inputs}->{$inputName};
error($c, "Invalid input name ‘$inputName’.") unless $inputName =~ /^[[:alpha:]][\w-]*$/;
foreach my $param (keys %{$c->stash->{params}}) {
next unless $param =~ /^input-(\w+)-name$/;
my $baseName = $1;
next if $baseName eq "template";
my $name = $c->stash->{params}->{$param};
my $type = $c->stash->{params}->{"input-$baseName-type"};
my $values = $c->stash->{params}->{"input-$baseName-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';
# Set the values for this input.
my @values = ref($values) eq 'ARRAY' ? @{$values} : ($values);
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]));
}
[% 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>
</form>
[% END %]
</fieldset>
[% 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") %]
<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>