< Summary

Information
Class: Elsa.Extensions.TriggerExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/TriggerExtensions.cs
Line coverage
70%
Covered lines: 21
Uncovered lines: 9
Coverable lines: 30
Total lines: 91
Line coverage: 70%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Filter(...)100%210%
CreateExpressionExecutionContextAsync()75%9873.07%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/TriggerExtensions.cs

#LineLine coverage
 1using Elsa.Expressions.Contracts;
 2using Elsa.Expressions.Models;
 3using Elsa.Workflows;
 4using Elsa.Workflows.Helpers;
 5using Elsa.Workflows.Models;
 6using Microsoft.Extensions.DependencyInjection;
 7using Microsoft.Extensions.Logging;
 8
 9// ReSharper disable once CheckNamespace
 10namespace Elsa.Extensions;
 11
 12/// <summary>
 13/// Extension methods for <see cref="Trigger"/>.
 14/// </summary>
 15public static class TriggerExtensions
 16{
 17    /// <summary>
 18    /// Returns a filtered list of triggers that match the specified activity type.
 19    /// </summary>
 20    /// <param name="triggers">The triggers to filter.</param>
 21    /// <typeparam name="T">The type of the activity.</typeparam>
 22    public static IEnumerable<Trigger> Filter<T>(this IEnumerable<Trigger> triggers) where T : IActivity
 23    {
 024        var activityTypeName = TypeNameHelper.GenerateTypeName<T>();
 025        return triggers.Where(x => x.Type == activityTypeName);
 26    }
 27
 28    /// <summary>
 29    /// Creates an expression execution context for the specified trigger.
 30    /// </summary>
 31    /// <param name="trigger">The trigger for which to create an expression execution context.</param>
 32    /// <param name="activityDescriptor">The activity descriptor.</param>
 33    /// <param name="serviceProvider">The service provider.</param>
 34    /// <param name="context">The workflow indexing context.</param>
 35    /// <param name="expressionEvaluator">The expression evaluator.</param>
 36    /// <param name="logger">The logger.</param>
 37    /// <returns>An expression execution context.</returns>
 38    public static async Task<ExpressionExecutionContext> CreateExpressionExecutionContextAsync(
 39        this ITrigger trigger,
 40        ActivityDescriptor activityDescriptor,
 41        IServiceProvider serviceProvider,
 42        WorkflowIndexingContext context,
 43        IExpressionEvaluator expressionEvaluator,
 44        ILogger logger)
 45    {
 6146        var namedInputs = trigger.GetNamedInputs();
 26347        var assignedInputs = namedInputs.Where(x => x.Value.MemoryBlockReference != null!).ToList();
 6148        var register = context.GetOrCreateRegister(trigger);
 6149        var cancellationToken = context.CancellationToken;
 6150        var expressionInput = new Dictionary<string, object>();
 6151        var applicationProperties = ExpressionExecutionContextExtensions.CreateTriggerIndexingPropertiesFrom(context.Wor
 6152        applicationProperties[ExpressionExecutionContextExtensions.ActivityKey] = trigger;
 6153        var expressionExecutionContext = new ExpressionExecutionContext(serviceProvider, register, null, applicationProp
 54
 55        // Evaluate activity inputs before requesting trigger data.
 52656        foreach (var namedInput in assignedInputs)
 57        {
 68658            var inputDescriptor = activityDescriptor.Inputs.FirstOrDefault(x => x.Name == namedInput.Key);
 59
 20260            if (inputDescriptor == null)
 61            {
 062                logger.LogWarning("Input descriptor not found for input '{InputName}'", namedInput.Key);
 063                continue;
 64            }
 65
 20266            if (!inputDescriptor.AutoEvaluate)
 67            {
 068                logger.LogDebug("Skipping input '{InputName}' because it is not set to auto-evaluate.", namedInput.Key);
 069                continue;
 70            }
 71
 20272            var input = namedInput.Value;
 20273            var locationReference = input.MemoryBlockReference();
 74
 20275            if (locationReference.Id == null!)
 76                continue;
 77
 78            try
 79            {
 20280                var value = await expressionEvaluator.EvaluateAsync(input, expressionExecutionContext);
 20281                locationReference.Set(expressionExecutionContext, value);
 20282            }
 083            catch (Exception e)
 84            {
 085                logger.LogWarning(e, "Failed to evaluate '{@Expression}'", input.Expression);
 086            }
 20287        }
 88
 6189        return expressionExecutionContext;
 6190    }
 91}