public TemplateToken EvaluateEnvironmentUrl( TemplateToken token, DictionaryContextData contextData, IList <IFunctionInfo> expressionFunctions) { var result = default(TemplateToken); if (token != null && token.Type != TokenType.Null) { var context = CreateContext(contextData, expressionFunctions); try { token = TemplateEvaluator.Evaluate(context, TemplateConstants.StringRunnerContextNoSecrets, token, 0, null, omitHeader: true); context.Errors.Check(); result = token.AssertString("environment.url"); } catch (Exception ex) when(!(ex is TemplateValidationException)) { context.Errors.Add(ex); } context.Errors.Check(); } return(result); }
internal static String ConvertToStepDisplayName( TemplateContext context, TemplateToken token, Boolean allowExpressions = false) { if (allowExpressions && token is ExpressionToken) { return(null); } var stringToken = token.AssertString($"step {PipelineTemplateConstants.Name}"); return(stringToken.Value); }
/// <summary> /// When empty, default to "success()". /// When a status function is not referenced, format as "success() && <CONDITION>". /// </summary> private static String ConvertToIfCondition( TemplateContext context, TemplateToken token, Boolean isJob) { String condition; if (token is null) { condition = null; } else if (token is BasicExpressionToken expressionToken) { condition = expressionToken.Expression; } else { var stringToken = token.AssertString($"{(isJob ? "job" : "step")} {PipelineTemplateConstants.If}"); condition = stringToken.Value; } if (String.IsNullOrWhiteSpace(condition)) { return($"{PipelineTemplateConstants.Success}()"); } var expressionParser = new ExpressionParser(); var functions = default(IFunctionInfo[]); var namedValues = default(INamedValueInfo[]); if (isJob) { namedValues = s_jobIfNamedValues; // TODO: refactor into seperate functions // functions = PhaseCondition.FunctionInfo; } else { namedValues = s_stepNamedValues; functions = s_stepConditionFunctions; } var node = default(ExpressionNode); try { node = expressionParser.CreateTree(condition, null, namedValues, functions) as ExpressionNode; } catch (Exception ex) { context.Error(token, ex); return(null); } if (node == null) { return($"{PipelineTemplateConstants.Success}()"); } var hasStatusFunction = node.Traverse().Any(x => { if (x is Function function) { return(String.Equals(function.Name, PipelineTemplateConstants.Always, StringComparison.OrdinalIgnoreCase) || String.Equals(function.Name, PipelineTemplateConstants.Cancelled, StringComparison.OrdinalIgnoreCase) || String.Equals(function.Name, PipelineTemplateConstants.Failure, StringComparison.OrdinalIgnoreCase) || String.Equals(function.Name, PipelineTemplateConstants.Success, StringComparison.OrdinalIgnoreCase)); } return(false); }); return(hasStatusFunction ? condition : $"{PipelineTemplateConstants.Success}() && ({condition})"); }