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