< Summary

Information
Class: Elsa.Workflows.Management.Activities.SetOutput.SetOutput
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Activities/SetOutput/SetOutput.cs
Line coverage
93%
Covered lines: 30
Uncovered lines: 2
Coverable lines: 32
Total lines: 86
Line coverage: 93.7%
Branch coverage
78%
Covered branches: 11
Total branches: 14
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_OutputName()100%11100%
get_OutputValue()100%11100%
Execute(...)100%44100%
SetAncestorActivityOutput(...)70%101090.47%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Activities/SetOutput/SetOutput.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Extensions;
 3using Elsa.Workflows.Attributes;
 4using Elsa.Workflows.UIHints;
 5using Elsa.Workflows.Models;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Workflows.Management.Activities.SetOutput;
 9
 10/// <summary>
 11/// Assigns a given value to a workflow definition's output.
 12/// </summary>
 13[Activity("Elsa", "Composition", "Assigns a given value to a the container's output.", Kind = ActivityKind.Action)]
 14[PublicAPI]
 15public class SetOutput : CodeActivity
 16{
 17    /// <inheritdoc />
 13618    public SetOutput([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 19    {
 13620    }
 21
 22    /// <summary>
 23    /// The name of the output to assign.
 24    /// </summary>
 25    [Input(
 26        DisplayName = "Output",
 27        Description = "The output to assign.",
 28        UIHint = InputUIHints.OutputPicker
 29    )]
 43430    public Input<string> OutputName { get; set; } = null!;
 31
 32    /// <summary>
 33    /// The value to assign to the output.
 34    /// </summary>
 35    [Input(Description = "The value to assign.")]
 43136    public Input<object?> OutputValue { get; set; } = null!;
 37
 38    /// <inheritdoc />
 39    protected override void Execute(ActivityExecutionContext context)
 40    {
 1841        var outputName = OutputName.Get(context);
 1842        var outputValue = OutputValue.GetOrDefault(context);
 3743        var ancestorContext = context.ParentActivityExecutionContext?.GetAncestors().FirstOrDefault(x => x.ActivityDescr
 44
 1845        if (ancestorContext != null)
 346            SetAncestorActivityOutput(context, ancestorContext, outputValue);
 47        else
 1548            context.WorkflowExecutionContext.Output[outputName] = outputValue!;
 1549    }
 50
 51    private void SetAncestorActivityOutput(ActivityExecutionContext context, ActivityExecutionContext ancestorContext, o
 52    {
 353        var ancestorActivity = ancestorContext.Activity;
 354        var ancestorActivityDescriptor = ancestorContext.ActivityDescriptor;
 355        var outputName = OutputName.Get(context);
 756        var ancestorOutputDescriptor = ancestorActivityDescriptor.Outputs.FirstOrDefault(x => x.Name == outputName);
 57
 358        if (ancestorOutputDescriptor == null)
 059            return;
 60
 361        var ancestorOutput = (Output)ancestorOutputDescriptor.ValueGetter(ancestorActivity)!;
 362        ancestorContext.Set(ancestorOutput, outputValue, outputName);
 63
 64        // If the ancestor activity is the root workflow, we need to update the workflow execution context's output coll
 365        if (ancestorContext.ParentActivityExecutionContext == null)
 066            context.WorkflowExecutionContext.Output[outputName] = outputValue!;
 67        else
 68        {
 69            // If this activity executes in a composite activity, we need to update the composite activity's output vari
 370            var variable = context.ExpressionExecutionContext.GetVariable(outputName);
 71
 372            if (variable != null)
 373                context.Set(variable, outputValue);
 74
 75            // Also set the output on the composite activity's output property.
 376            var compositeActivityContext = ancestorContext;
 377            var compositeActivity = compositeActivityContext.Activity;
 378            var compositeActivityDescriptor = compositeActivityContext.ActivityDescriptor;
 779            var compositeOutputDescriptor = compositeActivityDescriptor.Outputs.FirstOrDefault(x => x.Name == outputName
 380            var compositeOutput = (Output?)compositeOutputDescriptor?.ValueGetter(compositeActivity);
 81
 382            if(compositeOutput != null)
 183                ancestorContext.ParentActivityExecutionContext.Set(compositeOutput, outputValue);
 84        }
 385    }
 86}