Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions ProcessMaker/Http/Controllers/Api/DeployProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Process;
use ProcessMaker\Services\DeployCategoryResolver;
use ProcessMaker\Services\ProcessBpmnReferenceService;
use Throwable;

Expand All @@ -31,7 +32,8 @@ class DeployProcess extends Controller
];

public function __construct(
private readonly ProcessBpmnReferenceService $references
private readonly ProcessBpmnReferenceService $references,
private readonly DeployCategoryResolver $categories,
) {
}

Expand All @@ -50,6 +52,7 @@ public function deployProcess(Request $request): JsonResponse
'bpmn' => 'required|string',
'name' => 'required|string|max:255',
'description' => 'nullable|string|max:65535',
'process_category_name' => 'nullable|string|max:255',
], $isUpdate),
);

Expand Down Expand Up @@ -101,11 +104,32 @@ private function upsertProcessByName(Request $request, string $bpmn): array
if ($created) {
$process = new Process();
$process->name = $request->input('name');
$process->description = (string) $request->input('description', '');
$process->description = $this->nonEmptyDescription(
$request->input('description'),
(string) $request->input('name'),
);
$process->status = 'ACTIVE';
$process->user_id = $request->user()->id;
$categoryId = $this->categories->resolveProcessCategoryId(
$request->input('process_category_name')
);
if ($categoryId !== null) {
$process->process_category_id = $categoryId;
}
} elseif ($request->has('description')) {
$process->description = (string) $request->input('description');
$process->description = $this->nonEmptyDescription(
$request->input('description'),
(string) $process->name,
);
}

if (!$created && $request->has('process_category_name')) {
$categoryId = $this->categories->resolveProcessCategoryId(
$request->input('process_category_name')
);
if ($categoryId !== null) {
$process->process_category_id = $categoryId;
}
}

$process->bpmn = $bpmn;
Expand Down Expand Up @@ -186,4 +210,13 @@ private function validateXml(string $bpmn): array

return $valid ? [] : $errors;
}

private function nonEmptyDescription(mixed $description, string $fallbackName): string
{
if (is_string($description) && trim($description) !== '') {
return trim($description);
}

return 'Process: ' . trim($fallbackName);
}
}
32 changes: 31 additions & 1 deletion ProcessMaker/Http/Controllers/Api/DeployScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Screen;
use ProcessMaker\Services\DeployCategoryResolver;
use ProcessMaker\Traits\TaskScreenResourceTrait;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -20,6 +21,10 @@ class DeployScreen extends Controller

private const EMPTY_PRESET_DATA = '{}';

public function __construct(private readonly DeployCategoryResolver $categories)
{
}

/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
Expand Down Expand Up @@ -71,8 +76,20 @@ private function persistDeployedScreen(Request $request, string $name): JsonResp
if ($created) {
$screen = new Screen();
$screen->title = $name;
$screen->description = $parsedData['description'] ?? '';
$screen->description = $this->screenDescription($parsedData, $name);
$screen->type = $parsedData['type'] ?? 'FORM';
} elseif (array_key_exists('description', $parsedData)) {
$screen->description = $this->screenDescription($parsedData, $name);
}

$categoryName = $parsedData['category'] ?? $parsedData['screen_category_name'] ?? null;
if ($created || $categoryName !== null) {
$categoryId = $this->categories->resolveScreenCategoryId(
is_string($categoryName) ? $categoryName : null
);
if ($categoryId !== null) {
$screen->screen_category_id = (string) $categoryId;
}
}

$this->applyYamlFields($screen, $parsedData, $created);
Expand Down Expand Up @@ -281,4 +298,17 @@ private function isJsonString(string $data): bool

return true;
}

/**
* @param array<string, mixed> $parsedData
*/
private function screenDescription(array $parsedData, string $title): string
{
$description = $parsedData['description'] ?? '';
if (is_string($description) && trim($description) !== '') {
return trim($description);
}

return 'Screen: ' . trim($title);
}
}
34 changes: 31 additions & 3 deletions ProcessMaker/Http/Controllers/Api/DeployScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Script;
use ProcessMaker\Services\DeployCategoryResolver;
use ProcessMaker\Services\ScriptExecutorResolver;

class DeployScript extends Controller
Expand All @@ -32,8 +33,10 @@ class DeployScript extends Controller

private const DEFAULT_RETRY_WAIT_TIME = 5;

public function __construct(private readonly ScriptExecutorResolver $executors)
{
public function __construct(
private readonly ScriptExecutorResolver $executors,
private readonly DeployCategoryResolver $categories,
) {
}

/**
Expand All @@ -53,6 +56,8 @@ public function deployScript(Request $request): JsonResponse
$this->deployRulesForUpdate([
'code' => 'required|string',
'name' => 'required|string',
'description' => 'nullable|string|max:65535',
'script_category_name' => 'nullable|string|max:255',
], $isUpdate),
);

Expand Down Expand Up @@ -83,7 +88,7 @@ private function persistDeployedScript(Request $request, string $name): JsonResp

$script = new Script();
$script->title = $name;
$script->description = '';
$script->description = $this->deployScriptDescription($request, $name);
$script->language = $executor->language;
$script->script_executor_id = $executor->id;
$script->run_as_user_id = $request->user()->id;
Expand All @@ -92,6 +97,19 @@ private function persistDeployedScript(Request $request, string $name): JsonResp
$script->retry_wait_time = self::DEFAULT_RETRY_WAIT_TIME;
}

if (!$created && $request->has('description')) {
$script->description = $this->deployScriptDescription($request, $name);
}

if ($created || $request->has('script_category_name')) {
$categoryId = $this->categories->resolveScriptCategoryId(
$request->input('script_category_name')
);
if ($categoryId !== null) {
$script->script_category_id = (string) $categoryId;
}
}

$script->code = $code;
$publishedVersionIdBefore = $this->latestPublishedVersionId($script);
$script->save();
Expand Down Expand Up @@ -150,4 +168,14 @@ public function getScriptPhp(Request $request)
->header('X-Script-Id', (string) $script->id)
->header('X-Script-Name', $script->title);
}

private function deployScriptDescription(Request $request, string $title): string
{
$description = $request->input('description');
if (is_string($description) && trim($description) !== '') {
return trim($description);
}

return 'Script: ' . trim($title);
}
}
56 changes: 56 additions & 0 deletions ProcessMaker/Services/DeployCategoryResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Services;

use Illuminate\Database\Eloquent\Model;
use ProcessMaker\Models\ProcessCategory;
use ProcessMaker\Models\ScreenCategory;
use ProcessMaker\Models\ScriptCategory;

class DeployCategoryResolver
{
private const UNCATEGORIZED = 'Uncategorized';

public function resolveProcessCategoryId(?string $name): ?int
{
return $this->resolveActiveCategoryId(ProcessCategory::class, $name);
}

public function resolveScreenCategoryId(?string $name): ?int
{
return $this->resolveActiveCategoryId(ScreenCategory::class, $name);
}

public function resolveScriptCategoryId(?string $name): ?int
{
return $this->resolveActiveCategoryId(ScriptCategory::class, $name);
}

/**
* @param class-string<Model> $modelClass
*/
private function resolveActiveCategoryId(string $modelClass, ?string $name): ?int
{
$trimmed = $name !== null ? trim($name) : '';
if ($trimmed === '') {
$trimmed = self::UNCATEGORIZED;
}

$category = $modelClass::query()
->where('name', $trimmed)
->where('status', 'ACTIVE')
->first();

if ($category !== null) {
return (int) $category->getKey();
}

if ($trimmed !== self::UNCATEGORIZED) {
return $this->resolveActiveCategoryId($modelClass, self::UNCATEGORIZED);
}

return null;
}
}
16 changes: 12 additions & 4 deletions ProcessMaker/Services/ProcessBpmnReferenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ private function replaceNamedReferences(
return $bpmn;
}

$idsByName = $loadIds($names);
$missing = array_values(array_diff($names, array_keys($idsByName)));
$resolvedNames = array_map(
static fn (string $name): string => html_entity_decode($name, ENT_QUOTES | ENT_XML1, 'UTF-8'),
$names
);

$idsByName = $loadIds($resolvedNames);
$missing = array_values(array_diff($resolvedNames, array_keys($idsByName)));
if ($missing !== []) {
throw new MissingBpmnReferenceException(
'Missing ' . $type . ' reference(s): ' . implode(', ', $missing)
Expand All @@ -208,8 +213,11 @@ private function replaceNamedReferences(

return preg_replace_callback(
$pattern,
static fn (array $match): string => $match[1] . $match[2]
. $idsByName[$match[3]] . $match[2],
static function (array $match) use ($idsByName): string {
$name = html_entity_decode($match[3], ENT_QUOTES | ENT_XML1, 'UTF-8');

return $match[1] . $match[2] . $idsByName[$name] . $match[2];
},
$bpmn
);
}
Expand Down
14 changes: 5 additions & 9 deletions ProcessMaker/Services/ScriptExecutorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ class ScriptExecutorResolver
{
public function resolveForNewScript(): ?ScriptExecutor
{
$nayra = ScriptExecutor::query()
->where('language', 'php-nayra')
->orderBy('id')
->first();
if ($nayra !== null) {
return $nayra;
}

$php = ScriptExecutor::query()
->where('language', 'php')
->orderBy('id')
Expand All @@ -26,6 +18,10 @@ public function resolveForNewScript(): ?ScriptExecutor
return $php;
}

return ScriptExecutor::query()->orderBy('id')->first();
return ScriptExecutor::query()
->where('language', '!=', 'php-nayra')
->orderBy('id')
->first()
?? ScriptExecutor::query()->orderBy('id')->first();
}
}
14 changes: 8 additions & 6 deletions tests/Feature/Api/DeployScriptControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,27 @@ public function testGetScriptPhpByNameReturnsPhpCode(): void
$this->assertSame($code, $response->getContent());
}

public function testDeployScriptPrefersPhpNayraExecutorOnCreate(): void
public function testDeployScriptPrefersPhpExecutorOnCreate(): void
{
if (!ScriptExecutor::query()->where('language', 'php-nayra')->exists()) {
ScriptExecutor::factory()->create(['language' => 'php-nayra']);
if (!ScriptExecutor::query()->where('language', 'php')->exists()) {
ScriptExecutor::factory()->create(['language' => 'php', 'title' => 'PHP Executor']);
}

$executor = app(ScriptExecutorResolver::class)->resolveForNewScript();
$this->assertNotNull($executor);
$this->assertSame('php-nayra', $executor->language);
$this->assertSame('php', $executor->language);

$this->apiCall('POST', self::API_ENDPOINT, [
'code' => '<?php return [];',
'name' => 'new-nayra-script',
'name' => 'new-php-script',
'description' => 'Merged migration script.',
])
->assertStatus(200)
->assertJson(['action' => 'created']);

$script = Script::where('title', 'new-nayra-script')->first();
$script = Script::where('title', 'new-php-script')->first();
$this->assertSame($executor->id, $script->script_executor_id);
$this->assertSame('Merged migration script.', $script->description);
}

private function ensureExecutor(): ScriptExecutor
Expand Down