Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Http\Controllers\Api\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use ProcessMaker\Services\DeployVersionName;

trait AppliesDeployVersionName
{
private function deployVersionName(): DeployVersionName
{
return app(DeployVersionName::class);
}

/**
* @param array<string, string> $rules
* @return array<string, string>
*/
protected function deployRulesForUpdate(array $rules, bool $isUpdate): array
{
return $this->deployVersionName()->mergeRules($rules, $isUpdate);
}

protected function ensureDeployVersionHistoryAvailable(bool $isUpdate): ?JsonResponse
{
if ($isUpdate && !$this->deployVersionName()->canRecordHistory()) {
return response()->json([
'success' => false,
'message' => 'Version history (package-versions) is required to publish asset updates.',
], 422);
}

return null;
}

protected function latestPublishedVersionId(Model $asset): int
{
return $this->deployVersionName()->latestPublishedVersionId($asset);
}

protected function applyDeployVersionName(
Model $asset,
Request $request,
bool $isUpdate,
int $publishedVersionIdBefore,
): void {
$versionName = $request->input('version_name');
$description = $request->input('version_description');

$this->deployVersionName()->applyAfterSave(
$asset,
is_string($versionName) ? $versionName : null,
(int) $request->user()->id,
$isUpdate,
$publishedVersionIdBefore,
is_string($description) ? $description : null,
);
}
}
26 changes: 21 additions & 5 deletions ProcessMaker/Http/Controllers/Api/DeployProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Exception\MissingBpmnReferenceException;
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Process;
use ProcessMaker\Services\ProcessBpmnReferenceService;
use Throwable;

class DeployProcess extends Controller
{
use AppliesDeployVersionName;

/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
Expand All @@ -34,11 +37,21 @@ public function __construct(

public function deployProcess(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'bpmn' => 'required|string',
'name' => 'required|string|max:255',
'description' => 'nullable|string|max:65535',
]);
$name = (string) $request->input('name');
$isUpdate = Process::where('name', $name)->exists();

if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) {
return $unavailable;
}

$validator = Validator::make(
$request->all(),
$this->deployRulesForUpdate([
'bpmn' => 'required|string',
'name' => 'required|string|max:255',
'description' => 'nullable|string|max:65535',
], $isUpdate),
);

if ($validator->fails()) {
return response()->json([
Expand Down Expand Up @@ -96,8 +109,11 @@ private function upsertProcessByName(Request $request, string $bpmn): array
}

$process->bpmn = $bpmn;
$publishedVersionIdBefore = $this->latestPublishedVersionId($process);
$process->saveOrFail();

$this->applyDeployVersionName($process, $request, !$created, $publishedVersionIdBefore);

return [$process->refresh(), $created];
});
}
Expand Down
29 changes: 23 additions & 6 deletions ProcessMaker/Http/Controllers/Api/DeployScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Screen;
use ProcessMaker\Traits\TaskScreenResourceTrait;
use Symfony\Component\Yaml\Yaml;

class DeployScreen extends Controller
{
use AppliesDeployVersionName;
use TaskScreenResourceTrait;

private const EMPTY_PRESET_DATA = '{}';
Expand All @@ -30,10 +32,20 @@ class DeployScreen extends Controller

public function deployScreen(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'yml_content' => 'required|string',
'name' => 'required|string',
]);
$name = (string) $request->input('name');
$isUpdate = Screen::where('title', $name)->exists();

if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) {
return $unavailable;
}

$validator = Validator::make(
$request->all(),
$this->deployRulesForUpdate([
'yml_content' => 'required|string',
'name' => 'required|string',
], $isUpdate),
);

if ($validator->fails()) {
return response()->json([
Expand All @@ -42,9 +54,12 @@ public function deployScreen(Request $request): JsonResponse
], 422);
}

$ymlContent = $request->input('yml_content');
$name = $request->input('name');
return $this->persistDeployedScreen($request, $name);
}

private function persistDeployedScreen(Request $request, string $name): JsonResponse
{
$ymlContent = $request->input('yml_content');
$parsedData = $this->parseDeployYaml($ymlContent);
if ($parsedData instanceof JsonResponse) {
return $parsedData;
Expand All @@ -61,7 +76,9 @@ public function deployScreen(Request $request): JsonResponse
}

$this->applyYamlFields($screen, $parsedData, $created);
$publishedVersionIdBefore = $this->latestPublishedVersionId($screen);
$screen->save();
$this->applyDeployVersionName($screen, $request, !$created, $publishedVersionIdBefore);
$screen->refresh();

return response()->json([
Expand Down
30 changes: 24 additions & 6 deletions ProcessMaker/Http/Controllers/Api/DeployScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Script;
use ProcessMaker\Services\ScriptExecutorResolver;

class DeployScript extends Controller
{
use AppliesDeployVersionName;

/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
Expand All @@ -38,10 +41,20 @@ public function __construct(private readonly ScriptExecutorResolver $executors)
*/
public function deployScript(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'code' => 'required|string',
'name' => 'required|string',
]);
$name = (string) $request->input('name');
$isUpdate = Script::where('title', $name)->exists();

if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) {
return $unavailable;
}

$validator = Validator::make(
$request->all(),
$this->deployRulesForUpdate([
'code' => 'required|string',
'name' => 'required|string',
], $isUpdate),
);

if ($validator->fails()) {
return response()->json([
Expand All @@ -50,9 +63,12 @@ public function deployScript(Request $request): JsonResponse
], 422);
}

$name = $request->input('name');
$code = $request->input('code');
return $this->persistDeployedScript($request, $name);
}

private function persistDeployedScript(Request $request, string $name): JsonResponse
{
$code = $request->input('code');
$script = Script::where('title', $name)->first();
$created = $script === null;

Expand All @@ -77,7 +93,9 @@ public function deployScript(Request $request): JsonResponse
}

$script->code = $code;
$publishedVersionIdBefore = $this->latestPublishedVersionId($script);
$script->save();
$this->applyDeployVersionName($script, $request, !$created, $publishedVersionIdBefore);
$script->refresh();

return response()->json([
Expand Down
127 changes: 127 additions & 0 deletions ProcessMaker/Services/DeployVersionName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Services;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use Illuminate\Validation\ValidationException;
use ProcessMaker\Package\Versions\Models\VersionHistory;

final class DeployVersionName
{
/**
* @return array<string, string>
*/
public function validationRules(bool $isUpdate): array
{
if ($isUpdate) {
return ['version_name' => 'required|string|min:1|max:255'];
}

return ['version_name' => 'nullable|string|max:255'];
}

/**
* @param array<string, string> $rules
* @return array<string, string>
*/
public function mergeRules(array $rules, bool $isUpdate): array
{
return array_merge($rules, $this->validationRules($isUpdate));
}

public function canRecordHistory(): bool
{
return class_exists(VersionHistory::class)
&& Schema::hasTable('version_history');
}

public function latestPublishedVersionId(Model $asset): int
{
if (!method_exists($asset, 'versions')) {
return 0;
}

$maxId = $asset->versions()->published()->max('id');

return $maxId !== null ? (int) $maxId : 0;
}

public function applyAfterSave(
Model $asset,
?string $versionName,
int $userId,
bool $isUpdate,
int $publishedVersionIdBefore,
?string $description = null,
): void {
$subject = $versionName !== null ? trim($versionName) : '';
if (
$subject === ''
|| !$this->canRecordHistory()
|| !method_exists($asset, 'versions')
) {
return;
}

$version = $this->resolvePublishedVersionAfterSave($asset, $publishedVersionIdBefore)
?? $this->resolveCurrentPublishedForRedeploy($asset, $isUpdate, $publishedVersionIdBefore);
if ($version === null) {
if ($isUpdate) {
throw ValidationException::withMessages([
'version_name' => 'Deploy did not publish a new asset version.',
]);
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

return;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

$history = new VersionHistory();
$history->user_id = $userId;
$history->versionable_id = (int) $version->getKey();
$history->versionable_type = $version::class;
$history->subject = $subject;
$history->description = $description !== null && trim($description) !== ''
? trim($description)
: null;
$history->status = 'ACTIVE';
$history->saveOrFail();
}

private function resolvePublishedVersionAfterSave(Model $asset, int $publishedVersionIdBefore): ?Model
{
$version = $asset->versions()->published()->orderByDesc('id')->first();
Comment thread
gproly marked this conversation as resolved.
Resolved
if ($version === null) {
return null;
}

if ((int) $version->getKey() <= $publishedVersionIdBefore) {
return null;
}

return $version;
}

/**
* Scripts can skip publishing when a draft exists and versionable content is unchanged.
* Still record deploy version history on the current published row (idempotent upsert).
*/
private function resolveCurrentPublishedForRedeploy(
Model $asset,
bool $isUpdate,
int $publishedVersionIdBefore,
): ?Model {
if (!$isUpdate || $publishedVersionIdBefore < 1) {
return null;
}

$version = $asset->versions()->published()->orderByDesc('id')->first();
if ($version === null) {
return null;
}

return (int) $version->getKey() === $publishedVersionIdBefore ? $version : null;
}
}
Loading
Loading