public function createAction($scriptId)
{
$this->checkPermissions(Acl::RESOURCE_SCRIPTS_ENVIRONMENT, Acl::PERM_SCRIPTS_ENVIRONMENT_MANAGE);
$object = $this->request->getJsonBody();
/* @var $versionAdapter ScriptVersionAdapter */
$versionAdapter = $this->adapter('scriptVersion');
if (empty($object->body)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed version content");
}
$object->body = str_replace("\r\n", "\n", $object->body);
$versionAdapter->validateObject($object, Request::METHOD_POST);
$version = $versionAdapter->toEntity($object);
$version->scriptId = $scriptId;
$version->changedById = $this->getUser()->id;
$version->changedByEmail = $this->getUser()->email;
$versionAdapter->validateEntity($version);
/* @var $script Script */
$script = Script::findPk($version->scriptId);
try {
$version->version = $script->getLatestVersion()->version + 1;
} catch (Exception $e) {
$version->version = 1;
}
//Saves entity
$version->save();
if (empty($script->os)) {
$script->os = !strncmp($version->content, '#!cmd', strlen('#!cmd')) || !strncmp($version->content, '#!powershell', strlen('#!powershell')) ? Script::OS_WINDOWS : Script::OS_LINUX;
$script->save();
}
//Responds with 201 Created status
$this->response->setStatus(201);
return $this->result($versionAdapter->toData($version));
}