Skip to content

Commit

Permalink
switch hashFiles to extension function (actions#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple authored Mar 18, 2020
1 parent b0a7148 commit dfaf6e0
Show file tree
Hide file tree
Showing 36 changed files with 825 additions and 658 deletions.
40 changes: 26 additions & 14 deletions src/Runner.Worker/ActionManifestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public interface IActionManifestManager : IRunnerService
{
ActionDefinitionData Load(IExecutionContext executionContext, string manifestFile);

List<string> EvaluateContainerArguments(IExecutionContext executionContext, SequenceToken token, IDictionary<string, PipelineContextData> contextData);
List<string> EvaluateContainerArguments(IExecutionContext executionContext, SequenceToken token, IDictionary<string, PipelineContextData> extraExpressionValues);

Dictionary<string, string> EvaluateContainerEnvironment(IExecutionContext executionContext, MappingToken token, IDictionary<string, PipelineContextData> contextData);
Dictionary<string, string> EvaluateContainerEnvironment(IExecutionContext executionContext, MappingToken token, IDictionary<string, PipelineContextData> extraExpressionValues);

string EvaluateDefaultInput(IExecutionContext executionContext, string inputName, TemplateToken token, IDictionary<string, PipelineContextData> contextData);
string EvaluateDefaultInput(IExecutionContext executionContext, string inputName, TemplateToken token);
}

public sealed class ActionManifestManager : RunnerService, IActionManifestManager
Expand Down Expand Up @@ -54,7 +54,7 @@ public override void Initialize(IHostContext hostContext)

public ActionDefinitionData Load(IExecutionContext executionContext, string manifestFile)
{
var context = CreateContext(executionContext, null);
var context = CreateContext(executionContext);
ActionDefinitionData actionDefinition = new ActionDefinitionData();
try
{
Expand Down Expand Up @@ -133,13 +133,13 @@ public ActionDefinitionData Load(IExecutionContext executionContext, string mani
public List<string> EvaluateContainerArguments(
IExecutionContext executionContext,
SequenceToken token,
IDictionary<string, PipelineContextData> contextData)
IDictionary<string, PipelineContextData> extraExpressionValues)
{
var result = new List<string>();

if (token != null)
{
var context = CreateContext(executionContext, contextData);
var context = CreateContext(executionContext, extraExpressionValues);
try
{
var evaluateResult = TemplateEvaluator.Evaluate(context, "container-runs-args", token, 0, null, omitHeader: true);
Expand Down Expand Up @@ -172,13 +172,13 @@ public List<string> EvaluateContainerArguments(
public Dictionary<string, string> EvaluateContainerEnvironment(
IExecutionContext executionContext,
MappingToken token,
IDictionary<string, PipelineContextData> contextData)
IDictionary<string, PipelineContextData> extraExpressionValues)
{
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

if (token != null)
{
var context = CreateContext(executionContext, contextData);
var context = CreateContext(executionContext, extraExpressionValues);
try
{
var evaluateResult = TemplateEvaluator.Evaluate(context, "container-runs-env", token, 0, null, omitHeader: true);
Expand Down Expand Up @@ -216,13 +216,12 @@ public Dictionary<string, string> EvaluateContainerEnvironment(
public string EvaluateDefaultInput(
IExecutionContext executionContext,
string inputName,
TemplateToken token,
IDictionary<string, PipelineContextData> contextData)
TemplateToken token)
{
string result = "";
if (token != null)
{
var context = CreateContext(executionContext, contextData);
var context = CreateContext(executionContext);
try
{
var evaluateResult = TemplateEvaluator.Evaluate(context, "input-default-context", token, 0, null, omitHeader: true);
Expand All @@ -247,7 +246,7 @@ public string EvaluateDefaultInput(

private TemplateContext CreateContext(
IExecutionContext executionContext,
IDictionary<string, PipelineContextData> contextData)
IDictionary<string, PipelineContextData> extraExpressionValues = null)
{
var result = new TemplateContext
{
Expand All @@ -261,14 +260,27 @@ private TemplateContext CreateContext(
TraceWriter = executionContext.ToTemplateTraceWriter(),
};

if (contextData?.Count > 0)
// Expression values from execution context
foreach (var pair in executionContext.ExpressionValues)
{
foreach (var pair in contextData)
result.ExpressionValues[pair.Key] = pair.Value;
}

// Extra expression values
if (extraExpressionValues?.Count > 0)
{
foreach (var pair in extraExpressionValues)
{
result.ExpressionValues[pair.Key] = pair.Value;
}
}

// Expression functions from execution context
foreach (var item in executionContext.ExpressionFunctions)
{
result.ExpressionFunctions.Add(item);
}

// Add the file table
if (_fileTable?.Count > 0)
{
Expand Down
20 changes: 9 additions & 11 deletions src/Runner.Worker/ActionRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum ActionRunStage
public interface IActionRunner : IStep, IRunnerService
{
ActionRunStage Stage { get; set; }
Boolean TryEvaluateDisplayName(DictionaryContextData contextData, IExecutionContext context);
bool TryEvaluateDisplayName(DictionaryContextData contextData, IExecutionContext context);
Pipelines.ActionStep Action { get; set; }
}

Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task RunAsync()
// Load the inputs.
ExecutionContext.Debug("Loading inputs");
var templateEvaluator = ExecutionContext.ToPipelineTemplateEvaluator();
var inputs = templateEvaluator.EvaluateStepInputs(Action.Inputs, ExecutionContext.ExpressionValues);
var inputs = templateEvaluator.EvaluateStepInputs(Action.Inputs, ExecutionContext.ExpressionValues, ExecutionContext.ExpressionFunctions);

foreach (KeyValuePair<string, string> input in inputs)
{
Expand All @@ -162,13 +162,7 @@ public async Task RunAsync()
string key = input.Key.AssertString("action input name").Value;
if (!inputs.ContainsKey(key))
{
var evaluateContext = new Dictionary<string, PipelineContextData>(StringComparer.OrdinalIgnoreCase);
foreach (var data in ExecutionContext.ExpressionValues)
{
evaluateContext[data.Key] = data.Value;
}

inputs[key] = manifestManager.EvaluateDefaultInput(ExecutionContext, key, input.Value, evaluateContext);
inputs[key] = manifestManager.EvaluateDefaultInput(ExecutionContext, key, input.Value);
}
}
}
Expand Down Expand Up @@ -293,10 +287,14 @@ private string GenerateDisplayName(ActionStep action, DictionaryContextData cont
return displayName;
}
// Try evaluating fully
var templateEvaluator = context.ToPipelineTemplateEvaluator();
try
{
didFullyEvaluate = templateEvaluator.TryEvaluateStepDisplayName(tokenToParse, contextData, out displayName);
if (tokenToParse.CheckHasRequiredContext(contextData, context.ExpressionFunctions))
{
var templateEvaluator = context.ToPipelineTemplateEvaluator();
displayName = templateEvaluator.EvaluateStepDisplayName(tokenToParse, contextData, context.ExpressionFunctions);
didFullyEvaluate = true;
}
}
catch (TemplateValidationException e)
{
Expand Down
39 changes: 24 additions & 15 deletions src/Runner.Worker/ExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using GitHub.Runner.Worker.Container;
using GitHub.Services.WebApi;
using GitHub.DistributedTask.Expressions2;
using GitHub.DistributedTask.Pipelines;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.Pipelines.ObjectTemplating;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;
using GitHub.Runner.Worker.Container;
using GitHub.Services.WebApi;
using Newtonsoft.Json;
using System.Text;
using System.Collections;
using ObjectTemplating = GitHub.DistributedTask.ObjectTemplating;
using Pipelines = GitHub.DistributedTask.Pipelines;
using GitHub.DistributedTask.Expressions2;

namespace GitHub.Runner.Worker
{
Expand Down Expand Up @@ -55,6 +55,7 @@ public interface IExecutionContext : IRunnerService
IList<String> FileTable { get; }
StepsContext StepsContext { get; }
DictionaryContextData ExpressionValues { get; }
IList<IFunctionInfo> ExpressionFunctions { get; }
List<string> PrependPath { get; }
ContainerInfo Container { get; set; }
List<ContainerInfo> ServiceContainers { get; }
Expand Down Expand Up @@ -148,6 +149,7 @@ public sealed class ExecutionContext : RunnerService, IExecutionContext
public IList<String> FileTable { get; private set; }
public StepsContext StepsContext { get; private set; }
public DictionaryContextData ExpressionValues { get; } = new DictionaryContextData();
public IList<IFunctionInfo> ExpressionFunctions { get; } = new List<IFunctionInfo>();
public bool WriteDebug { get; private set; }
public List<string> PrependPath { get; private set; }
public ContainerInfo Container { get; set; }
Expand Down Expand Up @@ -280,6 +282,10 @@ public IExecutionContext CreateChild(Guid recordId, string displayName, string r
{
child.ExpressionValues[pair.Key] = pair.Value;
}
foreach (var item in ExpressionFunctions)
{
child.ExpressionFunctions.Add(item);
}
child._cancellationTokenSource = new CancellationTokenSource();
child.WriteDebug = WriteDebug;
child._parentExecutionContext = this;
Expand Down Expand Up @@ -593,12 +599,6 @@ public void InitializeJob(Pipelines.AgentJobRequestMessage message, Cancellation
// File table
FileTable = new List<String>(message.FileTable ?? new string[0]);

// Expression functions
if (Variables.GetBoolean("System.HashFilesV2") == true)
{
ExpressionConstants.UpdateFunction<Handlers.HashFiles>("hashFiles", 1, byte.MaxValue);
}

// Expression values
if (message.ContextData?.Count > 0)
{
Expand Down Expand Up @@ -915,11 +915,19 @@ public static void Debug(this IExecutionContext context, string message)
}
}

public static PipelineTemplateEvaluator ToPipelineTemplateEvaluator(this IExecutionContext context)
public static IEnumerable<KeyValuePair<string, object>> ToExpressionState(this IExecutionContext context)
{
var templateTrace = context.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
return new PipelineTemplateEvaluator(templateTrace, schema, context.FileTable);
return new[] { new KeyValuePair<string, object>(nameof(IExecutionContext), context) };
}

public static PipelineTemplateEvaluator ToPipelineTemplateEvaluator(this IExecutionContext context, ObjectTemplating.ITraceWriter traceWriter = null)
{
if (traceWriter == null)
{
traceWriter = context.ToTemplateTraceWriter();
}
var schema = PipelineTemplateSchemaFactory.GetSchema();
return new PipelineTemplateEvaluator(traceWriter, schema, context.FileTable);
}

public static ObjectTemplating.ITraceWriter ToTemplateTraceWriter(this IExecutionContext context)
Expand All @@ -934,6 +942,7 @@ internal sealed class TemplateTraceWriter : ObjectTemplating.ITraceWriter

internal TemplateTraceWriter(IExecutionContext executionContext)
{
ArgUtil.NotNull(executionContext, nameof(executionContext));
_executionContext = executionContext;
}

Expand Down
Loading

0 comments on commit dfaf6e0

Please sign in to comment.