Skip to content

Commit ab10806

Browse files
committed
improve fix action and schemas
1 parent 8b7b0fa commit ab10806

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

src/Service/Operation.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function create(OperationCreate $operation, UserContext $context): int
8585
$row->setParameters($this->wrapParameters($operation->getParameters()));
8686
$row->setIncoming(SchemaScheme::wrap($operation->getIncoming()));
8787
$row->setOutgoing(SchemaScheme::wrap($operation->getOutgoing()));
88-
$row->setThrows($this->wrapThrows($operation->getThrows(), $context));
88+
$row->setThrows($this->wrapThrows($operation->getThrows(), $context, false));
8989
$row->setAction(ActionScheme::wrap($operation->getAction()));
9090
$row->setCosts($operation->getCosts());
9191
$row->setMetadata($operation->getMetadata() !== null ? Parser::encode($operation->getMetadata()) : null);
@@ -131,21 +131,6 @@ public function update(string $operationId, OperationUpdate $operation, UserCont
131131

132132
$isStable = in_array($existing->getStability(), [OperationInterface::STABILITY_STABLE, OperationInterface::STABILITY_LEGACY], true);
133133

134-
$action = $operation->getAction();
135-
if (!empty($action)) {
136-
$action = $this->fixActionToCurrentCommitHash($action, $context);
137-
}
138-
139-
$incoming = $existing->getIncoming();
140-
if (!empty($incoming)) {
141-
$incoming = $this->fixSchemaToCurrentCommitHash($incoming, $context);
142-
}
143-
144-
$outgoing = $existing->getOutgoing();
145-
if (!empty($outgoing)) {
146-
$outgoing = $this->fixSchemaToCurrentCommitHash($outgoing, $context);
147-
}
148-
149134
try {
150135
$this->operationTable->beginTransaction();
151136

@@ -154,6 +139,24 @@ public function update(string $operationId, OperationUpdate $operation, UserCont
154139
// if the operation is stable or legacy we can only change the stability
155140
$existing->setStability($operation->getStability());
156141
} else {
142+
// we fix action and schemas to a specific commit if we transition the operation from experimental to stable
143+
$shouldFix = $existing->getStability() === OperationInterface::STABILITY_EXPERIMENTAL && $operation->getStability() === OperationInterface::STABILITY_STABLE;
144+
145+
$action = $operation->getAction() ?? $existing->getAction();
146+
if (!empty($action) && $shouldFix) {
147+
$action = $this->fixActionToCurrentCommitHash($action, $context);
148+
}
149+
150+
$incoming = $operation->getIncoming() ?? $existing->getIncoming();
151+
if (!empty($incoming) && $shouldFix) {
152+
$incoming = $this->fixSchemaToCurrentCommitHash($incoming, $context);
153+
}
154+
155+
$outgoing = $operation->getOutgoing() ?? $existing->getOutgoing();
156+
if (!empty($outgoing) && $shouldFix) {
157+
$outgoing = $this->fixSchemaToCurrentCommitHash($outgoing, $context);
158+
}
159+
157160
$existing->setActive($operation->getActive() !== null ? (int) $operation->getActive() : $existing->getActive());
158161
$existing->setPublic($operation->getPublic() !== null ? (int) $operation->getPublic() : $existing->getPublic());
159162
$existing->setStability($operation->getStability() ?? $existing->getStability());
@@ -174,7 +177,7 @@ public function update(string $operationId, OperationUpdate $operation, UserCont
174177
$existing->setOutgoing(SchemaScheme::wrap($outgoing ?? $existing->getOutgoing()));
175178
$throws = $operation->getThrows();
176179
if ($throws !== null) {
177-
$existing->setThrows($this->wrapThrows($throws, $context));
180+
$existing->setThrows($this->wrapThrows($throws, $context, $shouldFix));
178181
}
179182
$existing->setAction(ActionScheme::wrap($action ?? $existing->getAction()));
180183
$existing->setCosts($operation->getCosts() ?? $existing->getCosts());
@@ -240,14 +243,14 @@ private function wrapParameters(?OperationParameters $parameters): ?string
240243
return Parser::encode($parameters);
241244
}
242245

243-
private function wrapThrows(?OperationThrows $throws, UserContext $context): ?string
246+
private function wrapThrows(?OperationThrows $throws, UserContext $context, bool $shouldFix): ?string
244247
{
245248
if ($throws === null) {
246249
return null;
247250
}
248251

249252
foreach ($throws->getAll() as $code => $schema) {
250-
if (!empty($schema)) {
253+
if (!empty($schema) && $shouldFix) {
251254
$schema = $this->fixSchemaToCurrentCommitHash($schema, $context);
252255
}
253256

@@ -268,7 +271,13 @@ private function fixSchemaToCurrentCommitHash(?string $schema, UserContext $cont
268271
return $schema;
269272
}
270273

271-
$row = $this->schemaTable->findOneByTenantAndName($context->getTenantId(), $context->getCategoryId(), substr($scheme, 9));
274+
$name = substr($scheme, 9);
275+
if (str_contains($name, '@')) {
276+
$parts = explode('@', $scheme);
277+
$name = $parts[0] ?? '';
278+
}
279+
280+
$row = $this->schemaTable->findOneByTenantAndName($context->getTenantId(), $context->getCategoryId(), $name);
272281
if (!$row instanceof Table\Generated\SchemaRow) {
273282
return $schema;
274283
}
@@ -278,7 +287,7 @@ private function fixSchemaToCurrentCommitHash(?string $schema, UserContext $cont
278287
return $schema;
279288
}
280289

281-
return $schema . '@' . $schemaHash;
290+
return 'schema://' . $name . '@' . $schemaHash;
282291
}
283292

284293
private function fixActionToCurrentCommitHash(?string $action, UserContext $context): ?string
@@ -292,7 +301,13 @@ private function fixActionToCurrentCommitHash(?string $action, UserContext $cont
292301
return $action;
293302
}
294303

295-
$row = $this->actionTable->findOneByTenantAndName($context->getTenantId(), $context->getCategoryId(), substr($scheme, 9));
304+
$name = substr($scheme, 9);
305+
if (str_contains($name, '@')) {
306+
$parts = explode('@', $scheme);
307+
$name = $parts[0] ?? '';
308+
}
309+
310+
$row = $this->actionTable->findOneByTenantAndName($context->getTenantId(), $context->getCategoryId(), $name);
296311
if (!$row instanceof Table\Generated\ActionRow) {
297312
return $action;
298313
}
@@ -302,6 +317,6 @@ private function fixActionToCurrentCommitHash(?string $action, UserContext $cont
302317
return $action;
303318
}
304319

305-
return $action . '@' . $actionHash;
320+
return 'action://' . $name . '@' . $actionHash;
306321
}
307322
}

0 commit comments

Comments
 (0)