Skip to content

Instantly share code, notes, and snippets.

@ejhayes
Created November 18, 2011 17:58
Show Gist options
  • Select an option

  • Save ejhayes/1377214 to your computer and use it in GitHub Desktop.

Select an option

Save ejhayes/1377214 to your computer and use it in GitHub Desktop.
sub duplicateAction {
my ($s, $context) = @_;
my ($oldAction, $newAction) = eval {
# verify correct arguments specified
map { if(!exists($Form->{$_})) { die "invalid arguments"; } } qw(build type target);
BS::db::action->do_transaction(sub{
my $actionType = BS::db::actionType->find_or_create( name => $Form->{type} );
my $target = BS::db::target->find_or_create( name => $Form->{target} );
my $build = BS::db::build->retrieve($Form->{build});
my $sth = BS::db::action->db_Main->prepare("
select * from actions where action_id = (
select max(action_id) from actions
where build_id in (select build_id from builds where project_id = ? and environment_id = ? and completed = 1 and build_id < ?)
and action_type_id = ?
and target_id = ?
)
");
$sth->execute($build->project_id, $build->environment_id, $Form->{build}, $actionType->id, $target->id);
my $oldAction = BS::db::action->sth_to_objects($sth)->first;
my %params;
map { $params{$_} = $oldAction->$_; } qw(action_type_id build_id branch_id revision storm_branch_id storm_revision target_id created_on);
my $newAction = BS::db::action->create( %params );
return ($oldAction, $newAction)
});
};
diag explain $newAction;
diag explain $oldAction;
if( $@ )
{
apiError($@);
}
else
{
$Response->Write( XMLout({success => 1, preserved => $oldAction, id => $newAction }, XMLDecl => 1, RootName => $Config->system->settings->xml_root) );
}# end if()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment