|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package substitution |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// ApplyReplacements returns a string with references to parameters replaced, |
| 25 | +// based on the mapping provided in replacements. |
| 26 | +// For example, if the input string is "foo: $(params.foo)", and replacements maps "params.foo" to "bar", |
| 27 | +// the output would be "foo: bar". |
| 28 | +func ApplyReplacements(in string, replacements map[string]string) string { |
| 29 | + replacementsList := []string{} |
| 30 | + for k, v := range replacements { |
| 31 | + replacementsList = append(replacementsList, fmt.Sprintf("$(%s)", k), v) |
| 32 | + } |
| 33 | + // strings.Replacer does all replacements in one pass, preventing multiple replacements |
| 34 | + // See #2093 for an explanation on why we need to do this. |
| 35 | + replacer := strings.NewReplacer(replacementsList...) |
| 36 | + return replacer.Replace(in) |
| 37 | +} |
| 38 | + |
| 39 | +// ApplyArrayReplacements takes an input string, and output an array of strings related to possible arrayReplacements. If there aren't any |
| 40 | +// areas where the input can be split up via arrayReplacements, then just return an array with a single element, |
| 41 | +// which is ApplyReplacements(in, replacements). |
| 42 | +func ApplyArrayReplacements(in string, stringReplacements map[string]string, arrayReplacements map[string][]string) []string { |
| 43 | + for k, v := range arrayReplacements { |
| 44 | + stringToReplace := fmt.Sprintf("$(%s)", k) |
| 45 | + |
| 46 | + // If the input string matches a replacement's key (without padding characters), return the corresponding array. |
| 47 | + // Note that the webhook should prevent all instances where this could evaluate to false. |
| 48 | + if (strings.Count(in, stringToReplace) == 1) && len(in) == len(stringToReplace) { |
| 49 | + return v |
| 50 | + } |
| 51 | + |
| 52 | + // same replace logic for star array expressions |
| 53 | + starStringtoReplace := fmt.Sprintf("$(%s[*])", k) |
| 54 | + if (strings.Count(in, starStringtoReplace) == 1) && len(in) == len(starStringtoReplace) { |
| 55 | + return v |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Otherwise return a size-1 array containing the input string with standard stringReplacements applied. |
| 60 | + return []string{ApplyReplacements(in, stringReplacements)} |
| 61 | +} |
0 commit comments