1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00

Fix env var replacement (#3260)

* Fix envVar replacement (go part)

* Fix envVar replacement (groovy part)

* insert underscore also after 0-9 (go part)

* insert underscore also after 0-9 (groovy part)

* Adjust tests for trailing numbers

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Marcus Holl 2021-11-15 12:59:49 +01:00 committed by GitHub
parent e97242b7e7
commit cf62c35b1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 16 deletions

View File

@ -20,7 +20,6 @@ import (
"strconv"
"strings"
"time"
"unicode"
)
type cfFileUtil interface {
@ -775,18 +774,7 @@ func handleMtaExtensionCredentials(extFile string, credentials map[string]interf
func toEnvVarKey(key string) string {
key = regexp.MustCompile(`[^A-Za-z0-9]`).ReplaceAllString(key, "_")
// from here on we have only ascii
modifiedKey := ""
last := '_'
for _, runeVal := range key {
if unicode.IsUpper(runeVal) && last != '_' {
modifiedKey += "_"
}
modifiedKey += string(unicode.ToUpper(runeVal))
last = runeVal
}
return modifiedKey
// since golang regex does not support negative lookbehinds we have to code it ourselvs
return strings.ToUpper(regexp.MustCompile(`([a-z0-9])([A-Z])`).ReplaceAllString(key, "${1}_${2}"))
}
func toMap(keyValue []string, separator string) (map[string]string, error) {

View File

@ -1377,6 +1377,6 @@ func TestMtaExtensionCredentials(t *testing.T) {
}
func TestEnvVarKeyModification(t *testing.T) {
envVarCompatibleKey := toEnvVarKey("Mta.ExtensionCredential~Credential_Id1")
assert.Equal(t, "MTA_EXTENSION_CREDENTIAL_CREDENTIAL_ID1", envVarCompatibleKey)
envVarCompatibleKey := toEnvVarKey("Mta.EXtensionCredential~Credential_Id1Abc")
assert.Equal(t, "MTA_EXTENSION_CREDENTIAL_CREDENTIAL_ID1_ABC", envVarCompatibleKey)
}

View File

@ -34,6 +34,6 @@ void call(Map parameters = [:]) {
*/
private static String toEnvVarKey(String key) {
key = key.replaceAll(/[^A-Za-z0-9]/, "_")
key = key.replaceAll(/(.)(?<!_)([A-Z])/, "\$1_\$2")
key = key.replaceAll(/([a-z0-9])([A-Z])/, /$1_$2/)
return key.toUpperCase()
}