< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute.WorkflowExecutionHelper
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Execute/WorkflowExecutionHelper.cs
Line coverage
72%
Covered lines: 31
Uncovered lines: 12
Coverable lines: 43
Total lines: 91
Line coverage: 72%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ExecuteWorkflowAsync()75%181681.57%
HandleFaultAsync()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Execute/WorkflowExecutionHelper.cs

#LineLine coverage
 1using System.Net.Mime;
 2using Elsa.Common.Models;
 3using Elsa.Workflows.Management;
 4using Elsa.Workflows.Runtime;
 5using Elsa.Workflows.State;
 6using FastEndpoints;
 7using Microsoft.AspNetCore.Http;
 8
 9namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute;
 10
 11public static class WorkflowExecutionHelper
 12{
 13    public static async Task ExecuteWorkflowAsync(
 14        IExecutionRequest request,
 15        IWorkflowDefinitionService workflowDefinitionService,
 16        IWorkflowRuntime workflowRuntime,
 17        IWorkflowStarter workflowStarter,
 18        IApiSerializer apiSerializer,
 19        HttpContext httpContext,
 20        CancellationToken cancellationToken)
 21    {
 1122        var definitionId = request.DefinitionId;
 1123        var versionOptions = request.VersionOptions ?? VersionOptions.Published;
 1124        var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell
 25
 1126        if (workflowGraph == null)
 27        {
 028            await httpContext.Response.SendNotFoundAsync(cancellation: cancellationToken);
 029            return;
 30        }
 31
 1132        var startRequest = new StartWorkflowRequest
 1133        {
 1134            Workflow = workflowGraph.Workflow,
 1135            CorrelationId = request.CorrelationId,
 1136            Name = request.Name,
 1137            Input = request.GetInputAsDictionary(),
 1138            Variables = request.GetVariablesAsDictionary(),
 1139            TriggerActivityId = request.TriggerActivityId,
 1140            ActivityHandle = request.ActivityHandle
 1141        };
 42
 1143        var startResponse = await workflowStarter.StartWorkflowAsync(startRequest, cancellationToken);
 44
 1145        if(!httpContext.Response.HasStarted)
 1146            httpContext.Response.Headers.Append("x-elsa-workflow-cannot-start", startResponse.CannotStart.ToString());
 47
 1148        if (startResponse.CannotStart)
 49        {
 050            httpContext.Response.StatusCode = StatusCodes.Status200OK;
 051            await httpContext.Response.SendOkAsync(cancellationToken);
 052            return;
 53        }
 54
 1155        var instanceId = startResponse.WorkflowInstanceId!;
 56
 1157        if(!httpContext.Response.HasStarted)
 1158            httpContext.Response.Headers.Append("x-elsa-workflow-instance-id", instanceId);
 59
 1160        var workflowClient = await workflowRuntime.CreateClientAsync(instanceId, cancellationToken);
 61
 1162        if (startResponse.SubStatus == WorkflowSubStatus.Faulted)
 63        {
 064            var workflowState = await workflowClient.ExportStateAsync(cancellationToken);
 065            await HandleFaultAsync(workflowState, apiSerializer, httpContext, cancellationToken);
 66        }
 67        else
 68        {
 1169            if (!httpContext.Response.HasStarted)
 70            {
 1171                httpContext.Response.Headers.Append("x-elsa-response", "true");
 1172                if (httpContext.Response.StatusCode == StatusCodes.Status200OK)
 73                {
 1174                    var workflowState = await workflowClient.ExportStateAsync(cancellationToken);
 1175                    var response = apiSerializer.Serialize(new Response(workflowState));
 1176                    httpContext.Response.ContentType = MediaTypeNames.Application.Json;
 1177                    await httpContext.Response.WriteAsync(response, cancellationToken);
 78                }
 79            }
 80        }
 1181    }
 82
 83    private static async Task HandleFaultAsync(WorkflowState workflowState, IApiSerializer apiSerializer, HttpContext ht
 84    {
 085        var faultedResponse = apiSerializer.Serialize(new Response(workflowState));
 086        httpContext.Response.ContentType = MediaTypeNames.Application.Json;
 087        httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
 088        await httpContext.Response.WriteAsync(faultedResponse, cancellationToken);
 089    }
 90}
 91