Repository navigation
feat(FOUR-34469): require version_name on deploy upsert updates #9181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
76fe727
feat(FOUR-34469): require version_name on deploy upsert updates
gproly 11caea4
refactor(FOUR-34469): reduce return count in deploy-script handler
gproly 55d2ab8
refactor(FOUR-34469): reduce returns in DeployVersionName::applyAfter…
gproly c4f117f
refactor(FOUR-34469): reduce return count in deploy-screen handler
gproly b2acaa3
feat(FOUR-34469): require version_name on deploy upsert updates
gproly 76546bf
Merge branch 'feature/FOUR-34328' into FOUR-34469
gproly 79735a9
fix(FOUR-34469): stabilise inherited CI failures on deploy and MCP su…
gproly 66d9cb6
fix(FOUR-34469): send required fields on modules script duplicate API…
gproly 44e30d8
Merge branch 'feature/FOUR-34328' into FOUR-34469
gproly b6af21a
fix(FOUR-34469): require version_name on deploy screen preview preset…
gproly 4850290
fix(FOUR-34469): record deploy version history on unchanged script re…
gproly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
ProcessMaker/Http/Controllers/Api/Concerns/AppliesDeployVersionName.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.', | ||
| ]); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
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(); | ||
|
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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.