|
| 1 | +package jobparser |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/nektos/act/pkg/exprparser" |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +// ExpressionEvaluator is copied from runner.expressionEvaluator, |
| 13 | +// to avoid unnecessary dependencies |
| 14 | +type ExpressionEvaluator struct { |
| 15 | + interpreter exprparser.Interpreter |
| 16 | +} |
| 17 | + |
| 18 | +func NewExpressionEvaluator(interpreter exprparser.Interpreter) *ExpressionEvaluator { |
| 19 | + return &ExpressionEvaluator{interpreter: interpreter} |
| 20 | +} |
| 21 | + |
| 22 | +func (ee ExpressionEvaluator) evaluate(in string, defaultStatusCheck exprparser.DefaultStatusCheck) (interface{}, error) { |
| 23 | + evaluated, err := ee.interpreter.Evaluate(in, defaultStatusCheck) |
| 24 | + |
| 25 | + return evaluated, err |
| 26 | +} |
| 27 | + |
| 28 | +func (ee ExpressionEvaluator) evaluateScalarYamlNode(node *yaml.Node) error { |
| 29 | + var in string |
| 30 | + if err := node.Decode(&in); err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") { |
| 34 | + return nil |
| 35 | + } |
| 36 | + expr, _ := rewriteSubExpression(in, false) |
| 37 | + res, err := ee.evaluate(expr, exprparser.DefaultStatusCheckNone) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + return node.Encode(res) |
| 42 | +} |
| 43 | + |
| 44 | +func (ee ExpressionEvaluator) evaluateMappingYamlNode(node *yaml.Node) error { |
| 45 | + // GitHub has this undocumented feature to merge maps, called insert directive |
| 46 | + insertDirective := regexp.MustCompile(`\${{\s*insert\s*}}`) |
| 47 | + for i := 0; i < len(node.Content)/2; { |
| 48 | + k := node.Content[i*2] |
| 49 | + v := node.Content[i*2+1] |
| 50 | + if err := ee.EvaluateYamlNode(v); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + var sk string |
| 54 | + // Merge the nested map of the insert directive |
| 55 | + if k.Decode(&sk) == nil && insertDirective.MatchString(sk) { |
| 56 | + node.Content = append(append(node.Content[:i*2], v.Content...), node.Content[(i+1)*2:]...) |
| 57 | + i += len(v.Content) / 2 |
| 58 | + } else { |
| 59 | + if err := ee.EvaluateYamlNode(k); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + i++ |
| 63 | + } |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func (ee ExpressionEvaluator) evaluateSequenceYamlNode(node *yaml.Node) error { |
| 69 | + for i := 0; i < len(node.Content); { |
| 70 | + v := node.Content[i] |
| 71 | + // Preserve nested sequences |
| 72 | + wasseq := v.Kind == yaml.SequenceNode |
| 73 | + if err := ee.EvaluateYamlNode(v); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + // GitHub has this undocumented feature to merge sequences / arrays |
| 77 | + // We have a nested sequence via evaluation, merge the arrays |
| 78 | + if v.Kind == yaml.SequenceNode && !wasseq { |
| 79 | + node.Content = append(append(node.Content[:i], v.Content...), node.Content[i+1:]...) |
| 80 | + i += len(v.Content) |
| 81 | + } else { |
| 82 | + i++ |
| 83 | + } |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (ee ExpressionEvaluator) EvaluateYamlNode(node *yaml.Node) error { |
| 89 | + switch node.Kind { |
| 90 | + case yaml.ScalarNode: |
| 91 | + return ee.evaluateScalarYamlNode(node) |
| 92 | + case yaml.MappingNode: |
| 93 | + return ee.evaluateMappingYamlNode(node) |
| 94 | + case yaml.SequenceNode: |
| 95 | + return ee.evaluateSequenceYamlNode(node) |
| 96 | + default: |
| 97 | + return nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func (ee ExpressionEvaluator) Interpolate(in string) string { |
| 102 | + if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") { |
| 103 | + return in |
| 104 | + } |
| 105 | + |
| 106 | + expr, _ := rewriteSubExpression(in, true) |
| 107 | + evaluated, err := ee.evaluate(expr, exprparser.DefaultStatusCheckNone) |
| 108 | + if err != nil { |
| 109 | + return "" |
| 110 | + } |
| 111 | + |
| 112 | + value, ok := evaluated.(string) |
| 113 | + if !ok { |
| 114 | + panic(fmt.Sprintf("Expression %s did not evaluate to a string", expr)) |
| 115 | + } |
| 116 | + |
| 117 | + return value |
| 118 | +} |
| 119 | + |
| 120 | +func escapeFormatString(in string) string { |
| 121 | + return strings.ReplaceAll(strings.ReplaceAll(in, "{", "{{"), "}", "}}") |
| 122 | +} |
| 123 | + |
| 124 | +func rewriteSubExpression(in string, forceFormat bool) (string, error) { |
| 125 | + if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") { |
| 126 | + return in, nil |
| 127 | + } |
| 128 | + |
| 129 | + strPattern := regexp.MustCompile("(?:''|[^'])*'") |
| 130 | + pos := 0 |
| 131 | + exprStart := -1 |
| 132 | + strStart := -1 |
| 133 | + var results []string |
| 134 | + formatOut := "" |
| 135 | + for pos < len(in) { |
| 136 | + if strStart > -1 { |
| 137 | + matches := strPattern.FindStringIndex(in[pos:]) |
| 138 | + if matches == nil { |
| 139 | + panic("unclosed string.") |
| 140 | + } |
| 141 | + |
| 142 | + strStart = -1 |
| 143 | + pos += matches[1] |
| 144 | + } else if exprStart > -1 { |
| 145 | + exprEnd := strings.Index(in[pos:], "}}") |
| 146 | + strStart = strings.Index(in[pos:], "'") |
| 147 | + |
| 148 | + if exprEnd > -1 && strStart > -1 { |
| 149 | + if exprEnd < strStart { |
| 150 | + strStart = -1 |
| 151 | + } else { |
| 152 | + exprEnd = -1 |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if exprEnd > -1 { |
| 157 | + formatOut += fmt.Sprintf("{%d}", len(results)) |
| 158 | + results = append(results, strings.TrimSpace(in[exprStart:pos+exprEnd])) |
| 159 | + pos += exprEnd + 2 |
| 160 | + exprStart = -1 |
| 161 | + } else if strStart > -1 { |
| 162 | + pos += strStart + 1 |
| 163 | + } else { |
| 164 | + panic("unclosed expression.") |
| 165 | + } |
| 166 | + } else { |
| 167 | + exprStart = strings.Index(in[pos:], "${{") |
| 168 | + if exprStart != -1 { |
| 169 | + formatOut += escapeFormatString(in[pos : pos+exprStart]) |
| 170 | + exprStart = pos + exprStart + 3 |
| 171 | + pos = exprStart |
| 172 | + } else { |
| 173 | + formatOut += escapeFormatString(in[pos:]) |
| 174 | + pos = len(in) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if len(results) == 1 && formatOut == "{0}" && !forceFormat { |
| 180 | + return in, nil |
| 181 | + } |
| 182 | + |
| 183 | + out := fmt.Sprintf("format('%s', %s)", strings.ReplaceAll(formatOut, "'", "''"), strings.Join(results, ", ")) |
| 184 | + return out, nil |
| 185 | +} |
0 commit comments