Skip to content

Commit cf1e4af

Browse files
Fix update remote bundle when an asset is removed from the bundle
1 parent 40f43d1 commit cf1e4af

4 files changed

Lines changed: 122 additions & 94 deletions

File tree

‎ProcessMaker/Models/Bundle.php‎

Lines changed: 101 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -155,95 +155,122 @@ public function addAsset(ProcessMakerModel $asset)
155155
public function addSettings($setting, $newId, $type = null)
156156
{
157157
$existingSetting = $this->settings()->where('setting', $setting)->first();
158-
//If $newId is null, set config to null
159-
if (is_null($newId) && is_null($type)) {
160-
if ($existingSetting) {
161-
$existingSetting->update(['config' => null]);
162-
} else {
163-
BundleSetting::create([
164-
'bundle_id' => $this->id,
165-
'setting' => $setting,
166-
'config' => null,
167-
]);
168-
}
169158

170-
return;
159+
if ($this->shouldSetNullConfig($newId, $type)) {
160+
return $this->handleNullConfig($existingSetting, $setting);
171161
}
172-
// verify if newId is a json with id key
162+
163+
$decodedNewId = $this->parseNewId($newId);
164+
165+
if ($existingSetting) {
166+
return $this->updateExistingSetting($existingSetting, $decodedNewId);
167+
}
168+
169+
$this->createNewSetting($setting, $decodedNewId, $type);
170+
}
171+
172+
private function shouldSetNullConfig($newId, $type)
173+
{
174+
return is_null($newId) && is_null($type);
175+
}
176+
177+
private function handleNullConfig($existingSetting, $setting)
178+
{
179+
if ($existingSetting) {
180+
$existingSetting->update(['config' => null]);
181+
} else {
182+
BundleSetting::create([
183+
'bundle_id' => $this->id,
184+
'setting' => $setting,
185+
'config' => null,
186+
]);
187+
}
188+
}
189+
190+
private function parseNewId($newId)
191+
{
173192
$decodedNewId = json_decode($newId, true);
193+
174194
if (json_last_error() === JSON_ERROR_NONE) {
175195
if (is_array($decodedNewId)) {
176-
$newId = $decodedNewId;
177-
if ($existingSetting) {
178-
if ($decodedNewId['id'] === []) {
179-
$existingSetting->delete();
180-
181-
return;
182-
}
183-
$existingSetting->update(['config' => json_encode($newId)]);
184-
185-
return;
186-
}
196+
return $decodedNewId;
187197
} elseif (isset($decodedNewId['id'])) {
188-
$newId = [$decodedNewId['id']];
189-
} else {
190-
$newId = ['id' => [$decodedNewId]];
198+
return ['id' => [$decodedNewId['id']]];
191199
}
192-
} else {
193-
$newId = ['id' => [$newId]];
200+
201+
return ['id' => [$decodedNewId]];
194202
}
195203

196-
if ($existingSetting) {
197-
// Decode the existing JSON
198-
$config = json_decode($existingSetting->config, true);
199-
// Ensure 'id' is an array
200-
if (!isset($config['id']) || !is_array($config['id'])) {
201-
$config['id'] = [];
202-
}
203-
// Add the new ID
204+
return ['id' => [$newId]];
205+
}
206+
207+
private function updateExistingSetting($existingSetting, $decodedNewId)
208+
{
209+
if (isset($decodedNewId['id']) && $decodedNewId['id'] === []) {
210+
$existingSetting->delete();
211+
212+
return;
213+
}
214+
215+
$config = json_decode($existingSetting->config, true) ?: ['id' => []];
216+
217+
if (!isset($config['id']) || !is_array($config['id'])) {
218+
$config['id'] = [];
219+
}
220+
221+
foreach ($decodedNewId['id'] as $id) {
222+
$config['id'][] = $id;
223+
}
224+
225+
$config['id'] = array_values(array_unique($config['id']));
226+
$existingSetting->update(['config' => json_encode($config)]);
227+
}
228+
229+
private function createNewSetting($setting, $newId, $type)
230+
{
231+
$config = ['id' => []];
232+
233+
if ($type === 'settings') {
234+
$config = $this->getSettingsConfig($setting);
235+
} elseif ($type === 'ui_settings') {
236+
$config = $this->getUiSettingsConfig();
237+
} elseif ($newId && $type !== 'settings') {
204238
foreach ($newId['id'] as $id) {
205239
$config['id'][] = $id;
206240
}
207-
// Remove duplicates
208-
$config['id'] = array_unique($config['id']);
209-
//reindex the array
210-
$config['id'] = array_values($config['id']);
211-
// Update the config
212-
$existingSetting->update([
213-
'config' => json_encode($config),
214-
]);
215-
} else {
216-
// Create a new BundleSetting with the initial ID
217-
$config = ['id' => []];
218-
if ($newId && $type !== 'settings') {
219-
foreach ($newId['id'] as $id) {
220-
$config['id'][] = $id;
221-
}
222-
}
241+
}
223242

224-
if ($type === 'settings') {
225-
$settingsMenu = SettingsMenus::where('menu_group', $setting)->first();
226-
$settingsKeys = Setting::where([
227-
['group_id', '=', $settingsMenu->id],
228-
['hidden', '=', false],
229-
])->pluck('key')->toArray();
230-
if ($settingsMenu) {
231-
$config['id'] = $settingsKeys;
232-
$config['type'] = $type;
233-
}
234-
}
243+
BundleSetting::create([
244+
'bundle_id' => $this->id,
245+
'setting' => $setting,
246+
'config' => json_encode($config),
247+
]);
248+
}
235249

236-
if ($type === 'ui_settings') {
237-
$config['id'] = ['css-override', 'login-footer', 'logo-alt-text'];
238-
$config['type'] = $type;
239-
}
250+
private function getSettingsConfig($setting)
251+
{
252+
$config = ['id' => []];
253+
$settingsMenu = SettingsMenus::where('menu_group', $setting)->first();
240254

241-
BundleSetting::create([
242-
'bundle_id' => $this->id,
243-
'setting' => $setting,
244-
'config' => json_encode($config),
245-
]);
255+
if ($settingsMenu) {
256+
$settingsKeys = Setting::where([
257+
['group_id', '=', $settingsMenu->id],
258+
['hidden', '=', false],
259+
])->pluck('key')->toArray();
260+
261+
$config['id'] = $settingsKeys;
262+
$config['type'] = 'settings';
246263
}
264+
265+
return $config;
266+
}
267+
268+
private function getUiSettingsConfig()
269+
{
270+
return [
271+
'id' => ['css-override', 'login-footer', 'logo-alt-text'],
272+
'type' => 'ui_settings',
273+
];
247274
}
248275

249276
public function addAssetToBundles(ProcessMakerModel $asset)
@@ -349,7 +376,7 @@ public function installSettingsPayloads(array $payloads, $mode, $logger = null)
349376
foreach ($payloads as $payload) {
350377
if (isset($payload[0]['export'])) {
351378
$logger->status('Installing bundle settings on the this instance');
352-
$logger->setSteps($payloads[0]);
379+
$logger->setSteps($payload[0]);
353380
$assets[] = DevLink::import($payload[0], $options, $logger);
354381
} elseif (isset($payload[0]['setting_type'])) {
355382
foreach ($payload as $setting) {

‎ProcessMaker/Models/BundleAsset.php‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ class BundleAsset extends ProcessMakerModel
2121

2222
const FLOW_GENIE_CLASS = 'ProcessMaker\Package\PackageAi\Models\FlowGenie';
2323

24-
protected static function boot()
25-
{
26-
parent::boot();
27-
28-
static::deleting(function ($bundleAsset) {
29-
$bundleAsset->bundle->validateEditable();
30-
});
31-
}
32-
3324
public static function canExport(ProcessMakerModel $asset)
3425
{
3526
return method_exists($asset, 'export') && ExporterMap::getExporterClassForModel($asset);

‎ProcessMaker/Models/DevLink.php‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,6 @@ public function installRemoteBundle($remoteBundleId, $updateType)
138138
$this->logger->status(__('Downloading bundle from remote instance'));
139139

140140
$bundleInfo = $this->remoteBundle($remoteBundleId)->json();
141-
$token = Str::random(60);
142-
143-
$addBundleInstance = $this->client()->post(
144-
route('api.devlink.add-bundle-instance', ['bundle' => $remoteBundleId], false),
145-
[
146-
'instance_url' => env('APP_URL') . '/devlink/bundle-updated/' . $remoteBundleId . '/' . $token,
147-
]
148-
);
149141

150142
$bundleExport = $this->client()->get(
151143
route('api.devlink.export-local-bundle', ['bundle' => $remoteBundleId], false)
@@ -168,9 +160,21 @@ public function installRemoteBundle($remoteBundleId, $updateType)
168160
'name' => $bundleInfo['name'],
169161
'published' => $bundleInfo['published'],
170162
'version' => $bundleInfo['version'],
171-
'webhook_token' => $token,
163+
'description' => $bundleInfo['description'],
172164
]
173165
);
166+
if ($bundle->wasRecentlyCreated) {
167+
$token = Str::random(60);
168+
169+
$addBundleInstance = $this->client()->post(
170+
route('api.devlink.add-bundle-instance', ['bundle' => $remoteBundleId], false),
171+
[
172+
'instance_url' => env('APP_URL') . '/devlink/bundle-updated/' . $remoteBundleId . '/' . $token,
173+
]
174+
);
175+
$bundle->webhook_token = $token;
176+
$bundle->save();
177+
}
174178

175179
$bundle->savePayloadsToFile($bundleExport['payloads'], $bundleSettingsPayloads['payloads']);
176180

‎tests/Model/DevLinkTest.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function testInstallRemoteBundle()
8080
'name' => 'Test Bundle',
8181
'published' => true,
8282
'version' => '5',
83+
'description' => 'Test Bundle Description',
8384
]),
8485
'http://remote-instance.test/api/1.0/devlink/export-local-bundle/123' => Http::response([
8586
'payloads' => $exports,
@@ -91,7 +92,7 @@ public function testInstallRemoteBundle()
9192
]],
9293
]),
9394
'http://remote-instance.test/api/1.0/devlink/export-local-bundle/123/settings-payloads' => Http::response([
94-
'payloads' => $exportsSettingsPayloads,
95+
'payloads' => [$exportsSettingsPayloads],
9596
]),
9697
'http://remote-instance.test/api/1.0/devlink/local-bundles/123/add-bundle-instance' => Http::response([], 200),
9798
]);
@@ -188,30 +189,35 @@ public function testUpdateBundle()
188189
'name' => 'Test Bundle',
189190
'published' => true,
190191
'version' => '2',
192+
'description' => 'Test Bundle Description',
191193
], 200)
192194
->push([
193195
'id' => 123,
194196
'name' => 'Test Bundle',
195197
'published' => true,
196198
'version' => '3',
199+
'description' => 'Test Bundle Description',
197200
], 200)
198201
->push([
199202
'id' => 123,
200203
'name' => 'Test Bundle',
201204
'published' => true,
202205
'version' => '4',
206+
'description' => 'Test Bundle Description',
203207
], 200)
204208
->push([
205209
'id' => 123,
206210
'name' => 'Test Bundle',
207211
'published' => true,
208212
'version' => '8',
213+
'description' => 'Test Bundle Description',
209214
], 200)
210215
->push([
211216
'id' => 123,
212217
'name' => 'Test Bundle',
213218
'published' => true,
214219
'version' => '9',
220+
'description' => 'Test Bundle Description',
215221
], 200),
216222
'http://remote-instance.test/api/1.0/devlink/export-local-bundle/123' => Http::sequence()
217223
->push([
@@ -236,7 +242,7 @@ public function testUpdateBundle()
236242
]],
237243
]),
238244
'http://remote-instance.test/api/1.0/devlink/export-local-bundle/123/settings-payloads' => Http::response([
239-
'payloads' => $exportsSettingsPayloads,
245+
'payloads' => [$exportsSettingsPayloads],
240246
]),
241247
'http://remote-instance.test/api/1.0/devlink/local-bundles/123/add-bundle-instance' => Http::response([], 200),
242248
]);

0 commit comments

Comments
 (0)