| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Platform.Integration.Models; |
| | | 3 | | using Elsa.Platform.Integration.Services; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Models; |
| | | 7 | | using Loom; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Platform.Integration.Steps; |
| | | 10 | | |
| | | 11 | | [Step("elsa.import-workflow-definition")] |
| | 2 | 12 | | public sealed class ImportWorkflowDefinitionStep( |
| | 2 | 13 | | IWorkflowDefinitionImporter importer, |
| | 2 | 14 | | IApiSerializer apiSerializer, |
| | 2 | 15 | | PlatformRecipeArtifact artifact) : IStep, IValidatingStep |
| | | 16 | | { |
| | 5 | 17 | | public JsonElement WorkflowDefinition { get; init; } |
| | | 18 | | |
| | 2 | 19 | | public string? Path { get; init; } |
| | | 20 | | |
| | 3 | 21 | | public bool Publish { get; init; } = true; |
| | | 22 | | |
| | | 23 | | public ValueTask<IReadOnlyList<RecipeDiagnostic>> ValidateAsync( |
| | | 24 | | StepValidationContext context, |
| | | 25 | | CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 1 | 27 | | var hasInlineWorkflow = WorkflowDefinition.ValueKind == JsonValueKind.Object; |
| | 1 | 28 | | var hasPath = !string.IsNullOrWhiteSpace(Path); |
| | 1 | 29 | | if (!hasInlineWorkflow && !hasPath) |
| | | 30 | | { |
| | 0 | 31 | | return ValueTask.FromResult<IReadOnlyList<RecipeDiagnostic>>( |
| | 0 | 32 | | [ |
| | 0 | 33 | | context.Error( |
| | 0 | 34 | | "ELSA_PLATFORM_WORKFLOW_DEFINITION_MISSING", |
| | 0 | 35 | | "A workflow definition or artifact-relative path is required.", |
| | 0 | 36 | | context.Target("input.workflowDefinition")) |
| | 0 | 37 | | ]); |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | if (hasPath && !artifact.TryGetText(Path!, out _)) |
| | | 41 | | { |
| | 0 | 42 | | return ValueTask.FromResult<IReadOnlyList<RecipeDiagnostic>>( |
| | 0 | 43 | | [ |
| | 0 | 44 | | context.Error( |
| | 0 | 45 | | "ELSA_PLATFORM_WORKFLOW_DEFINITION_FILE_MISSING", |
| | 0 | 46 | | $"Workflow definition file '{Path}' was not found in the recipe artifact.", |
| | 0 | 47 | | context.Target("input.path")) |
| | 0 | 48 | | ]); |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | return ValueTask.FromResult<IReadOnlyList<RecipeDiagnostic>>([]); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public async ValueTask ExecuteAsync(StepContext context, CancellationToken cancellationToken = default) |
| | | 55 | | { |
| | 1 | 56 | | var workflowJson = ResolveWorkflowJson(); |
| | | 57 | | WorkflowDefinitionModel model; |
| | | 58 | | try |
| | | 59 | | { |
| | 1 | 60 | | model = apiSerializer.Deserialize<WorkflowDefinitionModel>(workflowJson); |
| | 1 | 61 | | } |
| | 0 | 62 | | catch (Exception ex) when (ex is JsonException or NotSupportedException or InvalidOperationException) |
| | | 63 | | { |
| | 0 | 64 | | throw new InvalidOperationException("Workflow definition payload is invalid.", ex); |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | var importResult = await importer.ImportAsync(new SaveWorkflowDefinitionRequest |
| | 1 | 68 | | { |
| | 1 | 69 | | Model = model, |
| | 1 | 70 | | Publish = Publish |
| | 1 | 71 | | }, cancellationToken); |
| | | 72 | | |
| | 1 | 73 | | if (!importResult.Succeeded) |
| | | 74 | | { |
| | 0 | 75 | | var errors = string.Join("; ", importResult.ValidationErrors.Select(x => x.Message)); |
| | 0 | 76 | | throw new InvalidOperationException($"Workflow definition validation failed: {errors}"); |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | context.Log($"Workflow definition '{model.DefinitionId}' was imported."); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | private string ResolveWorkflowJson() |
| | | 83 | | { |
| | 1 | 84 | | if (!string.IsNullOrWhiteSpace(Path)) |
| | | 85 | | { |
| | 0 | 86 | | if (artifact.TryGetText(Path, out var content)) |
| | 0 | 87 | | return content; |
| | | 88 | | |
| | 0 | 89 | | throw new InvalidOperationException($"Workflow definition file '{Path}' was not found in the recipe artifact |
| | | 90 | | } |
| | | 91 | | |
| | 1 | 92 | | if (WorkflowDefinition.ValueKind == JsonValueKind.Object) |
| | 1 | 93 | | return WorkflowDefinition.GetRawText(); |
| | | 94 | | |
| | 0 | 95 | | throw new InvalidOperationException("A workflow definition or artifact-relative path is required."); |
| | | 96 | | } |
| | | 97 | | } |