< Summary

Information
Class: Elsa.Alterations.Middleware.Workflows.RunAlterationsMiddleware
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Middleware/Workflows/RunAlterationsMiddleware.cs
Line coverage
87%
Covered lines: 21
Uncovered lines: 3
Coverable lines: 24
Total lines: 58
Line coverage: 87.5%
Branch coverage
66%
Covered branches: 12
Total branches: 18
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
InvokeAsync()50%44100%
RunAsync()71.42%151481.25%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Middleware/Workflows/RunAlterationsMiddleware.cs

#LineLine coverage
 1using Elsa.Alterations.Core.Contexts;
 2using Elsa.Alterations.Core.Contracts;
 3using Elsa.Alterations.Core.Models;
 4using Elsa.Extensions;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Pipelines.WorkflowExecution;
 7
 8namespace Elsa.Alterations.Middleware.Workflows;
 9
 10/// <summary>
 11/// Middleware that runs alterations.
 12/// </summary>
 113internal class RunAlterationsMiddleware(WorkflowMiddlewareDelegate next, IEnumerable<IAlterationHandler> handlers) : Wor
 14{
 115    public static readonly object AlterationsPropertyKey = new();
 116    public static readonly object AlterationsLogPropertyKey = new();
 17
 18    public override async ValueTask InvokeAsync(WorkflowExecutionContext context)
 19    {
 120        var alterations = (IEnumerable<IAlteration>)(context.TransientProperties.GetValue(AlterationsPropertyKey) ?? thr
 121        var log = (AlterationLog)(context.TransientProperties.GetValue(AlterationsLogPropertyKey) ?? throw new InvalidOp
 122        await RunAsync(context, alterations, log, context.CancellationToken);
 123    }
 24
 25    private async Task RunAsync(WorkflowExecutionContext workflowExecutionContext, IEnumerable<IAlteration> alterations,
 26    {
 127        var commitActions = new List<Func<Task>>();
 28
 429        foreach (var alteration in alterations)
 30        {
 31            // Find handlers.
 632            var supportedHandlers = handlers.Where(x => x.CanHandle(alteration)).ToList();
 33
 434            foreach (var handler in supportedHandlers)
 35            {
 36                // Execute handler.
 137                var alterationContext = new AlterationContext(alteration, workflowExecutionContext, log, cancellationTok
 138                await handler.HandleAsync(alterationContext);
 39
 40                // If the handler has failed, exit.
 141                if (alterationContext.HasFailed)
 042                    return;
 43
 44                // Collect the commit handler, if any.
 145                if (alterationContext.CommitAction != null)
 046                    commitActions.Add(alterationContext.CommitAction);
 147            }
 148        }
 49
 50        // Execute commit handlers.
 251        foreach (var commitAction in commitActions)
 052            await commitAction();
 53
 54        // Add alteration logs to the workflow execution log.
 455        foreach (var alterationLogEntry in log.LogEntries)
 156            workflowExecutionContext.AddExecutionLogEntry(alterationLogEntry.EventName ?? alterationLogEntry.Message, al
 157    }
 58}